gd32f10x_spi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*!
  2. \file gd32f10x_spi.c
  3. \brief SPI driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_spi.h"
  33. /* SPI/I2S parameter initialization mask */
  34. #define SPI_INIT_MASK ((uint32_t)0x00003040U) /*!< SPI parameter initialization mask */
  35. #define I2S_INIT_MASK ((uint32_t)0x0000F047U) /*!< I2S parameter initialization mask */
  36. /* I2S clock source selection, multiplication and division mask */
  37. #define I2S1_CLOCK_SEL ((uint32_t)0x00020000U) /* I2S1 clock source selection */
  38. #define I2S2_CLOCK_SEL ((uint32_t)0x00040000U) /* I2S2 clock source selection */
  39. #define I2S_CLOCK_MUL_MASK ((uint32_t)0x0000F000U) /* I2S clock multiplication mask */
  40. #define I2S_CLOCK_DIV_MASK ((uint32_t)0x000000F0U) /* I2S clock division mask */
  41. /* reset value and offset */
  42. #define SPI_I2SPSC_RESET ((uint32_t)0x00000002U) /*!< I2S clock prescaler register reset value */
  43. #define RCU_CFG1_PREDV1_OFFSET 4U /* PREDV1 offset in RCU_CFG1 */
  44. #define RCU_CFG1_PLL2MF_OFFSET 12U /* PLL2MF offset in RCU_CFG1 */
  45. /*!
  46. \brief reset SPI and I2S
  47. \param[in] spi_periph: SPIx(x=0,1,2)
  48. \param[out] none
  49. \retval none
  50. */
  51. void spi_i2s_deinit(uint32_t spi_periph)
  52. {
  53. switch(spi_periph) {
  54. case SPI0:
  55. /* reset SPI0 */
  56. rcu_periph_reset_enable(RCU_SPI0RST);
  57. rcu_periph_reset_disable(RCU_SPI0RST);
  58. break;
  59. case SPI1:
  60. /* reset SPI1 and I2S1 */
  61. rcu_periph_reset_enable(RCU_SPI1RST);
  62. rcu_periph_reset_disable(RCU_SPI1RST);
  63. break;
  64. case SPI2:
  65. /* reset SPI2 and I2S2 */
  66. rcu_periph_reset_enable(RCU_SPI2RST);
  67. rcu_periph_reset_disable(RCU_SPI2RST);
  68. break;
  69. default :
  70. break;
  71. }
  72. }
  73. /*!
  74. \brief initialize the parameters of SPI structure with the default values
  75. \param[in] none
  76. \param[out] spi_parameter_struct: the initialized structure spi_parameter_struct pointer
  77. \retval none
  78. */
  79. void spi_struct_para_init(spi_parameter_struct *spi_struct)
  80. {
  81. /* configure the SPI structure with the default values */
  82. spi_struct->device_mode = SPI_SLAVE;
  83. spi_struct->trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  84. spi_struct->frame_size = SPI_FRAMESIZE_8BIT;
  85. spi_struct->nss = SPI_NSS_HARD;
  86. spi_struct->endian = SPI_ENDIAN_MSB;
  87. spi_struct->clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  88. spi_struct->prescale = SPI_PSC_2;
  89. }
  90. /*!
  91. \brief initialize SPI parameters
  92. \param[in] spi_periph: SPIx(x=0,1,2)
  93. \param[in] spi_struct: SPI parameter initialization stuct members of the structure
  94. and the member values are shown as below:
  95. device_mode: SPI_MASTER, SPI_SLAVE
  96. trans_mode: SPI_TRANSMODE_FULLDUPLEX, SPI_TRANSMODE_RECEIVEONLY,
  97. SPI_TRANSMODE_BDRECEIVE, SPI_TRANSMODE_BDTRANSMIT
  98. frame_size: SPI_FRAMESIZE_16BIT, SPI_FRAMESIZE_8BIT
  99. nss: SPI_NSS_SOFT, SPI_NSS_HARD
  100. endian: SPI_ENDIAN_MSB, SPI_ENDIAN_LSB
  101. clock_polarity_phase: SPI_CK_PL_LOW_PH_1EDGE, SPI_CK_PL_HIGH_PH_1EDGE
  102. SPI_CK_PL_LOW_PH_2EDGE, SPI_CK_PL_HIGH_PH_2EDGE
  103. prescale: SPI_PSC_n (n=2,4,8,16,32,64,128,256)
  104. \param[out] none
  105. \retval none
  106. */
  107. void spi_init(uint32_t spi_periph, spi_parameter_struct *spi_struct)
  108. {
  109. uint32_t reg = 0U;
  110. reg = SPI_CTL0(spi_periph);
  111. reg &= SPI_INIT_MASK;
  112. /* select SPI as master or slave */
  113. reg |= spi_struct->device_mode;
  114. /* select SPI transfer mode */
  115. reg |= spi_struct->trans_mode;
  116. /* select SPI frame size */
  117. reg |= spi_struct->frame_size;
  118. /* select SPI NSS use hardware or software */
  119. reg |= spi_struct->nss;
  120. /* select SPI LSB or MSB */
  121. reg |= spi_struct->endian;
  122. /* select SPI polarity and phase */
  123. reg |= spi_struct->clock_polarity_phase;
  124. /* select SPI prescale to adjust transmit speed */
  125. reg |= spi_struct->prescale;
  126. /* write to SPI_CTL0 register */
  127. SPI_CTL0(spi_periph) = (uint32_t)reg;
  128. /* select SPI mode */
  129. SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SSEL);
  130. }
  131. /*!
  132. \brief enable SPI
  133. \param[in] spi_periph: SPIx(x=0,1,2)
  134. \param[out] none
  135. \retval none
  136. */
  137. void spi_enable(uint32_t spi_periph)
  138. {
  139. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SPIEN;
  140. }
  141. /*!
  142. \brief disable SPI
  143. \param[in] spi_periph: SPIx(x=0,1,2)
  144. \param[out] none
  145. \retval none
  146. */
  147. void spi_disable(uint32_t spi_periph)
  148. {
  149. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SPIEN);
  150. }
  151. /*!
  152. \brief initialize I2S parameter
  153. \param[in] spi_periph: SPIx(x=1,2)
  154. \param[in] mode: I2S operation mode
  155. only one parameter can be selected which is shown as below:
  156. \arg I2S_MODE_SLAVETX: I2S slave transmit mode
  157. \arg I2S_MODE_SLAVERX: I2S slave receive mode
  158. \arg I2S_MODE_MASTERTX: I2S master transmit mode
  159. \arg I2S_MODE_MASTERRX: I2S master receive mode
  160. \param[in] standard: I2S standard
  161. only one parameter can be selected which is shown as below:
  162. \arg I2S_STD_PHILLIPS: I2S phillips standard
  163. \arg I2S_STD_MSB: I2S MSB standard
  164. \arg I2S_STD_LSB: I2S LSB standard
  165. \arg I2S_STD_PCMSHORT: I2S PCM short standard
  166. \arg I2S_STD_PCMLONG: I2S PCM long standard
  167. \param[in] ckpl: I2S idle state clock polarity
  168. only one parameter can be selected which is shown as below:
  169. \arg I2S_CKPL_LOW: I2S clock polarity low level
  170. \arg I2S_CKPL_HIGH: I2S clock polarity high level
  171. \param[out] none
  172. \retval none
  173. */
  174. void i2s_init(uint32_t spi_periph, uint32_t mode, uint32_t standard, uint32_t ckpl)
  175. {
  176. uint32_t reg = 0U;
  177. reg = SPI_I2SCTL(spi_periph);
  178. reg &= I2S_INIT_MASK;
  179. /* enable I2S mode */
  180. reg |= (uint32_t)SPI_I2SCTL_I2SSEL;
  181. /* select I2S mode */
  182. reg |= (uint32_t)mode;
  183. /* select I2S standard */
  184. reg |= (uint32_t)standard;
  185. /* select I2S polarity */
  186. reg |= (uint32_t)ckpl;
  187. /* write to SPI_I2SCTL register */
  188. SPI_I2SCTL(spi_periph) = (uint32_t)reg;
  189. }
  190. /*!
  191. \brief configure I2S prescaler
  192. \param[in] spi_periph: SPIx(x=1,2)
  193. \param[in] audiosample: I2S audio sample rate
  194. only one parameter can be selected which is shown as below:
  195. \arg I2S_AUDIOSAMPLE_8K: audio sample rate is 8KHz
  196. \arg I2S_AUDIOSAMPLE_11K: audio sample rate is 11KHz
  197. \arg I2S_AUDIOSAMPLE_16K: audio sample rate is 16KHz
  198. \arg I2S_AUDIOSAMPLE_22K: audio sample rate is 22KHz
  199. \arg I2S_AUDIOSAMPLE_32K: audio sample rate is 32KHz
  200. \arg I2S_AUDIOSAMPLE_44K: audio sample rate is 44KHz
  201. \arg I2S_AUDIOSAMPLE_48K: audio sample rate is 48KHz
  202. \arg I2S_AUDIOSAMPLE_96K: audio sample rate is 96KHz
  203. \arg I2S_AUDIOSAMPLE_192K: audio sample rate is 192KHz
  204. \param[in] frameformat: I2S data length and channel length
  205. only one parameter can be selected which is shown as below:
  206. \arg I2S_FRAMEFORMAT_DT16B_CH16B: I2S data length is 16 bit and channel length is 16 bit
  207. \arg I2S_FRAMEFORMAT_DT16B_CH32B: I2S data length is 16 bit and channel length is 32 bit
  208. \arg I2S_FRAMEFORMAT_DT24B_CH32B: I2S data length is 24 bit and channel length is 32 bit
  209. \arg I2S_FRAMEFORMAT_DT32B_CH32B: I2S data length is 32 bit and channel length is 32 bit
  210. \param[in] mckout: I2S master clock output
  211. only one parameter can be selected which is shown as below:
  212. \arg I2S_MCKOUT_ENABLE: enable I2S master clock output
  213. \arg I2S_MCKOUT_DISABLE: disable 2S master clock output
  214. \param[out] none
  215. \retval none
  216. */
  217. void i2s_psc_config(uint32_t spi_periph, uint32_t audiosample, uint32_t frameformat, uint32_t mckout)
  218. {
  219. uint32_t i2sdiv = 2U, i2sof = 0U;
  220. uint32_t clks = 0U;
  221. uint32_t i2sclock = 0U;
  222. /* deinitialize SPI_I2SPSC register */
  223. SPI_I2SPSC(spi_periph) = SPI_I2SPSC_RESET;
  224. #ifdef GD32F10X_CL
  225. /* get the I2S clock source */
  226. if(SPI1 == ((uint32_t)spi_periph)) {
  227. /* I2S1 clock source selection */
  228. clks = I2S1_CLOCK_SEL;
  229. } else {
  230. /* I2S2 clock source selection */
  231. clks = I2S2_CLOCK_SEL;
  232. }
  233. if(0U != (RCU_CFG1 & clks)) {
  234. /* get RCU PLL2 clock multiplication factor */
  235. clks = (uint32_t)((RCU_CFG1 & I2S_CLOCK_MUL_MASK) >> RCU_CFG1_PLL2MF_OFFSET);
  236. if((clks > 5U) && (clks < 15U)) {
  237. /* multiplier is between 8 and 14 */
  238. clks += 2U;
  239. } else {
  240. if(15U == clks) {
  241. /* multiplier is 20 */
  242. clks = 20U;
  243. }
  244. }
  245. /* get the PREDV1 value */
  246. i2sclock = (uint32_t)(((RCU_CFG1 & I2S_CLOCK_DIV_MASK) >> RCU_CFG1_PREDV1_OFFSET) + 1U);
  247. /* calculate I2S clock based on PLL2 and PREDV1 */
  248. i2sclock = (uint32_t)((HXTAL_VALUE / i2sclock) * clks * 2U);
  249. } else {
  250. /* get system clock */
  251. i2sclock = rcu_clock_freq_get(CK_SYS);
  252. }
  253. #else
  254. /* get system clock */
  255. i2sclock = rcu_clock_freq_get(CK_SYS);
  256. #endif /* GD32F10X_CL */
  257. /* configure the prescaler depending on the mclk output state, the frame format and audio sample rate */
  258. if(I2S_MCKOUT_ENABLE == mckout) {
  259. clks = (uint32_t)(((i2sclock / 256U) * 10U) / audiosample);
  260. } else {
  261. if(I2S_FRAMEFORMAT_DT16B_CH16B == frameformat) {
  262. clks = (uint32_t)(((i2sclock / 32U) * 10U) / audiosample);
  263. } else {
  264. clks = (uint32_t)(((i2sclock / 64U) * 10U) / audiosample);
  265. }
  266. }
  267. /* remove the floating point */
  268. clks = (clks + 5U) / 10U;
  269. i2sof = (clks & 0x00000001U);
  270. i2sdiv = ((clks - i2sof) / 2U);
  271. i2sof = (i2sof << 8U);
  272. /* set the default values */
  273. if((i2sdiv < 2U) || (i2sdiv > 255U)) {
  274. i2sdiv = 2U;
  275. i2sof = 0U;
  276. }
  277. /* configure SPI_I2SPSC */
  278. SPI_I2SPSC(spi_periph) = (uint32_t)(i2sdiv | i2sof | mckout);
  279. /* clear SPI_I2SCTL_DTLEN and SPI_I2SCTL_CHLEN bits */
  280. SPI_I2SCTL(spi_periph) &= (uint32_t)(~(SPI_I2SCTL_DTLEN | SPI_I2SCTL_CHLEN));
  281. /* configure data frame format */
  282. SPI_I2SCTL(spi_periph) |= (uint32_t)frameformat;
  283. }
  284. /*!
  285. \brief enable I2S
  286. \param[in] spi_periph: SPIx(x=1,2)
  287. \param[out] none
  288. \retval none
  289. */
  290. void i2s_enable(uint32_t spi_periph)
  291. {
  292. SPI_I2SCTL(spi_periph) |= (uint32_t)SPI_I2SCTL_I2SEN;
  293. }
  294. /*!
  295. \brief disable I2S
  296. \param[in] spi_periph: SPIx(x=1,2)
  297. \param[out] none
  298. \retval none
  299. */
  300. void i2s_disable(uint32_t spi_periph)
  301. {
  302. SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SEN);
  303. }
  304. /*!
  305. \brief enable SPI NSS output
  306. \param[in] spi_periph: SPIx(x=0,1,2)
  307. \param[out] none
  308. \retval none
  309. */
  310. void spi_nss_output_enable(uint32_t spi_periph)
  311. {
  312. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_NSSDRV;
  313. }
  314. /*!
  315. \brief disable SPI NSS output
  316. \param[in] spi_periph: SPIx(x=0,1,2)
  317. \param[out] none
  318. \retval none
  319. */
  320. void spi_nss_output_disable(uint32_t spi_periph)
  321. {
  322. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_NSSDRV);
  323. }
  324. /*!
  325. \brief SPI NSS pin high level in software mode
  326. \param[in] spi_periph: SPIx(x=0,1,2)
  327. \param[out] none
  328. \retval none
  329. */
  330. void spi_nss_internal_high(uint32_t spi_periph)
  331. {
  332. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SWNSS;
  333. }
  334. /*!
  335. \brief SPI NSS pin low level in software mode
  336. \param[in] spi_periph: SPIx(x=0,1,2)
  337. \param[out] none
  338. \retval none
  339. */
  340. void spi_nss_internal_low(uint32_t spi_periph)
  341. {
  342. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SWNSS);
  343. }
  344. /*!
  345. \brief enable SPI DMA send or receive
  346. \param[in] spi_periph: SPIx(x=0,1,2)
  347. \param[in] dma: SPI DMA mode
  348. only one parameter can be selected which is shown as below:
  349. \arg SPI_DMA_TRANSMIT: SPI transmit data using DMA
  350. \arg SPI_DMA_RECEIVE: SPI receive data using DMA
  351. \param[out] none
  352. \retval none
  353. */
  354. void spi_dma_enable(uint32_t spi_periph, uint8_t dma)
  355. {
  356. if(SPI_DMA_TRANSMIT == dma) {
  357. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMATEN;
  358. } else {
  359. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMAREN;
  360. }
  361. }
  362. /*!
  363. \brief disable SPI DMA send or receive
  364. \param[in] spi_periph: SPIx(x=0,1,2)
  365. \param[in] dma: SPI DMA mode
  366. only one parameter can be selected which is shown as below:
  367. \arg SPI_DMA_TRANSMIT: SPI transmit data using DMA
  368. \arg SPI_DMA_RECEIVE: SPI receive data using DMA
  369. \param[out] none
  370. \retval none
  371. */
  372. void spi_dma_disable(uint32_t spi_periph, uint8_t dma)
  373. {
  374. if(SPI_DMA_TRANSMIT == dma) {
  375. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMATEN);
  376. } else {
  377. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMAREN);
  378. }
  379. }
  380. /*!
  381. \brief configure SPI data frame format
  382. \param[in] spi_periph: SPIx(x=0,1,2)
  383. \param[in] frame_format: SPI frame size
  384. only one parameter can be selected which is shown as below:
  385. \arg SPI_FRAMESIZE_16BIT: SPI frame size is 16 bits
  386. \arg SPI_FRAMESIZE_8BIT: SPI frame size is 8 bits
  387. \param[out] none
  388. \retval none
  389. */
  390. void spi_i2s_data_frame_format_config(uint32_t spi_periph, uint16_t frame_format)
  391. {
  392. /* clear SPI_CTL0_FF16 bit */
  393. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_FF16);
  394. /* configure SPI_CTL0_FF16 bit */
  395. SPI_CTL0(spi_periph) |= (uint32_t)frame_format;
  396. }
  397. /*!
  398. \brief configure SPI bidirectional transfer direction
  399. \param[in] spi_periph: SPIx(x=0,1,2)
  400. \param[in] transfer_direction: SPI transfer direction
  401. only one parameter can be selected which is shown as below:
  402. \arg SPI_BIDIRECTIONAL_TRANSMIT: SPI work in transmit-only mode
  403. \arg SPI_BIDIRECTIONAL_RECEIVE: SPI work in receive-only mode
  404. \param[out] none
  405. \retval none
  406. */
  407. void spi_bidirectional_transfer_config(uint32_t spi_periph, uint32_t transfer_direction)
  408. {
  409. if(SPI_BIDIRECTIONAL_TRANSMIT == transfer_direction) {
  410. /* set the transmit only mode */
  411. SPI_CTL0(spi_periph) |= (uint32_t)SPI_BIDIRECTIONAL_TRANSMIT;
  412. } else {
  413. /* set the receive only mode */
  414. SPI_CTL0(spi_periph) &= SPI_BIDIRECTIONAL_RECEIVE;
  415. }
  416. }
  417. /*!
  418. \brief SPI transmit data
  419. \param[in] spi_periph: SPIx(x=0,1,2)
  420. \param[in] data: 16-bit data
  421. \param[out] none
  422. \retval none
  423. */
  424. void spi_i2s_data_transmit(uint32_t spi_periph, uint16_t data)
  425. {
  426. SPI_DATA(spi_periph) = (uint32_t)data;
  427. }
  428. /*!
  429. \brief SPI receive data
  430. \param[in] spi_periph: SPIx(x=0,1,2)
  431. \param[out] none
  432. \retval 16-bit data
  433. */
  434. uint16_t spi_i2s_data_receive(uint32_t spi_periph)
  435. {
  436. return ((uint16_t)SPI_DATA(spi_periph));
  437. }
  438. /*!
  439. \brief set SPI CRC polynomial
  440. \param[in] spi_periph: SPIx(x=0,1,2)
  441. \param[in] crc_poly: CRC polynomial value
  442. \param[out] none
  443. \retval none
  444. */
  445. void spi_crc_polynomial_set(uint32_t spi_periph, uint16_t crc_poly)
  446. {
  447. /* enable SPI CRC */
  448. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCEN;
  449. /* set SPI CRC polynomial */
  450. SPI_CRCPOLY(spi_periph) = (uint32_t)crc_poly;
  451. }
  452. /*!
  453. \brief get SPI CRC polynomial
  454. \param[in] spi_periph: SPIx(x=0,1,2)
  455. \param[out] none
  456. \retval 16-bit CRC polynomial
  457. */
  458. uint16_t spi_crc_polynomial_get(uint32_t spi_periph)
  459. {
  460. return ((uint16_t)SPI_CRCPOLY(spi_periph));
  461. }
  462. /*!
  463. \brief turn on SPI CRC function
  464. \param[in] spi_periph: SPIx(x=0,1,2)
  465. \param[out] none
  466. \retval none
  467. */
  468. void spi_crc_on(uint32_t spi_periph)
  469. {
  470. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCEN;
  471. }
  472. /*!
  473. \brief turn off SPI CRC function
  474. \param[in] spi_periph: SPIx(x=0,1,2)
  475. \param[out] none
  476. \retval none
  477. */
  478. void spi_crc_off(uint32_t spi_periph)
  479. {
  480. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_CRCEN);
  481. }
  482. /*!
  483. \brief SPI next data is CRC value
  484. \param[in] spi_periph: SPIx(x=0,1,2)
  485. \param[out] none
  486. \retval none
  487. */
  488. void spi_crc_next(uint32_t spi_periph)
  489. {
  490. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCNT;
  491. }
  492. /*!
  493. \brief get SPI CRC send value or receive value
  494. \param[in] spi_periph: SPIx(x=0,1,2)
  495. \param[in] crc: SPI crc value
  496. only one parameter can be selected which is shown as below:
  497. \arg SPI_CRC_TX: get transmit crc value
  498. \arg SPI_CRC_RX: get receive crc value
  499. \param[out] none
  500. \retval 16-bit CRC value
  501. */
  502. uint16_t spi_crc_get(uint32_t spi_periph, uint8_t crc)
  503. {
  504. if(SPI_CRC_TX == crc) {
  505. return ((uint16_t)(SPI_TCRC(spi_periph)));
  506. } else {
  507. return ((uint16_t)(SPI_RCRC(spi_periph)));
  508. }
  509. }
  510. /*!
  511. \brief get SPI and I2S flag status
  512. \param[in] spi_periph: SPIx(x=0,1,2)
  513. \param[in] flag: SPI/I2S flag status
  514. one or more parameters can be selected which are shown as below:
  515. \arg SPI_FLAG_TBE: transmit buffer empty flag
  516. \arg SPI_FLAG_RBNE: receive buffer not empty flag
  517. \arg SPI_FLAG_TRANS: transmit on-going flag
  518. \arg SPI_FLAG_RXORERR: receive overrun error flag
  519. \arg SPI_FLAG_CONFERR: mode config error flag
  520. \arg SPI_FLAG_CRCERR: CRC error flag
  521. \arg I2S_FLAG_RXORERR: overrun error flag
  522. \arg I2S_FLAG_TXURERR: underrun error flag
  523. \arg I2S_FLAG_CH: channel side flag
  524. \param[out] none
  525. \retval FlagStatus: SET or RESET
  526. */
  527. FlagStatus spi_i2s_flag_get(uint32_t spi_periph, uint32_t flag)
  528. {
  529. if(RESET != (SPI_STAT(spi_periph) & flag)) {
  530. return SET;
  531. } else {
  532. return RESET;
  533. }
  534. }
  535. /*!
  536. \brief enable SPI and I2S interrupt
  537. \param[in] spi_periph: SPIx(x=0,1,2)
  538. \param[in] interrupt: SPI/I2S interrupt
  539. only one parameter can be selected which is shown as below:
  540. \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
  541. \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
  542. \arg SPI_I2S_INT_ERR: CRC error, configuration error, reception overrun error,
  543. transmission underrun error and format error interrupt
  544. \param[out] none
  545. \retval none
  546. */
  547. void spi_i2s_interrupt_enable(uint32_t spi_periph, uint8_t interrupt)
  548. {
  549. SPI_CTL1(spi_periph) |= (uint32_t)interrupt;
  550. }
  551. /*!
  552. \brief disable SPI and I2S interrupt
  553. \param[in] spi_periph: SPIx(x=0,1,2)
  554. \param[in] interrupt: SPI/I2S interrupt
  555. only one parameter can be selected which is shown as below:
  556. \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
  557. \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
  558. \arg SPI_I2S_INT_ERR: CRC error, configuration error, reception overrun error,
  559. transmission underrun error and format error interrupt
  560. \param[out] none
  561. \retval none
  562. */
  563. void spi_i2s_interrupt_disable(uint32_t spi_periph, uint8_t interrupt)
  564. {
  565. SPI_CTL1(spi_periph) &= ~(uint32_t)interrupt;
  566. }
  567. /*!
  568. \brief get SPI and I2S interrupt flag status
  569. \param[in] spi_periph: SPIx(x=0,1,2)
  570. \param[in] interrupt: SPI/I2S interrupt flag status
  571. only one parameter can be selected which is shown as below:
  572. \arg SPI_I2S_INT_FLAG_TBE: transmit buffer empty interrupt flag
  573. \arg SPI_I2S_INT_FLAG_RBNE: receive buffer not empty interrupt flag
  574. \arg SPI_I2S_INT_FLAG_RXORERR: overrun interrupt flag
  575. \arg SPI_INT_FLAG_CONFERR: config error interrupt flag
  576. \arg SPI_INT_FLAG_CRCERR: CRC error interrupt flag
  577. \arg I2S_INT_FLAG_TXURERR: underrun error interrupt flag
  578. \param[out] none
  579. \retval FlagStatus: SET or RESET
  580. */
  581. FlagStatus spi_i2s_interrupt_flag_get(uint32_t spi_periph, uint8_t interrupt)
  582. {
  583. uint32_t reg1 = SPI_STAT(spi_periph);
  584. uint32_t reg2 = SPI_CTL1(spi_periph);
  585. switch(interrupt) {
  586. /* SPI/I2S transmit buffer empty interrupt */
  587. case SPI_I2S_INT_FLAG_TBE:
  588. reg1 = reg1 & SPI_STAT_TBE;
  589. reg2 = reg2 & SPI_CTL1_TBEIE;
  590. break;
  591. /* SPI/I2S receive buffer not empty interrupt */
  592. case SPI_I2S_INT_FLAG_RBNE:
  593. reg1 = reg1 & SPI_STAT_RBNE;
  594. reg2 = reg2 & SPI_CTL1_RBNEIE;
  595. break;
  596. /* SPI/I2S overrun interrupt */
  597. case SPI_I2S_INT_FLAG_RXORERR:
  598. reg1 = reg1 & SPI_STAT_RXORERR;
  599. reg2 = reg2 & SPI_CTL1_ERRIE;
  600. break;
  601. /* SPI config error interrupt */
  602. case SPI_INT_FLAG_CONFERR:
  603. reg1 = reg1 & SPI_STAT_CONFERR;
  604. reg2 = reg2 & SPI_CTL1_ERRIE;
  605. break;
  606. /* SPI CRC error interrupt */
  607. case SPI_INT_FLAG_CRCERR:
  608. reg1 = reg1 & SPI_STAT_CRCERR;
  609. reg2 = reg2 & SPI_CTL1_ERRIE;
  610. break;
  611. /* I2S underrun error interrupt */
  612. case I2S_INT_FLAG_TXURERR:
  613. reg1 = reg1 & SPI_STAT_TXURERR;
  614. reg2 = reg2 & SPI_CTL1_ERRIE;
  615. break;
  616. default :
  617. break;
  618. }
  619. /*get SPI/I2S interrupt flag status */
  620. if((0U != reg1) && (0U != reg2)) {
  621. return SET;
  622. } else {
  623. return RESET;
  624. }
  625. }
  626. /*!
  627. \brief clear SPI CRC error flag status
  628. \param[in] spi_periph: SPIx(x=0,1,2)
  629. \param[out] none
  630. \retval none
  631. */
  632. void spi_crc_error_clear(uint32_t spi_periph)
  633. {
  634. SPI_STAT(spi_periph) &= (uint32_t)(~SPI_FLAG_CRCERR);
  635. }