1
0

gd32f10x_spi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. ******************************************************************************
  3. * @brief SPI header file of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Define to prevent recursive inclusion -------------------------------------*/
  7. #ifndef __GD32F10X_SPI_H
  8. #define __GD32F10X_SPI_H
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /* Includes ------------------------------------------------------------------*/
  13. #include "gd32f10x.h"
  14. /** @addtogroup GD32F10x_Firmware
  15. * @{
  16. */
  17. /** @addtogroup SPI
  18. * @{
  19. */
  20. /** @defgroup SPI_Exported_Types
  21. * @{
  22. */
  23. /**
  24. * @brief SPI Initial Parameters
  25. */
  26. typedef struct {
  27. uint16_t SPI_TransType; /*!< The transfer type, choose one from @ref SPI_transfer_type. */
  28. uint16_t SPI_Mode; /*!< The operating mode, choose one from @ref SPI_mode. */
  29. uint16_t SPI_FrameFormat; /*!< The SPI data frame format, choose one from @ref SPI_data_frame_format. */
  30. uint16_t SPI_SCKPL; /*!< The clock polarity in idel state, choose one from @ref SPI_Clock_Polarity. */
  31. uint16_t SPI_SCKPH; /*!< The clock phase, choose one from @ref SPI_Clock_Phase. */
  32. uint16_t SPI_SWNSSEN; /*!< The NSS signal management, choose one from @ref SPI_Software_NSS_management. */
  33. uint16_t SPI_PSC; /*!< The Baud Rate prescaler value, choose one from @ref SPI_BaudRate_Prescaler. */
  34. uint16_t SPI_FirstBit; /*!< The data transfers start from MSB or LSB bit, choose one from @ref SPI_MSB_LSB_transmission. */
  35. uint16_t SPI_CRCPOL; /*!< The polynomial used for the CRC calculation. */
  36. } SPI_InitPara;
  37. /**
  38. * @brief I2S Initial Parameters
  39. */
  40. typedef struct {
  41. uint16_t I2S_Mode; /*!< The operating mode and transfer direction, choose one from @ref I2S_Mode. */
  42. uint16_t I2S_STD; /*!< The I2S standard, choose one from @ref I2S_Standard. */
  43. uint16_t I2S_FrameFormat; /*!< The I2S data length and channel length, choose one from @ref I2S_Data_Format. */
  44. uint16_t I2S_MCKOE; /*!< The I2S MCK output is enabled or disable, choose one from @ref I2S_MCLK_Output. */
  45. uint32_t I2S_AudioFreq; /*!< The audio sampling frequency, choose one from @ref I2S_Audio_Frequency. */
  46. uint16_t I2S_CKPL; /*!< The clock polarity in idel state, choose one from @ref I2S_Clock_Polarity. */
  47. } I2S_InitPara;
  48. /**
  49. * @}
  50. */
  51. /** @defgroup SPI_Exported_Constants
  52. * @{
  53. */
  54. /** @defgroup SPI_transfer_type
  55. * @{
  56. */
  57. #define SPI_TRANSTYPE_FULLDUPLEX ((uint16_t)0x0000)
  58. #define SPI_TRANSTYPE_RXONLY SPI_CTLR1_RO
  59. #define SPI_TRANSTYPE_BDMRX SPI_CTLR1_BDM
  60. #define SPI_TRANSTYPE_BDMTX (SPI_CTLR1_BDM | SPI_CTLR1_BDOE)
  61. /**
  62. * @}
  63. */
  64. /** @defgroup SPI_mode
  65. * @{
  66. */
  67. #define SPI_MODE_MASTER (SPI_CTLR1_MSTMODE|SPI_CTLR1_SWNSS)
  68. #define SPI_MODE_SLAVE ((uint16_t)0x0000)
  69. /**
  70. * @}
  71. */
  72. /** @defgroup SPI_data_frame_format
  73. * @{
  74. */
  75. #define SPI_FRAMEFORMAT_16BIT SPI_CTLR1_FF16
  76. #define SPI_FRAMEFORMAT_8BIT ((uint16_t)0x0000)
  77. /**
  78. * @}
  79. */
  80. /** @defgroup SPI_Clock_Polarity
  81. * @{
  82. */
  83. #define SPI_SCKPL_LOW ((uint16_t)0x0000)
  84. #define SPI_SCKPL_HIGH SPI_CTLR1_SCKPL
  85. /**
  86. * @}
  87. */
  88. /** @defgroup SPI_Clock_Phase
  89. * @{
  90. */
  91. #define SPI_SCKPH_1EDGE ((uint16_t)0x0000)
  92. #define SPI_SCKPH_2EDGE SPI_CTLR1_SCKPH
  93. /**
  94. * @}
  95. */
  96. /** @defgroup SPI_Software_NSS_management
  97. * @{
  98. */
  99. #define SPI_SWNSS_SOFT SPI_CTLR1_SWNSSEN
  100. #define SPI_SWNSS_HARD ((uint16_t)0x0000)
  101. /**
  102. * @}
  103. */
  104. /** @defgroup SPI_BaudRate_Prescaler
  105. * @{
  106. */
  107. #define SPI_PSC_2 ((uint16_t)0x0000)
  108. #define SPI_PSC_4 ((uint16_t)0x0008)
  109. #define SPI_PSC_8 ((uint16_t)0x0010)
  110. #define SPI_PSC_16 ((uint16_t)0x0018)
  111. #define SPI_PSC_32 ((uint16_t)0x0020)
  112. #define SPI_PSC_64 ((uint16_t)0x0028)
  113. #define SPI_PSC_128 ((uint16_t)0x0030)
  114. #define SPI_PSC_256 ((uint16_t)0x0038)
  115. /**
  116. * @}
  117. */
  118. /** @defgroup SPI_MSB_LSB_transmission
  119. * @{
  120. */
  121. #define SPI_FIRSTBIT_MSB ((uint16_t)0x0000)
  122. #define SPI_FIRSTBIT_LSB SPI_CTLR1_LF
  123. /**
  124. * @}
  125. */
  126. /** @defgroup I2S_Mode
  127. * @{
  128. */
  129. #define I2S_MODE_SLAVETX ((uint16_t)0x0000)
  130. #define I2S_MODE_SLAVERX ((uint16_t)0x0100)
  131. #define I2S_MODE_MASTERTX ((uint16_t)0x0200)
  132. #define I2S_MODE_MASTERRX ((uint16_t)0x0300)
  133. /**
  134. * @}
  135. */
  136. /** @defgroup I2S_Standard
  137. * @{
  138. */
  139. #define I2S_STD_PHILLIPS ((uint16_t)0x0000)
  140. #define I2S_STD_MSB ((uint16_t)0x0010)
  141. #define I2S_STD_LSB ((uint16_t)0x0020)
  142. #define I2S_STD_PCMSHORT ((uint16_t)0x0030)
  143. #define I2S_STD_PCMLONG ((uint16_t)0x00B0)
  144. /**
  145. * @}
  146. */
  147. /** @defgroup I2S_Data_Format
  148. * @{
  149. */
  150. #define I2S_FRAMEFORMAT_DL16b_CL16b ((uint16_t)0x0000)
  151. #define I2S_FRAMEFORMAT_DL16b_CL32b ((uint16_t)0x0001)
  152. #define I2S_FRAMEFORMAT_DL24b_CL32b ((uint16_t)0x0003)
  153. #define I2S_FRAMEFORMAT_DL32b_CL32b ((uint16_t)0x0005)
  154. /**
  155. * @}
  156. */
  157. /** @defgroup I2S_MCLK_Output
  158. * @{
  159. */
  160. #define I2S_MCK_ENABLE SPI_I2SCKP_MCKOE
  161. #define I2S_MCK_DISABLE ((uint16_t)0x0000)
  162. /**
  163. * @}
  164. */
  165. /** @defgroup I2S_Audio_Frequency
  166. * @{
  167. */
  168. #define I2S_AUDIOFREQ_DEFAULT ((uint32_t)2)
  169. #define I2S_AUDIOFREQ_8K ((uint32_t)8000)
  170. #define I2S_AUDIOFREQ_11K ((uint32_t)11025)
  171. #define I2S_AUDIOFREQ_16K ((uint32_t)16000)
  172. #define I2S_AUDIOFREQ_22K ((uint32_t)22050)
  173. #define I2S_AUDIOFREQ_32K ((uint32_t)32000)
  174. #define I2S_AUDIOFREQ_44K ((uint32_t)44100)
  175. #define I2S_AUDIOFREQ_48K ((uint32_t)48000)
  176. #define I2S_AUDIOFREQ_96K ((uint32_t)96000)
  177. #define I2S_AUDIOFREQ_192K ((uint32_t)192000)
  178. /**
  179. * @}
  180. */
  181. /** @defgroup I2S_Clock_Polarity
  182. * @{
  183. */
  184. #define I2S_CKPL_LOW ((uint16_t)0x0000)
  185. #define I2S_CKPL_HIGH SPI_I2SCTLR_CKPL
  186. /**
  187. * @}
  188. */
  189. /** @defgroup SPI_I2S_DMA_transfer_requests
  190. * @{
  191. */
  192. #define SPI_I2S_DMA_TX SPI_CTLR2_DMATE
  193. #define SPI_I2S_DMA_RX SPI_CTLR2_DMARE
  194. /**
  195. * @}
  196. */
  197. /** @defgroup SPI_NSS_internal_software_management
  198. * @{
  199. */
  200. #define SPI_SWNSS_SET ((uint16_t)0x0001)
  201. #define SPI_SWNSS_RESET ((uint16_t)0x0000)
  202. /**
  203. * @}
  204. */
  205. /** @defgroup SPI_CRC_Transmit_Receive
  206. * @{
  207. */
  208. #define SPI_CRC_TX ((uint8_t)0x00)
  209. #define SPI_CRC_RX ((uint8_t)0x01)
  210. /**
  211. * @}
  212. */
  213. /** @defgroup SPI_direction_transmit_receive
  214. * @{
  215. */
  216. #define SPI_BDOE_RX (~SPI_CTLR1_BDOE)
  217. #define SPI_BDOE_TX SPI_CTLR1_BDOE
  218. /**
  219. * @}
  220. */
  221. /** @defgroup SPI_I2S_interrupts_definition
  222. * @{
  223. */
  224. #define SPI_I2S_INT_TBE ((uint8_t)0x71)
  225. #define SPI_I2S_INT_RBNE ((uint8_t)0x60)
  226. #define SPI_I2S_INT_ERR ((uint8_t)0x50)
  227. #define SPI_I2S_INT_OVR ((uint8_t)0x56)
  228. #define SPI_INT_MODF ((uint8_t)0x55)
  229. #define SPI_INT_CRCERR ((uint8_t)0x54)
  230. #define I2S_INT_UDR ((uint8_t)0x53)
  231. /**
  232. * @}
  233. */
  234. /** @defgroup SPI_I2S_flags_definition
  235. * @{
  236. */
  237. #define SPI_FLAG_RBNE SPI_STR_RBNE
  238. #define SPI_FLAG_TBE SPI_STR_TBE
  239. #define SPI_FLAG_CRCERR SPI_STR_CRCE
  240. #define SPI_FLAG_MODF SPI_STR_CONFE
  241. #define SPI_FLAG_OVR SPI_STR_RXORE
  242. #define SPI_FLAG_BSY SPI_STR_TRANS
  243. #define I2S_FLAG_RBNE SPI_STR_RBNE
  244. #define I2S_FLAG_TBE SPI_STR_TBE
  245. #define I2S_FLAG_CHSIDE SPI_STR_I2SCH
  246. #define I2S_FLAG_UDR SPI_STR_TXURE
  247. #define I2S_FLAG_OVR SPI_STR_RXORE
  248. #define I2S_FLAG_BSY SPI_STR_TRANS
  249. /**
  250. * @}
  251. */
  252. /**
  253. * @}
  254. */
  255. /** @defgroup SPI_Exported_Functions
  256. * @{
  257. */
  258. void SPI_I2S_DeInit(SPI_TypeDef *SPIx);
  259. void SPI_Init(SPI_TypeDef *SPIx, SPI_InitPara *SPI_InitParameter);
  260. void I2S_Init(SPI_TypeDef *SPIx, I2S_InitPara *I2S_InitParameter);
  261. void SPI_ParaInit(SPI_InitPara *SPI_InitParameter);
  262. void I2S_ParaInit(I2S_InitPara *I2S_InitParameter);
  263. void SPI_Enable(SPI_TypeDef *SPIx, TypeState NewValue);
  264. void I2S_Enable(SPI_TypeDef *SPIx, TypeState NewValue);
  265. void SPI_I2S_INTConfig(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT, TypeState NewValue);
  266. void SPI_I2S_DMA_Enable(SPI_TypeDef *SPIx, uint16_t SPI_I2S_DMAReq, TypeState NewValue);
  267. void SPI_I2S_SendData(SPI_TypeDef *SPIx, uint16_t Data);
  268. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef *SPIx);
  269. void SPI_SWNSSConfig(SPI_TypeDef *SPIx, uint16_t SPI_SWNSS);
  270. void SPI_NSSDRV(SPI_TypeDef *SPIx, TypeState NewValue);
  271. void SPI_FrameFormatConfig(SPI_TypeDef *SPIx, uint16_t SPI_DataSize);
  272. void SPI_SendCRCNext(SPI_TypeDef *SPIx);
  273. void SPI_CRC_Enable(SPI_TypeDef *SPIx, TypeState NewValue);
  274. uint16_t SPI_GetCRC(SPI_TypeDef *SPIx, uint8_t SPI_CRC);
  275. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef *SPIx);
  276. void SPI_BDOEConfig(SPI_TypeDef *SPIx, uint16_t SPI_BDOE);
  277. TypeState SPI_I2S_GetBitState(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG);
  278. void SPI_I2S_ClearBitState(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG);
  279. TypeState SPI_I2S_GetIntBitState(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT);
  280. void SPI_I2S_ClearIntBitState(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT);
  281. #ifdef __cplusplus
  282. }
  283. #endif
  284. #endif /*__GD32F10X_SPI_H */
  285. /**
  286. * @}
  287. */
  288. /**
  289. * @}
  290. */
  291. /**
  292. * @}
  293. */