gd32f10x_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /**
  2. ******************************************************************************
  3. * @brief SPI functions of the firmware library.
  4. ******************************************************************************
  5. */
  6. /* Includes ------------------------------------------------------------------*/
  7. #include "gd32f10x_spi.h"
  8. #include "gd32f10x_rcc.h"
  9. /** @addtogroup GD32F10x_Firmware
  10. * @{
  11. */
  12. /** @defgroup SPI
  13. * @brief SPI driver modules
  14. * @{
  15. */
  16. /** @defgroup SPI_Private_Defines
  17. * @{
  18. */
  19. /* SPI registers Masks */
  20. #define CTLR1_CLEAR_MASK ((uint16_t)0x3040)
  21. #define I2SCTLR_CLEAR_MASK ((uint16_t)0xF040)
  22. /* I2S clock source selection Masks */
  23. #define I2S2_CLOCK_SRC ((uint32_t)(0x00020000))
  24. #define I2S3_CLOCK_SRC ((uint32_t)(0x00040000))
  25. #define I2S_MUL_MASK ((uint32_t)(0x0000F000))
  26. #define I2S_DIV_MASK ((uint32_t)(0x000000F0))
  27. /**
  28. * @}
  29. */
  30. /** @defgroup SPI_Private_Functions
  31. * @{
  32. */
  33. /**
  34. * @brief Reset the SPIx and the I2Sx peripheral.
  35. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  36. * @retval None
  37. */
  38. void SPI_I2S_DeInit(SPI_TypeDef *SPIx)
  39. {
  40. if (SPIx == SPI1) {
  41. /* Reset SPI1 */
  42. RCC_APB2PeriphReset_Enable(RCC_APB2PERIPH_SPI1RST, ENABLE);
  43. RCC_APB2PeriphReset_Enable(RCC_APB2PERIPH_SPI1RST, DISABLE);
  44. } else if (SPIx == SPI2) {
  45. /* Reset SPI2 and I2S2 peripheral */
  46. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_SPI2RST, ENABLE);
  47. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_SPI2RST, DISABLE);
  48. } else if (SPIx == SPI3) {
  49. /* Reset SPI3 and I2S3 peripheral */
  50. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_SPI3RST, ENABLE);
  51. RCC_APB1PeriphReset_Enable(RCC_APB1PERIPH_SPI3RST, DISABLE);
  52. }
  53. }
  54. /**
  55. * @brief Initialize the SPIx peripheral.
  56. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  57. * @param SPI_InitParameter: The structuer contains configuration information.
  58. * @retval None
  59. */
  60. void SPI_Init(SPI_TypeDef *SPIx, SPI_InitPara *SPI_InitParameter)
  61. {
  62. uint16_t temp_ctrl1 = 0;
  63. /* Configure SPIx CTRL1 according to the SPI_InitParameter */
  64. temp_ctrl1 = SPIx->CTLR1;
  65. temp_ctrl1 &= CTLR1_CLEAR_MASK;
  66. temp_ctrl1 |= (uint16_t)((uint32_t)SPI_InitParameter->SPI_TransType | SPI_InitParameter->SPI_Mode |
  67. SPI_InitParameter->SPI_FrameFormat | SPI_InitParameter->SPI_SCKPL |
  68. SPI_InitParameter->SPI_SCKPH | SPI_InitParameter->SPI_SWNSSEN |
  69. SPI_InitParameter->SPI_PSC | SPI_InitParameter->SPI_FirstBit);
  70. SPIx->CTLR1 = temp_ctrl1;
  71. /* Configure SPIx CRC Polynomial */
  72. SPIx->CPR = SPI_InitParameter->SPI_CRCPOL;
  73. /* Configure the I2SSEL bit in I2SCTLR register as SPI mode */
  74. SPIx->I2SCTLR &= ~SPI_I2SCTLR_I2SSEL;
  75. }
  76. /**
  77. * @brief Initialize the I2Sx peripheral.
  78. * @param SPIx: the SPI/I2S peripheral where x can be 2 or 3.
  79. * @param I2S_InitParameter: The structuer contains configuration information.
  80. * @retval None
  81. */
  82. void I2S_Init(SPI_TypeDef *SPIx, I2S_InitPara *I2S_InitParameter)
  83. {
  84. uint16_t temp_i2sctrl = 0, div = 2, of = 0;
  85. uint32_t temp = 0;
  86. RCC_ClocksPara RCC_Clocks;
  87. uint32_t sourceclock = 0;
  88. /* SPIx I2SCTLR & I2SCKP Configuration */
  89. /* Deinit I2SCTLR I2SCKP register */
  90. SPIx->I2SCTLR &= I2SCTLR_CLEAR_MASK;
  91. SPIx->I2SCKP = 0x0002;
  92. /* Default config of the prescaler*/
  93. if (I2S_InitParameter->I2S_AudioFreq == I2S_AUDIOFREQ_DEFAULT) {
  94. of = (uint16_t)0;
  95. div = (uint16_t)2;
  96. } else {
  97. /* Get the I2S clock source */
  98. if (((uint32_t)SPIx) == SPI2_BASE) {
  99. temp = I2S2_CLOCK_SRC;
  100. } else {
  101. temp = I2S3_CLOCK_SRC;
  102. }
  103. /* I2S clock source configuration depend on different device */
  104. #ifdef GD32F10X_CL
  105. if ((RCC->GCFGR2 & temp) != 0) {
  106. /* Get RCC PLL3 multiplier */
  107. temp = (uint32_t)((RCC->GCFGR2 & I2S_MUL_MASK) >> 12);
  108. if ((temp > 5) && (temp < 15)) {
  109. /* Multiplier is between 8 and 14 (value 15 is forbidden) */
  110. temp += 2;
  111. } else {
  112. if (temp == 15) {
  113. /* Multiplier is 20 */
  114. temp = 20;
  115. }
  116. }
  117. /* Get the PREDV2 value */
  118. sourceclock = (uint32_t)(((RCC->GCFGR2 & I2S_DIV_MASK) >> 4) + 1);
  119. /* Calculate sourceclock based on PLL3 and PREDV2 */
  120. sourceclock = (uint32_t)((HSE_VALUE / sourceclock) * temp * 2);
  121. } else {
  122. /* Get system clock */
  123. RCC_GetClocksFreq(&RCC_Clocks);
  124. sourceclock = RCC_Clocks.CK_SYS_Frequency;
  125. }
  126. #else
  127. /* Get system clock */
  128. RCC_GetClocksFreq(&RCC_Clocks);
  129. sourceclock = RCC_Clocks.CK_SYS_Frequency;
  130. #endif /* GD32F10X_CL */
  131. /* Calculate the prescaler depending on the MCLK output state and the data format with a flaoting point. */
  132. if (I2S_InitParameter->I2S_MCKOE == I2S_MCK_ENABLE) {
  133. temp = (uint16_t)(((((sourceclock / 256) * 10) / I2S_InitParameter->I2S_AudioFreq)) + 5);
  134. } else {
  135. if (I2S_InitParameter->I2S_FrameFormat == I2S_FRAMEFORMAT_DL16b_CL16b) {
  136. temp = (uint16_t)(((((sourceclock / 32) * 10) / I2S_InitParameter->I2S_AudioFreq)) + 5);
  137. } else {
  138. temp = (uint16_t)(((((sourceclock / 64) * 10) / I2S_InitParameter->I2S_AudioFreq)) + 5);
  139. }
  140. }
  141. /* Remove the flaoting point */
  142. temp = temp / 10;
  143. of = (uint16_t)(temp & (uint16_t)0x0001);
  144. div = (uint16_t)((temp - of) / 2);
  145. of = (uint16_t)(of << 8);
  146. }
  147. /* Inappropriate prescaler, Set the default values */
  148. if ((div < 1) || (div > 0xFF)) {
  149. div = 2;
  150. of = 0;
  151. }
  152. /* Configure SPIx I2SCKP */
  153. SPIx->I2SCKP = (uint16_t)(div | (uint16_t)(of | (uint16_t)I2S_InitParameter->I2S_MCKOE));
  154. /* Configure SPIx I2SCTLR according to the I2S_InitParameter */
  155. temp_i2sctrl = SPIx->I2SCTLR;
  156. temp_i2sctrl |= (uint16_t)(SPI_I2SCTLR_I2SSEL | (uint16_t)(I2S_InitParameter->I2S_Mode | \
  157. (uint16_t)(I2S_InitParameter->I2S_STD | (uint16_t)(I2S_InitParameter->I2S_FrameFormat | \
  158. (uint16_t)I2S_InitParameter->I2S_CKPL))));
  159. SPIx->I2SCTLR = temp_i2sctrl;
  160. }
  161. /**
  162. * @brief Initial SPI_InitParameter members.
  163. * @param SPI_InitParameter : pointer to a SPI_InitPara structure.
  164. * @retval None
  165. */
  166. void SPI_ParaInit(SPI_InitPara *SPI_InitParameter)
  167. {
  168. /* Reset SPI init structure parameters values */
  169. SPI_InitParameter->SPI_TransType = SPI_TRANSTYPE_FULLDUPLEX;
  170. SPI_InitParameter->SPI_Mode = SPI_MODE_SLAVE;
  171. SPI_InitParameter->SPI_FrameFormat = SPI_FRAMEFORMAT_8BIT;
  172. SPI_InitParameter->SPI_SCKPL = SPI_SCKPL_LOW;
  173. SPI_InitParameter->SPI_SCKPH = SPI_SCKPH_1EDGE;
  174. SPI_InitParameter->SPI_SWNSSEN = SPI_SWNSS_HARD;
  175. SPI_InitParameter->SPI_PSC = SPI_PSC_2;
  176. SPI_InitParameter->SPI_FirstBit = SPI_FIRSTBIT_MSB;
  177. SPI_InitParameter->SPI_CRCPOL = 7;
  178. }
  179. /**
  180. * @brief Initial I2S_InitParameter members.
  181. * @param I2S_InitParameter : pointer to a I2S_InitPara structure.
  182. * @retval None
  183. */
  184. void I2S_ParaInit(I2S_InitPara *I2S_InitParameter)
  185. {
  186. /* Reset I2S init structure parameters values */
  187. I2S_InitParameter->I2S_Mode = I2S_MODE_SLAVETX;
  188. I2S_InitParameter->I2S_STD = I2S_STD_PHILLIPS;
  189. I2S_InitParameter->I2S_FrameFormat = I2S_FRAMEFORMAT_DL16b_CL16b;
  190. I2S_InitParameter->I2S_MCKOE = I2S_MCK_DISABLE;
  191. I2S_InitParameter->I2S_AudioFreq = I2S_AUDIOFREQ_DEFAULT;
  192. I2S_InitParameter->I2S_CKPL = I2S_CKPL_LOW;
  193. }
  194. /**
  195. * @brief Enable or disable the SPI peripheral.
  196. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  197. * @param NewValue: ENABLE or DISABLE.
  198. * @retval None
  199. */
  200. void SPI_Enable(SPI_TypeDef *SPIx, TypeState NewValue)
  201. {
  202. if (NewValue != DISABLE) {
  203. SPIx->CTLR1 |= SPI_CTLR1_SPIEN;
  204. } else {
  205. SPIx->CTLR1 &= ~SPI_CTLR1_SPIEN;
  206. }
  207. }
  208. /**
  209. * @brief Enable or disable the I2S peripheral.
  210. * @param SPIx: the SPI/I2S peripheral where x can be 2 or 3.
  211. * @param NewValue: ENABLE or DISABLE.
  212. * @retval None
  213. */
  214. void I2S_Enable(SPI_TypeDef *SPIx, TypeState NewValue)
  215. {
  216. if (NewValue != DISABLE) {
  217. SPIx->I2SCTLR |= SPI_I2SCTLR_I2SEN;
  218. } else {
  219. SPIx->I2SCTLR &= ~SPI_I2SCTLR_I2SEN;
  220. }
  221. }
  222. /**
  223. * @brief Enable or disable the SPI or I2S interrupts.
  224. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  225. * @param SPI_I2S_INT: specifies the SPI or I2S interrupt source. Select one of the follwing values :
  226. * @arg SPI_I2S_INT_TBE: Tx buffer empty interrupt mask
  227. * @arg SPI_I2S_INT_RBNE: Rx buffer not empty interrupt mask
  228. * @arg SPI_I2S_INT_ERR: Error interrupt mask
  229. * @param NewValue: ENABLE or DISABLE.
  230. * @retval None
  231. */
  232. void SPI_I2S_INTConfig(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT, TypeState NewValue)
  233. {
  234. uint16_t intmask = 0 ;
  235. /* Get the interrupt enable bit in CTRL2 */
  236. intmask = (uint16_t)1 << (uint16_t)(SPI_I2S_INT >> 4);
  237. /* Enable or disable the selected interrupt */
  238. if (NewValue != DISABLE) {
  239. SPIx->CTLR2 |= intmask;
  240. } else {
  241. SPIx->CTLR2 &= (uint16_t)~intmask;
  242. }
  243. }
  244. /**
  245. * @brief Enable or disable the SPIx or I2Sx DMA transfer request.
  246. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  247. * @param SPI_I2S_DMAReq: Select the SPI or I2S DMA transfer request. Select one of the follwing values :
  248. * @arg SPI_I2S_DMA_TX: Tx buffer DMA transfer request
  249. * @arg SPI_I2S_DMA_RX: Rx buffer DMA transfer request
  250. * @param NewValue: ENABLE or DISABLE.
  251. * @retval None
  252. */
  253. void SPI_I2S_DMA_Enable(SPI_TypeDef *SPIx, uint16_t SPI_I2S_DMAReq, TypeState NewValue)
  254. {
  255. if (NewValue != DISABLE) {
  256. SPIx->CTLR2 |= SPI_I2S_DMAReq;
  257. } else {
  258. SPIx->CTLR2 &= (uint16_t)~SPI_I2S_DMAReq;
  259. }
  260. }
  261. /**
  262. * @brief Send a Data by the SPIx or I2Sx peripheral.
  263. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  264. * @param Data : Data to be Send.
  265. * @retval None
  266. */
  267. void SPI_I2S_SendData(SPI_TypeDef *SPIx, uint16_t Data)
  268. {
  269. SPIx->DTR = Data;
  270. }
  271. /**
  272. * @brief Return the received data by the SPIx or I2Sx peripheral.
  273. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  274. * @retval The value of the received data.
  275. */
  276. uint16_t SPI_I2S_ReceiveData(SPI_TypeDef *SPIx)
  277. {
  278. return SPIx->DTR;
  279. }
  280. /**
  281. * @brief NSS pin internal software management.
  282. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  283. * @param SPI_SWNSS: specifies the SPI NSS internal state. Select one of the follwing values :
  284. * @arg SPI_SWNSS_SET: Set NSS pin internally
  285. * @arg SPI_SWNSS_RESET: Reset NSS pin internally
  286. * @retval None
  287. */
  288. void SPI_SWNSSConfig(SPI_TypeDef *SPIx, uint16_t SPI_SWNSS)
  289. {
  290. if (SPI_SWNSS != SPI_SWNSS_RESET) {
  291. /* Set NSS pin */
  292. SPIx->CTLR1 |= SPI_CTLR1_SWNSS;
  293. } else {
  294. /* Reset NSS pin */
  295. SPIx->CTLR1 &= ~SPI_CTLR1_SWNSS;
  296. }
  297. }
  298. /**
  299. * @brief Enable or disable the NSS output.
  300. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  301. * @param NewValue: ENABLE or DISABLE.
  302. * @retval None
  303. */
  304. void SPI_NSSDRV(SPI_TypeDef *SPIx, TypeState NewValue)
  305. {
  306. if (NewValue != DISABLE) {
  307. SPIx->CTLR2 |= SPI_CTLR2_NSSDRV;
  308. } else {
  309. SPIx->CTLR2 &= ~SPI_CTLR2_NSSDRV;
  310. }
  311. }
  312. /**
  313. * @brief Configure the data frame format.
  314. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  315. * @param SPI_FrameFormat: Select the data frame format. Select one of the follwing values :
  316. * @arg SPI_FRAMEFORMAT_16BIT: Set data frame format to 16bit
  317. * @arg SPI_FRAMEFORMAT_8BIT: Set data frame format to 8bit
  318. * @retval None
  319. */
  320. void SPI_FrameFormatConfig(SPI_TypeDef *SPIx, uint16_t SPI_FrameFormat)
  321. {
  322. /* Clear FF16 bit */
  323. SPIx->CTLR1 &= (uint16_t)~SPI_FRAMEFORMAT_16BIT;
  324. /* Set new FF16 bit value */
  325. SPIx->CTLR1 |= SPI_FrameFormat;
  326. }
  327. /**
  328. * @brief Send the SPIx CRC value.
  329. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  330. * @retval None
  331. */
  332. void SPI_SendCRCNext(SPI_TypeDef *SPIx)
  333. {
  334. /* Enable the CRC transmission */
  335. SPIx->CTLR1 |= SPI_CTLR1_CRCNT;
  336. }
  337. /**
  338. * @brief Enable or disable the CRC calculation.
  339. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  340. * @param NewValue: ENABLE or DISABLE.
  341. * @retval None
  342. */
  343. void SPI_CRC_Enable(SPI_TypeDef *SPIx, TypeState NewValue)
  344. {
  345. if (NewValue != DISABLE) {
  346. /* Enable the CRC calculation */
  347. SPIx->CTLR1 |= SPI_CTLR1_CRCEN;
  348. } else {
  349. /* Disable the CRC calculation */
  350. SPIx->CTLR1 &= ~SPI_CTLR1_CRCEN;
  351. }
  352. }
  353. /**
  354. * @brief Get the transmit or the receive CRC register value.
  355. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  356. * @param SPI_CRC: Select the transmit or the receive CRC register. Select one of the follwing values :
  357. * @arg SPI_CRC_TX: Selects Tx CRC register
  358. * @arg SPI_CRC_RX: Selects Rx CRC register
  359. * @retval The selected CRC register value.
  360. */
  361. uint16_t SPI_GetCRC(SPI_TypeDef *SPIx, uint8_t SPI_CRC)
  362. {
  363. if (SPI_CRC != SPI_CRC_RX) {
  364. /* Transmit CRC value */
  365. return SPIx->TCR;
  366. } else {
  367. /* Receive CRC value */
  368. return SPIx->RCR;
  369. }
  370. }
  371. /**
  372. * @brief Get the CRC Polynomial value.
  373. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  374. * @retval The CRC Polynomial value.
  375. */
  376. uint16_t SPI_GetCRCPolynomial(SPI_TypeDef *SPIx)
  377. {
  378. return SPIx->CPR;
  379. }
  380. /**
  381. * @brief Select the transfer direction in bidirectional mode.
  382. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  383. * @param SPI_BDOE: The transfer direction in bi-directional mode. Select one of the follwing values :
  384. * @arg SPI_BDOE_TX: Selects Tx transmission direction
  385. * @arg SPI_BDOE_RX: Selects Rx receive direction
  386. * @retval None
  387. */
  388. void SPI_BDOEConfig(SPI_TypeDef *SPIx, uint16_t SPI_BDOE)
  389. {
  390. if (SPI_BDOE == SPI_BDOE_TX) {
  391. /* Transmit only mode*/
  392. SPIx->CTLR1 |= SPI_BDOE_TX;
  393. } else {
  394. /* Receive only mode */
  395. SPIx->CTLR1 &= SPI_BDOE_RX;
  396. }
  397. }
  398. /**
  399. * @brief Check whether the flag is set or not.
  400. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  401. * @param SPI_I2S_FLAG: Select the flag. Select one of the follwing values :
  402. * @arg SPI_FLAG_TBE: Transmit buffer empty flag.
  403. * @arg SPI_FLAG_RBNE: Receive buffer not empty flag.
  404. * @arg SPI_FLAG_BSY: Busy flag.
  405. * @arg SPI_FLAG_OVR: Overrun flag.
  406. * @arg SPI_FLAG_MODF: Mode Fault flag.
  407. * @arg SPI_FLAG_CRCERR: CRC Error flag.
  408. * @arg I2S_FLAG_TBE: Transmit buffer empty flag.
  409. * @arg I2S_FLAG_RBNE: Receive buffer not empty flag.
  410. * @arg I2S_FLAG_BSY: Busy flag.
  411. * @arg I2S_FLAG_OVR: Overrun flag.
  412. * @arg I2S_FLAG_UDR: Underrun Error flag.
  413. * @arg I2S_FLAG_CHSIDE: Channel Side flag.
  414. * @retval The new state of SPI_I2S_FLAG.
  415. */
  416. TypeState SPI_I2S_GetBitState(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG)
  417. {
  418. /* Check the status of the selected flag */
  419. if ((SPIx->STR & SPI_I2S_FLAG) != (uint16_t)RESET) {
  420. return SET;
  421. } else {
  422. return RESET;
  423. }
  424. }
  425. /**
  426. * @brief Clear the flag, only used for clear CRCERR flag.
  427. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  428. * @param SPI_I2S_FLAG: Select the flag. This parametric only can be SPI_FLAG_CRCERR.
  429. * @note
  430. * The other flags are cleared by software sequences:
  431. * - OVR (OverRun error) flag is cleared by software sequence: a read
  432. * operation to SPI_DTR register (SPI_I2S_ReceiveData()) followed by a read
  433. * operation to SPI_STR register (SPI_I2S_GetBitState()).
  434. * - UDR (UnderRun error) flag is cleared by a read operation to
  435. * SPI_STR register (SPI_I2S_GetBitState()).
  436. * - MODF (Mode Fault) flag is cleared by software sequence: a read/write
  437. * operation to SPI_STR register (SPI_I2S_GetBitState()) followed by a
  438. * write operation to SPI_CTLR1 register (SPI_Enable() to enable the SPI).
  439. * @retval None
  440. */
  441. void SPI_I2S_ClearBitState(SPI_TypeDef *SPIx, uint16_t SPI_I2S_FLAG)
  442. {
  443. SPIx->STR = (uint16_t)~SPI_I2S_FLAG;
  444. }
  445. /**
  446. * @brief Check whether interrupt has occurred.
  447. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  448. * @param SPI_I2S_INT: Select the SPI/I2S interrupt. Select one of the follwing values :
  449. * @arg SPI_I2S_INT_TBE: Transmit buffer empty interrupt.
  450. * @arg SPI_I2S_INT_RBNE: Receive buffer not empty interrupt.
  451. * @arg SPI_I2S_INT_OVR: Overrun interrupt.
  452. * @arg SPI_INT_MODF: Mode Fault interrupt.
  453. * @arg SPI_INT_CRCERR: CRC Error interrupt.
  454. * @arg I2S_INT_UDR: Underrun Error interrupt.
  455. * @retval The new state of SPI_I2S_INT.
  456. */
  457. TypeState SPI_I2S_GetIntBitState(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT)
  458. {
  459. uint16_t intposition = 0, intmask = 0;
  460. /* Get the interrupt pending bit and enable bit */
  461. intposition = 0x01 << (SPI_I2S_INT & 0x0F);
  462. intmask = 0x01 << (SPI_I2S_INT >> 4);
  463. /* Check the status of the interrupt */
  464. if (((SPIx->STR & intposition) != (uint16_t)RESET) && (SPIx->CTLR2 & intmask)) {
  465. return SET;
  466. } else {
  467. return RESET;
  468. }
  469. }
  470. /**
  471. * @brief Clear the SPIx or I2S interrupt pending bit, only uesd for clear CRCERR interrupt.
  472. * @param SPIx: the SPI/I2S peripheral where x can be 1..3.
  473. * @param SPI_I2S_INT: Select the SPI or I2S interrupt. This parametric only can be SPI_INT_CRCERR.
  474. * @note
  475. * The other flags are cleared by software sequences:
  476. * - OVR (OverRun error) flag is cleared by software sequence: a read
  477. * operation to SPI_DTR register (SPI_I2S_ReceiveData()) followed by a read
  478. * operation to SPI_STR register (SPI_I2S_GetBitState()).
  479. * - UDR (UnderRun error) flag is cleared by a read operation to
  480. * SPI_STR register (SPI_I2S_GetBitState()).
  481. * - MODF (Mode Fault) flag is cleared by software sequence: a read/write
  482. * operation to SPI_STR register (SPI_I2S_GetBitState()) followed by a
  483. * write operation to SPI_CTLR1 register (SPI_Enable() to enable the SPI).
  484. * @retval None
  485. */
  486. void SPI_I2S_ClearIntBitState(SPI_TypeDef *SPIx, uint8_t SPI_I2S_INT)
  487. {
  488. uint16_t itpos = 0;
  489. /* Clear the select interrupt pending bit. */
  490. itpos = 0x01 << (SPI_I2S_INT & 0x0F);
  491. SPIx->STR = (uint16_t)~itpos;
  492. }
  493. /**
  494. * @}
  495. */
  496. /**
  497. * @}
  498. */
  499. /**
  500. * @}
  501. */