gd32f30x_spi.c 27 KB

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