gd32f30x_spi.c 28 KB

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