gd32e23x_spi.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*!
  2. \file gd32e23x_spi.c
  3. \brief SPI driver
  4. \version 2019-02-19, V1.0.0, firmware for GD32E23x
  5. \version 2020-12-12, V1.1.0, firmware for GD32E23x
  6. */
  7. /*
  8. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  9. Redistribution and use in source and binary forms, with or without modification,
  10. are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright notice, this
  12. list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holder nor the names of its contributors
  17. may be used to endorse or promote products derived from this software without
  18. specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  26. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  28. OF SUCH DAMAGE.
  29. */
  30. #include "gd32e23x_spi.h"
  31. /* SPI/I2S parameter initialization mask */
  32. #define SPI_INIT_MASK ((uint32_t)0x00003040U) /*!< SPI0 parameter initialization mask */
  33. #define SPI_FIFO_INIT_MASK1 ((uint32_t)0x00003840U) /*!< SPI1 parameter initialization mask1 */
  34. #define SPI_FIFO_INIT_MASK2 ((uint32_t)0x0000F0FFU) /*!< SPI1 parameter initialization mask2*/
  35. #define I2S_INIT_MASK ((uint32_t)0x0000F047U) /*!< I2S parameter initialization mask */
  36. #define SPI_FRAMESIZE_MASK ((uint32_t)0x00000800U) /*!< SPI0 frame size mask */
  37. #define SPI_BYTEN_MASK ((uint32_t)0x00001000U) /*!< SPI1 access to FIFO mask */
  38. #define SPI_TXLVL_EMPTY_MASK ((uint32_t)0x00001800U) /*!< SPI1 TXFIFO empty mask */
  39. #define SPI_RXLVL_EMPTY_MASK ((uint32_t)0x00000600U) /*!< SPI1 RXFIFO empty mask */
  40. /* I2S clock source selection, multiplication and division mask */
  41. #define SPI_I2SPSC_RESET ((uint32_t)0x00000002U) /*!< I2S clock prescaler register reset value */
  42. /*!
  43. \brief reset SPI and I2S
  44. \param[in] spi_periph: SPIx(x=0,1)
  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 and I2S0 */
  53. rcu_periph_reset_enable(RCU_SPI0RST);
  54. rcu_periph_reset_disable(RCU_SPI0RST);
  55. break;
  56. case SPI1:
  57. /* reset SPI1 */
  58. rcu_periph_reset_enable(RCU_SPI1RST);
  59. rcu_periph_reset_disable(RCU_SPI1RST);
  60. break;
  61. default :
  62. break;
  63. }
  64. }
  65. /*!
  66. \brief initialize the parameters of SPI structure with the default values
  67. \param[in] none
  68. \param[out] spi_parameter_struct: the initialized structure spi_parameter_struct pointer
  69. \retval none
  70. */
  71. void spi_struct_para_init(spi_parameter_struct *spi_struct)
  72. {
  73. /* configure the SPI structure with the default values */
  74. spi_struct->device_mode = SPI_SLAVE;
  75. spi_struct->trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  76. spi_struct->frame_size = SPI_FRAMESIZE_8BIT;
  77. spi_struct->nss = SPI_NSS_HARD;
  78. spi_struct->endian = SPI_ENDIAN_MSB;
  79. spi_struct->clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  80. spi_struct->prescale = SPI_PSC_2;
  81. }
  82. /*!
  83. \brief initialize SPI parameters
  84. \param[in] spi_periph: SPIx(x=0,1)
  85. \param[in] spi_struct: SPI parameter initialization stuct members of the structure
  86. and the member values are shown as below:
  87. device_mode: SPI_MASTER, SPI_SLAVE
  88. trans_mode: SPI_TRANSMODE_FULLDUPLEX, SPI_TRANSMODE_RECEIVEONLY,
  89. SPI_TRANSMODE_BDRECEIVE, SPI_TRANSMODE_BDTRANSMIT
  90. frame_size: SPI_FRAMESIZE_4BIT, SPI_FRAMESIZE_5BIT
  91. SPI_FRAMESIZE_6BIT, SPI_FRAMESIZE_7BIT
  92. SPI_FRAMESIZE_8BIT, SPI_FRAMESIZE_9BIT
  93. SPI_FRAMESIZE_10BIT, SPI_FRAMESIZE_11BIT
  94. SPI_FRAMESIZE_12BIT, SPI_FRAMESIZE_13BIT
  95. SPI_FRAMESIZE_14BIT, SPI_FRAMESIZE_15BIT
  96. SPI_FRAMESIZE_16BIT
  97. nss: SPI_NSS_SOFT, SPI_NSS_HARD
  98. endian: SPI_ENDIAN_MSB, SPI_ENDIAN_LSB
  99. clock_polarity_phase: SPI_CK_PL_LOW_PH_1EDGE, SPI_CK_PL_HIGH_PH_1EDGE
  100. SPI_CK_PL_LOW_PH_2EDGE, SPI_CK_PL_HIGH_PH_2EDGE
  101. prescale: SPI_PSC_n (n=2,4,8,16,32,64,128,256)
  102. \param[out] none
  103. \retval ErrStatus: ERROR or SUCCESS
  104. */
  105. ErrStatus spi_init(uint32_t spi_periph, spi_parameter_struct *spi_struct)
  106. {
  107. uint32_t reg1 = 0, reg2 = 0U, reg3 = 0U;
  108. reg1 = SPI_CTL0(spi_periph);
  109. reg1 &= SPI_INIT_MASK;
  110. reg2 = SPI_CTL0(spi_periph);
  111. reg2 &= SPI_FIFO_INIT_MASK1;
  112. reg3 = SPI_CTL1(spi_periph);
  113. reg3 &= SPI_FIFO_INIT_MASK2;
  114. if(SPI0 == spi_periph) {
  115. /* select SPI as master or slave */
  116. reg1 |= spi_struct->device_mode;
  117. /* select SPI transfer mode */
  118. reg1 |= spi_struct->trans_mode;
  119. /* select SPI NSS use hardware or software */
  120. reg1 |= spi_struct->nss;
  121. /* select SPI LSB or MSB */
  122. reg1 |= spi_struct->endian;
  123. /* select SPI polarity and phase */
  124. reg1 |= spi_struct->clock_polarity_phase;
  125. /* select SPI prescaler to adjust transmit speed */
  126. reg1 |= spi_struct->prescale;
  127. /* select SPI frame size */
  128. /* check SPI0 frame size is 8bits/16bits or not*/
  129. if((SPI_FRAMESIZE_8BIT != spi_struct->frame_size) && (SPI_FRAMESIZE_16BIT != spi_struct->frame_size)) {
  130. return ERROR;
  131. } else {
  132. reg1 |= (spi_struct->frame_size & SPI_FRAMESIZE_MASK);
  133. }
  134. /* write to SPI_CTL0 register */
  135. SPI_CTL0(spi_periph) = (uint32_t)reg1;
  136. } else {
  137. /* select SPI as master or slave */
  138. reg2 |= spi_struct->device_mode;
  139. /* select SPI transfer mode */
  140. reg2 |= spi_struct->trans_mode;
  141. /* select SPI NSS use hardware or software */
  142. reg2 |= spi_struct->nss;
  143. /* select SPI LSB or MSB */
  144. reg2 |= spi_struct->endian;
  145. /* select SPI polarity and phase */
  146. reg2 |= spi_struct->clock_polarity_phase;
  147. /* select SPI prescaler to adjust transmit speed */
  148. reg2 |= spi_struct->prescale;
  149. /* write to SPI_CTL0 register */
  150. SPI_CTL0(spi_periph) = (uint32_t)reg2;
  151. /* select SPI data size */
  152. reg3 |= spi_struct->frame_size;
  153. /* write to SPI_CTL0 register */
  154. SPI_CTL1(spi_periph) = (uint32_t)reg3;
  155. }
  156. /* select SPI mode */
  157. SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SSEL);
  158. return SUCCESS;
  159. }
  160. /*!
  161. \brief enable SPI
  162. \param[in] spi_periph: SPIx(x=0,1)
  163. \param[out] none
  164. \retval none
  165. */
  166. void spi_enable(uint32_t spi_periph)
  167. {
  168. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SPIEN;
  169. }
  170. /*!
  171. \brief disable SPI
  172. \param[in] spi_periph: SPIx(x=0,1)
  173. \param[out] none
  174. \retval none
  175. */
  176. void spi_disable(uint32_t spi_periph)
  177. {
  178. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SPIEN);
  179. }
  180. /*!
  181. \brief initialize I2S parameters
  182. \param[in] spi_periph: SPIx(x=0)
  183. \param[in] mode: I2S operation mode
  184. only one parameter can be selected which is shown as below:
  185. \arg I2S_MODE_SLAVETX: I2S slave transmit mode
  186. \arg I2S_MODE_SLAVERX: I2S slave receive mode
  187. \arg I2S_MODE_MASTERTX: I2S master transmit mode
  188. \arg I2S_MODE_MASTERRX: I2S master receive mode
  189. \param[in] standard: I2S standard
  190. only one parameter can be selected which is shown as below:
  191. \arg I2S_STD_PHILLIPS: I2S phillips standard
  192. \arg I2S_STD_MSB: I2S MSB standard
  193. \arg I2S_STD_LSB: I2S LSB standard
  194. \arg I2S_STD_PCMSHORT: I2S PCM short standard
  195. \arg I2S_STD_PCMLONG: I2S PCM long standard
  196. \param[in] ckpl: I2S idle state clock polarity
  197. only one parameter can be selected which is shown as below:
  198. \arg I2S_CKPL_LOW: I2S clock polarity low level
  199. \arg I2S_CKPL_HIGH: I2S clock polarity high level
  200. \param[out] none
  201. \retval none
  202. */
  203. void i2s_init(uint32_t spi_periph, uint32_t mode, uint32_t standard, uint32_t ckpl)
  204. {
  205. uint32_t reg = 0U;
  206. reg = SPI_I2SCTL(spi_periph);
  207. reg &= I2S_INIT_MASK;
  208. /* enable I2S mode */
  209. reg |= (uint32_t)SPI_I2SCTL_I2SSEL;
  210. /* select I2S mode */
  211. reg |= (uint32_t)mode;
  212. /* select I2S standard */
  213. reg |= (uint32_t)standard;
  214. /* select I2S polarity */
  215. reg |= (uint32_t)ckpl;
  216. /* write to SPI_I2SCTL register */
  217. SPI_I2SCTL(spi_periph) = (uint32_t)reg;
  218. }
  219. /*!
  220. \brief configure I2S prescaler
  221. \param[in] spi_periph: SPIx(x=0)
  222. \param[in] audiosample: I2S audio sample rate
  223. only one parameter can be selected which is shown as below:
  224. \arg I2S_AUDIOSAMPLE_8K: audio sample rate is 8KHz
  225. \arg I2S_AUDIOSAMPLE_11K: audio sample rate is 11KHz
  226. \arg I2S_AUDIOSAMPLE_16K: audio sample rate is 16KHz
  227. \arg I2S_AUDIOSAMPLE_22K: audio sample rate is 22KHz
  228. \arg I2S_AUDIOSAMPLE_32K: audio sample rate is 32KHz
  229. \arg I2S_AUDIOSAMPLE_44K: audio sample rate is 44KHz
  230. \arg I2S_AUDIOSAMPLE_48K: audio sample rate is 48KHz
  231. \arg I2S_AUDIOSAMPLE_96K: audio sample rate is 96KHz
  232. \arg I2S_AUDIOSAMPLE_192K: audio sample rate is 192KHz
  233. \param[in] frameformat: I2S data length and channel length
  234. only one parameter can be selected which is shown as below:
  235. \arg I2S_FRAMEFORMAT_DT16B_CH16B: I2S data length is 16 bit and channel length is 16 bit
  236. \arg I2S_FRAMEFORMAT_DT16B_CH32B: I2S data length is 16 bit and channel length is 32 bit
  237. \arg I2S_FRAMEFORMAT_DT24B_CH32B: I2S data length is 24 bit and channel length is 32 bit
  238. \arg I2S_FRAMEFORMAT_DT32B_CH32B: I2S data length is 32 bit and channel length is 32 bit
  239. \param[in] mckout: I2S master clock output
  240. only one parameter can be selected which is shown as below:
  241. \arg I2S_MCKOUT_ENABLE: enable I2S master clock output
  242. \arg I2S_MCKOUT_DISABLE: disable 2S master clock output
  243. \param[out] none
  244. \retval none
  245. */
  246. void i2s_psc_config(uint32_t spi_periph, uint32_t audiosample, uint32_t frameformat, uint32_t mckout)
  247. {
  248. uint32_t i2sdiv = 2U, i2sof = 0U;
  249. uint32_t clks = 0U;
  250. uint32_t i2sclock = 0U;
  251. /* deinitialize SPI_I2SPSC register */
  252. SPI_I2SPSC(spi_periph) = SPI_I2SPSC_RESET;
  253. /* get system clock */
  254. i2sclock = rcu_clock_freq_get(CK_SYS);
  255. /* configure the prescaler depending on the mclk output state, the frame format and audio sample rate */
  256. if(I2S_MCKOUT_ENABLE == mckout) {
  257. clks = (uint32_t)(((i2sclock / 256U) * 10U) / audiosample);
  258. } else {
  259. if(I2S_FRAMEFORMAT_DT16B_CH16B == frameformat) {
  260. clks = (uint32_t)(((i2sclock / 32U) * 10U) / audiosample);
  261. } else {
  262. clks = (uint32_t)(((i2sclock / 64U) * 10U) / audiosample);
  263. }
  264. }
  265. /* remove the floating point */
  266. clks = (clks + 5U) / 10U;
  267. i2sof = (clks & 0x00000001U);
  268. i2sdiv = ((clks - i2sof) / 2U);
  269. i2sof = (i2sof << 8U);
  270. /* set the default values */
  271. if((i2sdiv < 2U) || (i2sdiv > 255U)) {
  272. i2sdiv = 2U;
  273. i2sof = 0U;
  274. }
  275. /* configure SPI_I2SPSC */
  276. SPI_I2SPSC(spi_periph) = (uint32_t)(i2sdiv | i2sof | mckout);
  277. /* clear SPI_I2SCTL_DTLEN and SPI_I2SCTL_CHLEN bits */
  278. SPI_I2SCTL(spi_periph) &= (uint32_t)(~(SPI_I2SCTL_DTLEN | SPI_I2SCTL_CHLEN));
  279. /* configure data frame format */
  280. SPI_I2SCTL(spi_periph) |= (uint32_t)frameformat;
  281. }
  282. /*!
  283. \brief enable I2S
  284. \param[in] spi_periph: SPIx(x=0)
  285. \param[out] none
  286. \retval none
  287. */
  288. void i2s_enable(uint32_t spi_periph)
  289. {
  290. SPI_I2SCTL(spi_periph) |= (uint32_t)SPI_I2SCTL_I2SEN;
  291. }
  292. /*!
  293. \brief disable I2S
  294. \param[in] spi_periph: SPIx(x=0)
  295. \param[out] none
  296. \retval none
  297. */
  298. void i2s_disable(uint32_t spi_periph)
  299. {
  300. SPI_I2SCTL(spi_periph) &= (uint32_t)(~SPI_I2SCTL_I2SEN);
  301. }
  302. /*!
  303. \brief enable SPI NSS output
  304. \param[in] spi_periph: SPIx(x=0,1)
  305. \param[out] none
  306. \retval none
  307. */
  308. void spi_nss_output_enable(uint32_t spi_periph)
  309. {
  310. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_NSSDRV;
  311. }
  312. /*!
  313. \brief disable SPI NSS output
  314. \param[in] spi_periph: SPIx(x=0,1)
  315. \param[out] none
  316. \retval none
  317. */
  318. void spi_nss_output_disable(uint32_t spi_periph)
  319. {
  320. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_NSSDRV);
  321. }
  322. /*!
  323. \brief SPI NSS pin high level in software mode
  324. \param[in] spi_periph: SPIx(x=0,1)
  325. \param[out] none
  326. \retval none
  327. */
  328. void spi_nss_internal_high(uint32_t spi_periph)
  329. {
  330. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_SWNSS;
  331. }
  332. /*!
  333. \brief SPI NSS pin low level in software mode
  334. \param[in] spi_periph: SPIx(x=0,1)
  335. \param[out] none
  336. \retval none
  337. */
  338. void spi_nss_internal_low(uint32_t spi_periph)
  339. {
  340. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_SWNSS);
  341. }
  342. /*!
  343. \brief enable SPI DMA send or receive
  344. \param[in] spi_periph: SPIx(x=0,1)
  345. \param[in] dma: SPI DMA mode
  346. only one parameter can be selected which is shown as below:
  347. \arg SPI_DMA_TRANSMIT: SPI transmit data using DMA
  348. \arg SPI_DMA_RECEIVE: SPI receive data using DMA
  349. \param[out] none
  350. \retval none
  351. */
  352. void spi_dma_enable(uint32_t spi_periph, uint8_t dma)
  353. {
  354. if(SPI_DMA_TRANSMIT == dma) {
  355. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMATEN;
  356. } else {
  357. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_DMAREN;
  358. }
  359. }
  360. /*!
  361. \brief disable SPI DMA send or receive
  362. \param[in] spi_periph: SPIx(x=0,1)
  363. \param[in] dma: SPI DMA mode
  364. only one parameter can be selected which is shown as below:
  365. \arg SPI_DMA_TRANSMIT: SPI transmit data using DMA
  366. \arg SPI_DMA_RECEIVE: SPI receive data using DMA
  367. \param[out] none
  368. \retval none
  369. */
  370. void spi_dma_disable(uint32_t spi_periph, uint8_t dma)
  371. {
  372. if(SPI_DMA_TRANSMIT == dma) {
  373. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMATEN);
  374. } else {
  375. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_DMAREN);
  376. }
  377. }
  378. /*!
  379. \brief configure SPI total number of data to be transmitted by DMA is odd or not
  380. \param[in] spi_periph: SPIx(x=1)
  381. \param[in] odd: odd bytes in TX DMA channel
  382. only one parameter can be selected which is shown as below:
  383. \arg SPI_TXDMA_EVEN: number of byte in TX DMA channel is even
  384. \arg SPI_TXDMA_ODD: number of byte in TX DMA channel is odd
  385. \param[out] none
  386. \retval none
  387. */
  388. void spi_transmit_odd_config(uint32_t spi_periph, uint16_t odd)
  389. {
  390. /* clear SPI_CTL1_TXDMA_ODD bit */
  391. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_TXDMA_ODD);
  392. /* configure SPI_CTL1_TXDMA_ODD bit */
  393. SPI_CTL1(spi_periph) |= (uint32_t)odd;
  394. }
  395. /*!
  396. \brief configure SPI total number of data to be received by DMA is odd or not
  397. \param[in] spi_periph: SPIx(x=1)
  398. \param[in] odd: odd bytes in RX DMA channel
  399. only one parameter can be selected which is shown as below:
  400. \arg SPI_RXDMA_EVEN: number of bytes in RX DMA channel is even
  401. \arg SPI_RXDMA_ODD: number of bytes in RX DMA channel is odd
  402. \param[out] none
  403. \retval none
  404. */
  405. void spi_receive_odd_config(uint32_t spi_periph, uint16_t odd)
  406. {
  407. /* clear SPI_CTL1_RXDMA_ODD bit */
  408. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_RXDMA_ODD);
  409. /* configure SPI_CTL1_RXDMA_ODD bit */
  410. SPI_CTL1(spi_periph) |= (uint32_t)odd;
  411. }
  412. /*!
  413. \brief configure SPI data frame format
  414. \param[in] spi_periph: SPIx(x=0,1)
  415. \param[in] frame_format: SPI frame size
  416. only one parameter can be selected which is shown as below:
  417. \arg SPI_FRAMESIZE_xBIT(x=4,5..16, for SPI1, x=8,16, for SPI0):SPI frame size is x bits
  418. \param[out] none
  419. \retval ErrStatus: ERROR or SUCCESS
  420. */
  421. ErrStatus spi_i2s_data_frame_format_config(uint32_t spi_periph, uint16_t frame_format)
  422. {
  423. uint32_t reg;
  424. if(SPI0 == spi_periph) {
  425. /* check SPI0 frame size is 8bits/16bits or not*/
  426. if((SPI_FRAMESIZE_8BIT != frame_format) && (SPI_FRAMESIZE_16BIT != frame_format)) {
  427. return ERROR;
  428. } else {
  429. /* clear SPI_CTL0_FF16 bit */
  430. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_FF16);
  431. /* configure SPI_CTL0_FF16 bit */
  432. SPI_CTL0(spi_periph) |= ((uint32_t)frame_format & SPI_FRAMESIZE_MASK);
  433. }
  434. } else {
  435. reg = SPI_CTL1(spi_periph);
  436. /* clear SPI_CTL1_DZ bits */
  437. reg &= (uint32_t)(~SPI_CTL1_DZ);
  438. reg |= (uint32_t)frame_format;
  439. /* configure SPI_CTL1_DZ bits */
  440. SPI_CTL1(spi_periph) = reg;
  441. }
  442. return SUCCESS;
  443. }
  444. /*!
  445. \brief configure SPI access size to FIFO(8-bit or 16-bit)
  446. \param[in] spi_periph: SPIx(x=1)
  447. \param[in] fifo_access_size: byte access enable
  448. only one parameter can be selected which is shown as below:
  449. \arg SPI_HALFWORD_ACCESS: half-word access to FIFO
  450. \arg SPI_BYTE_ACCESS: byte access to FIFO
  451. \param[out] none
  452. \retval none
  453. */
  454. void spi_fifo_access_size_config(uint32_t spi_periph, uint16_t fifo_access_size)
  455. {
  456. /* clear SPI_CTL1_BYTEN bit */
  457. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_BYTEN);
  458. /* confige SPI_CTL1_BYTEN bit */
  459. SPI_CTL1(spi_periph) |= (uint32_t)fifo_access_size;
  460. }
  461. /*!
  462. \brief configure SPI bidirectional transfer direction
  463. \param[in] spi_periph: SPIx(x=0,1)
  464. \param[in] transfer_direction: SPI transfer direction
  465. only one parameter can be selected which is shown as below:
  466. \arg SPI_BIDIRECTIONAL_TRANSMIT: SPI work in transmit-only mode
  467. \arg SPI_BIDIRECTIONAL_RECEIVE: SPI work in receive-only mode
  468. \param[out] none
  469. \retval none
  470. */
  471. void spi_bidirectional_transfer_config(uint32_t spi_periph, uint32_t transfer_direction)
  472. {
  473. if(SPI_BIDIRECTIONAL_TRANSMIT == transfer_direction) {
  474. /* set the transmit-only mode */
  475. SPI_CTL0(spi_periph) |= (uint32_t)SPI_BIDIRECTIONAL_TRANSMIT;
  476. } else {
  477. /* set the receive-only mode */
  478. SPI_CTL0(spi_periph) &= SPI_BIDIRECTIONAL_RECEIVE;
  479. }
  480. }
  481. /*!
  482. \brief SPI transmit data
  483. \param[in] spi_periph: SPIx(x=0,1)
  484. \param[in] data: 16-bit data
  485. \param[out] none
  486. \retval none
  487. */
  488. void spi_i2s_data_transmit(uint32_t spi_periph, uint16_t data)
  489. {
  490. uint32_t reg, byten;
  491. if(SPI0 == spi_periph) {
  492. SPI_DATA(spi_periph) = (uint32_t)data;
  493. } else {
  494. /* get the access size to FIFO */
  495. byten = SPI_CTL1(spi_periph) & SPI_BYTEN_MASK;
  496. if(RESET != byten) {
  497. reg = spi_periph + 0x0CU;
  498. *(uint8_t *)(reg) = (uint8_t)data;
  499. } else {
  500. SPI_DATA(spi_periph) = (uint16_t)data;
  501. }
  502. }
  503. }
  504. /*!
  505. \brief SPI receive data
  506. \param[in] spi_periph: SPIx(x=0,1)
  507. \param[out] none
  508. \retval 16-bit data
  509. */
  510. uint16_t spi_i2s_data_receive(uint32_t spi_periph)
  511. {
  512. uint32_t reg, byten;
  513. if(SPI0 == spi_periph) {
  514. return ((uint16_t)SPI_DATA(spi_periph));
  515. } else {
  516. /* get the access size to FIFO */
  517. byten = SPI_CTL1(spi_periph) & SPI_BYTEN_MASK;
  518. if(RESET != byten) {
  519. reg = spi_periph + 0x0CU;
  520. return (uint16_t)(*(uint8_t *)(reg));
  521. } else {
  522. return ((uint16_t)SPI_DATA(spi_periph));
  523. }
  524. }
  525. }
  526. /*!
  527. \brief set SPI CRC polynomial
  528. \param[in] spi_periph: SPIx(x=0,1)
  529. \param[in] crc_poly: CRC polynomial value
  530. \param[out] none
  531. \retval none
  532. */
  533. void spi_crc_polynomial_set(uint32_t spi_periph, uint16_t crc_poly)
  534. {
  535. /* enable SPI CRC */
  536. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCEN;
  537. /* set SPI CRC polynomial */
  538. SPI_CRCPOLY(spi_periph) = (uint32_t)crc_poly;
  539. }
  540. /*!
  541. \brief get SPI CRC polynomial
  542. \param[in] spi_periph: SPIx(x=0,1)
  543. \param[out] none
  544. \retval 16-bit CRC polynomial
  545. */
  546. uint16_t spi_crc_polynomial_get(uint32_t spi_periph)
  547. {
  548. return ((uint16_t)SPI_CRCPOLY(spi_periph));
  549. }
  550. /*!
  551. \brief set CRC length
  552. \param[in] spi_periph: SPIx(x=1)
  553. \param[in] crc_length: CRC length
  554. only one parameter can be selected which is shown as below:
  555. \arg SPI_CRC_8BIT: CRC length is 8 bits
  556. \arg SPI_CRC_16BIT: CRC length is 16 bits
  557. \param[out] none
  558. \retval none
  559. */
  560. void spi_crc_length_set(uint32_t spi_periph, uint16_t crc_length)
  561. {
  562. /* clear SPI_CTL0_CRCL bit */
  563. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_CRCL);
  564. /* confige SPI_CTL0_CRCL bit */
  565. SPI_CTL0(spi_periph) |= (uint32_t)crc_length;
  566. }
  567. /*!
  568. \brief turn on CRC function
  569. \param[in] spi_periph: SPIx(x=0,1)
  570. \param[out] none
  571. \retval none
  572. */
  573. void spi_crc_on(uint32_t spi_periph)
  574. {
  575. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCEN;
  576. }
  577. /*!
  578. \brief turn off CRC function
  579. \param[in] spi_periph: SPIx(x=0,1)
  580. \param[out] none
  581. \retval none
  582. */
  583. void spi_crc_off(uint32_t spi_periph)
  584. {
  585. SPI_CTL0(spi_periph) &= (uint32_t)(~SPI_CTL0_CRCEN);
  586. }
  587. /*!
  588. \brief SPI next data is CRC value
  589. \param[in] spi_periph: SPIx(x=0,1)
  590. \param[out] none
  591. \retval none
  592. */
  593. void spi_crc_next(uint32_t spi_periph)
  594. {
  595. SPI_CTL0(spi_periph) |= (uint32_t)SPI_CTL0_CRCNT;
  596. }
  597. /*!
  598. \brief get SPI CRC send value or receive value
  599. \param[in] spi_periph: SPIx(x=0,1)
  600. \param[in] crc: SPI crc value
  601. only one parameter can be selected which is shown as below:
  602. \arg SPI_CRC_TX: get transmit crc value
  603. \arg SPI_CRC_RX: get receive crc value
  604. \param[out] none
  605. \retval 16-bit CRC value
  606. */
  607. uint16_t spi_crc_get(uint32_t spi_periph, uint8_t crc)
  608. {
  609. if(SPI_CRC_TX == crc) {
  610. return ((uint16_t)(SPI_TCRC(spi_periph)));
  611. } else {
  612. return ((uint16_t)(SPI_RCRC(spi_periph)));
  613. }
  614. }
  615. /*!
  616. \brief enable SPI TI mode
  617. \param[in] spi_periph: SPIx(x=0,1)
  618. \param[out] none
  619. \retval none
  620. */
  621. void spi_ti_mode_enable(uint32_t spi_periph)
  622. {
  623. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_TMOD;
  624. }
  625. /*!
  626. \brief disable SPI TI mode
  627. \param[in] spi_periph: SPIx(x=0,1)
  628. \param[out] none
  629. \retval none
  630. */
  631. void spi_ti_mode_disable(uint32_t spi_periph)
  632. {
  633. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_TMOD);
  634. }
  635. /*!
  636. \brief enable SPI NSS pulse mode
  637. \param[in] spi_periph: SPIx(x=0,1)
  638. \param[out] none
  639. \retval none
  640. */
  641. void spi_nssp_mode_enable(uint32_t spi_periph)
  642. {
  643. SPI_CTL1(spi_periph) |= (uint32_t)SPI_CTL1_NSSP;
  644. }
  645. /*!
  646. \brief disable SPI NSS pulse mode
  647. \param[in] spi_periph: SPIx(x=0,1)
  648. \param[out] none
  649. \retval none
  650. */
  651. void spi_nssp_mode_disable(uint32_t spi_periph)
  652. {
  653. SPI_CTL1(spi_periph) &= (uint32_t)(~SPI_CTL1_NSSP);
  654. }
  655. /*!
  656. \brief enable quad wire SPI
  657. \param[in] spi_periph: SPIx(x=1)
  658. \param[out] none
  659. \retval none
  660. */
  661. void spi_quad_enable(uint32_t spi_periph)
  662. {
  663. SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_QMOD;
  664. }
  665. /*!
  666. \brief disable quad wire SPI
  667. \param[in] spi_periph: SPIx(x=1)
  668. \param[out] none
  669. \retval none
  670. */
  671. void spi_quad_disable(uint32_t spi_periph)
  672. {
  673. SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_QMOD);
  674. }
  675. /*!
  676. \brief enable quad wire SPI write
  677. \param[in] spi_periph: SPIx(x=1)
  678. \param[out] none
  679. \retval none
  680. */
  681. void spi_quad_write_enable(uint32_t spi_periph)
  682. {
  683. SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_QRD);
  684. }
  685. /*!
  686. \brief enable quad wire SPI read
  687. \param[in] spi_periph: SPIx(x=1)
  688. \param[out] none
  689. \retval none
  690. */
  691. void spi_quad_read_enable(uint32_t spi_periph)
  692. {
  693. SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_QRD;
  694. }
  695. /*!
  696. \brief enable SPI_IO2 and SPI_IO3 pin output
  697. \param[in] spi_periph: SPIx(x=1)
  698. \param[out] none
  699. \retval none
  700. */
  701. void spi_quad_io23_output_enable(uint32_t spi_periph)
  702. {
  703. SPI_QCTL(spi_periph) |= (uint32_t)SPI_QCTL_IO23_DRV;
  704. }
  705. /*!
  706. \brief disable SPI_IO2 and SPI_IO3 pin output
  707. \param[in] spi_periph: SPIx(x=1)
  708. \param[out] none
  709. \retval none
  710. */
  711. void spi_quad_io23_output_disable(uint32_t spi_periph)
  712. {
  713. SPI_QCTL(spi_periph) &= (uint32_t)(~SPI_QCTL_IO23_DRV);
  714. }
  715. /*!
  716. \brief get SPI and I2S flag status
  717. \param[in] spi_periph: SPIx(x=0,1)
  718. \param[in] flag: SPI/I2S flag status
  719. only one parameter can be selected which are shown as below:
  720. \arg SPI_FLAG_TBE: transmit buffer empty flag
  721. \arg SPI_FLAG_RBNE: receive buffer not empty flag
  722. \arg SPI_FLAG_TRANS: transmit on-going flag
  723. \arg SPI_FLAG_RXORERR: receive overrun error flag
  724. \arg SPI_FLAG_CONFERR: mode config error flag
  725. \arg SPI_FLAG_CRCERR: CRC error flag
  726. \arg SPI_FLAG_FERR: SPI format error interrupt flag
  727. \arg I2S_FLAG_TBE: transmit buffer empty flag
  728. \arg I2S_FLAG_RBNE: receive buffer not empty flag
  729. \arg I2S_FLAG_TRANS: transmit on-going flag
  730. \arg I2S_FLAG_RXORERR: overrun error flag
  731. \arg I2S_FLAG_TXURERR: underrun error flag
  732. \arg I2S_FLAG_CH: channel side flag
  733. \arg I2S_FLAG_FERR: I2S format error interrupt flag
  734. only for SPI1:
  735. \arg SPI_TXLVL_EMPTY: SPI TXFIFO is empty
  736. \arg SPI_TXLVL_QUARTER_FULL: SPI TXFIFO is a quarter of full
  737. \arg SPI_TXLVL_HAlF_FULL: SPI TXFIFO is a half of full
  738. \arg SPI_TXLVL_FULL: SPI TXFIFO is full
  739. \arg SPI_RXLVL_EMPTY: SPI RXFIFO is empty
  740. \arg SPI_RXLVL_QUARTER_FULL: SPI RXFIFO is a quarter of full
  741. \arg SPI_RXLVL_HAlF_FULL: SPI RXFIFO is a half of full
  742. \arg SPI_RXLVL_FULL: SPI RXFIFO is full
  743. \param[out] none
  744. \retval FlagStatus: SET or RESET
  745. */
  746. FlagStatus spi_i2s_flag_get(uint32_t spi_periph, uint32_t flag)
  747. {
  748. if(RESET != (SPI_STAT(spi_periph) & flag)) {
  749. return SET;
  750. } else {
  751. if(SPI1 == spi_periph) {
  752. /* check TXFIFO is empty or not */
  753. if(SPI_TXLVL_EMPTY == flag) {
  754. if(RESET != (SPI_STAT(spi_periph) & SPI_TXLVL_EMPTY_MASK)) {
  755. return RESET;
  756. } else {
  757. return SET;
  758. }
  759. }
  760. /* check RXFIFO is empty or not */
  761. if(SPI_RXLVL_EMPTY == flag) {
  762. if(RESET != (SPI_STAT(spi_periph) & SPI_RXLVL_EMPTY_MASK)) {
  763. return RESET;
  764. } else {
  765. return SET;
  766. }
  767. }
  768. }
  769. return RESET;
  770. }
  771. }
  772. /*!
  773. \brief enable SPI and I2S interrupt
  774. \param[in] spi_periph: SPIx(x=0,1)
  775. \param[in] interrupt: SPI/I2S interrupt
  776. only one parameter can be selected which is shown as below:
  777. \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
  778. \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
  779. \arg SPI_I2S_INT_ERR: CRC error, configuration error, reception overrun error,
  780. transmission underrun error and format error interrupt
  781. \param[out] none
  782. \retval none
  783. */
  784. void spi_i2s_interrupt_enable(uint32_t spi_periph, uint8_t interrupt)
  785. {
  786. SPI_CTL1(spi_periph) |= (uint32_t)interrupt;
  787. }
  788. /*!
  789. \brief disable SPI and I2S interrupt
  790. \param[in] spi_periph: SPIx(x=0,1)
  791. \param[in] interrupt: SPI/I2S interrupt
  792. only one parameter can be selected which is shown as below:
  793. \arg SPI_I2S_INT_TBE: transmit buffer empty interrupt
  794. \arg SPI_I2S_INT_RBNE: receive buffer not empty interrupt
  795. \arg SPI_I2S_INT_ERR: CRC error, configuration error, reception overrun error,
  796. transmission underrun error and format error interrupt
  797. \param[out] none
  798. \retval none
  799. */
  800. void spi_i2s_interrupt_disable(uint32_t spi_periph, uint8_t interrupt)
  801. {
  802. SPI_CTL1(spi_periph) &= ~(uint32_t)interrupt;
  803. }
  804. /*!
  805. \brief get SPI and I2S interrupt flag status
  806. \param[in] spi_periph: SPIx(x=0,1)
  807. \param[in] interrupt: SPI/I2S interrupt flag status
  808. only one parameter can be selected which is shown as below:
  809. \arg SPI_I2S_INT_FLAG_TBE: transmit buffer empty interrupt flag
  810. \arg SPI_I2S_INT_FLAG_RBNE: receive buffer not empty interrupt flag
  811. \arg SPI_I2S_INT_FLAG_RXORERR: overrun interrupt flag
  812. \arg SPI_INT_FLAG_CONFERR: config error interrupt flag
  813. \arg SPI_INT_FLAG_CRCERR: CRC error interrupt flag
  814. \arg I2S_INT_FLAG_TXURERR: underrun error interrupt flag
  815. \arg SPI_I2S_INT_FLAG_FERR: format error interrupt flag
  816. \param[out] none
  817. \retval FlagStatus: SET or RESET
  818. */
  819. FlagStatus spi_i2s_interrupt_flag_get(uint32_t spi_periph, uint8_t interrupt)
  820. {
  821. uint32_t reg1 = SPI_STAT(spi_periph);
  822. uint32_t reg2 = SPI_CTL1(spi_periph);
  823. switch(interrupt) {
  824. /* SPI/I2S transmit buffer empty interrupt */
  825. case SPI_I2S_INT_FLAG_TBE:
  826. reg1 = reg1 & SPI_STAT_TBE;
  827. reg2 = reg2 & SPI_CTL1_TBEIE;
  828. break;
  829. /* SPI/I2S receive buffer not empty interrupt */
  830. case SPI_I2S_INT_FLAG_RBNE:
  831. reg1 = reg1 & SPI_STAT_RBNE;
  832. reg2 = reg2 & SPI_CTL1_RBNEIE;
  833. break;
  834. /* SPI/I2S overrun interrupt */
  835. case SPI_I2S_INT_FLAG_RXORERR:
  836. reg1 = reg1 & SPI_STAT_RXORERR;
  837. reg2 = reg2 & SPI_CTL1_ERRIE;
  838. break;
  839. /* SPI config error interrupt */
  840. case SPI_INT_FLAG_CONFERR:
  841. reg1 = reg1 & SPI_STAT_CONFERR;
  842. reg2 = reg2 & SPI_CTL1_ERRIE;
  843. break;
  844. /* SPI CRC error interrupt */
  845. case SPI_INT_FLAG_CRCERR:
  846. reg1 = reg1 & SPI_STAT_CRCERR;
  847. reg2 = reg2 & SPI_CTL1_ERRIE;
  848. break;
  849. /* I2S underrun error interrupt */
  850. case I2S_INT_FLAG_TXURERR:
  851. reg1 = reg1 & SPI_STAT_TXURERR;
  852. reg2 = reg2 & SPI_CTL1_ERRIE;
  853. break;
  854. /* SPI/I2S format error interrupt */
  855. case SPI_I2S_INT_FLAG_FERR:
  856. reg1 = reg1 & SPI_STAT_FERR;
  857. reg2 = reg2 & SPI_CTL1_ERRIE;
  858. break;
  859. default :
  860. break;
  861. }
  862. /*get SPI/I2S interrupt flag status */
  863. if((0U != reg1) && (0U != reg2)) {
  864. return SET;
  865. } else {
  866. return RESET;
  867. }
  868. }
  869. /*!
  870. \brief clear SPI CRC error flag status
  871. \param[in] spi_periph: SPIx(x=0,1)
  872. \param[out] none
  873. \retval none
  874. */
  875. void spi_crc_error_clear(uint32_t spi_periph)
  876. {
  877. SPI_STAT(spi_periph) &= (uint32_t)(~SPI_FLAG_CRCERR);
  878. }