hal_spi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_spi.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE SPI FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_SPI_C_
  19. #include <math.h>
  20. // Files includes
  21. #include "hal_spi.h"
  22. #include "hal_rcc.h"
  23. ////////////////////////////////////////////////////////////////////////////////
  24. /// @addtogroup MM32_Hardware_Abstract_Layer
  25. /// @{
  26. ////////////////////////////////////////////////////////////////////////////////
  27. /// @addtogroup SPI_HAL
  28. /// @{
  29. ////////////////////////////////////////////////////////////////////////////////
  30. ///@addtogroup SPI_Exported_Functions
  31. ///@{
  32. ////////////////////////////////////////////////////////////////////////////////
  33. /// @brief Deinitializes the spi peripheral registers to their
  34. /// default reset values.
  35. /// @param spi: Select the SPI peripheral.
  36. /// This parameter can be one of the following values:
  37. /// SPI1, SPI2.
  38. /// @retval None.
  39. ////////////////////////////////////////////////////////////////////////////////
  40. void SPI_DeInit(SPI_TypeDef* spi)
  41. {
  42. switch (*(vu32*)&spi) {
  43. case (u32)SPI2: // SPI2_BASE:
  44. RCC_APB1PeriphResetCmd(RCC_APB1ENR_SPI2, ENABLE);
  45. RCC_APB1PeriphResetCmd(RCC_APB1ENR_SPI2, DISABLE);
  46. break;
  47. case (u32)SPI3: // SPI3_BASE:
  48. RCC_APB1PeriphResetCmd(RCC_APB1ENR_SPI3, ENABLE);
  49. RCC_APB1PeriphResetCmd(RCC_APB1ENR_SPI3, DISABLE);
  50. break;
  51. case (u32)SPI1: // SPI1_BASE:
  52. RCC_APB2PeriphResetCmd(RCC_APB2ENR_SPI1, ENABLE);
  53. RCC_APB2PeriphResetCmd(RCC_APB2ENR_SPI1, DISABLE);
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. ////////////////////////////////////////////////////////////////////////////////
  60. /// @brief Initializes the spi peripheral according to the specified
  61. /// parameters in the init_struct .
  62. /// @param spi: Select the SPI peripheral.
  63. /// This parameter can be one of the following values:
  64. /// SPI1, SPI2.
  65. /// @param init_struct: pointer to a SPI_InitTypeDef structure
  66. /// that contains the configuration information for the
  67. /// specified SPI peripheral.
  68. /// @retval None.
  69. ////////////////////////////////////////////////////////////////////////////////
  70. void SPI_Init(SPI_TypeDef* spi, SPI_InitTypeDef* init_struct)
  71. {
  72. if (init_struct->SPI_DataSize == SPI_DataSize_32b) {
  73. SET_BIT(spi->GCR, SPI_GCR_DWSEL);
  74. }
  75. else {
  76. CLEAR_BIT(spi->GCR, SPI_GCR_DWSEL);
  77. }
  78. MODIFY_REG(spi->GCR, SPI_GCR_NSS, init_struct->SPI_NSS);
  79. MODIFY_REG(spi->GCR, SPI_GCR_MODE, init_struct->SPI_Mode);
  80. MODIFY_REG(spi->CCR, SPI_CCR_LSBFE, init_struct->SPI_FirstBit);
  81. MODIFY_REG(spi->CCR, SPI_CCR_CPOL, init_struct->SPI_CPOL);
  82. MODIFY_REG(spi->CCR, SPI_CCR_CPHA, init_struct->SPI_CPHA);
  83. SET_BIT(spi->CCR, SPI_CCR_SPILEN);
  84. MODIFY_REG(spi->BRR, BRR_Mask, init_struct->SPI_BaudRatePrescaler);
  85. if (init_struct->SPI_DataWidth >= 32) {
  86. MODIFY_REG(spi->ECR, ECR_Mask, 0);
  87. }
  88. else {
  89. MODIFY_REG(spi->ECR, ECR_Mask, init_struct->SPI_DataWidth);
  90. }
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. /// @brief Fills each init_struct member with its default value.
  94. /// @param init_struct: pointer to a SPI_InitTypeDef structure
  95. /// which will be initialized.
  96. /// @retval None.
  97. ////////////////////////////////////////////////////////////////////////////////
  98. void SPI_StructInit(SPI_InitTypeDef* init_struct)
  99. {
  100. init_struct->SPI_Mode = SPI_Mode_Slave;
  101. init_struct->SPI_DataSize = SPI_DataSize_8b;
  102. init_struct->SPI_DataWidth = 8;
  103. init_struct->SPI_CPOL = SPI_CPOL_Low;
  104. init_struct->SPI_CPHA = SPI_CPHA_1Edge;
  105. init_struct->SPI_NSS = SPI_NSS_Soft;
  106. init_struct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  107. init_struct->SPI_FirstBit = SPI_FirstBit_MSB;
  108. }
  109. ////////////////////////////////////////////////////////////////////////////////
  110. /// @brief Enables or disables the specified SPI peripheral.
  111. /// @param spi: Select the SPI peripheral.
  112. /// This parameter can be one of the following values:
  113. /// SPI1, SPI2.
  114. /// @param state: new state of the spi peripheral.
  115. /// This parameter can be: ENABLE or DISABLE.
  116. /// @retval None.
  117. ////////////////////////////////////////////////////////////////////////////////
  118. void SPI_Cmd(SPI_TypeDef* spi, FunctionalState state)
  119. {
  120. (state) ? SET_BIT(spi->GCR, SPI_GCR_SPIEN) : CLEAR_BIT(spi->GCR, SPI_GCR_SPIEN);
  121. }
  122. ////////////////////////////////////////////////////////////////////////////////
  123. /// @brief Enables or disables the specified SPI interrupts.
  124. /// @param spi: Select the SPI peripheral.
  125. /// This parameter can be one of the following values:SPI1, SPI2.
  126. /// @param interrupt: specifies the SPI interrupt sources to be
  127. /// enabled or disabled.
  128. /// This parameter can be one of the following values:
  129. /// @arg SPI_IT_TXEPT: Transmitter empty interrupt
  130. /// @arg SPI_IT_RXFULL: RX FIFO full interrupt
  131. /// @arg SPI_IT_RXMATCH: Receive data match the RXDNR number interrupt
  132. /// @arg SPI_IT_RXOERR: Receive overrun error interrupt
  133. /// @arg SPI_IT_UNDERRUN: underrun interrupt
  134. /// @arg SPI_IT_RX: Receive data available interrupt
  135. /// @arg SPI_IT_TX: Transmit FIFO available interrupt
  136. /// @param state: new state of the specified spi interrupts.
  137. /// This parameter can be: ENABLE or DISABLE.
  138. /// @retval None.
  139. ////////////////////////////////////////////////////////////////////////////////
  140. void SPI_ITConfig(SPI_TypeDef* spi, u8 interrupt, FunctionalState state)
  141. {
  142. if (state) {
  143. SET_BIT(spi->GCR, (u32)SPI_GCR_IEN);
  144. SET_BIT(spi->IER, (u32)interrupt);
  145. }
  146. else {
  147. CLEAR_BIT(spi->IER, interrupt);
  148. CLEAR_BIT(spi->GCR, SPI_GCR_IEN);
  149. }
  150. }
  151. ////////////////////////////////////////////////////////////////////////////////
  152. /// @brief Enables or disables the SPI DMA interface.
  153. /// @param spi: Select the SPI peripheral.
  154. /// This parameter can be one of the following values:
  155. /// SPI1, SPI2.
  156. /// @param state: new state of the DMA Request sources.
  157. /// This parameter can be: ENABLE or DISABLE.
  158. /// @retval None.
  159. ////////////////////////////////////////////////////////////////////////////////
  160. void SPI_DMACmd(SPI_TypeDef* spi, FunctionalState state)
  161. {
  162. (state) ? SET_BIT(spi->GCR, SPI_GCR_DMAEN) : CLEAR_BIT(spi->GCR, SPI_GCR_DMAEN);
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. /// @brief configure tn Fifo trigger level bit.
  166. /// @param spi: Select the SPI peripheral.
  167. /// This parameter can be one of the following values:
  168. /// SPI1, SPI2.
  169. /// @param fifo_trigger_value: specifies the Fifo trigger level
  170. /// This parameter can be any combination of the following values:
  171. /// SPI_TXTLF : SPI TX FIFO Trigger value set
  172. /// SPI_RXTLF : SPI RX FIFO Trigger value set
  173. /// @param state: new state of the selected SPI transfer request.
  174. /// This parameter can be: ENABLE or DISABLE.
  175. /// @retval None.
  176. ////////////////////////////////////////////////////////////////////////////////
  177. void SPI_FifoTrigger(SPI_TypeDef* spi, SPI_TLF_TypeDef fifo_trigger_value, FunctionalState state)
  178. {
  179. (state) ? SET_BIT(spi->GCR, (u32)fifo_trigger_value) : CLEAR_BIT(spi->GCR, (u32)fifo_trigger_value);
  180. }
  181. ////////////////////////////////////////////////////////////////////////////////
  182. /// @brief Transmits a Data through the spi peripheral.
  183. /// @param spi: Select the SPI peripheral.
  184. /// This parameter can be one of the following values:
  185. /// SPI1, SPI2.
  186. /// @param data : Data to be transmitted.
  187. /// @retval None.
  188. ////////////////////////////////////////////////////////////////////////////////
  189. void SPI_SendData(SPI_TypeDef* spi, u32 data)
  190. {
  191. u16 templen;
  192. __asm volatile("cpsid i");
  193. WRITE_REG(spi->TDR, data);
  194. templen = READ_REG(spi->ECR);
  195. if(templen == 0)
  196. templen = 32;
  197. if (templen > 8)
  198. WRITE_REG(spi->TDR, data >> 8);
  199. if (templen > 16)
  200. WRITE_REG(spi->TDR, data >> 16);
  201. if (templen > 24)
  202. WRITE_REG(spi->TDR, data >> 24);
  203. __asm volatile("cpsie i");
  204. }
  205. ////////////////////////////////////////////////////////////////////////////////
  206. /// @brief Returns the most recent received data by the spi peripheral.
  207. /// @param spi: Select the SPI peripheral.
  208. /// This parameter can be one of the following values:
  209. /// SPI1, SPI2.
  210. /// @retval The value of the received data.
  211. ////////////////////////////////////////////////////////////////////////////////
  212. u32 SPI_ReceiveData(SPI_TypeDef* spi)
  213. {
  214. u32 temp;
  215. u8 templen;
  216. __asm volatile("cpsid i");
  217. temp = READ_REG(spi->RDR);
  218. templen = READ_REG(spi->ECR);
  219. if(templen == 0)
  220. templen = 32;
  221. if (templen > 8)
  222. temp |= (u32)(READ_REG(spi->RDR) << 8);
  223. if (templen > 16)
  224. temp |= (u32)(READ_REG(spi->RDR) << 16);
  225. if (templen > 24)
  226. temp |= (u32)(READ_REG(spi->RDR) << 24);
  227. __asm volatile("cpsie i");
  228. return temp;
  229. }
  230. ////////////////////////////////////////////////////////////////////////////////
  231. /// @brief Slave chip csn single by selected
  232. /// @param spi: Select the SPI peripheral.
  233. /// This parameter can be one of the following values:
  234. /// SPI1, SPI2.
  235. /// @param state: new state of the selected SPI CS pin
  236. /// request.
  237. /// This parameter can be: ENABLE or DISABLE.
  238. /// @retval None.
  239. ////////////////////////////////////////////////////////////////////////////////
  240. void SPI_CSInternalSelected(SPI_TypeDef* spi, FunctionalState state)
  241. {
  242. (state) ? CLEAR_BIT(spi->NSSR, SPI_NSSR_NSS) : SET_BIT(spi->NSSR, SPI_NSSR_NSS); // illogical
  243. }
  244. ////////////////////////////////////////////////////////////////////////////////
  245. /// @brief Configures the NSS pin control mode for the selected SPI.
  246. /// @param spi: Select the SPI peripheral.
  247. /// This parameter can be one of the following values:
  248. /// SPI1, SPI2.
  249. /// @param nss: specifies the SPI NSS internal state.
  250. /// This parameter can be one of the following values:
  251. /// @arg SPI_NSS_Soft: NSS pin control by software
  252. /// @arg SPI_NSS_Hard: NSS pin control by hardware
  253. /// @retval None.
  254. ////////////////////////////////////////////////////////////////////////////////
  255. void SPI_NSSInternalSoftwareConfig(SPI_TypeDef* spi, SPI_NSS_TypeDef nss)
  256. {
  257. (nss != SPI_NSS_Soft) ? SET_BIT(spi->GCR, SPI_NSS_Hard) : CLEAR_BIT(spi->GCR, SPI_NSS_Hard);
  258. }
  259. ////////////////////////////////////////////////////////////////////////////////
  260. /// @brief Configures the data size for the selected SPI.
  261. /// @param spi: Select the SPI peripheral.
  262. /// This parameter can be one of the following values:
  263. /// SPI1, SPI2.
  264. /// @param data_size: specifies the SPI data size.
  265. /// This parameter can be one of the following values:
  266. /// 0 to 31, 0 = 32b, 1 = 1b, 2 = 2b
  267. /// @arg DataSize : 0 to 31
  268. /// @retval None.
  269. /// @retval None.
  270. bool SPI_DataSizeConfig(SPI_TypeDef* spi, u8 data_size)
  271. {
  272. if (data_size > 32)
  273. return false;
  274. data_size &= 0x1F;
  275. WRITE_REG(spi->ECR, data_size);
  276. return true;
  277. }
  278. //////////////////////////////////////////////////////////////////////////////////
  279. void SPI_DataSizeTypeConfig(SPI_TypeDef* spi, SPI_DataSize_TypeDef SPI_DataSize)
  280. {
  281. CLEAR_BIT(spi->GCR, (u32)SPI_DataSize_32b);
  282. SET_BIT(spi->GCR, (u32)SPI_DataSize);
  283. }
  284. ////////////////////////////////////////////////////////////////////////////////
  285. /// @brief Selects the data transfer direction in bi-directional mode
  286. /// for the specified SPI.
  287. /// @param spi: Select the SPI peripheral.
  288. /// This parameter can be one of the following values:
  289. /// SPI1, SPI2.
  290. /// @param direction: specifies the data transfer direction in
  291. /// bi-directional mode.
  292. /// This parameter can be one of the following values:
  293. /// @arg SPI_Direction_Tx: Selects Tx transmission direction
  294. /// @arg SPI_Direction_Rx: Selects Rx receive direction
  295. /// @arg SPI_Disable_Tx: Selects Rx receive direction
  296. /// @arg SPI_Disable_Rx: Selects Rx receive direction
  297. /// @retval None.
  298. ////////////////////////////////////////////////////////////////////////////////
  299. void SPI_BiDirectionalLineConfig(SPI_TypeDef* spi, SPI_Direction_TypeDef direction)
  300. {
  301. switch (direction) {
  302. case SPI_Direction_Rx:
  303. SET_BIT(spi->GCR, SPI_GCR_RXEN);
  304. break;
  305. case SPI_Direction_Tx:
  306. SET_BIT(spi->GCR, SPI_GCR_TXEN);
  307. break;
  308. case SPI_Disable_Rx:
  309. CLEAR_BIT(spi->GCR, SPI_GCR_RXEN);
  310. break;
  311. case SPI_Disable_Tx:
  312. CLEAR_BIT(spi->GCR, SPI_GCR_TXEN);
  313. break;
  314. default:
  315. break;
  316. }
  317. }
  318. ////////////////////////////////////////////////////////////////////////////////
  319. /// @brief Checks whether the specified SPI flag is set or not.
  320. /// @param spi: Select the SPI peripheral.
  321. /// This parameter can be one of the following values:
  322. /// SPI1, SPI2.
  323. /// @param flag: specifies the SPI flag to check.
  324. /// This parameter can be one of the following values:
  325. /// @arg SPI_FLAG_RXAVL: Rx buffer has bytes flag
  326. /// @arg SPI_FLAG_TXEPT: Tx buffer and tx shifter empty flag
  327. /// @arg SPI_FLAG_TXFULL: Tx buffer full flag
  328. /// @arg SPI_FLAG_RXAVL_4BYTE: Receive available 4 byte data message flag.
  329. /// @retval The new state of SPI_FLAG (SET or RESET).
  330. ////////////////////////////////////////////////////////////////////////////////
  331. FlagStatus SPI_GetFlagStatus(SPI_TypeDef* spi, SPI_FLAG_TypeDef flag)
  332. {
  333. // u8 number;
  334. return (spi->SR & flag) ? SET : RESET;
  335. // if (spi->ECR == 8 || spi->ECR == 0)
  336. // return (spi->SR & SPI_FLAG) ? SET : RESET;
  337. // else {
  338. // if ((spi->ECR > 0) && (spi->ECR <= 8))
  339. // number = 1;
  340. // else if ((spi->ECR) <= 16)
  341. // number = 2;
  342. // else if ((spi->ECR) <= 24)
  343. // number = 3;
  344. // else if (((spi->ECR) <= 31) || (spi->ECR == 0))
  345. // number = 4;
  346. // return (((spi->SR & 0xf00) >> 8) >= number) ? SET : RESET;
  347. // }
  348. }
  349. ////////////////////////////////////////////////////////////////////////////////
  350. /// @brief Checks whether the specified SPI interrupt has occurred or not.
  351. /// @param spi: Select the SPI peripheral.
  352. /// This parameter can be one of the following values:
  353. /// SPI1, SPI2.
  354. /// @param interrupt: specifies the SPI interrupt source to check.
  355. /// This parameter can be one of the following values:
  356. /// @arg SPI_IT_TX: Tx buffer empty interrupt
  357. /// @arg SPI_IT_RX: Rx buffer interrupt
  358. /// @arg SPI_IT_UNDERRUN: under Error interrupt in slave mode
  359. /// @arg SPI_IT_RXOVER: RX OVER Error interrupt
  360. /// @arg SPI_IT_RXMATCH: spectials rx data numbers interrupt
  361. /// @arg SPI_IT_RXFULL: Rx buffer full interrupt
  362. /// @arg SPI_IT_TXEPT: Tx buffer and tx shifter empty interrupt
  363. /// @retval The new state of SPI_IT (SET or RESET).
  364. ////////////////////////////////////////////////////////////////////////////////
  365. ITStatus SPI_GetITStatus(SPI_TypeDef* spi, SPI_IT_TypeDef interrupt)
  366. {
  367. return (spi->ISR & interrupt) ? SET : RESET;
  368. }
  369. ////////////////////////////////////////////////////////////////////////////////
  370. /// @brief Clears the spi interrupt pending bit.
  371. /// @param spi: Select the SPI peripheral.
  372. /// This parameter can be one of the following values:
  373. /// SPI1, SPI2.
  374. /// @param interrupt: specifies the SPI interrupt pending bit to clear.
  375. /// @arg SPI_IT_TX: Tx buffer empty interrupt
  376. /// @arg SPI_IT_RX: Rx buffer interrupt
  377. /// @arg SPI_IT_UNDERRUN: under Error interrupt in slave mode
  378. /// @arg SPI_IT_RXOVER: RX OVER Error interrupt
  379. /// @arg SPI_IT_RXMATCH: spectials rx data numbers interrupt
  380. /// @arg SPI_IT_RXFULL: Rx buffer full interrupt
  381. /// @arg SPI_IT_TXEPT: Tx buffer and tx shifter empty interrupt
  382. /// This function clears only ERR intetrrupt pending bit.
  383. /// @retval None.
  384. ////////////////////////////////////////////////////////////////////////////////
  385. void SPI_ClearITPendingBit(SPI_TypeDef* spi, SPI_IT_TypeDef interrupt)
  386. {
  387. SET_BIT(spi->ICR, interrupt);
  388. }
  389. ////////////////////////////////////////////////////////////////////////////////
  390. /// @brief SPI Hole a count Received bytes in next receive process.
  391. /// @param spi: Select the SPI peripheral.
  392. /// This parameter can be one of the following values:
  393. /// SPI1, SPI2.
  394. /// @param number: specifies the SPI receive Number.
  395. /// This parament can be 1-65535.
  396. /// This function can use only in SPI master single receive mode.
  397. /// @retval None.
  398. ////////////////////////////////////////////////////////////////////////////////
  399. void SPI_RxBytes(SPI_TypeDef* spi, u16 number)
  400. {
  401. WRITE_REG(spi->RDNR, number);
  402. }
  403. ////////////////////////////////////////////////////////////////////////////////
  404. /// @brief slave mode tx data transmit phase adjust set.
  405. /// @param spi: Select the SPI peripheral.
  406. /// This parameter can be one of the following values:
  407. /// SPI1, SPI2.
  408. /// @param adjust_value: slave mode tx data transmit phase adjust enum.
  409. /// This parament can be :
  410. /// SPI_SlaveAdjust_FAST: fast speed use
  411. /// SPI_SlaveAdjust_LOW: low speed use
  412. /// This function can use only in SPI master single receive mode.
  413. /// @retval None.
  414. ////////////////////////////////////////////////////////////////////////////////
  415. void SPI_SlaveAdjust(SPI_TypeDef* spi, SPI_SlaveAdjust_TypeDef adjust_value)
  416. {
  417. (adjust_value) ? SET_BIT(spi->CCR, SPI_CCR_RXEDGE) : CLEAR_BIT(spi->CCR, SPI_CCR_RXEDGE);
  418. }
  419. ////////////////////////////////////////////////////////////////////////////////
  420. /// @brief Enables or disables all SPI interrupts.
  421. /// @param spi: Select the SPI peripheral.
  422. /// This parameter can be one of the following values:
  423. /// SPI1, SPI2.
  424. /// @param state: new state of all spi interrupts.
  425. /// This parameter can be: ENABLE or DISABLE.
  426. /// @retval None.
  427. ////////////////////////////////////////////////////////////////////////////////
  428. void exSPI_ITCmd(SPI_TypeDef* spi, FunctionalState state)
  429. {
  430. (state) ? SET_BIT(spi->IER, (u32)SPI_GCR_IEN) : CLEAR_BIT(spi->IER, (u32)SPI_GCR_IEN);
  431. }
  432. ////////////////////////////////////////////////////////////////////////////////
  433. /// @brief Enables or disables the specified SPI interrupts.
  434. /// @param spi: Select the SPI peripheral.
  435. /// This parameter can be one of the following values:
  436. /// SPI1, SPI2.
  437. /// @param interrupt: specifies the SPI interrupt sources to be enabled or disabled.
  438. /// This parameter can be one of the following values:
  439. /// @arg SPI_IT_TXEPT: Transmitter empty interrupt
  440. /// @arg SPI_IT_RXFULL: RX FIFO full interrupt
  441. /// @arg SPI_IT_RXMATCH: Receive data match the RXDNR number interrupt
  442. /// @arg SPI_IT_RXOERR: Receive overrun error interrupt
  443. /// @arg SPI_IT_UNDERRUN: underrun interrupt
  444. /// @arg SPI_IT_RX: Receive data available interrupt
  445. /// @arg SPI_IT_TX: Transmit FIFO available interrupt
  446. /// @param state: new state of the specified spi interrupts.
  447. /// This parameter can be: ENABLE or DISABLE.
  448. /// @retval None.
  449. ////////////////////////////////////////////////////////////////////////////////
  450. void exSPI_ITConfig(SPI_TypeDef* spi, SPI_IT_TypeDef interrupt, FunctionalState state)
  451. {
  452. (state) ? SET_BIT(spi->IER, (u32)interrupt) : CLEAR_BIT(spi->IER, (u32)interrupt);
  453. }
  454. ////////////////////////////////////////////////////////////////////////////////
  455. /// @brief Enables or disables the SPI DMA request.
  456. /// @param spi: Select the SPI peripheral.
  457. /// This parameter can be one of the following values:
  458. /// SPI1, SPI2.
  459. /// @param state: new state of the DMA Request.
  460. /// This parameter can be: ENABLE or DISABLE.
  461. /// @retval None.
  462. ////////////////////////////////////////////////////////////////////////////////
  463. void exSPI_DMACmd(SPI_TypeDef* spi, FunctionalState state)
  464. {
  465. (state) ? SET_BIT(spi->GCR, SPI_GCR_DMAEN) : CLEAR_BIT(spi->GCR, SPI_GCR_DMAEN);
  466. }
  467. ////////////////////////////////////////////////////////////////////////////////
  468. /// @brief Set or reset Slave chip csn signal output
  469. /// @param spi: Select the SPI peripheral.
  470. /// This parameter can be one of the following values:
  471. /// SPI1, SPI2.
  472. /// @param state: new state of Slave chip csn signal output.
  473. /// This parameter can be: ENABLE or DISABLE.
  474. /// @retval None.
  475. ////////////////////////////////////////////////////////////////////////////////
  476. void exSPI_CSInternalSelected(SPI_TypeDef* spi, FunctionalState state)
  477. {
  478. (state) ? CLEAR_BIT(spi->NSSR, SPI_NSSR_NSS) : SET_BIT(spi->NSSR, SPI_NSSR_NSS); // illogical
  479. }
  480. ////////////////////////////////////////////////////////////////////////////////
  481. /// @brief tx data and rx data phase adjust.
  482. /// @param spi: Select the SPI peripheral.
  483. /// This parameter can be one of the following values:
  484. /// SPI1, SPI2.
  485. /// @param adjust_value: choose adjust mode.
  486. /// This parament can be :
  487. /// SPI_DataEdgeAdjust_LOW,
  488. /// SPI_DataEdgeAdjust_FAST
  489. /// @retval None.
  490. ////////////////////////////////////////////////////////////////////////////////
  491. void exSPI_DataEdgeAdjust(SPI_TypeDef* spi, SPI_DataEdgeAdjust_TypeDef adjust_value)
  492. {
  493. // master mode
  494. if (spi->GCR & SPI_GCR_MODE) {
  495. adjust_value ? SET_BIT(spi->CCR, SPI_CCR_RXEDGE) : CLEAR_BIT(spi->CCR, SPI_CCR_RXEDGE);
  496. }
  497. // slave mode
  498. else {
  499. adjust_value ? SET_BIT(spi->CCR, SPI_CCR_TXEDGE) : CLEAR_BIT(spi->CCR, SPI_CCR_TXEDGE);
  500. }
  501. }
  502. ////////////////////////////////////////////////////////////////////////////////
  503. /// @brief Set or reset i2s
  504. /// @param spi: Select the SPI peripheral.
  505. /// This parameter can be one of the following values:
  506. /// SPI1, SPI2, SPI3.
  507. /// @param state: new state of Slave chip csn signal output.
  508. /// This parameter can be: ENABLE or DISABLE.
  509. /// @retval None.
  510. ////////////////////////////////////////////////////////////////////////////////
  511. void I2S_Cmd(SPI_TypeDef* spi, FunctionalState state)
  512. {
  513. (state) ? SET_BIT(spi->CFGR, I2S_CFGR_SPI_I2S) : CLEAR_BIT(spi->CFGR, I2S_CFGR_SPI_I2S);
  514. }
  515. ////////////////////////////////////////////////////////////////////////////////
  516. /// @brief i2s Config
  517. /// @param spi: Select the SPI peripheral.
  518. /// This parameter can be one of the following values:
  519. /// SPI1, SPI2, SPI3.
  520. /// @param state: new state of Slave chip csn signal output.
  521. /// This parameter can be: ENABLE or DISABLE.
  522. /// @retval None.
  523. ////////////////////////////////////////////////////////////////////////////////
  524. void I2S_Init(SPI_TypeDef* spi, I2S_InitTypeDef* I2S_InitStruct)
  525. {
  526. u32 i2sdiv = 2;
  527. u32 tmpreg = 0;
  528. u32 packetlength = 1;
  529. u32 result = 0, yushu = 0;
  530. u32 sourceclock = 0;
  531. RCC_ClocksTypeDef RCC_Clocks;
  532. if(I2S_InitStruct->I2S_AudioFreq == I2S_AudioFreq_Default) {
  533. i2sdiv = 2;
  534. }
  535. else {
  536. if(I2S_InitStruct->I2S_DataFormat == I2S_DataFormat_16b) {
  537. packetlength = 1;
  538. }
  539. else {
  540. packetlength = 2;
  541. }
  542. RCC_GetClocksFreq(&RCC_Clocks);
  543. if((SPI2 == spi) || (SPI3 == spi)) {
  544. sourceclock = RCC_Clocks.PCLK1_Frequency;
  545. }
  546. else {
  547. sourceclock = RCC_Clocks.PCLK2_Frequency;
  548. }
  549. if(I2S_InitStruct->I2S_MCLKOutput == I2S_MCLKOutput_Enable) {
  550. result = (sourceclock) / (256 * (I2S_InitStruct->I2S_AudioFreq));
  551. yushu = (sourceclock) % (256 * (I2S_InitStruct->I2S_AudioFreq));
  552. if(yushu > (128 * (I2S_InitStruct->I2S_AudioFreq))) {
  553. result = result + 1;
  554. }
  555. i2sdiv = result;
  556. if ((i2sdiv < 2) || (i2sdiv > 0x1FF)) {
  557. i2sdiv = 2;
  558. }
  559. }
  560. else {
  561. result = (sourceclock) / (16 * 2 * packetlength * (I2S_InitStruct->I2S_AudioFreq));
  562. yushu = (sourceclock) % (16 * 2 * packetlength * (I2S_InitStruct->I2S_AudioFreq));
  563. if(yushu > ((16 * packetlength * (I2S_InitStruct->I2S_AudioFreq)))) {
  564. result = result + 1;
  565. }
  566. if ((i2sdiv < 1) || (i2sdiv > 0x1FF)) {
  567. i2sdiv = 1;
  568. }
  569. }
  570. }
  571. if(I2S_CPOL_High == I2S_InitStruct->I2S_CPOL) {
  572. spi->CCTL |= SPI_CCR_CPOL;
  573. }
  574. else {
  575. spi->CCTL &= ~SPI_CCR_CPOL;
  576. }
  577. spi->CFGR = 0x2 << I2S_CFGR_I2SDIV_Pos;
  578. if((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterTx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_MasterRx)) {
  579. spi->GCTL |= SPI_GCR_MODE;
  580. }
  581. else {
  582. spi->GCTL &= ~SPI_GCR_MODE;
  583. }
  584. if((I2S_InitStruct->I2S_Mode == I2S_Mode_MasterTx) || (I2S_InitStruct->I2S_Mode == I2S_Mode_SlaveTx)) {
  585. spi->GCTL |= SPI_GCR_TXEN;
  586. spi->GCTL &= ~SPI_GCR_RXEN;
  587. }
  588. else {
  589. spi->GCTL &= ~SPI_GCR_TXEN;
  590. spi->GCTL |= SPI_GCR_RXEN;
  591. }
  592. // tmpreg = spi->GCTL;
  593. // tmpreg &= ~(1 << 2);
  594. // tmpreg |= (u16)(I2S_InitStruct->I2S_Mode);
  595. // spi->GCTL = tmpreg;
  596. //
  597. tmpreg = 0;
  598. tmpreg |= (i2sdiv << I2S_CFGR_I2SDIV_Pos) | \
  599. (I2S_InitStruct->I2S_MCLKOutput) | \
  600. (I2S_CFGR_SPI_I2S) | \
  601. (I2S_InitStruct->I2S_Standard) | \
  602. (I2S_InitStruct->I2S_DataFormat);
  603. spi->CFGR &= ~I2S_CFGR_I2SDIV;
  604. spi->CFGR |= tmpreg;
  605. }
  606. /// @}
  607. /// @}
  608. /// @}