1
0

HAL_SPI.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. ******************************************************************************
  3. * @file HAL_Spi.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief SPI HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Serial Peripheral Interface (SPI) peripheral.
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. #define SPI_RX_TIMEOUT 2000
  16. #define SPI_TX_DMA_TIMEOUT 2000
  17. volatile uint32_t lu32_ReceiveTimeOut = SPI_RX_TIMEOUT;
  18. volatile uint32_t lu32_TX_DMA_TimeOut = SPI_TX_DMA_TIMEOUT;
  19. /************************************************************************
  20. * function : HAL_SPI_IRQHandler
  21. * Description: This function handles SPI interrupt request.
  22. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  23. * the configuration information for SPI module
  24. ************************************************************************/
  25. __weak void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
  26. {
  27. uint8_t lu8_TempValue = 0;
  28. uint32_t lu32_Length = 0;
  29. /*
  30. NOTE : This function should be modified by the user.
  31. */
  32. if ( (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY) && ((hspi->Instance->IE) & SPI_STATUS_RX_NOT_EMPTY) )
  33. {
  34. /* In master mode */
  35. if (hspi->Instance->CTL & SPI_CTL_MST_MODE)
  36. {
  37. while (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY)
  38. {
  39. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  40. if (hspi->Rx_Count >= hspi->Rx_Size)
  41. {
  42. /* Wait Transmit Done */
  43. while (!(hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE));
  44. /* Clear Batch Done Flag */
  45. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  46. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  47. /* Rx Disable */
  48. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  49. /* Receive End */
  50. hspi->Instance->CS &= (~SPI_CS_CS0);
  51. /* Disable Rx Not Empty Interrupt */
  52. CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
  53. if(hspi->Instance == SPI1)
  54. NVIC_ClearPendingIRQ(SPI1_IRQn);
  55. else if(hspi->Instance == SPI2)
  56. NVIC_ClearPendingIRQ(SPI2_IRQn);
  57. else if(hspi->Instance == SPI3)
  58. NVIC_ClearPendingIRQ(SPI3_IRQn);
  59. else if(hspi->Instance == SPI4)
  60. NVIC_ClearPendingIRQ(SPI4_IRQn);
  61. /* Clear Batch Done Flag */
  62. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  63. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  64. /* Set machine is DILE */
  65. hspi->RxState = SPI_RX_STATE_IDLE;
  66. }
  67. }
  68. }
  69. /* In Slave mode */
  70. else
  71. {
  72. while ((hspi->Rx_Count < hspi->Rx_Size) && (lu32_ReceiveTimeOut > 0) )
  73. {
  74. if (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY)
  75. {
  76. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  77. lu32_ReceiveTimeOut = SPI_RX_TIMEOUT; //If recieve data, Reset the timeout value
  78. }
  79. else
  80. {
  81. lu32_ReceiveTimeOut--;
  82. }
  83. }
  84. /* Rx Disable */
  85. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  86. /* Disable Rx Not Empty Interrupt */
  87. CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
  88. if(hspi->Instance == SPI1)
  89. NVIC_ClearPendingIRQ(SPI1_IRQn);
  90. else if(hspi->Instance == SPI2)
  91. NVIC_ClearPendingIRQ(SPI2_IRQn);
  92. else if(hspi->Instance == SPI3)
  93. NVIC_ClearPendingIRQ(SPI3_IRQn);
  94. else if(hspi->Instance == SPI4)
  95. NVIC_ClearPendingIRQ(SPI4_IRQn);
  96. /* Clear Batch Done Flag */
  97. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  98. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  99. /* Set machine is DILE */
  100. hspi->RxState = SPI_RX_STATE_IDLE;
  101. }
  102. }
  103. if ( (hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_HALF_EMPTY) && ((hspi->Instance->IE) & SPI_IE_TX_FIFO_HALF_EMPTY_EN) )
  104. {
  105. while (hspi->Tx_Count < hspi->Tx_Size)
  106. {
  107. if (!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL))
  108. {
  109. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  110. }
  111. else
  112. {
  113. break;
  114. }
  115. }
  116. /* Clear Tx FIFO half empty Flag */
  117. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_FIFO_HALF_EMPTY);
  118. if(hspi->Tx_Count == hspi->Tx_Size)
  119. {
  120. /* Disable Tx FIFO half empty Interrupt */
  121. CLEAR_BIT(hspi->Instance->IE, SPI_IE_TX_FIFO_HALF_EMPTY_EN);
  122. }
  123. }
  124. if ((hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE) && ((hspi->Instance->IE) & SPI_IE_TX_BATCH_DONE_EN) )
  125. {
  126. /* Clear Batch Done Flag */
  127. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  128. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  129. /* Disable TX Batch Done Interrupt */
  130. CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_TX_BATCH_DONE);
  131. /* Disable Tx FIFO half empty Interrupt */
  132. CLEAR_BIT(hspi->Instance->IE, SPI_IE_TX_FIFO_HALF_EMPTY_EN);
  133. if(hspi->Instance == SPI1)
  134. NVIC_ClearPendingIRQ(SPI1_IRQn);
  135. else if(hspi->Instance == SPI2)
  136. NVIC_ClearPendingIRQ(SPI2_IRQn);
  137. else if(hspi->Instance == SPI3)
  138. NVIC_ClearPendingIRQ(SPI3_IRQn);
  139. else if(hspi->Instance == SPI4)
  140. NVIC_ClearPendingIRQ(SPI4_IRQn);
  141. lu32_TX_DMA_TimeOut = SPI_TX_DMA_TIMEOUT;
  142. while (hspi->Instance->STATUS & SPI_STATUS_TX_BUSY)
  143. {
  144. lu32_TX_DMA_TimeOut--;
  145. if(0 == lu32_TX_DMA_TimeOut)
  146. {
  147. break;
  148. }
  149. }
  150. /* Tx Disable */
  151. hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
  152. hspi->Instance->TX_CTL &= (~SPI_TX_CTL_DMA_REQ_EN);
  153. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  154. {
  155. /* Transmit End */
  156. hspi->Instance->CS &= (~SPI_CS_CS0);
  157. }
  158. /* Tx Disable */
  159. hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
  160. hspi->TxState = SPI_TX_STATE_IDLE;
  161. }
  162. if ( (hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE) && ((hspi->Instance->IE) & SPI_STATUS_RX_BATCH_DONE) )
  163. {
  164. /* Clear Batch Done Flag */
  165. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  166. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  167. /* Disable RX Batch Done Interrupt */
  168. CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_BATCH_DONE);
  169. if(hspi->Instance == SPI1)
  170. NVIC_ClearPendingIRQ(SPI1_IRQn);
  171. else if(hspi->Instance == SPI2)
  172. NVIC_ClearPendingIRQ(SPI2_IRQn);
  173. else if(hspi->Instance == SPI3)
  174. NVIC_ClearPendingIRQ(SPI3_IRQn);
  175. else if(hspi->Instance == SPI4)
  176. NVIC_ClearPendingIRQ(SPI4_IRQn);
  177. /* Rx Disable */
  178. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_DMA_REQ_EN);
  179. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  180. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  181. {
  182. /* Receive End */
  183. hspi->Instance->CS &= (~SPI_CS_CS0);
  184. }
  185. hspi->RxState = SPI_RX_STATE_IDLE;
  186. }
  187. }
  188. /************************************************************************
  189. * function : HAL_SPI_MspInit
  190. * Description:
  191. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  192. * the configuration information for SPI module
  193. ************************************************************************/
  194. __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  195. {
  196. /*
  197. NOTE : This function should be modified by the user.
  198. */
  199. /* For Example */
  200. GPIO_InitTypeDef GPIO_Handle;
  201. /* SPI1 */
  202. if (hspi->Instance == SPI1)
  203. {
  204. }
  205. /* SPI2 */
  206. else if (hspi->Instance == SPI2)
  207. {
  208. }
  209. /* SPI3 */
  210. else if (hspi->Instance == SPI3)
  211. {
  212. /* Enable Clock */
  213. System_Module_Enable(EN_SPI3);
  214. /* SPI3 CS PortA Pin15 */
  215. /* SPI3 CLK PortC Pin10 */
  216. /* SPI3 MOSI PortC Pin12 */
  217. /* SPI3 MISO PortC Pin11 */
  218. GPIO_Handle.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  219. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  220. GPIO_Handle.Pull = GPIO_PULLUP;
  221. GPIO_Handle.Alternate = GPIO_FUNCTION_3;
  222. HAL_GPIO_Init(GPIOC, &GPIO_Handle);
  223. GPIO_Handle.Pin = GPIO_PIN_15;
  224. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  225. GPIO_Handle.Pull = GPIO_PULLUP;
  226. GPIO_Handle.Alternate = GPIO_FUNCTION_5;
  227. HAL_GPIO_Init(GPIOA, &GPIO_Handle);
  228. if (hspi->Init.X_Mode == SPI_4X_MODE)
  229. {
  230. /* SPI3 IO3 PortC Pin8 */
  231. /* SPI3 IO2 PortC Pin9 */
  232. GPIO_Handle.Pin = GPIO_PIN_8 | GPIO_PIN_9;
  233. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  234. GPIO_Handle.Pull = GPIO_PULLUP;
  235. GPIO_Handle.Alternate = GPIO_FUNCTION_3;
  236. HAL_GPIO_Init(GPIOC, &GPIO_Handle);
  237. }
  238. /* Clear Pending Interrupt */
  239. NVIC_ClearPendingIRQ(SPI3_IRQn);
  240. /* Enable External Interrupt */
  241. NVIC_EnableIRQ(SPI3_IRQn);
  242. }
  243. }
  244. /************************************************************************
  245. * function : HAL_SPI_MspDeInit
  246. * Description: SPI De-Initialize the SPI clock, GPIO, IRQ.
  247. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  248. * the configuration information for SPI module
  249. ************************************************************************/
  250. __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  251. {
  252. /*
  253. NOTE : This function should be modified by the user.
  254. */
  255. /* For Example */
  256. /* SPI1 */
  257. if (hspi->Instance == SPI1)
  258. {
  259. }
  260. /* SPI2 */
  261. else if (hspi->Instance == SPI2)
  262. {
  263. }
  264. /* SPI3 */
  265. else if (hspi->Instance == SPI3)
  266. {
  267. /* Disable Clock */
  268. System_Module_Disable(EN_SPI3);
  269. /* Reset the used GPIO to analog */
  270. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
  271. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15);
  272. if (hspi->Init.X_Mode == SPI_4X_MODE)
  273. {
  274. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8 | GPIO_PIN_9);
  275. }
  276. /* Clear Pending Interrupt */
  277. NVIC_ClearPendingIRQ(SPI3_IRQn);
  278. /* Disable External Interrupt */
  279. NVIC_DisableIRQ(SPI3_IRQn);
  280. }
  281. }
  282. /************************************************************************
  283. * function : HAL_SPI_Init
  284. * Description: SPI initial with parameters.
  285. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  286. * the configuration information for SPI module
  287. ************************************************************************/
  288. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  289. {
  290. /* Check SPI Parameter */
  291. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  292. if (!IS_SPI_ALL_MODE(hspi->Init.SPI_Mode)) return HAL_ERROR;
  293. if (!IS_SPI_WORK_MODE(hspi->Init.SPI_Work_Mode)) return HAL_ERROR;
  294. if (!IS_SPI_X_MODE(hspi->Init.X_Mode)) return HAL_ERROR;
  295. if (!IS_SPI_FIRST_BIT(hspi->Init.First_Bit)) return HAL_ERROR;
  296. if (!IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRate_Prescaler)) return HAL_ERROR;
  297. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  298. HAL_SPI_MspInit(hspi);
  299. /* Automatic change direction */
  300. hspi->Instance->CTL |= (SPI_CTL_IO_MODE);
  301. /* Set SPI Work mode */
  302. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  303. {
  304. SET_BIT(hspi->Instance->CTL, SPI_CTL_MST_MODE);
  305. }
  306. else
  307. {
  308. CLEAR_BIT(hspi->Instance->CTL, SPI_CTL_MST_MODE);
  309. hspi->Instance->BATCH = (hspi->Instance->BATCH & (~0x000FFFFFU)) | (1 << 0);
  310. hspi->Instance->TX_CTL |= SPI_TX_CTL_MODE | (0x88 << 8); // dummy data = 0x88
  311. if (hspi->Init.X_Mode != SPI_1X_MODE)
  312. {
  313. hspi->Instance->CTL |= SPI_CTL_SFILTER;
  314. }
  315. /* Slave Alternate Enable */
  316. hspi->Instance->CTL |= SPI_CTL_SLAVE_EN;
  317. /* Slave Mode Enable Rx By Default */
  318. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  319. }
  320. /* Set SPI First Bit */
  321. if (hspi->Init.First_Bit == SPI_FIRSTBIT_LSB)
  322. SET_BIT(hspi->Instance->CTL, SPI_CTL_LSB_FIRST);
  323. else
  324. CLEAR_BIT(hspi->Instance->CTL, SPI_CTL_LSB_FIRST);
  325. /* Set SPI Work Mode */
  326. hspi->Instance->CTL = ((hspi->Instance->CTL) & (~(SPI_CTL_CPHA | SPI_CTL_CPOL))) | (hspi->Init.SPI_Work_Mode);
  327. /* Set SPI X_Mode */
  328. hspi->Instance->CTL = ((hspi->Instance->CTL) & (~SPI_CTL_X_MODE)) | (hspi->Init.X_Mode);
  329. /* Set SPI BaudRate Prescaler */
  330. hspi->Instance->BAUD = ((hspi->Instance->BAUD) & (~0x0000FFFF)) | (hspi->Init.BaudRate_Prescaler);
  331. /* Disable All Interrupt */
  332. hspi->Instance->IE = 0x00000000;
  333. return HAL_OK;
  334. }
  335. /************************************************************************
  336. * function : HAL_SPI_DeInit
  337. * Description: De-Initialize the SPI peripheral.
  338. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  339. * the configuration information for SPI module
  340. * return : HAL_StatusTypeDef : HAL status
  341. ************************************************************************/
  342. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
  343. {
  344. /* Check the SPI handle allocation */
  345. if (hspi == NULL)
  346. {
  347. return HAL_ERROR;
  348. }
  349. /* Check SPI Instance parameter */
  350. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  351. hspi->RxState = SPI_RX_STATE_IDLE;
  352. hspi->TxState = SPI_TX_STATE_IDLE;
  353. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  354. HAL_SPI_MspDeInit(hspi);
  355. hspi->Rx_Size = 0;
  356. hspi->Tx_Size = 0;
  357. hspi->Rx_Count = 0;
  358. hspi->Tx_Count = 0;
  359. return HAL_OK;
  360. }
  361. /************************************************************************
  362. * function : HAL_SPI_Transmit
  363. * Description: Transmits an amount of data in blocking mode.
  364. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  365. * the configuration information for SPI module
  366. * pData : Pointer to data buffer
  367. * Size : Amount of data to be sent
  368. * Timeout : Transmit Timeout
  369. ************************************************************************/
  370. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  371. {
  372. uint32_t i;
  373. HAL_StatusTypeDef Status = HAL_OK;
  374. __IO uint32_t uiTimeout;
  375. /* Check SPI Parameter */
  376. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  377. if(!Size) return HAL_ERROR;
  378. if (pData == NULL) return HAL_ERROR;
  379. hspi->Tx_Count = 0;
  380. hspi->Tx_Size = Size;
  381. hspi->Tx_Buffer = pData;
  382. uiTimeout = Timeout;
  383. /* Clear Batch Done Flag */
  384. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  385. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  386. /* Clear TX FIFO */
  387. SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  388. CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  389. /* Set Data Size */
  390. hspi->Instance->BATCH = Size;
  391. /* Tx Enable */
  392. hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
  393. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  394. {
  395. /* Transmit Start */
  396. hspi->Instance->CS |= SPI_CS_CS0;
  397. }
  398. else
  399. {
  400. /* Rx Disable */
  401. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  402. }
  403. while(hspi->Tx_Size > 0)
  404. {
  405. /* Wait Tx FIFO Not Full */
  406. while (hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)
  407. {
  408. if(uiTimeout)
  409. {
  410. uiTimeout--;
  411. if (uiTimeout == 0)
  412. {
  413. Status = HAL_TIMEOUT;
  414. goto End;
  415. }
  416. }
  417. }
  418. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  419. hspi->Tx_Size--;
  420. uiTimeout = Timeout;
  421. }
  422. if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
  423. {
  424. /* Wait Transmit Done */
  425. while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BUSY));
  426. while (hspi->Instance->STATUS & SPI_STATUS_TX_BUSY)
  427. {
  428. if(uiTimeout)
  429. {
  430. uiTimeout--;
  431. if (uiTimeout == 0)
  432. {
  433. Status = HAL_TIMEOUT;
  434. goto End;
  435. }
  436. }
  437. }
  438. }
  439. else
  440. {
  441. /* Wait Transmit Done */
  442. while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE));
  443. Status = HAL_OK;
  444. }
  445. End:
  446. /* Clear Batch Done Flag */
  447. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  448. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  449. /* Tx Disable */
  450. hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
  451. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  452. {
  453. /* Transmit End */
  454. hspi->Instance->CS &= (~SPI_CS_CS0);
  455. }
  456. return Status;
  457. }
  458. /************************************************************************
  459. * function : HAL_SPI_Transmit_DMA
  460. * Description: Transmits an amount of data in blocking mode with DMA.
  461. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  462. * the configuration information for SPI module
  463. * pData : Pointer to data buffer
  464. * Size : Amount of data to be sent
  465. ************************************************************************/
  466. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
  467. {
  468. /* Check SPI Parameter */
  469. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  470. /* Rx machine is running */
  471. if (hspi->TxState != SPI_TX_STATE_IDLE)
  472. {
  473. return HAL_ERROR;
  474. }
  475. /* Set machine is Sending */
  476. hspi->TxState = SPI_TX_STATE_SENDING;
  477. /* Clear Batch Done Flag */
  478. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  479. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  480. /* Enable Tx Batch Done Interrupt */
  481. SET_BIT(hspi->Instance->IE, SPI_STATUS_TX_BATCH_DONE);
  482. /* Set Data Size */
  483. hspi->Instance->BATCH = Size;
  484. /* Tx FIFO */
  485. hspi->Instance->TX_CTL &= ~SPI_TX_CTL_DMA_LEVEL;
  486. hspi->Instance->TX_CTL |= SPI_TX_CTL_DMA_LEVEL_0;
  487. /* Tx Enable */
  488. hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
  489. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  490. {
  491. /* Transmit Start */
  492. hspi->Instance->CS |= SPI_CS_CS0;
  493. }
  494. else
  495. {
  496. /* Rx Disable */
  497. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  498. }
  499. HAL_DMA_Start(hspi->HDMA_Tx, (uint32_t)pData, (uint32_t)&hspi->Instance->DAT, Size);
  500. hspi->Instance->TX_CTL |= SPI_TX_CTL_DMA_REQ_EN;
  501. return HAL_OK;
  502. }
  503. /************************************************************************
  504. * function : HAL_SPI_Receive
  505. * Description: Receive an amount of data in blocking mode.
  506. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  507. * the configuration information for SPI module
  508. * pData : Pointer to data buffer
  509. * Size : Amount of data to be Receive
  510. * Timeout : Receive Timeout
  511. ************************************************************************/
  512. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  513. {
  514. uint32_t i;
  515. HAL_StatusTypeDef Status = HAL_OK;
  516. __IO uint32_t uiTimeout;
  517. /* Check SPI Parameter */
  518. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  519. if (pData == NULL) return HAL_ERROR;
  520. hspi->Rx_Count = 0;
  521. hspi->Rx_Size = Size;
  522. hspi->Rx_Buffer = pData;
  523. uiTimeout = Timeout;
  524. if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
  525. {
  526. hspi->Instance->BATCH = 1;
  527. /* Rx Enable */
  528. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  529. while ( hspi->Rx_Size > 0)
  530. {
  531. while (READ_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_FIFO_EMPTY))
  532. {
  533. if(uiTimeout)
  534. {
  535. uiTimeout--;
  536. if (uiTimeout == 0)
  537. {
  538. /* Rx Disable */
  539. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  540. return HAL_TIMEOUT;
  541. }
  542. }
  543. }
  544. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  545. hspi->Rx_Size--;
  546. uiTimeout = Timeout;
  547. }
  548. /* Rx Disable */
  549. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  550. return HAL_OK;
  551. }
  552. /* Clear Batch Done Flag */
  553. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  554. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  555. /* Set Data Size */
  556. hspi->Instance->BATCH = Size;
  557. /* Rx Enable */
  558. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  559. /* Receive Start */
  560. hspi->Instance->CS |= SPI_CS_CS0;
  561. while(hspi->Rx_Size > 0)
  562. {
  563. /* have no timeout */
  564. if (uiTimeout == 0)
  565. {
  566. /* Wait Rx FIFO Not Empty */
  567. while (hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY);
  568. }
  569. else
  570. {
  571. while (hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)
  572. {
  573. if (uiTimeout-- == 0)
  574. {
  575. Status = HAL_TIMEOUT;
  576. goto End;
  577. }
  578. }
  579. }
  580. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  581. hspi->Rx_Size--;
  582. }
  583. Status = HAL_OK;
  584. /* Wait Transmit Done */
  585. while (!(hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE));
  586. End:
  587. /* Clear Batch Done Flag */
  588. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  589. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  590. /* Rx Disable */
  591. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  592. /* Receive End */
  593. hspi->Instance->CS &= (~SPI_CS_CS0);
  594. return Status;
  595. }
  596. /************************************************************************
  597. * function : HAL_SPI_Receive_DMA
  598. * Description: Receive an amount of data in blocking mode with DMA.
  599. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  600. * the configuration information for SPI module
  601. * pData : Pointer to data buffer
  602. * Size : Amount of data to be Receive
  603. ************************************************************************/
  604. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
  605. {
  606. /* Check SPI Parameter */
  607. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  608. /* Rx machine is running */
  609. if (hspi->RxState != SPI_RX_STATE_IDLE)
  610. {
  611. return HAL_ERROR;
  612. }
  613. /* Set Slave machine is receiving */
  614. hspi->RxState = SPI_RX_STATE_RECEIVING;
  615. /* Clear Batch Done Flag */
  616. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  617. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  618. /* Enable Rx Batch Done Interrupt */
  619. SET_BIT(hspi->Instance->IE, SPI_STATUS_RX_BATCH_DONE);
  620. /* Set Data Size */
  621. hspi->Instance->BATCH = Size;
  622. /* Rx Enable */
  623. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  624. /* Rx FIFO */
  625. hspi->Instance->RX_CTL |= SPI_RX_CTL_DMA_LEVEL_0;
  626. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  627. {
  628. /* Receive Start */
  629. hspi->Instance->CS |= SPI_CS_CS0;
  630. }
  631. HAL_DMA_Start(hspi->HDMA_Rx, (uint32_t)&hspi->Instance->DAT, (uint32_t)pData, Size);
  632. hspi->Instance->RX_CTL |= SPI_RX_CTL_DMA_REQ_EN;
  633. return HAL_OK;
  634. }
  635. /************************************************************************
  636. * function : HAL_SPI_Wire_Config
  637. * Description: SPI Wire Config
  638. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  639. * the configuration information for SPI module
  640. * Mode : This parameter can be a value of @ref X_MODE
  641. ************************************************************************/
  642. HAL_StatusTypeDef HAL_SPI_Wire_Config(SPI_HandleTypeDef *hspi, uint32_t X_Mode)
  643. {
  644. /* Check SPI Parameter */
  645. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  646. /* Set SPI X_Mode */
  647. hspi->Instance->CTL = ((hspi->Instance->CTL) & (~SPI_CTL_X_MODE)) | X_Mode;
  648. return HAL_OK;
  649. }
  650. /************************************************************************
  651. * function : HAL_SPI_Transmit_IT
  652. * Description: Transmit an amount of data in blocking mode with interrupt.
  653. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  654. * the configuration information for SPI module
  655. * pData : Pointer to data buffer
  656. ************************************************************************/
  657. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
  658. {
  659. /* Check SPI Parameter */
  660. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  661. /* Tx machine is running */
  662. if (hspi->TxState != SPI_TX_STATE_IDLE)
  663. {
  664. return HAL_ERROR;
  665. }
  666. hspi->Tx_Size = Size;
  667. hspi->Tx_Buffer = pData;
  668. hspi->Tx_Count = 0;
  669. /* Set machine is Sending */
  670. hspi->TxState = SPI_TX_STATE_SENDING;
  671. /* Clear TX FIFO */
  672. SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  673. CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  674. /* Clear Batch Done Flag */
  675. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  676. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  677. /* Set Data Size */
  678. hspi->Instance->BATCH = Size;
  679. /* Tx Enable */
  680. hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
  681. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  682. {
  683. /* Transmit Start */
  684. hspi->Instance->CS |= SPI_CS_CS0;
  685. }
  686. else
  687. {
  688. /* Rx Disable */
  689. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  690. }
  691. while (hspi->Tx_Count < hspi->Tx_Size)
  692. {
  693. if (!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL))
  694. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  695. else
  696. break;
  697. }
  698. /* Clear Tx FIFO half empty Flag */
  699. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_FIFO_HALF_EMPTY);
  700. /* Enable Tx FIFO half empty Interrupt and Tx batch done Interrupt*/
  701. SET_BIT(hspi->Instance->IE, (SPI_IE_TX_FIFO_HALF_EMPTY_EN | SPI_IE_TX_BATCH_DONE_EN));
  702. return HAL_OK;
  703. }
  704. /************************************************************************
  705. * function : HAL_SPI_Receive_IT
  706. * Description: Receive an amount of data in blocking mode with interrupt.
  707. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  708. * the configuration information for SPI module
  709. * pData : Pointer to data buffer
  710. ************************************************************************/
  711. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
  712. {
  713. /* Check SPI Parameter */
  714. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  715. /* Rx machine is running */
  716. if (hspi->RxState != SPI_RX_STATE_IDLE)
  717. {
  718. return HAL_ERROR;
  719. }
  720. /* Set Slave machine is receiving */
  721. hspi->RxState = SPI_RX_STATE_RECEIVING;
  722. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  723. {
  724. /* Clear Batch Done Flag */
  725. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
  726. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  727. /* Set Data Size */
  728. hspi->Instance->BATCH = Size;
  729. /* Rx Enable */
  730. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  731. /* Receive Start */
  732. hspi->Instance->CS |= SPI_CS_CS0;
  733. }
  734. else
  735. {
  736. /* Reset BATCH register */
  737. hspi->Instance->BATCH = 1;
  738. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  739. }
  740. hspi->Rx_Size = Size;
  741. hspi->Rx_Buffer = pData;
  742. hspi->Rx_Count = 0;
  743. lu32_ReceiveTimeOut = SPI_RX_TIMEOUT;
  744. /* Enable Rx FIFO Not Empty Interrupt */
  745. SET_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
  746. return HAL_OK;
  747. }
  748. /************************************************************************
  749. * function : HAL_SPI_TransmitReceive
  750. * Description: Transmits and recieve an amount of data in blocking mode.
  751. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  752. * the configuration information for SPI module
  753. * pTxData : Pointer to transmit data buffer
  754. * pRxData : Pointer to recieve data buffer
  755. * Size : Amount of data to be sent
  756. * Timeout : TransmitReceive Timeout
  757. ************************************************************************/
  758. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint32_t Size, uint32_t Timeout)
  759. {
  760. uint32_t i;
  761. __IO uint32_t TxFlag = 1U, uiTimeout;
  762. HAL_StatusTypeDef Status = HAL_OK;
  763. /* Check SPI Parameter */
  764. if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
  765. if ((pTxData == NULL)||(pRxData == NULL)) return HAL_ERROR;
  766. hspi->Tx_Count = 0;
  767. hspi->Rx_Count = 0;
  768. hspi->Tx_Buffer = pTxData;
  769. hspi->Rx_Buffer = pRxData;
  770. hspi->Tx_Size = Size;
  771. hspi->Rx_Size = Size;
  772. uiTimeout = Timeout;
  773. /* Clear Batch Done Flag */
  774. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  775. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  776. /* Tx Enable */
  777. hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
  778. /* Rx Enable */
  779. hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
  780. /* Clear TX FIFO */
  781. SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  782. CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
  783. if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
  784. {
  785. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  786. hspi->Tx_Size--;
  787. TxFlag = 0;
  788. }
  789. else
  790. {
  791. /* Set Data Size */
  792. hspi->Instance->BATCH = hspi->Tx_Size;
  793. /* Transmit Start */
  794. hspi->Instance->CS |= SPI_CS_CS0;
  795. TxFlag = 1;
  796. }
  797. while((hspi->Tx_Size>0) || (hspi->Rx_Size>0))
  798. {
  799. if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
  800. {
  801. /* Wait Rx FIFO Not Empty */
  802. if((!(hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)) && (hspi->Rx_Size>0))
  803. {
  804. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  805. hspi->Rx_Size--;
  806. TxFlag = 1U;
  807. }
  808. /* Wait Tx FIFO Not Full */
  809. if((!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)) && (hspi->Tx_Size>0) && (TxFlag == 1U))
  810. {
  811. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  812. hspi->Tx_Size--;
  813. TxFlag = 0;
  814. }
  815. }
  816. else
  817. {
  818. /* Wait Tx FIFO Not Full */
  819. if((!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)) && (hspi->Tx_Size>0) && (TxFlag == 1U))
  820. {
  821. hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
  822. hspi->Tx_Size--;
  823. TxFlag = 0;
  824. }
  825. /* Wait Rx FIFO Not Empty */
  826. if((!(hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)) && (hspi->Rx_Size>0))
  827. {
  828. hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
  829. hspi->Rx_Size--;
  830. TxFlag = 1U;
  831. }
  832. }
  833. /* Wait Timeout */
  834. if(uiTimeout)
  835. {
  836. uiTimeout--;
  837. if(uiTimeout == 0)
  838. {
  839. Status = HAL_TIMEOUT;
  840. goto End;
  841. }
  842. }
  843. }
  844. /* Wait Transmit Done */
  845. while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE));
  846. Status = HAL_OK;
  847. End:
  848. /* Clear Batch Done Flag */
  849. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
  850. SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
  851. /* Tx Disable */
  852. hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
  853. /* Rx Disable */
  854. hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
  855. if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
  856. {
  857. /* Transmit End */
  858. hspi->Instance->CS &= (~SPI_CS_CS0);
  859. }
  860. return Status;
  861. }
  862. /************************************************************************
  863. * function : HAL_SPI_GetTxState
  864. * Description: Get Tx state.
  865. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  866. * the configuration information for SPI module
  867. ************************************************************************/
  868. uint8_t HAL_SPI_GetTxState(SPI_HandleTypeDef *hspi)
  869. {
  870. return hspi->TxState;
  871. }
  872. /************************************************************************
  873. * function : HAL_SPI_GetRxState
  874. * Description: Get Rx state.
  875. * input : hspi : pointer to a SPI_HandleTypeDef structure that contains
  876. * the configuration information for SPI module
  877. ************************************************************************/
  878. uint8_t HAL_SPI_GetRxState(SPI_HandleTypeDef *hspi)
  879. {
  880. return hspi->RxState;
  881. }