gd32f4xx_spi.c 29 KB

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