at32f415_spi.h 17 KB

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