at32f413_spi.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /**
  2. **************************************************************************
  3. * @file at32f413_spi.h
  4. * @brief at32f413 spi header file
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. /* Define to prevent recursive inclusion -------------------------------------*/
  25. #ifndef __AT32F413_SPI_H
  26. #define __AT32F413_SPI_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* Includes ------------------------------------------------------------------*/
  31. #include "at32f413.h"
  32. /** @addtogroup AT32F413_periph_driver
  33. * @{
  34. */
  35. /** @addtogroup SPI
  36. * @{
  37. */
  38. /**
  39. * @defgroup SPI_I2S_flags_definition
  40. * @brief spi i2s flag
  41. * @{
  42. */
  43. #define SPI_I2S_RDBF_FLAG 0x0001 /*!< spi or i2s receive data buffer full flag */
  44. #define SPI_I2S_TDBE_FLAG 0x0002 /*!< spi or i2s transmit data buffer empty flag */
  45. #define I2S_ACS_FLAG 0x0004 /*!< i2s audio channel state flag */
  46. #define I2S_TUERR_FLAG 0x0008 /*!< i2s transmitter underload error flag */
  47. #define SPI_CCERR_FLAG 0x0010 /*!< spi crc calculation error flag */
  48. #define SPI_MMERR_FLAG 0x0020 /*!< spi master mode error flag */
  49. #define SPI_I2S_ROERR_FLAG 0x0040 /*!< spi or i2s receiver overflow error flag */
  50. #define SPI_I2S_BF_FLAG 0x0080 /*!< spi or i2s busy flag */
  51. /**
  52. * @}
  53. */
  54. /**
  55. * @defgroup SPI_I2S_interrupts_definition
  56. * @brief spi i2s interrupt
  57. * @{
  58. */
  59. #define SPI_I2S_ERROR_INT 0x0020 /*!< error interrupt */
  60. #define SPI_I2S_RDBF_INT 0x0040 /*!< receive data buffer full interrupt */
  61. #define SPI_I2S_TDBE_INT 0x0080 /*!< transmit data buffer empty interrupt */
  62. /**
  63. * @}
  64. */
  65. /** @defgroup SPI_exported_types
  66. * @{
  67. */
  68. /**
  69. * @brief spi frame bit num type
  70. */
  71. typedef enum
  72. {
  73. SPI_FRAME_8BIT = 0x00, /*!< 8-bit data frame format */
  74. SPI_FRAME_16BIT = 0x01 /*!< 16-bit data frame format */
  75. } spi_frame_bit_num_type;
  76. /**
  77. * @brief spi master/slave mode type
  78. */
  79. typedef enum
  80. {
  81. SPI_MODE_SLAVE = 0x00, /*!< select as slave mode */
  82. SPI_MODE_MASTER = 0x01 /*!< select as master mode */
  83. } spi_master_slave_mode_type;
  84. /**
  85. * @brief spi clock polarity (clkpol) type
  86. */
  87. typedef enum
  88. {
  89. SPI_CLOCK_POLARITY_LOW = 0x00, /*!< sck keeps low at idle state */
  90. SPI_CLOCK_POLARITY_HIGH = 0x01 /*!< sck keeps high at idle state */
  91. } spi_clock_polarity_type;
  92. /**
  93. * @brief spi clock phase (clkpha) type
  94. */
  95. typedef enum
  96. {
  97. SPI_CLOCK_PHASE_1EDGE = 0x00, /*!< data capture start from the first clock edge */
  98. SPI_CLOCK_PHASE_2EDGE = 0x01 /*!< data capture start from the second clock edge */
  99. } spi_clock_phase_type;
  100. /**
  101. * @brief spi cs mode type
  102. */
  103. typedef enum
  104. {
  105. SPI_CS_HARDWARE_MODE = 0x00, /*!< cs is hardware mode */
  106. SPI_CS_SOFTWARE_MODE = 0x01 /*!< cs is software mode */
  107. } spi_cs_mode_type;
  108. /**
  109. * @brief spi master clock frequency division type
  110. */
  111. typedef enum
  112. {
  113. SPI_MCLK_DIV_2 = 0x00, /*!< master clock frequency division 2 */
  114. SPI_MCLK_DIV_4 = 0x01, /*!< master clock frequency division 4 */
  115. SPI_MCLK_DIV_8 = 0x02, /*!< master clock frequency division 8 */
  116. SPI_MCLK_DIV_16 = 0x03, /*!< master clock frequency division 16 */
  117. SPI_MCLK_DIV_32 = 0x04, /*!< master clock frequency division 32 */
  118. SPI_MCLK_DIV_64 = 0x05, /*!< master clock frequency division 64 */
  119. SPI_MCLK_DIV_128 = 0x06, /*!< master clock frequency division 128 */
  120. SPI_MCLK_DIV_256 = 0x07, /*!< master clock frequency division 256 */
  121. SPI_MCLK_DIV_512 = 0x08, /*!< master clock frequency division 512 */
  122. SPI_MCLK_DIV_1024 = 0x09 /*!< master clock frequency division 1024 */
  123. } spi_mclk_freq_div_type;
  124. /**
  125. * @brief spi transmit first bit (lsb/msb) type
  126. */
  127. typedef enum
  128. {
  129. SPI_FIRST_BIT_MSB = 0x00, /*!< the frame format is msb first */
  130. SPI_FIRST_BIT_LSB = 0x01 /*!< the frame format is lsb first */
  131. } spi_first_bit_type;
  132. /**
  133. * @brief spi transmission mode type
  134. */
  135. typedef enum
  136. {
  137. SPI_TRANSMIT_FULL_DUPLEX = 0x00, /*!< dual line unidirectional full-duplex mode(slben = 0 and ora = 0) */
  138. SPI_TRANSMIT_SIMPLEX_RX = 0x01, /*!< dual line unidirectional simplex receive-only mode(slben = 0 and ora = 1) */
  139. SPI_TRANSMIT_HALF_DUPLEX_RX = 0x02, /*!< single line bidirectional half duplex mode-receiving(slben = 1 and slbtd = 0) */
  140. SPI_TRANSMIT_HALF_DUPLEX_TX = 0x03 /*!< single line bidirectional half duplex mode-transmitting(slben = 1 and slbtd = 1) */
  141. } spi_transmission_mode_type;
  142. /**
  143. * @brief spi crc direction type
  144. */
  145. typedef enum
  146. {
  147. SPI_CRC_RX = 0x0014, /*!< crc direction is rx */
  148. SPI_CRC_TX = 0x0018 /*!< crc direction is tx */
  149. } spi_crc_direction_type;
  150. /**
  151. * @brief spi single line bidirectional direction type
  152. */
  153. typedef enum
  154. {
  155. SPI_HALF_DUPLEX_DIRECTION_RX = 0x00, /*!< single line bidirectional half duplex mode direction: receive(slbtd = 0) */
  156. SPI_HALF_DUPLEX_DIRECTION_TX = 0x01 /*!< single line bidirectional half duplex mode direction: transmit(slbtd = 1) */
  157. } spi_half_duplex_direction_type;
  158. /**
  159. * @brief spi software cs internal level type
  160. */
  161. typedef enum
  162. {
  163. SPI_SWCS_INTERNAL_LEVEL_LOW = 0x00, /*!< internal level low */
  164. SPI_SWCS_INTERNAL_LEVEL_HIGHT = 0x01 /*!< internal level high */
  165. } spi_software_cs_level_type;
  166. /**
  167. * @brief i2s audio protocol type
  168. */
  169. typedef enum
  170. {
  171. I2S_AUDIO_PROTOCOL_PHILLIPS = 0x00, /*!< i2s philip standard */
  172. I2S_AUDIO_PROTOCOL_MSB = 0x01, /*!< msb-justified standard */
  173. I2S_AUDIO_PROTOCOL_LSB = 0x02, /*!< lsb-justified standard */
  174. I2S_AUDIO_PROTOCOL_PCM_SHORT = 0x03, /*!< pcm standard-short frame */
  175. I2S_AUDIO_PROTOCOL_PCM_LONG = 0x04 /*!< pcm standard-long frame */
  176. } i2s_audio_protocol_type;
  177. /**
  178. * @brief i2s audio frequency type
  179. */
  180. typedef enum
  181. {
  182. I2S_AUDIO_FREQUENCY_DEFAULT = 2, /*!< i2s audio sampling frequency default */
  183. I2S_AUDIO_FREQUENCY_8K = 8000, /*!< i2s audio sampling frequency 8k */
  184. I2S_AUDIO_FREQUENCY_11_025K = 11025, /*!< i2s audio sampling frequency 11.025k */
  185. I2S_AUDIO_FREQUENCY_16K = 16000, /*!< i2s audio sampling frequency 16k */
  186. I2S_AUDIO_FREQUENCY_22_05K = 22050, /*!< i2s audio sampling frequency 22.05k */
  187. I2S_AUDIO_FREQUENCY_32K = 32000, /*!< i2s audio sampling frequency 32k */
  188. I2S_AUDIO_FREQUENCY_44_1K = 44100, /*!< i2s audio sampling frequency 44.1k */
  189. I2S_AUDIO_FREQUENCY_48K = 48000, /*!< i2s audio sampling frequency 48k */
  190. I2S_AUDIO_FREQUENCY_96K = 96000, /*!< i2s audio sampling frequency 96k */
  191. I2S_AUDIO_FREQUENCY_192K = 192000 /*!< i2s audio sampling frequency 192k */
  192. } i2s_audio_sampling_freq_type;
  193. /**
  194. * @brief i2s data bit num and channel bit num type
  195. */
  196. typedef enum
  197. {
  198. I2S_DATA_16BIT_CHANNEL_16BIT = 0x01, /*!< 16-bit data packed in 16-bit channel frame */
  199. I2S_DATA_16BIT_CHANNEL_32BIT = 0x02, /*!< 16-bit data packed in 32-bit channel frame */
  200. I2S_DATA_24BIT_CHANNEL_32BIT = 0x03, /*!< 24-bit data packed in 32-bit channel frame */
  201. I2S_DATA_32BIT_CHANNEL_32BIT = 0x04 /*!< 32-bit data packed in 32-bit channel frame */
  202. } i2s_data_channel_format_type;
  203. /**
  204. * @brief i2s operation mode type
  205. */
  206. typedef enum
  207. {
  208. I2S_MODE_SLAVE_TX = 0x00, /*!< slave transmission mode */
  209. I2S_MODE_SLAVE_RX = 0x01, /*!< slave reception mode */
  210. I2S_MODE_MASTER_TX = 0x02, /*!< master transmission mode */
  211. I2S_MODE_MASTER_RX = 0x03 /*!< master reception mode */
  212. } i2s_operation_mode_type;
  213. /**
  214. * @brief i2s clock polarity type
  215. */
  216. typedef enum
  217. {
  218. I2S_CLOCK_POLARITY_LOW = 0x00, /*!< i2s clock steady state is low level */
  219. I2S_CLOCK_POLARITY_HIGH = 0x01 /*!< i2s clock steady state is high level */
  220. } i2s_clock_polarity_type;
  221. /**
  222. * @brief spi init type
  223. */
  224. typedef struct
  225. {
  226. spi_transmission_mode_type transmission_mode; /*!< transmission mode selection */
  227. spi_master_slave_mode_type master_slave_mode; /*!< master or slave mode selection */
  228. spi_mclk_freq_div_type mclk_freq_division; /*!< master clock frequency division selection */
  229. spi_first_bit_type first_bit_transmission;/*!< transmit lsb or msb selection */
  230. spi_frame_bit_num_type frame_bit_num; /*!< frame bit num 8 or 16 bit selection */
  231. spi_clock_polarity_type clock_polarity; /*!< clock polarity selection */
  232. spi_clock_phase_type clock_phase; /*!< clock phase selection */
  233. spi_cs_mode_type cs_mode_selection; /*!< hardware or software cs mode selection */
  234. } spi_init_type;
  235. /**
  236. * @brief i2s init type
  237. */
  238. typedef struct
  239. {
  240. i2s_operation_mode_type operation_mode; /*!< operation mode selection */
  241. i2s_audio_protocol_type audio_protocol; /*!< audio protocol selection */
  242. i2s_audio_sampling_freq_type audio_sampling_freq; /*!< audio frequency selection */
  243. i2s_data_channel_format_type data_channel_format; /*!< data bit num and channel bit num selection */
  244. i2s_clock_polarity_type clock_polarity; /*!< clock polarity selection */
  245. confirm_state mclk_output_enable; /*!< mclk_output selection */
  246. } i2s_init_type;
  247. /**
  248. * @brief type define spi register all
  249. */
  250. typedef struct
  251. {
  252. /**
  253. * @brief spi ctrl1 register, offset:0x00
  254. */
  255. union
  256. {
  257. __IO uint32_t ctrl1;
  258. struct
  259. {
  260. __IO uint32_t clkpha : 1; /* [0] */
  261. __IO uint32_t clkpol : 1; /* [1] */
  262. __IO uint32_t msten : 1; /* [2] */
  263. __IO uint32_t mdiv_l : 3; /* [5:3] */
  264. __IO uint32_t spien : 1; /* [6] */
  265. __IO uint32_t ltf : 1; /* [7] */
  266. __IO uint32_t swcsil : 1; /* [8] */
  267. __IO uint32_t swcsen : 1; /* [9] */
  268. __IO uint32_t ora : 1; /* [10] */
  269. __IO uint32_t fbn : 1; /* [11] */
  270. __IO uint32_t ntc : 1; /* [12] */
  271. __IO uint32_t ccen : 1; /* [13] */
  272. __IO uint32_t slbtd : 1; /* [14] */
  273. __IO uint32_t slben : 1; /* [15] */
  274. __IO uint32_t reserved1 : 16;/* [31:16] */
  275. } ctrl1_bit;
  276. };
  277. /**
  278. * @brief spi ctrl2 register, offset:0x04
  279. */
  280. union
  281. {
  282. __IO uint32_t ctrl2;
  283. struct
  284. {
  285. __IO uint32_t dmaren : 1; /* [0] */
  286. __IO uint32_t dmaten : 1; /* [1] */
  287. __IO uint32_t hwcsoe : 1; /* [2] */
  288. __IO uint32_t reserved1 : 2; /* [4:3] */
  289. __IO uint32_t errie : 1; /* [5] */
  290. __IO uint32_t rdbfie : 1; /* [6] */
  291. __IO uint32_t tdbeie : 1; /* [7] */
  292. __IO uint32_t mdiv_h : 1; /* [8] */
  293. __IO uint32_t reserved2 : 23;/* [31:9] */
  294. } ctrl2_bit;
  295. };
  296. /**
  297. * @brief spi sts register, offset:0x08
  298. */
  299. union
  300. {
  301. __IO uint32_t sts;
  302. struct
  303. {
  304. __IO uint32_t rdbf : 1; /* [0] */
  305. __IO uint32_t tdbe : 1; /* [1] */
  306. __IO uint32_t acs : 1; /* [2] */
  307. __IO uint32_t tuerr : 1; /* [3] */
  308. __IO uint32_t ccerr : 1; /* [4] */
  309. __IO uint32_t mmerr : 1; /* [5] */
  310. __IO uint32_t roerr : 1; /* [6] */
  311. __IO uint32_t bf : 1; /* [7] */
  312. __IO uint32_t reserved1 : 24;/* [31:8] */
  313. } sts_bit;
  314. };
  315. /**
  316. * @brief spi dt register, offset:0x0C
  317. */
  318. union
  319. {
  320. __IO uint32_t dt;
  321. struct
  322. {
  323. __IO uint32_t dt : 16;/* [15:0] */
  324. __IO uint32_t reserved1 : 16;/* [31:16] */
  325. } dt_bit;
  326. };
  327. /**
  328. * @brief spi cpoly register, offset:0x10
  329. */
  330. union
  331. {
  332. __IO uint32_t cpoly;
  333. struct
  334. {
  335. __IO uint32_t cpoly : 16;/* [15:0] */
  336. __IO uint32_t reserved1 : 16;/* [31:16] */
  337. } cpoly_bit;
  338. };
  339. /**
  340. * @brief spi rcrc register, offset:0x14
  341. */
  342. union
  343. {
  344. __IO uint32_t rcrc;
  345. struct
  346. {
  347. __IO uint32_t rcrc : 16;/* [15:0] */
  348. __IO uint32_t reserved1 : 16;/* [31:16] */
  349. } rcrc_bit;
  350. };
  351. /**
  352. * @brief spi tcrc register, offset:0x18
  353. */
  354. union
  355. {
  356. __IO uint32_t tcrc;
  357. struct
  358. {
  359. __IO uint32_t tcrc : 16;/* [15:0] */
  360. __IO uint32_t reserved1 : 16;/* [31:16] */
  361. } tcrc_bit;
  362. };
  363. /**
  364. * @brief spi i2sctrl register, offset:0x1C
  365. */
  366. union
  367. {
  368. __IO uint32_t i2sctrl;
  369. struct
  370. {
  371. __IO uint32_t i2scbn : 1; /* [0] */
  372. __IO uint32_t i2sdbn : 2; /* [2:1] */
  373. __IO uint32_t i2sclkpol : 1; /* [3] */
  374. __IO uint32_t stdsel : 2; /* [5:4] */
  375. __IO uint32_t reserved1 : 1; /* [6] */
  376. __IO uint32_t pcmfssel : 1; /* [7] */
  377. __IO uint32_t opersel : 2; /* [9:8] */
  378. __IO uint32_t i2sen : 1; /* [10] */
  379. __IO uint32_t i2smsel : 1; /* [11] */
  380. __IO uint32_t reserved2 : 20;/* [31:12] */
  381. } i2sctrl_bit;
  382. };
  383. /**
  384. * @brief spi i2sclk register, offset:0x20
  385. */
  386. union
  387. {
  388. __IO uint32_t i2sclk;
  389. struct
  390. {
  391. __IO uint32_t i2sdiv_l : 8; /* [7:0] */
  392. __IO uint32_t i2sodd : 1; /* [8] */
  393. __IO uint32_t i2smclkoe : 1; /* [9] */
  394. __IO uint32_t i2sdiv_h : 2; /* [11:10] */
  395. __IO uint32_t reserved1 : 20;/* [31:12] */
  396. } i2sclk_bit;
  397. };
  398. } spi_type;
  399. /**
  400. * @}
  401. */
  402. #define SPI1 ((spi_type *) SPI1_BASE)
  403. #define SPI2 ((spi_type *) SPI2_BASE)
  404. /** @defgroup SPI_exported_functions
  405. * @{
  406. */
  407. void spi_i2s_reset(spi_type *spi_x);
  408. void spi_default_para_init(spi_init_type* spi_init_struct);
  409. void spi_init(spi_type* spi_x, spi_init_type* spi_init_struct);
  410. void spi_crc_next_transmit(spi_type* spi_x);
  411. void spi_crc_polynomial_set(spi_type* spi_x, uint16_t crc_poly);
  412. uint16_t spi_crc_polynomial_get(spi_type* spi_x);
  413. void spi_crc_enable(spi_type* spi_x, confirm_state new_state);
  414. uint16_t spi_crc_value_get(spi_type* spi_x, spi_crc_direction_type crc_direction);
  415. void spi_hardware_cs_output_enable(spi_type* spi_x, confirm_state new_state);
  416. void spi_software_cs_internal_level_set(spi_type* spi_x, spi_software_cs_level_type level);
  417. void spi_frame_bit_num_set(spi_type* spi_x, spi_frame_bit_num_type bit_num);
  418. void spi_half_duplex_direction_set(spi_type* spi_x, spi_half_duplex_direction_type direction);
  419. void spi_enable(spi_type* spi_x, confirm_state new_state);
  420. void i2s_default_para_init(i2s_init_type* i2s_init_struct);
  421. void i2s_init(spi_type* spi_x, i2s_init_type* i2s_init_struct);
  422. void i2s_enable(spi_type* spi_x, confirm_state new_state);
  423. void spi_i2s_interrupt_enable(spi_type* spi_x, uint32_t spi_i2s_int, confirm_state new_state);
  424. void spi_i2s_dma_transmitter_enable(spi_type* spi_x, confirm_state new_state);
  425. void spi_i2s_dma_receiver_enable(spi_type* spi_x, confirm_state new_state);
  426. void spi_i2s_data_transmit(spi_type* spi_x, uint16_t tx_data);
  427. uint16_t spi_i2s_data_receive(spi_type* spi_x);
  428. flag_status spi_i2s_flag_get(spi_type* spi_x, uint32_t spi_i2s_flag);
  429. flag_status spi_i2s_interrupt_flag_get(spi_type* spi_x, uint32_t spi_i2s_flag);
  430. void spi_i2s_flag_clear(spi_type* spi_x, uint32_t spi_i2s_flag);
  431. /**
  432. * @}
  433. */
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. #ifdef __cplusplus
  441. }
  442. #endif
  443. #endif