n32g45x_spi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_spi.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_SPI_H__
  35. #define __N32G45X_SPI_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "n32g45x.h"
  40. /** @addtogroup N32G45X_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @addtogroup SPI
  44. * @{
  45. */
  46. /** @addtogroup SPI_Exported_Types
  47. * @{
  48. */
  49. /**
  50. * @brief SPI Init structure definition
  51. */
  52. typedef struct
  53. {
  54. uint16_t DataDirection; /*!< Specifies the SPI unidirectional or bidirectional data mode.
  55. This parameter can be a value of @ref SPI_data_direction */
  56. uint16_t SpiMode; /*!< Specifies the SPI operating mode.
  57. This parameter can be a value of @ref SPI_mode */
  58. uint16_t DataLen; /*!< Specifies the SPI data size.
  59. This parameter can be a value of @ref SPI_data_size */
  60. uint16_t CLKPOL; /*!< Specifies the serial clock steady state.
  61. This parameter can be a value of @ref SPI_Clock_Polarity */
  62. uint16_t CLKPHA; /*!< Specifies the clock active edge for the bit capture.
  63. This parameter can be a value of @ref SPI_Clock_Phase */
  64. uint16_t NSS; /*!< Specifies whether the NSS signal is managed by
  65. hardware (NSS pin) or by software using the SSI bit.
  66. This parameter can be a value of @ref SPI_Slave_Select_management */
  67. uint16_t BaudRatePres; /*!< Specifies the Baud Rate prescaler value which will be
  68. used to configure the transmit and receive SCK clock.
  69. This parameter can be a value of @ref SPI_BaudRate_Prescaler.
  70. @note The communication clock is derived from the master
  71. clock. The slave clock does not need to be set. */
  72. uint16_t FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
  73. This parameter can be a value of @ref SPI_MSB_LSB_transmission */
  74. uint16_t CRCPoly; /*!< Specifies the polynomial used for the CRC calculation. */
  75. } SPI_InitType;
  76. /**
  77. * @brief I2S Init structure definition
  78. */
  79. typedef struct
  80. {
  81. uint16_t I2sMode; /*!< Specifies the I2S operating mode.
  82. This parameter can be a value of @ref I2sMode */
  83. uint16_t Standard; /*!< Specifies the standard used for the I2S communication.
  84. This parameter can be a value of @ref Standard */
  85. uint16_t DataFormat; /*!< Specifies the data format for the I2S communication.
  86. This parameter can be a value of @ref I2S_Data_Format */
  87. uint16_t MCLKEnable; /*!< Specifies whether the I2S MCLK output is enabled or not.
  88. This parameter can be a value of @ref I2S_MCLK_Output */
  89. uint32_t AudioFrequency; /*!< Specifies the frequency selected for the I2S communication.
  90. This parameter can be a value of @ref I2S_Audio_Frequency */
  91. uint16_t CLKPOL; /*!< Specifies the idle state of the I2S clock.
  92. This parameter can be a value of @ref I2S_Clock_Polarity */
  93. } I2S_InitType;
  94. /**
  95. * @}
  96. */
  97. /** @addtogroup SPI_Exported_Constants
  98. * @{
  99. */
  100. #define IS_SPI_PERIPH(PERIPH) (((PERIPH) == SPI1) || ((PERIPH) == SPI2) || ((PERIPH) == SPI3))
  101. #define IS_SPI_2OR3_PERIPH(PERIPH) (((PERIPH) == SPI2) || ((PERIPH) == SPI3))
  102. /** @addtogroup SPI_data_direction
  103. * @{
  104. */
  105. #define SPI_DIR_DOUBLELINE_FULLDUPLEX ((uint16_t)0x0000)
  106. #define SPI_DIR_DOUBLELINE_RONLY ((uint16_t)0x0400)
  107. #define SPI_DIR_SINGLELINE_RX ((uint16_t)0x8000)
  108. #define SPI_DIR_SINGLELINE_TX ((uint16_t)0xC000)
  109. #define IS_SPI_DIR_MODE(MODE) \
  110. (((MODE) == SPI_DIR_DOUBLELINE_FULLDUPLEX) || ((MODE) == SPI_DIR_DOUBLELINE_RONLY) \
  111. || ((MODE) == SPI_DIR_SINGLELINE_RX) || ((MODE) == SPI_DIR_SINGLELINE_TX))
  112. /**
  113. * @}
  114. */
  115. /** @addtogroup SPI_mode
  116. * @{
  117. */
  118. #define SPI_MODE_MASTER ((uint16_t)0x0104)
  119. #define SPI_MODE_SLAVE ((uint16_t)0x0000)
  120. #define IS_SPI_MODE(MODE) (((MODE) == SPI_MODE_MASTER) || ((MODE) == SPI_MODE_SLAVE))
  121. /**
  122. * @}
  123. */
  124. /** @addtogroup SPI_data_size
  125. * @{
  126. */
  127. #define SPI_DATA_SIZE_16BITS ((uint16_t)0x0800)
  128. #define SPI_DATA_SIZE_8BITS ((uint16_t)0x0000)
  129. #define IS_SPI_DATASIZE(DATASIZE) (((DATASIZE) == SPI_DATA_SIZE_16BITS) || ((DATASIZE) == SPI_DATA_SIZE_8BITS))
  130. /**
  131. * @}
  132. */
  133. /** @addtogroup SPI_Clock_Polarity
  134. * @{
  135. */
  136. #define SPI_CLKPOL_LOW ((uint16_t)0x0000)
  137. #define SPI_CLKPOL_HIGH ((uint16_t)0x0002)
  138. #define IS_SPI_CLKPOL(CPOL) (((CPOL) == SPI_CLKPOL_LOW) || ((CPOL) == SPI_CLKPOL_HIGH))
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup SPI_Clock_Phase
  143. * @{
  144. */
  145. #define SPI_CLKPHA_FIRST_EDGE ((uint16_t)0x0000)
  146. #define SPI_CLKPHA_SECOND_EDGE ((uint16_t)0x0001)
  147. #define IS_SPI_CLKPHA(CPHA) (((CPHA) == SPI_CLKPHA_FIRST_EDGE) || ((CPHA) == SPI_CLKPHA_SECOND_EDGE))
  148. /**
  149. * @}
  150. */
  151. /** @addtogroup SPI_Slave_Select_management
  152. * @{
  153. */
  154. #define SPI_NSS_SOFT ((uint16_t)0x0200)
  155. #define SPI_NSS_HARD ((uint16_t)0x0000)
  156. #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_SOFT) || ((NSS) == SPI_NSS_HARD))
  157. /**
  158. * @}
  159. */
  160. /** @addtogroup SPI_BaudRate_Prescaler
  161. * @{
  162. */
  163. #define SPI_BR_PRESCALER_2 ((uint16_t)0x0000)
  164. #define SPI_BR_PRESCALER_4 ((uint16_t)0x0008)
  165. #define SPI_BR_PRESCALER_8 ((uint16_t)0x0010)
  166. #define SPI_BR_PRESCALER_16 ((uint16_t)0x0018)
  167. #define SPI_BR_PRESCALER_32 ((uint16_t)0x0020)
  168. #define SPI_BR_PRESCALER_64 ((uint16_t)0x0028)
  169. #define SPI_BR_PRESCALER_128 ((uint16_t)0x0030)
  170. #define SPI_BR_PRESCALER_256 ((uint16_t)0x0038)
  171. #define IS_SPI_BR_PRESCALER(PRESCALER) \
  172. (((PRESCALER) == SPI_BR_PRESCALER_2) || ((PRESCALER) == SPI_BR_PRESCALER_4) || ((PRESCALER) == SPI_BR_PRESCALER_8) \
  173. || ((PRESCALER) == SPI_BR_PRESCALER_16) || ((PRESCALER) == SPI_BR_PRESCALER_32) \
  174. || ((PRESCALER) == SPI_BR_PRESCALER_64) || ((PRESCALER) == SPI_BR_PRESCALER_128) \
  175. || ((PRESCALER) == SPI_BR_PRESCALER_256))
  176. /**
  177. * @}
  178. */
  179. /** @addtogroup SPI_MSB_LSB_transmission
  180. * @{
  181. */
  182. #define SPI_FB_MSB ((uint16_t)0x0000)
  183. #define SPI_FB_LSB ((uint16_t)0x0080)
  184. #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FB_MSB) || ((BIT) == SPI_FB_LSB))
  185. /**
  186. * @}
  187. */
  188. /** @addtogroup I2sMode
  189. * @{
  190. */
  191. #define I2S_MODE_SlAVE_TX ((uint16_t)0x0000)
  192. #define I2S_MODE_SlAVE_RX ((uint16_t)0x0100)
  193. #define I2S_MODE_MASTER_TX ((uint16_t)0x0200)
  194. #define I2S_MODE_MASTER_RX ((uint16_t)0x0300)
  195. #define IS_I2S_MODE(MODE) \
  196. (((MODE) == I2S_MODE_SlAVE_TX) || ((MODE) == I2S_MODE_SlAVE_RX) || ((MODE) == I2S_MODE_MASTER_TX) \
  197. || ((MODE) == I2S_MODE_MASTER_RX))
  198. /**
  199. * @}
  200. */
  201. /** @addtogroup Standard
  202. * @{
  203. */
  204. #define I2S_STD_PHILLIPS ((uint16_t)0x0000)
  205. #define I2S_STD_MSB_ALIGN ((uint16_t)0x0010)
  206. #define I2S_STD_LSB_ALIGN ((uint16_t)0x0020)
  207. #define I2S_STD_PCM_SHORTFRAME ((uint16_t)0x0030)
  208. #define I2S_STD_PCM_LONGFRAME ((uint16_t)0x00B0)
  209. #define IS_I2S_STANDARD(STANDARD) \
  210. (((STANDARD) == I2S_STD_PHILLIPS) || ((STANDARD) == I2S_STD_MSB_ALIGN) || ((STANDARD) == I2S_STD_LSB_ALIGN) \
  211. || ((STANDARD) == I2S_STD_PCM_SHORTFRAME) || ((STANDARD) == I2S_STD_PCM_LONGFRAME))
  212. /**
  213. * @}
  214. */
  215. /** @addtogroup I2S_Data_Format
  216. * @{
  217. */
  218. #define I2S_DATA_FMT_16BITS ((uint16_t)0x0000)
  219. #define I2S_DATA_FMT_16BITS_EXTENDED ((uint16_t)0x0001)
  220. #define I2S_DATA_FMT_24BITS ((uint16_t)0x0003)
  221. #define I2S_DATA_FMT_32BITS ((uint16_t)0x0005)
  222. #define IS_I2S_DATA_FMT(FORMAT) \
  223. (((FORMAT) == I2S_DATA_FMT_16BITS) || ((FORMAT) == I2S_DATA_FMT_16BITS_EXTENDED) \
  224. || ((FORMAT) == I2S_DATA_FMT_24BITS) || ((FORMAT) == I2S_DATA_FMT_32BITS))
  225. /**
  226. * @}
  227. */
  228. /** @addtogroup I2S_MCLK_Output
  229. * @{
  230. */
  231. #define I2S_MCLK_ENABLE ((uint16_t)0x0200)
  232. #define I2S_MCLK_DISABLE ((uint16_t)0x0000)
  233. #define IS_I2S_MCLK_ENABLE(OUTPUT) (((OUTPUT) == I2S_MCLK_ENABLE) || ((OUTPUT) == I2S_MCLK_DISABLE))
  234. /**
  235. * @}
  236. */
  237. /** @addtogroup I2S_Audio_Frequency
  238. * @{
  239. */
  240. #define I2S_AUDIO_FREQ_192K ((uint32_t)192000)
  241. #define I2S_AUDIO_FREQ_96K ((uint32_t)96000)
  242. #define I2S_AUDIO_FREQ_48K ((uint32_t)48000)
  243. #define I2S_AUDIO_FREQ_44K ((uint32_t)44100)
  244. #define I2S_AUDIO_FREQ_32K ((uint32_t)32000)
  245. #define I2S_AUDIO_FREQ_22K ((uint32_t)22050)
  246. #define I2S_AUDIO_FREQ_16K ((uint32_t)16000)
  247. #define I2S_AUDIO_FREQ_11K ((uint32_t)11025)
  248. #define I2S_AUDIO_FREQ_8K ((uint32_t)8000)
  249. #define I2S_AUDIO_FREQ_DEFAULT ((uint32_t)2)
  250. #define IS_I2S_AUDIO_FREQ(FREQ) \
  251. ((((FREQ) >= I2S_AUDIO_FREQ_8K) && ((FREQ) <= I2S_AUDIO_FREQ_192K)) || ((FREQ) == I2S_AUDIO_FREQ_DEFAULT))
  252. /**
  253. * @}
  254. */
  255. /** @addtogroup I2S_Clock_Polarity
  256. * @{
  257. */
  258. #define I2S_CLKPOL_LOW ((uint16_t)0x0000)
  259. #define I2S_CLKPOL_HIGH ((uint16_t)0x0008)
  260. #define IS_I2S_CLKPOL(CPOL) (((CPOL) == I2S_CLKPOL_LOW) || ((CPOL) == I2S_CLKPOL_HIGH))
  261. /**
  262. * @}
  263. */
  264. /** @addtogroup SPI_I2S_DMA_transfer_requests
  265. * @{
  266. */
  267. #define SPI_I2S_DMA_TX ((uint16_t)0x0002)
  268. #define SPI_I2S_DMA_RX ((uint16_t)0x0001)
  269. #define IS_SPI_I2S_DMA(DMAREQ) ((((DMAREQ) & (uint16_t)0xFFFC) == 0x00) && ((DMAREQ) != 0x00))
  270. /**
  271. * @}
  272. */
  273. /** @addtogroup SPI_NSS_internal_software_management
  274. * @{
  275. */
  276. #define SPI_NSS_HIGH ((uint16_t)0x0100)
  277. #define SPI_NSS_LOW ((uint16_t)0xFEFF)
  278. #define IS_SPI_NSS_LEVEL(INTERNAL) (((INTERNAL) == SPI_NSS_HIGH) || ((INTERNAL) == SPI_NSS_LOW))
  279. /**
  280. * @}
  281. */
  282. /** @addtogroup SPI_CRC_Transmit_Receive
  283. * @{
  284. */
  285. #define SPI_CRC_TX ((uint8_t)0x00)
  286. #define SPI_CRC_RX ((uint8_t)0x01)
  287. #define IS_SPI_CRC(CRC) (((CRC) == SPI_CRC_TX) || ((CRC) == SPI_CRC_RX))
  288. /**
  289. * @}
  290. */
  291. /** @addtogroup SPI_direction_transmit_receive
  292. * @{
  293. */
  294. #define SPI_BIDIRECTION_RX ((uint16_t)0xBFFF)
  295. #define SPI_BIDIRECTION_TX ((uint16_t)0x4000)
  296. #define IS_SPI_BIDIRECTION(DIRECTION) (((DIRECTION) == SPI_BIDIRECTION_RX) || ((DIRECTION) == SPI_BIDIRECTION_TX))
  297. /**
  298. * @}
  299. */
  300. /** @addtogroup SPI_I2S_interrupts_definition
  301. * @{
  302. */
  303. #define SPI_I2S_INT_TE ((uint8_t)0x71)
  304. #define SPI_I2S_INT_RNE ((uint8_t)0x60)
  305. #define SPI_I2S_INT_ERR ((uint8_t)0x50)
  306. #define IS_SPI_I2S_CONFIG_INT(IT) (((IT) == SPI_I2S_INT_TE) || ((IT) == SPI_I2S_INT_RNE) || ((IT) == SPI_I2S_INT_ERR))
  307. #define SPI_I2S_INT_OVER ((uint8_t)0x56)
  308. #define SPI_INT_MODERR ((uint8_t)0x55)
  309. #define SPI_INT_CRCERR ((uint8_t)0x54)
  310. #define I2S_INT_UNDER ((uint8_t)0x53)
  311. #define IS_SPI_I2S_CLR_INT(IT) (((IT) == SPI_INT_CRCERR))
  312. #define IS_SPI_I2S_GET_INT(IT) \
  313. (((IT) == SPI_I2S_INT_RNE) || ((IT) == SPI_I2S_INT_TE) || ((IT) == I2S_INT_UNDER) || ((IT) == SPI_INT_CRCERR) \
  314. || ((IT) == SPI_INT_MODERR) || ((IT) == SPI_I2S_INT_OVER))
  315. /**
  316. * @}
  317. */
  318. /** @addtogroup SPI_I2S_flags_definition
  319. * @{
  320. */
  321. #define SPI_I2S_RNE_FLAG ((uint16_t)0x0001)
  322. #define SPI_I2S_TE_FLAG ((uint16_t)0x0002)
  323. #define I2S_CHSIDE_FLAG ((uint16_t)0x0004)
  324. #define I2S_UNDER_FLAG ((uint16_t)0x0008)
  325. #define SPI_CRCERR_FLAG ((uint16_t)0x0010)
  326. #define SPI_MODERR_FLAG ((uint16_t)0x0020)
  327. #define SPI_I2S_OVER_FLAG ((uint16_t)0x0040)
  328. #define SPI_I2S_BUSY_FLAG ((uint16_t)0x0080)
  329. #define IS_SPI_I2S_CLR_FLAG(FLAG) (((FLAG) == SPI_CRCERR_FLAG))
  330. #define IS_SPI_I2S_GET_FLAG(FLAG) \
  331. (((FLAG) == SPI_I2S_BUSY_FLAG) || ((FLAG) == SPI_I2S_OVER_FLAG) || ((FLAG) == SPI_MODERR_FLAG) \
  332. || ((FLAG) == SPI_CRCERR_FLAG) || ((FLAG) == I2S_UNDER_FLAG) || ((FLAG) == I2S_CHSIDE_FLAG) \
  333. || ((FLAG) == SPI_I2S_TE_FLAG) || ((FLAG) == SPI_I2S_RNE_FLAG))
  334. /**
  335. * @}
  336. */
  337. /** @addtogroup SPI_CRC_polynomial
  338. * @{
  339. */
  340. #define IS_SPI_CRC_POLYNOMIAL(POLYNOMIAL) ((POLYNOMIAL) >= 0x1)
  341. /**
  342. * @}
  343. */
  344. /**
  345. * @}
  346. */
  347. /** @addtogroup SPI_Exported_Macros
  348. * @{
  349. */
  350. /**
  351. * @}
  352. */
  353. /** @addtogroup SPI_Exported_Functions
  354. * @{
  355. */
  356. void SPI_I2S_DeInit(SPI_Module* SPIx);
  357. void SPI_Init(SPI_Module* SPIx, SPI_InitType* SPI_InitStruct);
  358. void I2S_Init(SPI_Module* SPIx, I2S_InitType* I2S_InitStruct);
  359. void SPI_InitStruct(SPI_InitType* SPI_InitStruct);
  360. void I2S_InitStruct(I2S_InitType* I2S_InitStruct);
  361. void SPI_Enable(SPI_Module* SPIx, FunctionalState Cmd);
  362. void I2S_Enable(SPI_Module* SPIx, FunctionalState Cmd);
  363. void SPI_I2S_EnableInt(SPI_Module* SPIx, uint8_t SPI_I2S_IT, FunctionalState Cmd);
  364. void SPI_I2S_EnableDma(SPI_Module* SPIx, uint16_t SPI_I2S_DMAReq, FunctionalState Cmd);
  365. void SPI_I2S_TransmitData(SPI_Module* SPIx, uint16_t Data);
  366. uint16_t SPI_I2S_ReceiveData(SPI_Module* SPIx);
  367. void SPI_SetNssLevel(SPI_Module* SPIx, uint16_t SPI_NSSInternalSoft);
  368. void SPI_SSOutputEnable(SPI_Module* SPIx, FunctionalState Cmd);
  369. void SPI_ConfigDataLen(SPI_Module* SPIx, uint16_t DataLen);
  370. void SPI_TransmitCrcNext(SPI_Module* SPIx);
  371. void SPI_EnableCalculateCrc(SPI_Module* SPIx, FunctionalState Cmd);
  372. uint16_t SPI_GetCRCDat(SPI_Module* SPIx, uint8_t SPI_CRC);
  373. uint16_t SPI_GetCRCPoly(SPI_Module* SPIx);
  374. void SPI_ConfigBidirectionalMode(SPI_Module* SPIx, uint16_t DataDirection);
  375. FlagStatus SPI_I2S_GetStatus(SPI_Module* SPIx, uint16_t SPI_I2S_FLAG);
  376. void SPI_I2S_ClrCRCErrFlag(SPI_Module* SPIx, uint16_t SPI_I2S_FLAG);
  377. INTStatus SPI_I2S_GetIntStatus(SPI_Module* SPIx, uint8_t SPI_I2S_IT);
  378. void SPI_I2S_ClrITPendingBit(SPI_Module* SPIx, uint8_t SPI_I2S_IT);
  379. #ifdef __cplusplus
  380. }
  381. #endif
  382. #endif /*__N32G45X_SPI_H__ */
  383. /**
  384. * @}
  385. */
  386. /**
  387. * @}
  388. */
  389. /**
  390. * @}
  391. */