gd32vf103_spi.c 25 KB

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