gd32e23x_spi.c 33 KB

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