apm32f4xx_spi.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*!
  2. * @file apm32f4x_spi.h
  3. *
  4. * @brief This file contains all the functions prototypes for the SPI firmware library
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-06-23
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F4XX_SPI_H
  27. #define __APM32F4XX_SPI_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes */
  32. #include "apm32f4xx.h"
  33. /** @addtogroup APM32F4xx_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup SPI_Driver
  37. @{
  38. */
  39. /** @defgroup SPI_Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief SPI data direction mode
  44. *
  45. * @note BIT0 refer to 1 line or 2 lines
  46. * BIT4 refer to Tx or Rx in 1 line
  47. * BIT8 refer to receive only or full-duplex in 2 lines
  48. */
  49. typedef enum
  50. {
  51. SPI_DIRECTION_2LINES_FULLDUPLEX = 0x0000, /*!< 2 lines full duplex */
  52. SPI_DIRECTION_2LINES_RXONLY = 0x0100, /*!< 2 lines but receive only */
  53. SPI_DIRECTION_1LINE_RX = 0x0001, /*!< 1 line receive only */
  54. SPI_DIRECTION_1LINE_TX = 0x0011 /*!< 1 line send only */
  55. } SPI_DIRECTION_T;
  56. /**
  57. * @brief SPI mode
  58. */
  59. typedef enum
  60. {
  61. SPI_MODE_SLAVE, /*!< Slave mode */
  62. SPI_MODE_MASTER /*!< Master mode */
  63. } SPI_MODE_T;
  64. /**
  65. * @brief SPI Data length
  66. */
  67. typedef enum
  68. {
  69. SPI_DATA_LENGTH_8B, /*!< Set data frame format to 8bit */
  70. SPI_DATA_LENGTH_16B /*!< Set data frame format to 16bit */
  71. } SPI_DATA_LENGTH_T;
  72. /**
  73. * @brief SPI Clock Polarity
  74. */
  75. typedef enum
  76. {
  77. SPI_CLKPOL_LOW, /*!< Set SCK polarity to low in idle state */
  78. SPI_CLKPOL_HIGH /*!< Set SCK polarity to high in idle state */
  79. } SPI_CLKPOL_T;
  80. /**
  81. * @brief SPI Clock Phase
  82. */
  83. typedef enum
  84. {
  85. SPI_CLKPHA_1EDGE, /*!< Sample value in the first clock edge */
  86. SPI_CLKPHA_2EDGE /*!< Sample value in the second clock edge */
  87. } SPI_CLKPHA_T;
  88. /**
  89. * @brief SPI Slave Select management
  90. */
  91. typedef enum
  92. {
  93. SPI_NSS_HARD, /*!< Disable software NSS mode */
  94. SPI_NSS_SOFT /*!< Enable software NSS mode */
  95. } SPI_NSS_T;
  96. /**
  97. * @brief SPI BaudRate Prescaler
  98. */
  99. typedef enum
  100. {
  101. SPI_BAUDRATE_DIV_2, /*!< BaudRate DIV = 2 */
  102. SPI_BAUDRATE_DIV_4, /*!< BaudRate DIV = 4 */
  103. SPI_BAUDRATE_DIV_8, /*!< BaudRate DIV = 8 */
  104. SPI_BAUDRATE_DIV_16, /*!< BaudRate DIV = 16 */
  105. SPI_BAUDRATE_DIV_32, /*!< BaudRate DIV = 32 */
  106. SPI_BAUDRATE_DIV_64, /*!< BaudRate DIV = 64 */
  107. SPI_BAUDRATE_DIV_128, /*!< BaudRate DIV = 128 */
  108. SPI_BAUDRATE_DIV_256, /*!< BaudRate DIV = 256 */
  109. } SPI_BAUDRATE_DIV_T;
  110. /**
  111. * @brief SPI MSB LSB transmission
  112. */
  113. typedef enum
  114. {
  115. SPI_FIRSTBIT_MSB, /*!< MSB first */
  116. SPI_FIRSTBIT_LSB /*!< LSB first */
  117. } SPI_FIRSTBIT_T;
  118. /**
  119. * @brief I2S Mode
  120. */
  121. typedef enum
  122. {
  123. I2S_MODE_SLAVE_TX, /*!< Slave transmit mode */
  124. I2S_MODE_SLAVE_RX, /*!< Slave receive mode */
  125. I2S_MODE_MASTER_TX, /*!< Master transmit mode */
  126. I2S_MODE_MASTER_RX /*!< Master receive mode */
  127. } I2S_MODE_T;
  128. /**
  129. * @brief I2S Standard
  130. */
  131. typedef enum
  132. {
  133. I2S_STANDARD_PHILLIPS = 0x00, /*!< I2S phillips mode */
  134. I2S_STANDARD_MSB = 0x01, /*!< MSB alignment mode */
  135. I2S_STANDARD_LSB = 0x02, /*!< LSB alignment mode */
  136. I2S_STANDARD_PCMSHORT = 0x03, /*!< PCB short frame synchronization mode */
  137. I2S_STANDARD_PCMLONG = 0x13 /*!< PCB long frame synchronization mode */
  138. } I2S_STANDARD_T;
  139. /**
  140. * @brief I2S data length
  141. */
  142. typedef enum
  143. {
  144. I2S_DATA_LENGHT_16B = 0x00, /*!< Set the length of data to 16-bit */
  145. I2S_DATA_LENGHT_16BEX = 0x01, /*!< Set the length of data to 16-bit extended */
  146. I2S_DATA_LENGHT_24B = 0x03, /*!< Set the length of data to 24-bit */
  147. I2S_DATA_LENGHT_32B = 0x05, /*!< Set the length of data to 32-bit */
  148. } I2S_DATA_LENGTH_T;
  149. /**
  150. * @brief I2S MCLK Output
  151. */
  152. typedef enum
  153. {
  154. I2S_MCLK_OUTPUT_DISABLE, /*!< Disable I2S MCLK output */
  155. I2S_MCLK_OUTPUT_ENABLE /*!< Enable I2S MCLK output */
  156. } I2S_MCLK_OUTPUT_T;
  157. /**
  158. * @brief I2S Audio divider
  159. */
  160. typedef enum
  161. {
  162. I2S_AUDIO_DIV_192K = 192000, /*!< Audio div = 192K */
  163. I2S_AUDIO_DIV_96K = 96000, /*!< Audio div = 96K */
  164. I2S_AUDIO_DIV_48K = 48000, /*!< Audio div = 48K */
  165. I2S_AUDIO_DIV_44K = 44100, /*!< Audio div = 44.1K */
  166. I2S_AUDIO_DIV_32K = 32000, /*!< Audio div = 32K */
  167. I2S_AUDIO_DIV_22K = 22050, /*!< Audio div = 22.05K */
  168. I2S_AUDIO_DIV_16K = 16000, /*!< Audio div = 16K */
  169. I2S_AUDIO_DIV_11K = 11025, /*!< Audio div = 11.025K */
  170. I2S_AUDIO_DIV_8K = 8000, /*!< Audio div = 8K */
  171. I2S_AUDIO_DIV_DEFAULT = 2 /*!< Audio div = 2 */
  172. } I2S_AUDIO_DIV_T;
  173. /**
  174. * @brief I2S Clock Polarity
  175. */
  176. typedef enum
  177. {
  178. I2S_CLKPOL_LOW, /*!< Set clock polarity to low */
  179. I2S_CLKPOL_HIGH /*!< Set clock polarity to high */
  180. } I2S_CLKPOL_T;
  181. /**
  182. * @brief SPI I2S DMA requests
  183. */
  184. typedef enum
  185. {
  186. SPI_I2S_DMA_REQ_TX, /*!< Tx buffer DMA transfer request */
  187. SPI_I2S_DMA_REQ_RX /*!< Rx buffer DMA transfer request */
  188. } SPI_I2S_DMA_REQ_T;
  189. /**
  190. * @brief SPI Direction select
  191. */
  192. typedef enum
  193. {
  194. SPI_DIRECTION_RX, /*!< Selects Rx receive direction */
  195. SPI_DIRECTION_TX /*!< Selects Tx transmission direction */
  196. } SPI_DIRECTION_SELECT_T;
  197. /**
  198. * @brief SPI interrupts definition
  199. */
  200. typedef enum
  201. {
  202. SPI_I2S_INT_TXBE = 0x8002, /*!< Tx buffer empty interrupt */
  203. SPI_I2S_INT_RXBNE = 0x4001, /*!< Rx buffer not empty interrupt */
  204. SPI_I2S_INT_ERR = 0x2000, /*!< Error interrupt */
  205. I2S_INT_UDR = 0x2008, /*!< Underrun Error interrupt flag */
  206. SPI_INT_CRCE = 0x2010, /*!< CRC Error interrupt flag */
  207. SPI_INT_ME = 0x2020, /*!< Mode Error interrupt flag */
  208. SPI_I2S_INT_OVR = 0x2040 /*!< Overrun interrupt flag */
  209. } SPI_I2S_INT_T;
  210. /**
  211. * @brief SPI flags definition
  212. */
  213. typedef enum
  214. {
  215. SPI_FLAG_RXBNE = 0x0001, /*!< Receive buffer not empty flag */
  216. SPI_FLAG_TXBE = 0x0002, /*!< Transmit buffer empty flag */
  217. I2S_FLAG_SCHDIR = 0x0004, /*!< Side Channel flag */
  218. I2S_FLAG_UDR = 0x0008, /*!< Underrun Error flag */
  219. SPI_FLAG_CRCE = 0x0010, /*!< CRC Error flag */
  220. SPI_FLAG_ME = 0x0020, /*!< Mode Error flag */
  221. SPI_FLAG_OVR = 0x0040, /*!< Overrun flag */
  222. SPI_FLAG_BSY = 0x0080 /*!< Busy flag */
  223. } SPI_FLAG_T;
  224. /**@} end of group SPI_Enumerations*/
  225. /** @addtogroup SPI_Structure Data Structure
  226. @{
  227. */
  228. /**
  229. * @brief SPI Config structure definition
  230. */
  231. typedef struct
  232. {
  233. SPI_MODE_T mode; /*!< SPI mode selection */
  234. SPI_DATA_LENGTH_T length; /*!< SPI data length selection */
  235. SPI_CLKPHA_T phase; /*!< SPI clock phase selection */
  236. SPI_CLKPOL_T polarity; /*!< SPI clock polarity selection */
  237. SPI_NSS_T nss; /*!< Set SPI software NSS */
  238. SPI_FIRSTBIT_T firstBit; /*!< Set first bit */
  239. SPI_DIRECTION_T direction; /*!< SPI data direction mode selection */
  240. SPI_BAUDRATE_DIV_T baudrateDiv; /*!< SPI BaudRate Prescaler selection */
  241. uint16_t crcPolynomial; /*!< Setup CRC polynomial value */
  242. } SPI_Config_T;
  243. /**
  244. * @brief I2S Config structure definition
  245. */
  246. typedef struct
  247. {
  248. I2S_MODE_T mode; /*!< I2S mode selection */
  249. I2S_STANDARD_T standard; /*!< I2S standard selection */
  250. I2S_DATA_LENGTH_T length; /*!< Set I2S data length */
  251. I2S_MCLK_OUTPUT_T MCLKOutput; /*!< Set I2S MCLK output */
  252. I2S_AUDIO_DIV_T audioDiv; /*!< I2S Audio divider selection */
  253. I2S_CLKPOL_T polarity; /*!< I2S Clock polarity selection */
  254. } I2S_Config_T;
  255. /**@} end of group SPI_Structure*/
  256. /** @defgroup SPI_Functions
  257. @{
  258. */
  259. /* Reset */
  260. void SPI_I2S_Reset(SPI_T* spi);
  261. /* Initialization and Configuration */
  262. void SPI_Config(SPI_T* spi, SPI_Config_T* spiConfig);
  263. void I2S_Config(SPI_T* spi, I2S_Config_T* i2sConfig);
  264. void SPI_ConfigStructInit(SPI_Config_T* spiConfig);
  265. void I2S_ConfigStructInit(I2S_Config_T* i2sConfig);
  266. void SPI_Enable(SPI_T* spi);
  267. void SPI_Disable(SPI_T* spi);
  268. void I2S_Enable(SPI_T* spi);
  269. void I2S_Disable(SPI_T* spi);
  270. void SPI_ConfigDataSize(SPI_T* spi, SPI_DATA_LENGTH_T length);
  271. void SPI_SetSoftwareNSS(SPI_T* spi);
  272. void SPI_ResetSoftwareNSS(SPI_T* spi);
  273. void SPI_EnableSSOutput(SPI_T* spi);
  274. void SPI_DisableSSOutput(SPI_T* spi);
  275. void SPI_ConfigBiDirectionalLine(SPI_T* spi, SPI_DIRECTION_SELECT_T direction);
  276. void SPI_EnableTIMode(SPI_T* spi);
  277. void SPI_DisableTIMode(SPI_T* spi);
  278. void I2S_ConfigFullDuplex(SPI_T* spi, I2S_Config_T* i2sConfig);
  279. /* Data transfers */
  280. void SPI_I2S_TxData(SPI_T* spi, uint16_t data);
  281. uint16_t SPI_I2S_RxData(SPI_T* spi);
  282. /* Hardware CRC Calculation */
  283. void SPI_EnableCRC(SPI_T* spi);
  284. void SPI_DisableCRC(SPI_T* spi);
  285. void SPI_TxCRC(SPI_T* spi);
  286. uint16_t SPI_ReadTxCRC(SPI_T* spi);
  287. uint16_t SPI_ReadRxCRC(SPI_T* spi);
  288. uint16_t SPI_ReadCRCPolynomial(SPI_T* spi);
  289. /* DMA transfers */
  290. void SPI_I2S_EnableDMA(SPI_T* spi, SPI_I2S_DMA_REQ_T dmaReq);
  291. void SPI_I2S_DisableDMA(SPI_T* spi, SPI_I2S_DMA_REQ_T dmaReq);
  292. /* Interrupts and flags */
  293. void SPI_I2S_EnableInterrupt(SPI_T* spi, uint32_t interrupt);
  294. void SPI_I2S_DisableInterrupt(SPI_T* spi, uint32_t interrupt);
  295. uint8_t SPI_I2S_ReadStatusFlag(SPI_T* spi, SPI_FLAG_T flag);
  296. void SPI_I2S_ClearStatusFlag(SPI_T* spi, SPI_FLAG_T flag);
  297. uint8_t SPI_I2S_ReadIntFlag(SPI_T* spi, SPI_I2S_INT_T flag);
  298. void SPI_I2S_ClearIntFlag(SPI_T* spi, SPI_I2S_INT_T flag);
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* __APM32F4XX_SPI_H */
  303. /**@} end of group SPI_Enumerations */
  304. /**@} end of group SPI_Driver */
  305. /**@} end of group APM32F4xx_StdPeriphDriver */