You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2770 lines
81 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_spi.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  7. * @brief SPI HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Serial Peripheral Interface (SPI) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The SPI HAL driver can be used as follows:
  21. (#) Declare a SPI_HandleTypeDef handle structure, for example:
  22. SPI_HandleTypeDef hspi;
  23. (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
  24. (##) Enable the SPIx interface clock
  25. (##) SPI pins configuration
  26. (+++) Enable the clock for the SPI GPIOs
  27. (+++) Configure these SPI pins as alternate function push-pull
  28. (##) NVIC configuration if you need to use interrupt process
  29. (+++) Configure the SPIx interrupt priority
  30. (+++) Enable the NVIC SPI IRQ handle
  31. (##) DMA Configuration if you need to use DMA process
  32. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  33. (+++) Enable the DMAx clock
  34. (+++) Configure the DMA handle parameters
  35. (+++) Configure the DMA Tx or Rx channel
  36. (+++) Associate the initialized hdma_tx handle to the hspi DMA Tx or Rx handle
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
  38. (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
  39. management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
  40. (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
  41. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  42. by calling the customized HAL_SPI_MspInit() API.
  43. [..]
  44. Circular mode restriction:
  45. (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
  46. (##) Master 2Lines RxOnly
  47. (##) Master 1Line Rx
  48. (#) The CRC feature is not managed when the DMA circular mode is enabled
  49. (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
  50. the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  56. *
  57. * Redistribution and use in source and binary forms, with or without modification,
  58. * are permitted provided that the following conditions are met:
  59. * 1. Redistributions of source code must retain the above copyright notice,
  60. * this list of conditions and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright notice,
  62. * this list of conditions and the following disclaimer in the documentation
  63. * and/or other materials provided with the distribution.
  64. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  69. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  76. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  77. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32l4xx_hal.h"
  83. /** @addtogroup STM32L4xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup SPI SPI
  87. * @brief SPI HAL module driver
  88. * @{
  89. */
  90. #ifdef HAL_SPI_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private defines -----------------------------------------------------------*/
  93. /** @defgroup SPI_Private_Constants SPI Private Constants
  94. * @{
  95. */
  96. #define SPI_DEFAULT_TIMEOUT 50
  97. /**
  98. * @}
  99. */
  100. /* Private macros ------------------------------------------------------------*/
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private function prototypes -----------------------------------------------*/
  103. /** @defgroup SPI_Private_Functions SPI Private Functions
  104. * @{
  105. */
  106. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  107. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  108. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  109. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
  110. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
  111. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  112. static void SPI_DMAError(DMA_HandleTypeDef *hdma);
  113. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout);
  114. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State, uint32_t Timeout);
  115. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  116. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  117. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  118. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  119. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  120. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  121. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  122. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  123. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  124. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  125. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  126. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  127. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
  128. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
  129. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
  130. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
  131. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
  132. /**
  133. * @}
  134. */
  135. /* Exported functions ---------------------------------------------------------*/
  136. /** @defgroup SPI_Exported_Functions SPI Exported Functions
  137. * @{
  138. */
  139. /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
  140. * @brief Initialization and Configuration functions
  141. *
  142. @verbatim
  143. ===============================================================================
  144. ##### Initialization and de-initialization functions #####
  145. ===============================================================================
  146. [..] This subsection provides a set of functions allowing to initialize and
  147. de-initialize the SPIx peripheral:
  148. (+) User must implement HAL_SPI_MspInit() function in which he configures
  149. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  150. (+) Call the function HAL_SPI_Init() to configure the selected device with
  151. the selected configuration:
  152. (++) Mode
  153. (++) Direction
  154. (++) Data Size
  155. (++) Clock Polarity and Phase
  156. (++) NSS Management
  157. (++) BaudRate Prescaler
  158. (++) FirstBit
  159. (++) TIMode
  160. (++) CRC Calculation
  161. (++) CRC Polynomial if CRC enabled
  162. (++) CRC Length, used only with Data8 and Data16
  163. (++) FIFO reception threshold
  164. (+) Call the function HAL_SPI_DeInit() to restore the default configuration
  165. of the selected SPIx peripheral.
  166. @endverbatim
  167. * @{
  168. */
  169. /**
  170. * @brief Initialize the SPI according to the specified parameters
  171. * in the SPI_InitTypeDef and initialize the associated handle.
  172. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  173. * the configuration information for SPI module.
  174. * @retval HAL status
  175. */
  176. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  177. {
  178. uint32_t frxth;
  179. /* Check the SPI handle allocation */
  180. if(hspi == NULL)
  181. {
  182. return HAL_ERROR;
  183. }
  184. /* Check the parameters */
  185. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  186. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  187. assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
  188. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  189. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  190. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  191. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  192. assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
  193. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  194. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  195. assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
  196. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  197. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  198. assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
  199. if(hspi->State == HAL_SPI_STATE_RESET)
  200. {
  201. /* Allocate lock resource and initialize it */
  202. hspi->Lock = HAL_UNLOCKED;
  203. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  204. HAL_SPI_MspInit(hspi);
  205. }
  206. hspi->State = HAL_SPI_STATE_BUSY;
  207. /* Disable the selected SPI peripheral */
  208. __HAL_SPI_DISABLE(hspi);
  209. /* Align by default the rs fifo threshold on the data size */
  210. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  211. {
  212. frxth = SPI_RXFIFO_THRESHOLD_HF;
  213. }
  214. else
  215. {
  216. frxth = SPI_RXFIFO_THRESHOLD_QF;
  217. }
  218. /* CRC calculation is valid only for 16Bit and 8 Bit */
  219. if(( hspi->Init.DataSize != SPI_DATASIZE_16BIT ) && ( hspi->Init.DataSize != SPI_DATASIZE_8BIT ))
  220. {
  221. /* CRC must be disabled */
  222. hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  223. }
  224. /* Align the CRC Length on the data size */
  225. if( hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
  226. {
  227. /* CRC Length aligned on the data size : value set by default */
  228. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  229. {
  230. hspi->Init.CRCLength = SPI_CRC_LENGTH_16BIT;
  231. }
  232. else
  233. {
  234. hspi->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  235. }
  236. }
  237. /*---------------------------- SPIx CR1 & CR2 Configuration ------------------------*/
  238. /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
  239. Communication speed, First bit, CRC calculation state, CRC Length */
  240. hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction |
  241. hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
  242. hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation);
  243. if( hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  244. {
  245. hspi->Instance->CR1|= SPI_CR1_CRCL;
  246. }
  247. /* Configure : NSS management */
  248. /* Configure : Rx Fifo Threshold */
  249. hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode | hspi->Init.NSSPMode |
  250. hspi->Init.DataSize ) | frxth;
  251. /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  252. /* Configure : CRC Polynomial */
  253. hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
  254. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  255. hspi->State= HAL_SPI_STATE_READY;
  256. return HAL_OK;
  257. }
  258. /**
  259. * @brief DeInitialize the SPI peripheral.
  260. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  261. * the configuration information for SPI module.
  262. * @retval HAL status
  263. */
  264. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
  265. {
  266. /* Check the SPI handle allocation */
  267. if(hspi == NULL)
  268. {
  269. return HAL_ERROR;
  270. }
  271. /* Check the parameters */
  272. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  273. hspi->State = HAL_SPI_STATE_BUSY;
  274. /* Disable the SPI Peripheral Clock */
  275. __HAL_SPI_DISABLE(hspi);
  276. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  277. HAL_SPI_MspDeInit(hspi);
  278. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  279. hspi->State = HAL_SPI_STATE_RESET;
  280. __HAL_UNLOCK(hspi);
  281. return HAL_OK;
  282. }
  283. /**
  284. * @brief Initialize the SPI MSP.
  285. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  286. * the configuration information for SPI module.
  287. * @retval None
  288. */
  289. __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  290. {
  291. /* Prevent unused argument(s) compilation warning */
  292. UNUSED(hspi);
  293. /* NOTE : This function should not be modified, when the callback is needed,
  294. the HAL_SPI_MspInit should be implemented in the user file
  295. */
  296. }
  297. /**
  298. * @brief DeInitialize the SPI MSP.
  299. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  300. * the configuration information for SPI module.
  301. * @retval None
  302. */
  303. __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  304. {
  305. /* Prevent unused argument(s) compilation warning */
  306. UNUSED(hspi);
  307. /* NOTE : This function should not be modified, when the callback is needed,
  308. the HAL_SPI_MspDeInit should be implemented in the user file
  309. */
  310. }
  311. /**
  312. * @}
  313. */
  314. /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
  315. * @brief Data transfers functions
  316. *
  317. @verbatim
  318. ==============================================================================
  319. ##### IO operation functions #####
  320. ===============================================================================
  321. [..]
  322. This subsection provides a set of functions allowing to manage the SPI
  323. data transfers.
  324. [..] The SPI supports master and slave mode :
  325. (#) There are two modes of transfer:
  326. (++) Blocking mode: The communication is performed in polling mode.
  327. The HAL status of all data processing is returned by the same function
  328. after finishing transfer.
  329. (++) No-Blocking mode: The communication is performed using Interrupts
  330. or DMA, These APIs return the HAL status.
  331. The end of the data processing will be indicated through the
  332. dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
  333. using DMA mode.
  334. The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
  335. will be executed respectively at the end of the transmit or Receive process
  336. The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
  337. (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
  338. exist for 1Line (simplex) and 2Lines (full duplex) modes.
  339. @endverbatim
  340. * @{
  341. */
  342. /**
  343. * @brief Transmit an amount of data in blocking mode.
  344. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  345. * the configuration information for SPI module.
  346. * @param pData: pointer to data buffer
  347. * @param Size: amount of data to be sent
  348. * @param Timeout: Timeout duration
  349. * @retval HAL status
  350. */
  351. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  352. {
  353. uint32_t tickstart = HAL_GetTick();
  354. HAL_StatusTypeDef errorcode = HAL_OK;
  355. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  356. /* Process Locked */
  357. __HAL_LOCK(hspi);
  358. if(hspi->State != HAL_SPI_STATE_READY)
  359. {
  360. errorcode = HAL_BUSY;
  361. goto error;
  362. }
  363. if((pData == NULL ) || (Size == 0))
  364. {
  365. errorcode = HAL_ERROR;
  366. goto error;
  367. }
  368. /* Set the transaction information */
  369. hspi->State = HAL_SPI_STATE_BUSY_TX;
  370. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  371. hspi->pTxBuffPtr = pData;
  372. hspi->TxXferSize = Size;
  373. hspi->TxXferCount = Size;
  374. hspi->pRxBuffPtr = (uint8_t *)NULL;
  375. hspi->RxXferSize = 0;
  376. hspi->RxXferCount = 0;
  377. /* Configure communication direction : 1Line */
  378. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  379. {
  380. SPI_1LINE_TX(hspi);
  381. }
  382. /* Reset CRC Calculation */
  383. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  384. {
  385. SPI_RESET_CRC(hspi);
  386. }
  387. /* Check if the SPI is already enabled */
  388. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  389. {
  390. /* Enable SPI peripheral */
  391. __HAL_SPI_ENABLE(hspi);
  392. }
  393. /* Transmit data in 16 Bit mode */
  394. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  395. {
  396. /* Transmit data in 16 Bit mode */
  397. while (hspi->TxXferCount > 0)
  398. {
  399. /* Wait until TXE flag is set to send data */
  400. if((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE)
  401. {
  402. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  403. hspi->pTxBuffPtr += sizeof(uint16_t);
  404. hspi->TxXferCount--;
  405. }
  406. else
  407. {
  408. /* Timeout management */
  409. if((Timeout == 0) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)))
  410. {
  411. errorcode = HAL_TIMEOUT;
  412. goto error;
  413. }
  414. }
  415. }
  416. }
  417. /* Transmit data in 8 Bit mode */
  418. else
  419. {
  420. while (hspi->TxXferCount > 0)
  421. {
  422. /* Wait until TXE flag is set to send data */
  423. if((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE)
  424. {
  425. if(hspi->TxXferCount > 1)
  426. {
  427. /* write on the data register in packing mode */
  428. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  429. hspi->pTxBuffPtr += sizeof(uint16_t);
  430. hspi->TxXferCount -= 2;
  431. }
  432. else
  433. {
  434. *((__IO uint8_t*)&hspi->Instance->DR) = (*hspi->pTxBuffPtr++);
  435. hspi->TxXferCount--;
  436. }
  437. }
  438. else
  439. {
  440. /* Timeout management */
  441. if((Timeout == 0) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)))
  442. {
  443. errorcode = HAL_TIMEOUT;
  444. goto error;
  445. }
  446. }
  447. }
  448. }
  449. /* Enable CRC Transmission */
  450. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  451. {
  452. hspi->Instance->CR1|= SPI_CR1_CRCNEXT;
  453. }
  454. /* Check the end of the transaction */
  455. if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
  456. {
  457. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  458. }
  459. /* Clear overrun flag in 2 Lines communication mode because received is not read */
  460. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  461. {
  462. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  463. }
  464. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  465. {
  466. errorcode = HAL_ERROR;
  467. }
  468. error:
  469. hspi->State = HAL_SPI_STATE_READY;
  470. /* Process Unlocked */
  471. __HAL_UNLOCK(hspi);
  472. return errorcode;
  473. }
  474. /**
  475. * @brief Receive an amount of data in blocking mode.
  476. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  477. * the configuration information for SPI module.
  478. * @param pData: pointer to data buffer
  479. * @param Size: amount of data to be received
  480. * @param Timeout: Timeout duration
  481. * @retval HAL status
  482. */
  483. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  484. {
  485. __IO uint16_t tmpreg;
  486. uint32_t tickstart = HAL_GetTick();
  487. HAL_StatusTypeDef errorcode = HAL_OK;
  488. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  489. {
  490. /* the receive process is not supported in 2Lines direction master mode */
  491. /* in this case we call the TransmitReceive process */
  492. /* Process Locked */
  493. return HAL_SPI_TransmitReceive(hspi,pData,pData,Size,Timeout);
  494. }
  495. /* Process Locked */
  496. __HAL_LOCK(hspi);
  497. if(hspi->State != HAL_SPI_STATE_READY)
  498. {
  499. errorcode = HAL_BUSY;
  500. goto error;
  501. }
  502. if((pData == NULL ) || (Size == 0))
  503. {
  504. errorcode = HAL_ERROR;
  505. goto error;
  506. }
  507. hspi->State = HAL_SPI_STATE_BUSY_RX;
  508. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  509. hspi->pRxBuffPtr = pData;
  510. hspi->RxXferSize = Size;
  511. hspi->RxXferCount = Size;
  512. hspi->pTxBuffPtr = (uint8_t *)NULL;
  513. hspi->TxXferSize = 0;
  514. hspi->TxXferCount = 0;
  515. /* Reset CRC Calculation */
  516. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  517. {
  518. SPI_RESET_CRC(hspi);
  519. /* this is done to handle the CRCNEXT before the latest data */
  520. hspi->RxXferCount--;
  521. }
  522. /* Set the Rx Fido threshold */
  523. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  524. {
  525. /* set fiforxthresold according the reception data length: 16bit */
  526. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  527. }
  528. else
  529. {
  530. /* set fiforxthresold according the reception data length: 8bit */
  531. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  532. }
  533. /* Configure communication direction 1Line and enabled SPI if needed */
  534. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  535. {
  536. SPI_1LINE_RX(hspi);
  537. }
  538. /* Check if the SPI is already enabled */
  539. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  540. {
  541. /* Enable SPI peripheral */
  542. __HAL_SPI_ENABLE(hspi);
  543. }
  544. if(hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
  545. {
  546. /* Transfer loop */
  547. while(hspi->RxXferCount > 0)
  548. {
  549. /* Check the RXNE flag */
  550. if((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE)
  551. {
  552. /* read the received data */
  553. (*hspi->pRxBuffPtr++)= *(__IO uint8_t *)&hspi->Instance->DR;
  554. hspi->RxXferCount--;
  555. }
  556. else
  557. {
  558. /* Timeout management */
  559. if((Timeout == 0) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)))
  560. {
  561. errorcode = HAL_TIMEOUT;
  562. goto error;
  563. }
  564. }
  565. }
  566. }
  567. else
  568. {
  569. /* Transfer loop */
  570. while(hspi->RxXferCount > 0)
  571. {
  572. /* Check the RXNE flag */
  573. if((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE)
  574. {
  575. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  576. hspi->pRxBuffPtr += sizeof(uint16_t);
  577. hspi->RxXferCount--;
  578. }
  579. else
  580. {
  581. /* Timeout management */
  582. if((Timeout == 0) || ((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout)))
  583. {
  584. errorcode = HAL_TIMEOUT;
  585. goto error;
  586. }
  587. }
  588. }
  589. }
  590. /* Handle the CRC Transmission */
  591. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  592. {
  593. /* freeze the CRC before the latest data */
  594. hspi->Instance->CR1|= SPI_CR1_CRCNEXT;
  595. /* Read the latest data */
  596. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  597. {
  598. /* the latest data has not been received */
  599. errorcode = HAL_TIMEOUT;
  600. goto error;
  601. }
  602. /* Receive last data in 16 Bit mode */
  603. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  604. {
  605. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  606. }
  607. /* Receive last data in 8 Bit mode */
  608. else
  609. {
  610. *hspi->pRxBuffPtr = *(__IO uint8_t *)&hspi->Instance->DR;
  611. }
  612. /* Wait until TXE flag */
  613. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  614. {
  615. /* Flag Error*/
  616. hspi->ErrorCode = HAL_SPI_ERROR_CRC;
  617. errorcode = HAL_TIMEOUT;
  618. goto error;
  619. }
  620. if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  621. {
  622. tmpreg = hspi->Instance->DR;
  623. UNUSED(tmpreg); /* To avoid GCC warning */
  624. }
  625. else
  626. {
  627. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  628. UNUSED(tmpreg); /* To avoid GCC warning */
  629. if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  630. {
  631. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  632. {
  633. /* Error on the CRC reception */
  634. hspi->ErrorCode = HAL_SPI_ERROR_CRC;
  635. errorcode = HAL_TIMEOUT;
  636. goto error;
  637. }
  638. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  639. UNUSED(tmpreg); /* To avoid GCC warning */
  640. }
  641. }
  642. }
  643. /* Check the end of the transaction */
  644. if(SPI_EndRxTransaction(hspi,Timeout) != HAL_OK)
  645. {
  646. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  647. }
  648. /* Check if CRC error occurred */
  649. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  650. {
  651. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  652. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  653. }
  654. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  655. {
  656. errorcode = HAL_ERROR;
  657. }
  658. error :
  659. hspi->State = HAL_SPI_STATE_READY;
  660. __HAL_UNLOCK(hspi);
  661. return errorcode;
  662. }
  663. /**
  664. * @brief Transmit and Receive an amount of data in blocking mode.
  665. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  666. * the configuration information for SPI module.
  667. * @param pTxData: pointer to transmission data buffer
  668. * @param pRxData: pointer to reception data buffer
  669. * @param Size: amount of data to be sent and received
  670. * @param Timeout: Timeout duration
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  674. {
  675. __IO uint16_t tmpreg;
  676. uint32_t tickstart = HAL_GetTick();
  677. HAL_StatusTypeDef errorcode = HAL_OK;
  678. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  679. /* Process Locked */
  680. __HAL_LOCK(hspi);
  681. if(hspi->State != HAL_SPI_STATE_READY)
  682. {
  683. errorcode = HAL_BUSY;
  684. goto error;
  685. }
  686. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  687. {
  688. errorcode = HAL_ERROR;
  689. goto error;
  690. }
  691. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  692. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  693. hspi->pRxBuffPtr = pRxData;
  694. hspi->RxXferCount = Size;
  695. hspi->RxXferSize = Size;
  696. hspi->pTxBuffPtr = pTxData;
  697. hspi->TxXferCount = Size;
  698. hspi->TxXferSize = Size;
  699. /* Reset CRC Calculation */
  700. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  701. {
  702. SPI_RESET_CRC(hspi);
  703. }
  704. /* Set the Rx Fido threshold */
  705. if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount > 1))
  706. {
  707. /* set fiforxthreshold according the reception data length: 16bit */
  708. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  709. }
  710. else
  711. {
  712. /* set fiforxthreshold according the reception data length: 8bit */
  713. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  714. }
  715. /* Check if the SPI is already enabled */
  716. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  717. {
  718. /* Enable SPI peripheral */
  719. __HAL_SPI_ENABLE(hspi);
  720. }
  721. /* Transmit and Receive data in 16 Bit mode */
  722. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  723. {
  724. while ((hspi->TxXferCount > 0 ) || (hspi->RxXferCount > 0))
  725. {
  726. /* Check TXE flag */
  727. if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
  728. {
  729. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  730. hspi->pTxBuffPtr += sizeof(uint16_t);
  731. hspi->TxXferCount--;
  732. /* Enable CRC Transmission */
  733. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  734. {
  735. hspi->Instance->CR1|= SPI_CR1_CRCNEXT;
  736. }
  737. }
  738. /* Check RXNE flag */
  739. if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
  740. {
  741. *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
  742. hspi->pRxBuffPtr += sizeof(uint16_t);
  743. hspi->RxXferCount--;
  744. }
  745. if((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))
  746. {
  747. errorcode = HAL_TIMEOUT;
  748. goto error;
  749. }
  750. }
  751. }
  752. /* Transmit and Receive data in 8 Bit mode */
  753. else
  754. {
  755. while((hspi->TxXferCount > 0) || (hspi->RxXferCount > 0))
  756. {
  757. /* check TXE flag */
  758. if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
  759. {
  760. if(hspi->TxXferCount > 1)
  761. {
  762. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  763. hspi->pTxBuffPtr += sizeof(uint16_t);
  764. hspi->TxXferCount -= 2;
  765. }
  766. else
  767. {
  768. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  769. hspi->TxXferCount--;
  770. }
  771. /* Enable CRC Transmission */
  772. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  773. {
  774. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  775. }
  776. }
  777. /* Wait until RXNE flag is reset */
  778. if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
  779. {
  780. if(hspi->RxXferCount > 1)
  781. {
  782. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  783. hspi->pRxBuffPtr += sizeof(uint16_t);
  784. hspi->RxXferCount -= 2;
  785. if(hspi->RxXferCount <= 1)
  786. {
  787. /* set fiforxthresold before to switch on 8 bit data size */
  788. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  789. }
  790. }
  791. else
  792. {
  793. (*hspi->pRxBuffPtr++) = *(__IO uint8_t *)&hspi->Instance->DR;
  794. hspi->RxXferCount--;
  795. }
  796. }
  797. if((Timeout != HAL_MAX_DELAY) && ((HAL_GetTick()-tickstart) >= Timeout))
  798. {
  799. errorcode = HAL_TIMEOUT;
  800. goto error;
  801. }
  802. }
  803. }
  804. /* Read CRC from DR to close CRC calculation process */
  805. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  806. {
  807. /* Wait until TXE flag */
  808. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  809. {
  810. /* Error on the CRC reception */
  811. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  812. errorcode = HAL_TIMEOUT;
  813. goto error;
  814. }
  815. if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  816. {
  817. tmpreg = hspi->Instance->DR;
  818. UNUSED(tmpreg); /* To avoid GCC warning */
  819. }
  820. else
  821. {
  822. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  823. UNUSED(tmpreg); /* To avoid GCC warning */
  824. if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  825. {
  826. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  827. {
  828. /* Error on the CRC reception */
  829. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  830. errorcode = HAL_TIMEOUT;
  831. goto error;
  832. }
  833. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  834. UNUSED(tmpreg); /* To avoid GCC warning */
  835. }
  836. }
  837. }
  838. /* Check if CRC error occurred */
  839. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  840. {
  841. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  842. /* Clear CRC Flag */
  843. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  844. errorcode = HAL_ERROR;
  845. }
  846. /* Check the end of the transaction */
  847. if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
  848. {
  849. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  850. }
  851. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  852. {
  853. errorcode = HAL_ERROR;
  854. }
  855. error :
  856. hspi->State = HAL_SPI_STATE_READY;
  857. __HAL_UNLOCK(hspi);
  858. return errorcode;
  859. }
  860. /**
  861. * @brief Transmit an amount of data in non-blocking mode with Interrupt.
  862. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  863. * the configuration information for SPI module.
  864. * @param pData: pointer to data buffer
  865. * @param Size: amount of data to be sent
  866. * @retval HAL status
  867. */
  868. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  869. {
  870. HAL_StatusTypeDef errorcode = HAL_OK;
  871. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  872. /* Process Locked */
  873. __HAL_LOCK(hspi);
  874. if((pData == NULL) || (Size == 0))
  875. {
  876. errorcode = HAL_ERROR;
  877. goto error;
  878. }
  879. if(hspi->State != HAL_SPI_STATE_READY)
  880. {
  881. errorcode = HAL_BUSY;
  882. goto error;
  883. }
  884. /* prepare the transfer */
  885. hspi->State = HAL_SPI_STATE_BUSY_TX;
  886. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  887. hspi->pTxBuffPtr = pData;
  888. hspi->TxXferSize = Size;
  889. hspi->TxXferCount = Size;
  890. hspi->pRxBuffPtr = (uint8_t *)NULL;
  891. hspi->RxXferSize = 0;
  892. hspi->RxXferCount = 0;
  893. hspi->RxISR = NULL;
  894. /* Set the function for IT treatment */
  895. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  896. {
  897. hspi->TxISR = SPI_TxISR_16BIT;
  898. }
  899. else
  900. {
  901. hspi->TxISR = SPI_TxISR_8BIT;
  902. }
  903. /* Configure communication direction : 1Line */
  904. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  905. {
  906. SPI_1LINE_TX(hspi);
  907. }
  908. /* Reset CRC Calculation */
  909. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  910. {
  911. SPI_RESET_CRC(hspi);
  912. }
  913. /* Enable TXE and ERR interrupt */
  914. __HAL_SPI_ENABLE_IT(hspi,(SPI_IT_TXE));
  915. /* Check if the SPI is already enabled */
  916. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  917. {
  918. /* Enable SPI peripheral */
  919. __HAL_SPI_ENABLE(hspi);
  920. }
  921. error :
  922. __HAL_UNLOCK(hspi);
  923. return errorcode;
  924. }
  925. /**
  926. * @brief Receive an amount of data in non-blocking mode with Interrupt.
  927. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  928. * the configuration information for SPI module.
  929. * @param pData: pointer to data buffer
  930. * @param Size: amount of data to be sent
  931. * @retval HAL status
  932. */
  933. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  934. {
  935. HAL_StatusTypeDef errorcode = HAL_OK;
  936. /* Process Locked */
  937. __HAL_LOCK(hspi);
  938. if(hspi->State != HAL_SPI_STATE_READY)
  939. {
  940. errorcode = HAL_BUSY;
  941. goto error;
  942. }
  943. if((pData == NULL) || (Size == 0))
  944. {
  945. errorcode = HAL_ERROR;
  946. goto error;
  947. }
  948. /* Configure communication */
  949. hspi->State = HAL_SPI_STATE_BUSY_RX;
  950. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  951. hspi->pRxBuffPtr = pData;
  952. hspi->RxXferSize = Size;
  953. hspi->RxXferCount = Size;
  954. hspi->pTxBuffPtr = (uint8_t *)NULL;
  955. hspi->TxXferSize = 0;
  956. hspi->TxXferCount = 0;
  957. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  958. {
  959. /* Process Unlocked */
  960. __HAL_UNLOCK(hspi);
  961. /* the receive process is not supported in 2Lines direction master mode */
  962. /* in this we call the TransmitReceive process */
  963. return HAL_SPI_TransmitReceive_IT(hspi,pData,pData,Size);
  964. }
  965. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  966. {
  967. hspi->CRCSize = 1;
  968. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  969. {
  970. hspi->CRCSize = 2;
  971. }
  972. }
  973. else
  974. {
  975. hspi->CRCSize = 0;
  976. }
  977. hspi->TxISR = NULL;
  978. /* check the data size to adapt Rx threshold and the set the function for IT treatment */
  979. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  980. {
  981. /* set fiforxthresold according the reception data length: 16 bit */
  982. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  983. hspi->RxISR = SPI_RxISR_16BIT;
  984. }
  985. else
  986. {
  987. /* set fiforxthresold according the reception data length: 8 bit */
  988. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  989. hspi->RxISR = SPI_RxISR_8BIT;
  990. }
  991. /* Configure communication direction : 1Line */
  992. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  993. {
  994. SPI_1LINE_RX(hspi);
  995. }
  996. /* Reset CRC Calculation */
  997. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  998. {
  999. SPI_RESET_CRC(hspi);
  1000. }
  1001. /* Enable TXE and ERR interrupt */
  1002. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  1003. /* Check if the SPI is already enabled */
  1004. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1005. {
  1006. /* Enable SPI peripheral */
  1007. __HAL_SPI_ENABLE(hspi);
  1008. }
  1009. error :
  1010. /* Process Unlocked */
  1011. __HAL_UNLOCK(hspi);
  1012. return errorcode;
  1013. }
  1014. /**
  1015. * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt.
  1016. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1017. * the configuration information for SPI module.
  1018. * @param pTxData: pointer to transmission data buffer
  1019. * @param pRxData: pointer to reception data buffer
  1020. * @param Size: amount of data to be sent and received
  1021. * @retval HAL status
  1022. */
  1023. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1024. {
  1025. HAL_StatusTypeDef errorcode = HAL_OK;
  1026. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1027. /* Process locked */
  1028. __HAL_LOCK(hspi);
  1029. if(!((hspi->State == HAL_SPI_STATE_READY) || \
  1030. ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX))))
  1031. {
  1032. errorcode = HAL_BUSY;
  1033. goto error;
  1034. }
  1035. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  1036. {
  1037. errorcode = HAL_ERROR;
  1038. goto error;
  1039. }
  1040. hspi->CRCSize = 0;
  1041. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1042. {
  1043. hspi->CRCSize = 1;
  1044. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  1045. {
  1046. hspi->CRCSize = 2;
  1047. }
  1048. }
  1049. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  1050. {
  1051. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1052. }
  1053. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1054. hspi->pTxBuffPtr = pTxData;
  1055. hspi->TxXferSize = Size;
  1056. hspi->TxXferCount = Size;
  1057. hspi->pRxBuffPtr = pRxData;
  1058. hspi->RxXferSize = Size;
  1059. hspi->RxXferCount = Size;
  1060. /* Set the function for IT treatment */
  1061. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  1062. {
  1063. hspi->RxISR = SPI_2linesRxISR_16BIT;
  1064. hspi->TxISR = SPI_2linesTxISR_16BIT;
  1065. }
  1066. else
  1067. {
  1068. hspi->RxISR = SPI_2linesRxISR_8BIT;
  1069. hspi->TxISR = SPI_2linesTxISR_8BIT;
  1070. }
  1071. /* Reset CRC Calculation */
  1072. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1073. {
  1074. SPI_RESET_CRC(hspi);
  1075. }
  1076. /* check if packing mode is enabled and if there is more than 2 data to receive */
  1077. if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount >= 2))
  1078. {
  1079. /* set fiforxthresold according the reception data length: 16 bit */
  1080. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1081. }
  1082. else
  1083. {
  1084. /* set fiforxthresold according the reception data length: 8 bit */
  1085. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1086. }
  1087. /* Enable TXE, RXNE and ERR interrupt */
  1088. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  1089. /* Check if the SPI is already enabled */
  1090. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1091. {
  1092. /* Enable SPI peripheral */
  1093. __HAL_SPI_ENABLE(hspi);
  1094. }
  1095. error :
  1096. /* Process Unlocked */
  1097. __HAL_UNLOCK(hspi);
  1098. return errorcode;
  1099. }
  1100. /**
  1101. * @brief Transmit an amount of data in non-blocking mode with DMA.
  1102. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1103. * the configuration information for SPI module.
  1104. * @param pData: pointer to data buffer
  1105. * @param Size: amount of data to be sent
  1106. * @retval HAL status
  1107. */
  1108. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1109. {
  1110. HAL_StatusTypeDef errorcode = HAL_OK;
  1111. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  1112. /* Process Locked */
  1113. __HAL_LOCK(hspi);
  1114. if(hspi->State != HAL_SPI_STATE_READY)
  1115. {
  1116. errorcode = HAL_BUSY;
  1117. goto error;
  1118. }
  1119. if((pData == NULL) || (Size == 0))
  1120. {
  1121. errorcode = HAL_ERROR;
  1122. goto error;
  1123. }
  1124. hspi->State = HAL_SPI_STATE_BUSY_TX;
  1125. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1126. hspi->pTxBuffPtr = pData;
  1127. hspi->TxXferSize = Size;
  1128. hspi->TxXferCount = Size;
  1129. hspi->pRxBuffPtr = (uint8_t *)NULL;
  1130. hspi->RxXferSize = 0;
  1131. hspi->RxXferCount = 0;
  1132. /* Configure communication direction : 1Line */
  1133. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1134. {
  1135. SPI_1LINE_TX(hspi);
  1136. }
  1137. /* Reset CRC Calculation */
  1138. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1139. {
  1140. SPI_RESET_CRC(hspi);
  1141. }
  1142. /* Set the SPI TxDMA Half transfer complete callback */
  1143. hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
  1144. /* Set the SPI TxDMA transfer complete callback */
  1145. hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
  1146. /* Set the DMA error callback */
  1147. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1148. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1149. /* packing mode is enabled only if the DMA setting is HALWORD */
  1150. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1151. {
  1152. /* Check the even/odd of the data size + crc if enabled */
  1153. if((hspi->TxXferCount & 0x1) == 0)
  1154. {
  1155. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1156. hspi->TxXferCount = (hspi->TxXferCount >> 1);
  1157. }
  1158. else
  1159. {
  1160. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1161. hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
  1162. }
  1163. }
  1164. /* Enable the Tx DMA channel */
  1165. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1166. /* Check if the SPI is already enabled */
  1167. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1168. {
  1169. /* Enable SPI peripheral */
  1170. __HAL_SPI_ENABLE(hspi);
  1171. }
  1172. /* Enable Tx DMA Request */
  1173. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1174. error :
  1175. /* Process Unlocked */
  1176. __HAL_UNLOCK(hspi);
  1177. return errorcode;
  1178. }
  1179. /**
  1180. * @brief Receive an amount of data in non-blocking mode with DMA.
  1181. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1182. * the configuration information for SPI module.
  1183. * @param pData: pointer to data buffer
  1184. * @note When the CRC feature is enabled the pData Length must be Size + 1.
  1185. * @param Size: amount of data to be sent
  1186. * @retval HAL status
  1187. */
  1188. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1189. {
  1190. HAL_StatusTypeDef errorcode = HAL_OK;
  1191. /* Process Locked */
  1192. __HAL_LOCK(hspi);
  1193. if(hspi->State != HAL_SPI_STATE_READY)
  1194. {
  1195. errorcode = HAL_BUSY;
  1196. goto error;
  1197. }
  1198. if((pData == NULL) || (Size == 0))
  1199. {
  1200. errorcode = HAL_ERROR;
  1201. goto error;
  1202. }
  1203. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1204. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1205. hspi->pRxBuffPtr = pData;
  1206. hspi->RxXferSize = Size;
  1207. hspi->RxXferCount = Size;
  1208. hspi->pTxBuffPtr = (uint8_t *)NULL;
  1209. hspi->TxXferSize = 0;
  1210. hspi->TxXferCount = 0;
  1211. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  1212. {
  1213. /* Process Unlocked */
  1214. __HAL_UNLOCK(hspi);
  1215. /* the receive process is not supported in 2Lines direction master mode */
  1216. /* in this case we call the TransmitReceive process */
  1217. return HAL_SPI_TransmitReceive_DMA(hspi,pData,pData,Size);
  1218. }
  1219. /* Configure communication direction : 1Line */
  1220. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1221. {
  1222. SPI_1LINE_RX(hspi);
  1223. }
  1224. /* Reset CRC Calculation */
  1225. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1226. {
  1227. SPI_RESET_CRC(hspi);
  1228. }
  1229. /* packing mode management is enabled by the DMA settings */
  1230. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1231. {
  1232. /* Restriction the DMA data received is not allowed in this mode */
  1233. errorcode = HAL_ERROR;
  1234. goto error;
  1235. }
  1236. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1237. if( hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1238. {
  1239. /* set fiforxthresold according the reception data length: 16bit */
  1240. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1241. }
  1242. else
  1243. {
  1244. /* set fiforxthresold according the reception data length: 8bit */
  1245. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1246. }
  1247. /* Set the SPI RxDMA Half transfer complete callback */
  1248. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1249. /* Set the SPI Rx DMA transfer complete callback */
  1250. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1251. /* Set the DMA error callback */
  1252. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1253. /* Enable Rx DMA Request */
  1254. SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  1255. /* Enable the Rx DMA channel */
  1256. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
  1257. /* Check if the SPI is already enabled */
  1258. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1259. {
  1260. /* Enable SPI peripheral */
  1261. __HAL_SPI_ENABLE(hspi);
  1262. }
  1263. error:
  1264. /* Process Unlocked */
  1265. __HAL_UNLOCK(hspi);
  1266. return errorcode;
  1267. }
  1268. /**
  1269. * @brief Transmit and Receive an amount of data in non-blocking mode with DMA.
  1270. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1271. * the configuration information for SPI module.
  1272. * @param pTxData: pointer to transmission data buffer
  1273. * @param pRxData: pointer to reception data buffer
  1274. * @note When the CRC feature is enabled the pRxData Length must be Size + 1
  1275. * @param Size: amount of data to be sent
  1276. * @retval HAL status
  1277. */
  1278. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1279. {
  1280. HAL_StatusTypeDef errorcode = HAL_OK;
  1281. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1282. /* Process locked */
  1283. __HAL_LOCK(hspi);
  1284. if(!((hspi->State == HAL_SPI_STATE_READY) ||
  1285. ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX))))
  1286. {
  1287. errorcode = HAL_BUSY;
  1288. goto error;
  1289. }
  1290. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  1291. {
  1292. errorcode = HAL_ERROR;
  1293. goto error;
  1294. }
  1295. /* check if the transmit Receive function is not called by a receive master */
  1296. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  1297. {
  1298. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1299. }
  1300. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1301. hspi->pTxBuffPtr = (uint8_t *)pTxData;
  1302. hspi->TxXferSize = Size;
  1303. hspi->TxXferCount = Size;
  1304. hspi->pRxBuffPtr = (uint8_t *)pRxData;
  1305. hspi->RxXferSize = Size;
  1306. hspi->RxXferCount = Size;
  1307. /* Reset CRC Calculation + increase the rxsize */
  1308. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1309. {
  1310. SPI_RESET_CRC(hspi);
  1311. }
  1312. /* Reset the threshold bit */
  1313. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX | SPI_CR2_LDMARX);
  1314. /* the packing mode management is enabled by the DMA settings according the spi data size */
  1315. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1316. {
  1317. /* set fiforxthreshold according the reception data length: 16bit */
  1318. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1319. }
  1320. else
  1321. {
  1322. /* set fiforxthresold according the reception data length: 8bit */
  1323. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1324. if(hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1325. {
  1326. if((hspi->TxXferSize & 0x1) == 0x0)
  1327. {
  1328. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1329. hspi->TxXferCount = hspi->TxXferCount >> 1;
  1330. }
  1331. else
  1332. {
  1333. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1334. hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
  1335. }
  1336. }
  1337. if(hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1338. {
  1339. /* set fiforxthresold according the reception data length: 16bit */
  1340. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1341. if((hspi->RxXferCount & 0x1) == 0x0 )
  1342. {
  1343. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1344. hspi->RxXferCount = hspi->RxXferCount >> 1;
  1345. }
  1346. else
  1347. {
  1348. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1349. hspi->RxXferCount = (hspi->RxXferCount >> 1) + 1;
  1350. }
  1351. }
  1352. }
  1353. /* Set the SPI Rx DMA transfer complete callback if the transfer request is a
  1354. reception request (RXNE) */
  1355. if(hspi->State == HAL_SPI_STATE_BUSY_RX)
  1356. {
  1357. /* Set the SPI Rx DMA Half transfer complete callback */
  1358. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1359. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1360. }
  1361. else
  1362. {
  1363. /* Set the SPI Rx DMA Half transfer complete callback */
  1364. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
  1365. hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
  1366. }
  1367. /* Set the DMA error callback */
  1368. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1369. /* Enable Rx DMA Request */
  1370. SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  1371. /* Enable the Rx DMA channel */
  1372. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t) hspi->pRxBuffPtr, hspi->RxXferCount);
  1373. /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
  1374. is performed in DMA reception complete callback */
  1375. hspi->hdmatx->XferHalfCpltCallback = NULL;
  1376. hspi->hdmatx->XferCpltCallback = NULL;
  1377. /* Set the DMA error callback */
  1378. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1379. /* Enable the Tx DMA channel */
  1380. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1381. /* Check if the SPI is already enabled */
  1382. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1383. {
  1384. /* Enable SPI peripheral */
  1385. __HAL_SPI_ENABLE(hspi);
  1386. }
  1387. /* Enable Tx DMA Request */
  1388. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1389. error :
  1390. /* Process Unlocked */
  1391. __HAL_UNLOCK(hspi);
  1392. return errorcode;
  1393. }
  1394. /**
  1395. * @brief Pause the DMA Transfer.
  1396. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1397. * the configuration information for the specified SPI module.
  1398. * @retval HAL status
  1399. */
  1400. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
  1401. {
  1402. /* Process Locked */
  1403. __HAL_LOCK(hspi);
  1404. /* Disable the SPI DMA Tx & Rx requests */
  1405. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1406. /* Process Unlocked */
  1407. __HAL_UNLOCK(hspi);
  1408. return HAL_OK;
  1409. }
  1410. /**
  1411. * @brief Resume the DMA Transfer.
  1412. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1413. * the configuration information for the specified SPI module.
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
  1417. {
  1418. /* Process Locked */
  1419. __HAL_LOCK(hspi);
  1420. /* Enable the SPI DMA Tx & Rx requests */
  1421. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1422. /* Process Unlocked */
  1423. __HAL_UNLOCK(hspi);
  1424. return HAL_OK;
  1425. }
  1426. /**
  1427. * @brief Stop the DMA Transfer.
  1428. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1429. * the configuration information for the specified SPI module.
  1430. * @retval HAL status
  1431. */
  1432. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
  1433. {
  1434. /* The Lock is not implemented on this API to allow the user application
  1435. to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
  1436. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  1437. and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
  1438. */
  1439. /* Abort the SPI DMA tx channel */
  1440. if(hspi->hdmatx != NULL)
  1441. {
  1442. HAL_DMA_Abort(hspi->hdmatx);
  1443. }
  1444. /* Abort the SPI DMA rx channel */
  1445. if(hspi->hdmarx != NULL)
  1446. {
  1447. HAL_DMA_Abort(hspi->hdmarx);
  1448. }
  1449. /* Disable the SPI DMA Tx & Rx requests */
  1450. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1451. hspi->State = HAL_SPI_STATE_READY;
  1452. return HAL_OK;
  1453. }
  1454. /**
  1455. * @brief Handle SPI interrupt request.
  1456. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1457. * the configuration information for the specified SPI module.
  1458. * @retval None
  1459. */
  1460. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
  1461. {
  1462. uint32_t itsource = hspi->Instance->CR2;
  1463. uint32_t itflag = hspi->Instance->SR;
  1464. /* SPI in mode Receiver ----------------------------------------------------*/
  1465. if(((itflag & SPI_FLAG_OVR) == RESET) &&
  1466. ((itflag & SPI_FLAG_RXNE) != RESET) && ((itsource & SPI_IT_RXNE) != RESET))
  1467. {
  1468. hspi->RxISR(hspi);
  1469. return;
  1470. }
  1471. /* SPI in mode Transmitter ---------------------------------------------------*/
  1472. if(((itflag & SPI_FLAG_TXE) != RESET) && ((itsource & SPI_IT_TXE) != RESET))
  1473. {
  1474. hspi->TxISR(hspi);
  1475. return;
  1476. }
  1477. /* SPI in Error Treatment ---------------------------------------------------*/
  1478. if((itflag & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE)) != RESET)
  1479. {
  1480. /* SPI Overrun error interrupt occurred -------------------------------------*/
  1481. if((itflag & SPI_FLAG_OVR) != RESET)
  1482. {
  1483. if(hspi->State != HAL_SPI_STATE_BUSY_TX)
  1484. {
  1485. hspi->ErrorCode |= HAL_SPI_ERROR_OVR;
  1486. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1487. }
  1488. else
  1489. {
  1490. return;
  1491. }
  1492. }
  1493. /* SPI Mode Fault error interrupt occurred -------------------------------------*/
  1494. if((itflag & SPI_FLAG_MODF) != RESET)
  1495. {
  1496. hspi->ErrorCode |= HAL_SPI_ERROR_MODF;
  1497. __HAL_SPI_CLEAR_MODFFLAG(hspi);
  1498. }
  1499. /* SPI Frame error interrupt occurred ----------------------------------------*/
  1500. if((itflag & SPI_FLAG_FRE) != RESET)
  1501. {
  1502. hspi->ErrorCode |= HAL_SPI_ERROR_FRE;
  1503. __HAL_SPI_CLEAR_FREFLAG(hspi);
  1504. }
  1505. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
  1506. hspi->State = HAL_SPI_STATE_READY;
  1507. HAL_SPI_ErrorCallback(hspi);
  1508. return;
  1509. }
  1510. }
  1511. /**
  1512. * @brief Tx Transfer completed callback.
  1513. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1514. * the configuration information for SPI module.
  1515. * @retval None
  1516. */
  1517. __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  1518. {
  1519. /* Prevent unused argument(s) compilation warning */
  1520. UNUSED(hspi);
  1521. /* NOTE : This function should not be modified, when the callback is needed,
  1522. the HAL_SPI_TxCpltCallback should be implemented in the user file
  1523. */
  1524. }
  1525. /**
  1526. * @brief Rx Transfer completed callback.
  1527. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1528. * the configuration information for SPI module.
  1529. * @retval None
  1530. */
  1531. __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  1532. {
  1533. /* Prevent unused argument(s) compilation warning */
  1534. UNUSED(hspi);
  1535. /* NOTE : This function should not be modified, when the callback is needed,
  1536. the HAL_SPI_RxCpltCallback should be implemented in the user file
  1537. */
  1538. }
  1539. /**
  1540. * @brief Tx and Rx Transfer completed callback.
  1541. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1542. * the configuration information for SPI module.
  1543. * @retval None
  1544. */
  1545. __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  1546. {
  1547. /* Prevent unused argument(s) compilation warning */
  1548. UNUSED(hspi);
  1549. /* NOTE : This function should not be modified, when the callback is needed,
  1550. the HAL_SPI_TxRxCpltCallback should be implemented in the user file
  1551. */
  1552. }
  1553. /**
  1554. * @brief Tx Half Transfer completed callback.
  1555. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1556. * the configuration information for SPI module.
  1557. * @retval None
  1558. */
  1559. __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1560. {
  1561. /* Prevent unused argument(s) compilation warning */
  1562. UNUSED(hspi);
  1563. /* NOTE : This function should not be modified, when the callback is needed,
  1564. the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
  1565. */
  1566. }
  1567. /**
  1568. * @brief Rx Half Transfer completed callback.
  1569. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1570. * the configuration information for SPI module.
  1571. * @retval None
  1572. */
  1573. __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1574. {
  1575. /* Prevent unused argument(s) compilation warning */
  1576. UNUSED(hspi);
  1577. /* NOTE : This function should not be modified, when the callback is needed,
  1578. the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
  1579. */
  1580. }
  1581. /**
  1582. * @brief Tx and Rx Half Transfer callback.
  1583. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1584. * the configuration information for SPI module.
  1585. * @retval None
  1586. */
  1587. __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1588. {
  1589. /* Prevent unused argument(s) compilation warning */
  1590. UNUSED(hspi);
  1591. /* NOTE : This function should not be modified, when the callback is needed,
  1592. the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
  1593. */
  1594. }
  1595. /**
  1596. * @brief SPI error callback.
  1597. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1598. * the configuration information for SPI module.
  1599. * @retval None
  1600. */
  1601. __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
  1602. {
  1603. /* Prevent unused argument(s) compilation warning */
  1604. UNUSED(hspi);
  1605. /* NOTE : This function should not be modified, when the callback is needed,
  1606. the HAL_SPI_ErrorCallback should be implemented in the user file
  1607. */
  1608. /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
  1609. and user can use HAL_SPI_GetError() API to check the latest error occurred
  1610. */
  1611. }
  1612. /**
  1613. * @}
  1614. */
  1615. /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
  1616. * @brief SPI control functions
  1617. *
  1618. @verbatim
  1619. ===============================================================================
  1620. ##### Peripheral State and Errors functions #####
  1621. ===============================================================================
  1622. [..]
  1623. This subsection provides a set of functions allowing to control the SPI.
  1624. (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
  1625. (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
  1626. @endverbatim
  1627. * @{
  1628. */
  1629. /**
  1630. * @brief Return the SPI handle state.
  1631. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1632. * the configuration information for SPI module.
  1633. * @retval SPI state
  1634. */
  1635. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
  1636. {
  1637. /* Return SPI handle state */
  1638. return hspi->State;
  1639. }
  1640. /**
  1641. * @brief Return the SPI error code.
  1642. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1643. * the configuration information for SPI module.
  1644. * @retval SPI error code in bitmap format
  1645. */
  1646. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
  1647. {
  1648. return hspi->ErrorCode;
  1649. }
  1650. /**
  1651. * @}
  1652. */
  1653. /**
  1654. * @}
  1655. */
  1656. /** @addtogroup SPI_Private_Functions
  1657. * @brief Private functions
  1658. * @{
  1659. */
  1660. /**
  1661. * @brief DMA SPI transmit process complete callback.
  1662. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1663. * the configuration information for the specified DMA module.
  1664. * @retval None
  1665. */
  1666. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1667. {
  1668. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1669. if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  1670. {
  1671. /* Disable Tx DMA Request */
  1672. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1673. /* Check the end of the transaction */
  1674. if(SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1675. {
  1676. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  1677. }
  1678. /* Clear overrun flag in 2 Lines communication mode because received data is not read */
  1679. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  1680. {
  1681. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1682. }
  1683. hspi->TxXferCount = 0;
  1684. hspi->State = HAL_SPI_STATE_READY;
  1685. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1686. {
  1687. HAL_SPI_ErrorCallback(hspi);
  1688. return;
  1689. }
  1690. }
  1691. HAL_SPI_TxCpltCallback(hspi);
  1692. }
  1693. /**
  1694. * @brief DMA SPI receive process complete callback.
  1695. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1696. * the configuration information for the specified DMA module.
  1697. * @retval None
  1698. */
  1699. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1700. {
  1701. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1702. if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  1703. {
  1704. __IO uint16_t tmpreg;
  1705. /* CRC handling */
  1706. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1707. {
  1708. /* Wait until TXE flag */
  1709. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1710. {
  1711. /* Error on the CRC reception */
  1712. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1713. }
  1714. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1715. {
  1716. tmpreg = hspi->Instance->DR;
  1717. UNUSED(tmpreg); /* To avoid GCC warning */
  1718. }
  1719. else
  1720. {
  1721. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1722. UNUSED(tmpreg); /* To avoid GCC warning */
  1723. if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  1724. {
  1725. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1726. {
  1727. /* Error on the CRC reception */
  1728. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1729. }
  1730. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1731. UNUSED(tmpreg); /* To avoid GCC warning */
  1732. }
  1733. }
  1734. }
  1735. /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
  1736. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1737. /* Check the end of the transaction */
  1738. if(SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT)!=HAL_OK)
  1739. {
  1740. hspi->ErrorCode|= HAL_SPI_ERROR_FLAG;
  1741. }
  1742. hspi->RxXferCount = 0;
  1743. hspi->State = HAL_SPI_STATE_READY;
  1744. /* Check if CRC error occurred */
  1745. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1746. {
  1747. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1748. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1749. }
  1750. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1751. {
  1752. HAL_SPI_ErrorCallback(hspi);
  1753. return;
  1754. }
  1755. }
  1756. HAL_SPI_RxCpltCallback(hspi);
  1757. }
  1758. /**
  1759. * @brief DMA SPI transmit receive process complete callback.
  1760. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1761. * the configuration information for the specified DMA module.
  1762. * @retval None
  1763. */
  1764. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1765. {
  1766. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1767. if((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
  1768. {
  1769. __IO int16_t tmpreg;
  1770. /* CRC handling */
  1771. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1772. {
  1773. if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_8BIT))
  1774. {
  1775. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_QUARTER_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1776. {
  1777. /* Error on the CRC reception */
  1778. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1779. }
  1780. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1781. UNUSED(tmpreg); /* To avoid GCC warning */
  1782. }
  1783. else
  1784. {
  1785. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_HALF_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1786. {
  1787. /* Error on the CRC reception */
  1788. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1789. }
  1790. tmpreg = hspi->Instance->DR;
  1791. UNUSED(tmpreg); /* To avoid GCC warning */
  1792. }
  1793. }
  1794. /* Check the end of the transaction */
  1795. if(SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1796. {
  1797. hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
  1798. }
  1799. /* Disable Rx/Tx DMA Request */
  1800. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1801. hspi->TxXferCount = 0;
  1802. hspi->RxXferCount = 0;
  1803. hspi->State = HAL_SPI_STATE_READY;
  1804. /* Check if CRC error occurred */
  1805. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1806. {
  1807. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1808. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1809. }
  1810. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1811. {
  1812. HAL_SPI_ErrorCallback(hspi);
  1813. return;
  1814. }
  1815. }
  1816. HAL_SPI_TxRxCpltCallback(hspi);
  1817. }
  1818. /**
  1819. * @brief DMA SPI half transmit process complete callback.
  1820. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1821. * the configuration information for the specified DMA module.
  1822. * @retval None
  1823. */
  1824. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
  1825. {
  1826. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1827. HAL_SPI_TxHalfCpltCallback(hspi);
  1828. }
  1829. /**
  1830. * @brief DMA SPI half receive process complete callback.
  1831. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1832. * the configuration information for the specified DMA module.
  1833. * @retval None
  1834. */
  1835. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
  1836. {
  1837. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1838. HAL_SPI_RxHalfCpltCallback(hspi);
  1839. }
  1840. /**
  1841. * @brief DMA SPI half transmit receive process complete callback.
  1842. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1843. * the configuration information for the specified DMA module.
  1844. * @retval None
  1845. */
  1846. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1847. {
  1848. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1849. HAL_SPI_TxRxHalfCpltCallback(hspi);
  1850. }
  1851. /**
  1852. * @brief DMA SPI communication error callback.
  1853. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1854. * the configuration information for the specified DMA module.
  1855. * @retval None
  1856. */
  1857. static void SPI_DMAError(DMA_HandleTypeDef *hdma)
  1858. {
  1859. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1860. /* Stop the disable DMA transfer on SPI side */
  1861. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1862. hspi->ErrorCode|= HAL_SPI_ERROR_DMA;
  1863. hspi->State = HAL_SPI_STATE_READY;
  1864. HAL_SPI_ErrorCallback(hspi);
  1865. }
  1866. /**
  1867. * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
  1868. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1869. * the configuration information for SPI module.
  1870. * @retval None
  1871. */
  1872. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  1873. {
  1874. /* Receive data in packing mode */
  1875. if(hspi->RxXferCount > 1)
  1876. {
  1877. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1878. hspi->pRxBuffPtr += sizeof(uint16_t);
  1879. hspi->RxXferCount -= 2;
  1880. if(hspi->RxXferCount == 1)
  1881. {
  1882. /* set fiforxthresold according the reception data length: 8bit */
  1883. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1884. }
  1885. }
  1886. /* Receive data in 8 Bit mode */
  1887. else
  1888. {
  1889. *hspi->pRxBuffPtr++ = *((__IO uint8_t *)&hspi->Instance->DR);
  1890. hspi->RxXferCount--;
  1891. }
  1892. /* check end of the reception */
  1893. if(hspi->RxXferCount == 0)
  1894. {
  1895. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1896. {
  1897. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1898. hspi->RxISR = SPI_2linesRxISR_8BITCRC;
  1899. return;
  1900. }
  1901. /* Disable RXNE interrupt */
  1902. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1903. if(hspi->TxXferCount == 0)
  1904. {
  1905. SPI_CloseRxTx_ISR(hspi);
  1906. }
  1907. }
  1908. }
  1909. /**
  1910. * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
  1911. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1912. * the configuration information for SPI module.
  1913. * @retval None
  1914. */
  1915. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  1916. {
  1917. __IO uint8_t tmpreg = *((__IO uint8_t *)&hspi->Instance->DR);
  1918. UNUSED(tmpreg); /* To avoid GCC warning */
  1919. hspi->CRCSize--;
  1920. /* check end of the reception */
  1921. if(hspi->CRCSize == 0)
  1922. {
  1923. /* Disable RXNE interrupt */
  1924. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1925. if(hspi->TxXferCount == 0)
  1926. {
  1927. SPI_CloseRxTx_ISR(hspi);
  1928. }
  1929. }
  1930. }
  1931. /**
  1932. * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode.
  1933. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1934. * the configuration information for SPI module.
  1935. * @retval None
  1936. */
  1937. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  1938. {
  1939. /* Transmit data in packing Bit mode */
  1940. if(hspi->TxXferCount >= 2)
  1941. {
  1942. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  1943. hspi->pTxBuffPtr += sizeof(uint16_t);
  1944. hspi->TxXferCount -= 2;
  1945. }
  1946. /* Transmit data in 8 Bit mode */
  1947. else
  1948. {
  1949. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  1950. hspi->TxXferCount--;
  1951. }
  1952. /* check the end of the transmission */
  1953. if(hspi->TxXferCount == 0)
  1954. {
  1955. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1956. {
  1957. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  1958. }
  1959. /* Disable TXE interrupt */
  1960. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  1961. if(hspi->RxXferCount == 0)
  1962. {
  1963. SPI_CloseRxTx_ISR(hspi);
  1964. }
  1965. }
  1966. }
  1967. /**
  1968. * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode.
  1969. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1970. * the configuration information for SPI module.
  1971. * @retval None
  1972. */
  1973. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  1974. {
  1975. /* Receive data in 16 Bit mode */
  1976. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1977. hspi->pRxBuffPtr += sizeof(uint16_t);
  1978. hspi->RxXferCount--;
  1979. if(hspi->RxXferCount == 0)
  1980. {
  1981. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1982. {
  1983. hspi->RxISR = SPI_2linesRxISR_16BITCRC;
  1984. return;
  1985. }
  1986. /* Disable RXNE interrupt */
  1987. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1988. if(hspi->TxXferCount == 0)
  1989. {
  1990. SPI_CloseRxTx_ISR(hspi);
  1991. }
  1992. }
  1993. }
  1994. /**
  1995. * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode.
  1996. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1997. * the configuration information for SPI module.
  1998. * @retval None
  1999. */
  2000. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  2001. {
  2002. /* Receive data in 16 Bit mode */
  2003. __IO uint16_t tmpreg = hspi->Instance->DR;
  2004. UNUSED(tmpreg); /* To avoid GCC warning */
  2005. /* Disable RXNE interrupt */
  2006. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  2007. SPI_CloseRxTx_ISR(hspi);
  2008. }
  2009. /**
  2010. * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode.
  2011. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2012. * the configuration information for SPI module.
  2013. * @retval None
  2014. */
  2015. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  2016. {
  2017. /* Transmit data in 16 Bit mode */
  2018. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  2019. hspi->pTxBuffPtr += sizeof(uint16_t);
  2020. hspi->TxXferCount--;
  2021. /* Enable CRC Transmission */
  2022. if(hspi->TxXferCount == 0)
  2023. {
  2024. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2025. {
  2026. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2027. }
  2028. /* Disable TXE interrupt */
  2029. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  2030. if(hspi->RxXferCount == 0)
  2031. {
  2032. SPI_CloseRxTx_ISR(hspi);
  2033. }
  2034. }
  2035. }
  2036. /**
  2037. * @brief Manage the CRC 8-bit receive in Interrupt context.
  2038. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2039. * the configuration information for SPI module.
  2040. * @retval None
  2041. */
  2042. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  2043. {
  2044. __IO uint8_t tmpreg = *((uint8_t*)&hspi->Instance->DR);
  2045. UNUSED(tmpreg); /* To avoid GCC warning */
  2046. hspi->CRCSize--;
  2047. if(hspi->CRCSize == 0)
  2048. {
  2049. SPI_CloseRx_ISR(hspi);
  2050. }
  2051. }
  2052. /**
  2053. * @brief Manage the receive 8-bit in Interrupt context.
  2054. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2055. * the configuration information for SPI module.
  2056. * @retval None
  2057. */
  2058. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  2059. {
  2060. *hspi->pRxBuffPtr++ = (*(__IO uint8_t *)&hspi->Instance->DR);
  2061. hspi->RxXferCount--;
  2062. /* Enable CRC Transmission */
  2063. if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  2064. {
  2065. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2066. }
  2067. if(hspi->RxXferCount == 0)
  2068. {
  2069. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2070. {
  2071. hspi->RxISR = SPI_RxISR_8BITCRC;
  2072. return;
  2073. }
  2074. SPI_CloseRx_ISR(hspi);
  2075. }
  2076. }
  2077. /**
  2078. * @brief Manage the CRC 16-bit receive in Interrupt context.
  2079. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2080. * the configuration information for SPI module.
  2081. * @retval None
  2082. */
  2083. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  2084. {
  2085. __IO uint16_t tmpreg;
  2086. tmpreg = hspi->Instance->DR;
  2087. UNUSED(tmpreg); /* To avoid GCC warning */
  2088. /* Disable RXNE and ERR interrupt */
  2089. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  2090. SPI_CloseRx_ISR(hspi);
  2091. }
  2092. /**
  2093. * @brief Manage the 16-bit receive in Interrupt context.
  2094. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2095. * the configuration information for SPI module.
  2096. * @retval None
  2097. */
  2098. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  2099. {
  2100. *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
  2101. hspi->pRxBuffPtr += sizeof(uint16_t);
  2102. hspi->RxXferCount--;
  2103. /* Enable CRC Transmission */
  2104. if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  2105. {
  2106. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2107. }
  2108. if(hspi->RxXferCount == 0)
  2109. {
  2110. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2111. {
  2112. hspi->RxISR = SPI_RxISR_16BITCRC;
  2113. return;
  2114. }
  2115. SPI_CloseRx_ISR(hspi);
  2116. }
  2117. }
  2118. /**
  2119. * @brief Handle the data 8-bit transmit in Interrupt mode.
  2120. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2121. * the configuration information for SPI module.
  2122. * @retval None
  2123. */
  2124. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  2125. {
  2126. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  2127. hspi->TxXferCount--;
  2128. if(hspi->TxXferCount == 0)
  2129. {
  2130. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2131. {
  2132. /* Enable CRC Transmission */
  2133. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2134. }
  2135. SPI_CloseTx_ISR(hspi);
  2136. }
  2137. }
  2138. /**
  2139. * @brief Handle the data 16-bit transmit in Interrupt mode.
  2140. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2141. * the configuration information for SPI module.
  2142. * @retval None
  2143. */
  2144. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  2145. {
  2146. /* Transmit data in 16 Bit mode */
  2147. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  2148. hspi->pTxBuffPtr += sizeof(uint16_t);
  2149. hspi->TxXferCount--;
  2150. if(hspi->TxXferCount == 0)
  2151. {
  2152. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2153. {
  2154. /* Enable CRC Transmission */
  2155. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2156. }
  2157. SPI_CloseTx_ISR(hspi);
  2158. }
  2159. }
  2160. /**
  2161. * @brief Handle SPI Communication Timeout.
  2162. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2163. * the configuration information for SPI module.
  2164. * @param Flag : SPI flag to check
  2165. * @param State : flag state to check
  2166. * @param Timeout : Timeout duration
  2167. * @retval HAL status
  2168. */
  2169. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout)
  2170. {
  2171. uint32_t tickstart = HAL_GetTick();
  2172. while((hspi->Instance->SR & Flag) != State)
  2173. {
  2174. if(Timeout != HAL_MAX_DELAY)
  2175. {
  2176. if((Timeout == 0) || ((HAL_GetTick()-tickstart) >= Timeout))
  2177. {
  2178. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  2179. on both master and slave sides in order to resynchronize the master
  2180. and slave for their respective CRC calculation */
  2181. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  2182. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  2183. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2184. {
  2185. /* Disable SPI peripheral */
  2186. __HAL_SPI_DISABLE(hspi);
  2187. }
  2188. /* Reset CRC Calculation */
  2189. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2190. {
  2191. SPI_RESET_CRC(hspi);
  2192. }
  2193. hspi->State= HAL_SPI_STATE_READY;
  2194. /* Process Unlocked */
  2195. __HAL_UNLOCK(hspi);
  2196. return HAL_TIMEOUT;
  2197. }
  2198. }
  2199. }
  2200. return HAL_OK;
  2201. }
  2202. /**
  2203. * @brief Handle SPI FIFO Communication Timeout.
  2204. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2205. * the configuration information for SPI module.
  2206. * @param Fifo : Fifo to check
  2207. * @param State : Fifo state to check
  2208. * @param Timeout : Timeout duration
  2209. * @retval HAL status
  2210. */
  2211. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State, uint32_t Timeout)
  2212. {
  2213. __IO uint8_t tmpreg;
  2214. uint32_t tickstart = HAL_GetTick();
  2215. while((hspi->Instance->SR & Fifo) != State)
  2216. {
  2217. if((Fifo == SPI_SR_FRLVL) && (State == SPI_FRLVL_EMPTY))
  2218. {
  2219. tmpreg = *((__IO uint8_t*)&hspi->Instance->DR);
  2220. UNUSED(tmpreg); /* To avoid GCC warning */
  2221. }
  2222. if(Timeout != HAL_MAX_DELAY)
  2223. {
  2224. if((Timeout == 0) || ((HAL_GetTick()-tickstart) >= Timeout))
  2225. {
  2226. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  2227. on both master and slave sides in order to resynchronize the master
  2228. and slave for their respective CRC calculation */
  2229. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  2230. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  2231. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2232. {
  2233. /* Disable SPI peripheral */
  2234. __HAL_SPI_DISABLE(hspi);
  2235. }
  2236. /* Reset CRC Calculation */
  2237. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2238. {
  2239. SPI_RESET_CRC(hspi);
  2240. }
  2241. hspi->State = HAL_SPI_STATE_READY;
  2242. /* Process Unlocked */
  2243. __HAL_UNLOCK(hspi);
  2244. return HAL_TIMEOUT;
  2245. }
  2246. }
  2247. }
  2248. return HAL_OK;
  2249. }
  2250. /**
  2251. * @brief Handle the check of the RX transaction complete.
  2252. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2253. * the configuration information for SPI module.
  2254. * @param Timeout : Timeout duration
  2255. * @retval None
  2256. */
  2257. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout)
  2258. {
  2259. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2260. {
  2261. /* Disable SPI peripheral */
  2262. __HAL_SPI_DISABLE(hspi);
  2263. }
  2264. /* Control the BSY flag */
  2265. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
  2266. {
  2267. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2268. return HAL_TIMEOUT;
  2269. }
  2270. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2271. {
  2272. /* Empty the FRLVL fifo */
  2273. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout) != HAL_OK)
  2274. {
  2275. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2276. return HAL_TIMEOUT;
  2277. }
  2278. }
  2279. return HAL_OK;
  2280. }
  2281. /**
  2282. * @brief Handle the check of the RXTX or TX transaction complete.
  2283. * @param hspi: SPI handle
  2284. * @param Timeout : Timeout duration
  2285. */
  2286. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout)
  2287. {
  2288. /* Control if the TX fifo is empty */
  2289. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, Timeout) != HAL_OK)
  2290. {
  2291. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2292. return HAL_TIMEOUT;
  2293. }
  2294. /* Control the BSY flag */
  2295. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
  2296. {
  2297. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2298. return HAL_TIMEOUT;
  2299. }
  2300. return HAL_OK;
  2301. }
  2302. /**
  2303. * @brief Handle the end of the RXTX transaction.
  2304. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2305. * the configuration information for SPI module.
  2306. * @retval None
  2307. */
  2308. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
  2309. {
  2310. /* Disable ERR interrupt */
  2311. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  2312. /* Check the end of the transaction */
  2313. if(SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT)!=HAL_OK)
  2314. {
  2315. hspi->ErrorCode|= HAL_SPI_ERROR_FLAG;
  2316. }
  2317. /* Check if CRC error occurred */
  2318. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  2319. {
  2320. hspi->State = HAL_SPI_STATE_READY;
  2321. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  2322. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2323. HAL_SPI_ErrorCallback(hspi);
  2324. }
  2325. else
  2326. {
  2327. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  2328. {
  2329. if(hspi->State == HAL_SPI_STATE_BUSY_RX)
  2330. {
  2331. hspi->State = HAL_SPI_STATE_READY;
  2332. HAL_SPI_RxCpltCallback(hspi);
  2333. }
  2334. else
  2335. {
  2336. hspi->State = HAL_SPI_STATE_READY;
  2337. HAL_SPI_TxRxCpltCallback(hspi);
  2338. }
  2339. }
  2340. else
  2341. {
  2342. hspi->State = HAL_SPI_STATE_READY;
  2343. HAL_SPI_ErrorCallback(hspi);
  2344. }
  2345. }
  2346. }
  2347. /**
  2348. * @brief Handle the end of the RX transaction.
  2349. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2350. * the configuration information for SPI module.
  2351. * @retval None
  2352. */
  2353. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
  2354. {
  2355. /* Disable RXNE and ERR interrupt */
  2356. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  2357. /* Check the end of the transaction */
  2358. if(SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT)!=HAL_OK)
  2359. {
  2360. hspi->ErrorCode|= HAL_SPI_ERROR_FLAG;
  2361. }
  2362. hspi->State = HAL_SPI_STATE_READY;
  2363. /* Check if CRC error occurred */
  2364. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  2365. {
  2366. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  2367. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2368. HAL_SPI_ErrorCallback(hspi);
  2369. }
  2370. else
  2371. {
  2372. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  2373. {
  2374. HAL_SPI_RxCpltCallback(hspi);
  2375. }
  2376. else
  2377. {
  2378. HAL_SPI_ErrorCallback(hspi);
  2379. }
  2380. }
  2381. }
  2382. /**
  2383. * @brief Handle the end of the TX transaction.
  2384. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2385. * the configuration information for SPI module.
  2386. * @retval None
  2387. */
  2388. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
  2389. {
  2390. /* Disable TXE and ERR interrupt */
  2391. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
  2392. /* Check the end of the transaction */
  2393. if(SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT)!=HAL_OK)
  2394. {
  2395. hspi->ErrorCode|= HAL_SPI_ERROR_FLAG;
  2396. }
  2397. /* Clear overrun flag in 2 Lines communication mode because received is not read */
  2398. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  2399. {
  2400. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2401. }
  2402. hspi->State = HAL_SPI_STATE_READY;
  2403. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2404. {
  2405. HAL_SPI_ErrorCallback(hspi);
  2406. }
  2407. else
  2408. {
  2409. HAL_SPI_TxCpltCallback(hspi);
  2410. }
  2411. }
  2412. /**
  2413. * @}
  2414. */
  2415. #endif /* HAL_SPI_MODULE_ENABLED */
  2416. /**
  2417. * @}
  2418. */
  2419. /**
  2420. * @}
  2421. */
  2422. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/