2 ******************************************************************************
3 * @file stm32l1xx_hal_spi.c
4 * @author MCD Application Team
6 * @date 5-September-2014
7 * @brief SPI HAL module driver.
9 * This file provides firmware functions to manage the following
10 * functionalities of the Serial Peripheral Interface (SPI) peripheral:
11 * + Initialization and de-initialization functions
12 * + IO operation functions
13 * + Peripheral Control functions
14 * + Peripheral State functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The SPI HAL driver can be used as follows:
22 (#) Declare a SPI_HandleTypeDef handle structure, for example:
23 SPI_HandleTypeDef hspi;
25 (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit ()API:
26 (##) Enable the SPIx interface clock
27 (##) SPI pins configuration
28 (+++) Enable the clock for the SPI GPIOs
29 (+++) Configure these SPI pins as alternate function push-pull
30 (##) NVIC configuration if you need to use interrupt process
31 (+++) Configure the SPIx interrupt priority
32 (+++) Enable the NVIC SPI IRQ handle
33 (##) DMA Configuration if you need to use DMA process
34 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Channel
35 (+++) Enable the DMAx clock
36 (+++) Configure the DMA handle parameters
37 (+++) Configure the DMA Tx or Rx Channel
38 (+++) Associate the initilalized hdma_tx(or _rx) handle to the hspi DMA Tx (or Rx) handle
39 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Channel
41 (#) Program the Mode, Direction , Data size, Baudrate Prescaler, NSS
42 management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
44 (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
45 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
46 by calling the customed HAL_SPI_MspInit() API.
48 Circular mode restriction:
49 (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
50 (##) Master 2Lines RxOnly
52 (#) The CRC feature is not managed when the DMA circular mode is enabled
53 (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
54 the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
59 ******************************************************************************
62 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
64 * Redistribution and use in source and binary forms, with or without modification,
65 * are permitted provided that the following conditions are met:
66 * 1. Redistributions of source code must retain the above copyright notice,
67 * this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright notice,
69 * this list of conditions and the following disclaimer in the documentation
70 * and/or other materials provided with the distribution.
71 * 3. Neither the name of STMicroelectronics nor the names of its contributors
72 * may be used to endorse or promote products derived from this software
73 * without specific prior written permission.
75 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
76 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
78 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
81 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
82 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86 ******************************************************************************
89 /* Includes ------------------------------------------------------------------*/
90 #include "stm32l1xx_hal.h"
92 /** @addtogroup STM32L1xx_HAL_Driver
97 * @brief SPI HAL module driver
101 #ifdef HAL_SPI_MODULE_ENABLED
103 /* Private typedef -----------------------------------------------------------*/
104 /* Private define ------------------------------------------------------------*/
105 /** @defgroup SPI_Private_Constants SPI Private Constants
108 #define SPI_TIMEOUT_VALUE 10
113 /* Private macro -------------------------------------------------------------*/
114 /* Private variables ---------------------------------------------------------*/
115 /* Private function prototypes -----------------------------------------------*/
116 /** @defgroup SPI_Private_Functions SPI Private Functions
120 static void SPI_TxCloseIRQHandler(struct __SPI_HandleTypeDef *hspi);
121 static void SPI_TxISR(struct __SPI_HandleTypeDef *hspi);
122 static void SPI_RxCloseIRQHandler(struct __SPI_HandleTypeDef *hspi);
123 static void SPI_2LinesRxISR(struct __SPI_HandleTypeDef *hspi);
124 static void SPI_RxISR(struct __SPI_HandleTypeDef *hspi);
125 static void SPI_DMATransmitCplt(struct __DMA_HandleTypeDef *hdma);
126 static void SPI_DMAReceiveCplt(struct __DMA_HandleTypeDef *hdma);
127 static void SPI_DMATransmitReceiveCplt(struct __DMA_HandleTypeDef *hdma);
128 static void SPI_DMAHalfTransmitCplt(struct __DMA_HandleTypeDef *hdma);
129 static void SPI_DMAHalfReceiveCplt(struct __DMA_HandleTypeDef *hdma);
130 static void SPI_DMAHalfTransmitReceiveCplt(struct __DMA_HandleTypeDef *hdma);
131 static void SPI_DMAError(struct __DMA_HandleTypeDef *hdma);
132 static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(struct __SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
137 /* Exported functions ---------------------------------------------------------*/
139 /** @defgroup SPI_Exported_Functions SPI Exported Functions
143 /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
144 * @brief Initialization and Configuration functions
147 ===============================================================================
148 ##### Initialization and de-initialization functions #####
149 ===============================================================================
150 [..] This subsection provides a set of functions allowing to initialize and
151 de-initialiaze the SPIx peripheral:
153 (+) User must implement HAL_SPI_MspInit() function in which he configures
154 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
156 (+) Call the function HAL_SPI_Init() to configure the selected device with
157 the selected configuration:
161 (++) Clock Polarity and Phase
163 (++) BaudRate Prescaler
167 (++) CRC Polynomial if CRC enabled
169 (+) Call the function HAL_SPI_DeInit() to restore the default configuration
170 of the selected SPIx periperal.
177 * @brief Initializes the SPI according to the specified parameters
178 * in the SPI_InitTypeDef and create the associated handle.
179 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
180 * the configuration information for SPI module.
183 __weak HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
189 * @brief DeInitializes the SPI peripheral
190 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
191 * the configuration information for SPI module.
194 HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
196 /* Check the SPI handle allocation */
202 /* Disable the SPI Peripheral Clock */
203 __HAL_SPI_DISABLE(hspi);
205 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
206 HAL_SPI_MspDeInit(hspi);
208 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
209 hspi->State = HAL_SPI_STATE_RESET;
218 * @brief SPI MSP Init
219 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
220 * the configuration information for SPI module.
223 __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
225 /* NOTE : This function Should not be modified, when the callback is needed,
226 the HAL_SPI_MspInit could be implenetd in the user file
231 * @brief SPI MSP DeInit
232 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
233 * the configuration information for SPI module.
236 __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
238 /* NOTE : This function Should not be modified, when the callback is needed,
239 the HAL_SPI_MspDeInit could be implenetd in the user file
247 /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
248 * @brief Data transfers functions
251 ==============================================================================
252 ##### IO operation functions #####
253 ===============================================================================
254 This subsection provides a set of functions allowing to manage the SPI
257 [..] The SPI supports master and slave mode :
259 (#) There are two modes of transfer:
260 (++) Blocking mode: The communication is performed in polling mode.
261 The HAL status of all data processing is returned by the same function
262 after finishing transfer.
263 (++) No-Blocking mode: The communication is performed using Interrupts
264 or DMA, These APIs return the HAL status.
265 The end of the data processing will be indicated through the
266 dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
268 The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
269 will be executed respectivelly at the end of the transmit or Receive process
270 The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
272 (#) Blocking mode APIs are :
273 (++) HAL_SPI_Transmit()in 1Line (simplex) and 2Lines (full duplex) mode
274 (++) HAL_SPI_Receive() in 1Line (simplex) and 2Lines (full duplex) mode
275 (++) HAL_SPI_TransmitReceive() in full duplex mode
277 (#) Non Blocking mode API's with Interrupt are :
278 (++) HAL_SPI_Transmit_IT()in 1Line (simplex) and 2Lines (full duplex) mode
279 (++) HAL_SPI_Receive_IT() in 1Line (simplex) and 2Lines (full duplex) mode
280 (++) HAL_SPI_TransmitReceive_IT()in full duplex mode
281 (++) HAL_SPI_IRQHandler()
283 (#) Non Blocking mode functions with DMA are :
284 (++) HAL_SPI_Transmit_DMA()in 1Line (simplex) and 2Lines (full duplex) mode
285 (++) HAL_SPI_Receive_DMA() in 1Line (simplex) and 2Lines (full duplex) mode
286 (++) HAL_SPI_TransmitReceive_DMA() in full duplex mode
288 (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
289 (++) HAL_SPI_TxCpltCallback()
290 (++) HAL_SPI_RxCpltCallback()
291 (++) HAL_SPI_TxRxCpltCallback()
292 (++) HAL_SPI_TxHalfCpltCallback()
293 (++) HAL_SPI_RxHalfCpltCallback()
294 (++) HAL_SPI_TxRxHalfCpltCallback()
295 (++) HAL_SPI_ErrorCallback()
302 * @brief Transmit an amount of data in blocking mode
303 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
304 * the configuration information for SPI module.
305 * @param pData: pointer to data buffer
306 * @param Size: amount of data to be sent
307 * @param Timeout: Timeout duration
310 HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
313 if(hspi->State == HAL_SPI_STATE_READY)
315 if((pData == HAL_NULL ) || (Size == 0))
320 /* Check the parameters */
321 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
326 /* Configure communication */
327 hspi->State = HAL_SPI_STATE_BUSY_TX;
328 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
330 hspi->pTxBuffPtr = pData;
331 hspi->TxXferSize = Size;
332 hspi->TxXferCount = Size;
334 /*Init field not used in handle to zero */
337 hspi->pRxBuffPtr = HAL_NULL;
338 hspi->RxXferSize = 0;
339 hspi->RxXferCount = 0;
341 /* Reset CRC Calculation */
342 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
347 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
349 /* Configure communication direction : 1Line */
353 /* Check if the SPI is already enabled */
354 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
356 /* Enable SPI peripheral */
357 __HAL_SPI_ENABLE(hspi);
360 /* Transmit data in 8 Bit mode */
361 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
363 if((hspi->Init.Mode == SPI_MODE_SLAVE)|| (hspi->TxXferCount == 0x01))
365 hspi->Instance->DR = (*hspi->pTxBuffPtr++);
368 while(hspi->TxXferCount > 0)
370 /* Wait until TXE flag is set to send data */
371 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
375 hspi->Instance->DR = (*hspi->pTxBuffPtr++);
378 /* Enable CRC Transmission */
379 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
381 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
384 /* Transmit data in 16 Bit mode */
387 if((hspi->Init.Mode == SPI_MODE_SLAVE) || (hspi->TxXferCount == 0x01))
389 hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
393 while(hspi->TxXferCount > 0)
395 /* Wait until TXE flag is set to send data */
396 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
400 hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
404 /* Enable CRC Transmission */
405 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
407 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
411 /* Wait until TXE flag is set to send data */
412 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
414 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
418 /* Wait until Busy flag is reset before disabling SPI */
419 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, Timeout) != HAL_OK)
421 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
425 /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
426 if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
428 __HAL_SPI_CLEAR_OVRFLAG(hspi);
431 hspi->State = HAL_SPI_STATE_READY;
433 /* Process Unlocked */
445 * @brief Receive an amount of data in blocking mode
446 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
447 * the configuration information for SPI module.
448 * @param pData: pointer to data buffer
449 * @param Size: amount of data to be sent
450 * @param Timeout: Timeout duration
453 HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
455 __IO uint16_t tmpreg = 0;
457 if(hspi->State == HAL_SPI_STATE_READY)
459 if((pData == HAL_NULL ) || (Size == 0))
467 /* Configure communication */
468 hspi->State = HAL_SPI_STATE_BUSY_RX;
469 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
471 hspi->pRxBuffPtr = pData;
472 hspi->RxXferSize = Size;
473 hspi->RxXferCount = Size;
475 /*Init field not used in handle to zero */
478 hspi->pTxBuffPtr = HAL_NULL;
479 hspi->TxXferSize = 0;
480 hspi->TxXferCount = 0;
482 /* Configure communication direction : 1Line */
483 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
488 /* Reset CRC Calculation */
489 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
494 if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
496 /* Process Unlocked */
499 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
500 return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
503 /* Check if the SPI is already enabled */
504 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
506 /* Enable SPI peripheral */
507 __HAL_SPI_ENABLE(hspi);
510 /* Receive data in 8 Bit mode */
511 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
513 while(hspi->RxXferCount > 1)
515 /* Wait until RXNE flag is set */
516 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
521 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
524 /* Enable CRC Transmission */
525 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
527 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
530 /* Receive data in 16 Bit mode */
533 while(hspi->RxXferCount > 1)
535 /* Wait until RXNE flag is set to read data */
536 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
541 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
545 /* Enable CRC Transmission */
546 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
548 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
552 /* Wait until RXNE flag is set */
553 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
558 /* Receive last data in 8 Bit mode */
559 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
561 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
563 /* Receive last data in 16 Bit mode */
566 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
571 /* Wait until RXNE flag is set: CRC Received */
572 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
574 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
576 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
580 /* Read CRC to Flush RXNE flag */
581 tmpreg = hspi->Instance->DR;
584 if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
586 /* Disable SPI peripheral */
587 __HAL_SPI_DISABLE(hspi);
590 hspi->State = HAL_SPI_STATE_READY;
592 /* Check if CRC error occurred */
593 if((hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET))
595 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
597 /* Reset CRC Calculation */
600 /* Process Unlocked */
606 /* Process Unlocked */
618 * @brief Transmit and Receive an amount of data in blocking mode
619 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
620 * the configuration information for SPI module.
621 * @param pTxData: pointer to transmission data buffer
622 * @param pRxData: pointer to reception data buffer to be
623 * @param Size: amount of data to be sent
624 * @param Timeout: Timeout duration
627 HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
629 __IO uint16_t tmpreg = 0;
631 if((hspi->State == HAL_SPI_STATE_READY) || (hspi->State == HAL_SPI_STATE_BUSY_RX))
633 if((pTxData == HAL_NULL ) || (pRxData == HAL_NULL ) || (Size == 0))
638 /* Check the parameters */
639 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
644 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
645 if(hspi->State == HAL_SPI_STATE_READY)
647 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
650 /* Configure communication */
651 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
653 hspi->pRxBuffPtr = pRxData;
654 hspi->RxXferSize = Size;
655 hspi->RxXferCount = Size;
657 hspi->pTxBuffPtr = pTxData;
658 hspi->TxXferSize = Size;
659 hspi->TxXferCount = Size;
661 /*Init field not used in handle to zero */
665 /* Reset CRC Calculation */
666 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
671 /* Check if the SPI is already enabled */
672 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
674 /* Enable SPI peripheral */
675 __HAL_SPI_ENABLE(hspi);
678 /* Transmit and Receive data in 16 Bit mode */
679 if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
681 if((hspi->Init.Mode == SPI_MODE_SLAVE) || ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->TxXferCount == 0x01)))
683 hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
687 if(hspi->TxXferCount == 0)
689 /* Enable CRC Transmission */
690 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
692 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
695 /* Wait until RXNE flag is set */
696 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
701 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
707 while(hspi->TxXferCount > 0)
709 /* Wait until TXE flag is set to send data */
710 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
715 hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
719 /* Enable CRC Transmission */
720 if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED))
722 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
725 /* Wait until RXNE flag is set */
726 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
731 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
735 /* Receive the last byte */
736 if(hspi->Init.Mode == SPI_MODE_SLAVE)
738 /* Wait until RXNE flag is set */
739 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
744 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
750 /* Transmit and Receive data in 8 Bit mode */
753 if((hspi->Init.Mode == SPI_MODE_SLAVE) || ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->TxXferCount == 0x01)))
755 hspi->Instance->DR = (*hspi->pTxBuffPtr++);
758 if(hspi->TxXferCount == 0)
760 /* Enable CRC Transmission */
761 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
763 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
766 /* Wait until RXNE flag is set */
767 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
772 (*hspi->pRxBuffPtr) = hspi->Instance->DR;
777 while(hspi->TxXferCount > 0)
779 /* Wait until TXE flag is set to send data */
780 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, Timeout) != HAL_OK)
785 hspi->Instance->DR = (*hspi->pTxBuffPtr++);
788 /* Enable CRC Transmission */
789 if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED))
791 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
794 /* Wait until RXNE flag is set */
795 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
800 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
803 if(hspi->Init.Mode == SPI_MODE_SLAVE)
805 /* Wait until RXNE flag is set */
806 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
811 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
817 /* Read CRC from DR to close CRC calculation process */
818 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
820 /* Wait until RXNE flag is set */
821 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout) != HAL_OK)
823 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
827 tmpreg = hspi->Instance->DR;
830 /* Wait until Busy flag is reset before disabling SPI */
831 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, Timeout) != HAL_OK)
833 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
837 hspi->State = HAL_SPI_STATE_READY;
839 /* Check if CRC error occurred */
840 if((hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET))
842 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
844 /* Reset CRC Calculation */
845 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
850 /* Process Unlocked */
856 /* Process Unlocked */
868 * @brief Transmit an amount of data in no-blocking mode with Interrupt
869 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
870 * the configuration information for SPI module.
871 * @param pData: pointer to data buffer
872 * @param Size: amount of data to be sent
875 HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
877 if(hspi->State == HAL_SPI_STATE_READY)
879 if((pData == HAL_NULL) || (Size == 0))
884 /* Check the parameters */
885 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
890 /* Configure communication */
891 hspi->State = HAL_SPI_STATE_BUSY_TX;
892 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
894 hspi->TxISR = &SPI_TxISR;
895 hspi->pTxBuffPtr = pData;
896 hspi->TxXferSize = Size;
897 hspi->TxXferCount = Size;
899 /*Init field not used in handle to zero */
901 hspi->pRxBuffPtr = HAL_NULL;
902 hspi->RxXferSize = 0;
903 hspi->RxXferCount = 0;
905 /* Configure communication direction : 1Line */
906 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
911 /* Reset CRC Calculation */
912 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
917 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
919 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE));
922 /* Enable TXE and ERR interrupt */
923 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
925 /* Process Unlocked */
928 /* Check if the SPI is already enabled */
929 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
931 /* Enable SPI peripheral */
932 __HAL_SPI_ENABLE(hspi);
944 * @brief Receive an amount of data in no-blocking mode with Interrupt
945 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
946 * the configuration information for SPI module.
947 * @param pData: pointer to data buffer
948 * @param Size: amount of data to be sent
951 HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
953 if(hspi->State == HAL_SPI_STATE_READY)
955 if((pData == HAL_NULL) || (Size == 0))
963 /* Configure communication */
964 hspi->State = HAL_SPI_STATE_BUSY_RX;
965 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
967 hspi->RxISR = &SPI_RxISR;
968 hspi->pRxBuffPtr = pData;
969 hspi->RxXferSize = Size;
970 hspi->RxXferCount = Size ;
972 /*Init field not used in handle to zero */
974 hspi->pTxBuffPtr = HAL_NULL;
975 hspi->TxXferSize = 0;
976 hspi->TxXferCount = 0;
978 /* Configure communication direction : 1Line */
979 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
983 else if((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
985 /* Process Unlocked */
988 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
989 return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
992 /* Reset CRC Calculation */
993 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
998 /* Enable TXE and ERR interrupt */
999 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
1001 /* Process Unlocked */
1004 /* Note : The SPI must be enabled after unlocking current process
1005 to avoid the risk of SPI interrupt handle execution before current
1008 /* Check if the SPI is already enabled */
1009 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1011 /* Enable SPI peripheral */
1012 __HAL_SPI_ENABLE(hspi);
1024 * @brief Transmit and Receive an amount of data in no-blocking mode with Interrupt
1025 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1026 * the configuration information for SPI module.
1027 * @param pTxData: pointer to transmission data buffer
1028 * @param pRxData: pointer to reception data buffer to be
1029 * @param Size: amount of data to be sent
1030 * @retval HAL status
1032 HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1035 if((hspi->State == HAL_SPI_STATE_READY) || \
1036 ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
1038 if((pTxData == HAL_NULL ) || (pRxData == HAL_NULL ) || (Size == 0))
1043 /* Check the parameters */
1044 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1046 /* Process locked */
1049 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1050 if(hspi->State != HAL_SPI_STATE_BUSY_RX)
1052 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
1055 /* Configure communication */
1056 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1058 hspi->TxISR = &SPI_TxISR;
1059 hspi->pTxBuffPtr = pTxData;
1060 hspi->TxXferSize = Size;
1061 hspi->TxXferCount = Size;
1063 hspi->RxISR = &SPI_2LinesRxISR;
1064 hspi->pRxBuffPtr = pRxData;
1065 hspi->RxXferSize = Size;
1066 hspi->RxXferCount = Size;
1068 /* Reset CRC Calculation */
1069 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1071 SPI_RESET_CRC(hspi);
1074 /* Enable TXE, RXNE and ERR interrupt */
1075 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
1077 /* Process Unlocked */
1080 /* Check if the SPI is already enabled */
1081 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1083 /* Enable SPI peripheral */
1084 __HAL_SPI_ENABLE(hspi);
1096 * @brief Transmit an amount of data in no-blocking mode with DMA
1097 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1098 * the configuration information for SPI module.
1099 * @param pData: pointer to data buffer
1100 * @param Size: amount of data to be sent
1101 * @retval HAL status
1103 HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1105 if(hspi->State == HAL_SPI_STATE_READY)
1107 if((pData == HAL_NULL) || (Size == 0))
1112 /* Check the parameters */
1113 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
1115 /* Process Locked */
1118 /* Configure communication */
1119 hspi->State = HAL_SPI_STATE_BUSY_TX;
1120 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1122 hspi->pTxBuffPtr = pData;
1123 hspi->TxXferSize = Size;
1124 hspi->TxXferCount = Size;
1126 /*Init field not used in handle to zero */
1129 hspi->pRxBuffPtr = HAL_NULL;
1130 hspi->RxXferSize = 0;
1131 hspi->RxXferCount = 0;
1133 /* Configure communication direction : 1Line */
1134 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1139 /* Reset CRC Calculation */
1140 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1142 SPI_RESET_CRC(hspi);
1145 /* Set the SPI TxDMA Half transfer complete callback */
1146 hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
1148 /* Set the SPI TxDMA transfer complete callback */
1149 hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
1151 /* Set the DMA error callback */
1152 hspi->hdmatx->XferErrorCallback = SPI_DMAError;
1154 /* Enable the Tx DMA Channel */
1155 HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
1157 /* Enable Tx DMA Request */
1158 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1160 /* Process Unlocked */
1163 /* Check if the SPI is already enabled */
1164 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1166 /* Enable SPI peripheral */
1167 __HAL_SPI_ENABLE(hspi);
1179 * @brief Receive an amount of data in no-blocking mode with DMA
1180 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1181 * the configuration information for SPI module.
1182 * @param pData: pointer to data buffer
1183 * @note When the CRC feature is enabled the pData Length must be Size + 1.
1184 * @param Size: amount of data to be sent
1185 * @retval HAL status
1187 HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1189 if(hspi->State == HAL_SPI_STATE_READY)
1191 if((pData == HAL_NULL) || (Size == 0))
1196 /* Process Locked */
1199 /* Configure communication */
1200 hspi->State = HAL_SPI_STATE_BUSY_RX;
1201 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1203 hspi->pRxBuffPtr = pData;
1204 hspi->RxXferSize = Size;
1205 hspi->RxXferCount = Size;
1207 /*Init field not used in handle to zero */
1210 hspi->pTxBuffPtr = HAL_NULL;
1211 hspi->TxXferSize = 0;
1212 hspi->TxXferCount = 0;
1214 /* Configure communication direction : 1Line */
1215 if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
1219 else if((hspi->Init.Direction == SPI_DIRECTION_2LINES)&&(hspi->Init.Mode == SPI_MODE_MASTER))
1221 /* Process Unlocked */
1224 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1225 return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
1228 /* Reset CRC Calculation */
1229 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1231 SPI_RESET_CRC(hspi);
1234 /* Set the SPI RxDMA Half transfer complete callback */
1235 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
1237 /* Set the SPI Rx DMA transfer complete callback */
1238 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1240 /* Set the DMA error callback */
1241 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1243 /* Enable the Rx DMA Channel */
1244 HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
1246 /* Enable Rx DMA Request */
1247 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1249 /* Process Unlocked */
1252 /* Check if the SPI is already enabled */
1253 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1255 /* Enable SPI peripheral */
1256 __HAL_SPI_ENABLE(hspi);
1268 * @brief Transmit and Receive an amount of data in no-blocking mode with DMA
1269 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1270 * the configuration information for SPI module.
1271 * @param pTxData: pointer to transmission data buffer
1272 * @param pRxData: pointer to reception data buffer
1273 * @note When the CRC feature is enabled the pRxData Length must be Size + 1
1274 * @param Size: amount of data to be sent
1275 * @retval HAL status
1277 HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
1279 if((hspi->State == HAL_SPI_STATE_READY) || \
1280 ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
1282 if((pTxData == HAL_NULL ) || (pRxData == HAL_NULL ) || (Size == 0))
1287 /* Check the parameters */
1288 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1290 /* Process locked */
1293 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1294 if(hspi->State != HAL_SPI_STATE_BUSY_RX)
1296 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
1299 /* Configure communication */
1300 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
1302 hspi->pTxBuffPtr = (uint8_t*)pTxData;
1303 hspi->TxXferSize = Size;
1304 hspi->TxXferCount = Size;
1306 hspi->pRxBuffPtr = (uint8_t*)pRxData;
1307 hspi->RxXferSize = Size;
1308 hspi->RxXferCount = Size;
1310 /*Init field not used in handle to zero */
1314 /* Reset CRC Calculation */
1315 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1317 SPI_RESET_CRC(hspi);
1320 /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
1321 if(hspi->State == HAL_SPI_STATE_BUSY_RX)
1323 /* Set the SPI Rx DMA Half transfer complete callback */
1324 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
1326 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
1330 /* Set the SPI Tx/Rx DMA Half transfer complete callback */
1331 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
1333 hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
1336 /* Set the DMA error callback */
1337 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
1339 /* Enable the Rx DMA Channel */
1340 HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
1342 /* Enable Rx DMA Request */
1343 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1345 /* Set the SPI Tx DMA transfer complete callback as HAL_NULL because the communication closing
1346 is performed in DMA reception complete callback */
1347 if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
1349 /* Set the DMA error callback */
1350 hspi->hdmatx->XferErrorCallback = SPI_DMAError;
1354 hspi->hdmatx->XferErrorCallback = HAL_NULL;
1357 /* Enable the Tx DMA Channel */
1358 HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
1360 /* Check if the SPI is already enabled */
1361 if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
1363 /* Enable SPI peripheral */
1364 __HAL_SPI_ENABLE(hspi);
1367 /* Enable Tx DMA Request */
1368 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1370 /* Process Unlocked */
1383 * @brief Pauses the DMA Transfer.
1384 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1385 * the configuration information for the specified SPI module.
1386 * @retval HAL status
1388 HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
1390 /* Process Locked */
1393 /* Disable the SPI DMA Tx & Rx requests */
1394 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1395 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1397 /* Process Unlocked */
1404 * @brief Resumes the DMA Transfer.
1405 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1406 * the configuration information for the specified SPI module.
1407 * @retval HAL status
1409 HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
1411 /* Process Locked */
1414 /* Enable the SPI DMA Tx & Rx requests */
1415 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1416 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1418 /* Process Unlocked */
1425 * @brief Stops the DMA Transfer.
1426 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1427 * the configuration information for the specified UART module.
1428 * @retval HAL status
1430 HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
1432 /* The Lock is not implemented on this API to allow the user application
1433 to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
1434 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
1435 and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
1438 /* Abort the SPI DMA tx Channel */
1439 if(hspi->hdmatx != HAL_NULL)
1441 HAL_DMA_Abort(hspi->hdmatx);
1443 /* Abort the SPI DMA rx Channel */
1444 if(hspi->hdmarx != HAL_NULL)
1446 HAL_DMA_Abort(hspi->hdmarx);
1449 /* Disable the SPI DMA Tx & Rx requests */
1450 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1451 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1453 hspi->State = HAL_SPI_STATE_READY;
1459 * @brief This function handles SPI interrupt request.
1460 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1461 * the configuration information for SPI module.
1462 * @retval HAL status
1464 void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
1466 /* SPI in mode Receiver and Overrun not occurred ---------------------------*/
1467 if((__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE) != RESET) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE) != RESET) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) == RESET))
1473 /* SPI in mode Tramitter ---------------------------------------------------*/
1474 if((__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE) != RESET) && (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE) != RESET))
1480 if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_ERR) != RESET)
1482 /* SPI CRC error interrupt occurred ---------------------------------------*/
1483 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
1485 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1486 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
1488 /* SPI Mode Fault error interrupt occurred --------------------------------*/
1489 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_MODF) != RESET)
1491 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
1492 __HAL_SPI_CLEAR_MODFFLAG(hspi);
1495 /* SPI Overrun error interrupt occurred -----------------------------------*/
1496 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) != RESET)
1498 if(hspi->State != HAL_SPI_STATE_BUSY_TX)
1500 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
1501 __HAL_SPI_CLEAR_OVRFLAG(hspi);
1505 /* SPI Frame error interrupt occurred -------------------------------------*/
1506 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_FRE) != RESET)
1508 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
1509 __HAL_SPI_CLEAR_FREFLAG(hspi);
1512 /* Call the Error call Back in case of Errors */
1513 if(hspi->ErrorCode!=HAL_SPI_ERROR_NONE)
1515 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
1516 hspi->State = HAL_SPI_STATE_READY;
1517 HAL_SPI_ErrorCallback(hspi);
1523 * @brief Tx Transfer completed callbacks
1524 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1525 * the configuration information for SPI module.
1528 __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
1530 /* NOTE : This function Should not be modified, when the callback is needed,
1531 the HAL_SPI_TxCpltCallback could be implenetd in the user file
1536 * @brief Rx Transfer completed callbacks
1537 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1538 * the configuration information for SPI module.
1541 __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
1543 /* NOTE : This function Should not be modified, when the callback is needed,
1544 the HAL_SPI_RxCpltCallback() could be implenetd in the user file
1549 * @brief Tx and Rx Transfer completed callbacks
1550 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1551 * the configuration information for SPI module.
1554 __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
1556 /* NOTE : This function Should not be modified, when the callback is needed,
1557 the HAL_SPI_TxRxCpltCallback() could be implenetd in the user file
1562 * @brief Tx Half Transfer completed callbacks
1563 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1564 * the configuration information for SPI module.
1567 __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
1569 /* NOTE : This function Should not be modified, when the callback is needed,
1570 the HAL_SPI_TxHalfCpltCallback could be implenetd in the user file
1575 * @brief Rx Half Transfer completed callbacks
1576 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1577 * the configuration information for SPI module.
1580 __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
1582 /* NOTE : This function Should not be modified, when the callback is needed,
1583 the HAL_SPI_RxHalfCpltCallback() could be implenetd in the user file
1588 * @brief Tx and Rx Transfer completed callbacks
1589 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1590 * the configuration information for SPI module.
1593 __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
1595 /* NOTE : This function Should not be modified, when the callback is needed,
1596 the HAL_SPI_TxRxHalfCpltCallback() could be implenetd in the user file
1601 * @brief SPI error callbacks
1602 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1603 * the configuration information for SPI module.
1606 __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
1608 /* NOTE : - This function Should not be modified, when the callback is needed,
1609 the HAL_SPI_ErrorCallback() could be implenetd in the user file.
1610 - The ErrorCode parameter in the hspi handle is updated by the SPI processes
1611 and user can use HAL_SPI_GetError() API to check the latest error occurred.
1619 /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
1620 * @brief SPI control functions
1623 ===============================================================================
1624 ##### Peripheral State and Errors functions #####
1625 ===============================================================================
1627 This subsection provides a set of functions allowing to control the SPI.
1628 (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
1629 (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
1635 * @brief Return the SPI state
1636 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1637 * the configuration information for SPI module.
1640 HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
1646 * @brief Return the SPI error code
1647 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1648 * the configuration information for SPI module.
1649 * @retval SPI Error Code
1651 HAL_SPI_ErrorTypeDef HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
1653 return hspi->ErrorCode;
1666 /** @addtogroup SPI_Private_Functions
1672 * @brief Interrupt Handler to close Tx transfer
1673 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1674 * the configuration information for SPI module.
1677 static void SPI_TxCloseIRQHandler(struct __SPI_HandleTypeDef *hspi)
1679 /* Wait until TXE flag is set to send data */
1680 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
1682 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1685 /* Disable TXE interrupt */
1686 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE ));
1688 /* Disable ERR interrupt if Receive process is finished */
1689 if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE) == RESET)
1691 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_ERR));
1693 /* Wait until Busy flag is reset before disabling SPI */
1694 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
1696 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1699 /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
1700 if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
1702 __HAL_SPI_CLEAR_OVRFLAG(hspi);
1705 /* Check if Errors has been detected during transfer */
1706 if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
1708 /* Check if we are in Tx or in Rx/Tx Mode */
1709 if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
1711 /* Set state to READY before run the Callback Complete */
1712 hspi->State = HAL_SPI_STATE_READY;
1713 HAL_SPI_TxRxCpltCallback(hspi);
1717 /* Set state to READY before run the Callback Complete */
1718 hspi->State = HAL_SPI_STATE_READY;
1719 HAL_SPI_TxCpltCallback(hspi);
1724 /* Set state to READY before run the Callback Complete */
1725 hspi->State = HAL_SPI_STATE_READY;
1726 /* Call Error call back in case of Error */
1727 HAL_SPI_ErrorCallback(hspi);
1733 * @brief Interrupt Handler to transmit amount of data in no-blocking mode
1734 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1735 * the configuration information for SPI module.
1738 static void SPI_TxISR(struct __SPI_HandleTypeDef *hspi)
1740 /* Transmit data in 8 Bit mode */
1741 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
1743 hspi->Instance->DR = (*hspi->pTxBuffPtr++);
1745 /* Transmit data in 16 Bit mode */
1748 hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
1749 hspi->pTxBuffPtr+=2;
1751 hspi->TxXferCount--;
1753 if(hspi->TxXferCount == 0)
1755 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1757 /* calculate and transfer CRC on Tx line */
1758 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1760 SPI_TxCloseIRQHandler(hspi);
1765 * @brief Interrupt Handler to close Rx transfer
1766 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1767 * the configuration information for SPI module.
1770 static void SPI_RxCloseIRQHandler(struct __SPI_HandleTypeDef *hspi)
1772 __IO uint16_t tmpreg = 0;
1774 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1776 /* Wait until RXNE flag is set to send data */
1777 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
1779 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1782 /* Read CRC to reset RXNE flag */
1783 tmpreg = hspi->Instance->DR;
1785 /* Wait until RXNE flag is set to send data */
1786 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
1788 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1791 /* Check if CRC error occurred */
1792 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
1794 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
1796 /* Reset CRC Calculation */
1797 SPI_RESET_CRC(hspi);
1801 /* Disable RXNE interrupt */
1802 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE));
1804 /* if Transmit process is finished */
1805 if(__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE) == RESET)
1807 /* Disable ERR interrupt */
1808 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_ERR));
1810 if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
1812 /* Disable SPI peripheral */
1813 __HAL_SPI_DISABLE(hspi);
1816 /* Check if Errors has been detected during transfer */
1817 if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
1819 /* Check if we are in Rx or in Rx/Tx Mode */
1820 if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
1822 /* Set state to READY before run the Callback Complete */
1823 hspi->State = HAL_SPI_STATE_READY;
1824 HAL_SPI_TxRxCpltCallback(hspi);
1828 /* Set state to READY before run the Callback Complete */
1829 hspi->State = HAL_SPI_STATE_READY;
1830 HAL_SPI_RxCpltCallback(hspi);
1835 /* Set state to READY before run the Callback Complete */
1836 hspi->State = HAL_SPI_STATE_READY;
1837 /* Call Error call back in case of Error */
1838 HAL_SPI_ErrorCallback(hspi);
1844 * @brief Interrupt Handler to receive amount of data in 2Lines mode
1845 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1846 * the configuration information for SPI module.
1849 static void SPI_2LinesRxISR(struct __SPI_HandleTypeDef *hspi)
1851 /* Receive data in 8 Bit mode */
1852 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
1854 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
1856 /* Receive data in 16 Bit mode */
1859 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
1860 hspi->pRxBuffPtr+=2;
1862 hspi->RxXferCount--;
1864 if(hspi->RxXferCount==0)
1866 SPI_RxCloseIRQHandler(hspi);
1871 * @brief Interrupt Handler to receive amount of data in no-blocking mode
1872 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
1873 * the configuration information for SPI module.
1876 static void SPI_RxISR(struct __SPI_HandleTypeDef *hspi)
1878 /* Receive data in 8 Bit mode */
1879 if(hspi->Init.DataSize == SPI_DATASIZE_8BIT)
1881 (*hspi->pRxBuffPtr++) = hspi->Instance->DR;
1883 /* Receive data in 16 Bit mode */
1886 *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
1887 hspi->pRxBuffPtr+=2;
1889 hspi->RxXferCount--;
1891 /* Enable CRC Transmission */
1892 if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED))
1894 /* Set CRC Next to calculate CRC on Rx side */
1895 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
1898 if(hspi->RxXferCount == 0)
1900 SPI_RxCloseIRQHandler(hspi);
1905 * @brief DMA SPI transmit process complete callback
1906 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1907 * the configuration information for the specified DMA module.
1910 static void SPI_DMATransmitCplt(struct __DMA_HandleTypeDef *hdma)
1912 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1914 /* DMA Normal Mode */
1915 if((hdma->Instance->CCR & DMA_CIRCULAR) == 0)
1917 /* Wait until TXE flag is set to send data */
1918 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
1920 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1923 /* Disable Tx DMA Request */
1924 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1926 /* Wait until Busy flag is reset before disabling SPI */
1927 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
1929 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1932 hspi->TxXferCount = 0;
1933 hspi->State = HAL_SPI_STATE_READY;
1936 /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
1937 if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
1939 __HAL_SPI_CLEAR_OVRFLAG(hspi);
1942 /* Check if Errors has been detected during transfer */
1943 if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1945 HAL_SPI_ErrorCallback(hspi);
1949 HAL_SPI_TxCpltCallback(hspi);
1954 * @brief DMA SPI receive process complete callback
1955 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
1956 * the configuration information for the specified DMA module.
1959 static void SPI_DMAReceiveCplt(struct __DMA_HandleTypeDef *hdma)
1961 __IO uint16_t tmpreg = 0;
1963 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1965 /* DMA Normal mode */
1966 if((hdma->Instance->CCR & DMA_CIRCULAR) == 0)
1968 if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
1970 /* Disable SPI peripheral */
1971 __HAL_SPI_DISABLE(hspi);
1974 /* Disable Rx DMA Request */
1975 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
1977 /* Disable Tx DMA Request (done by default to handle the case Master RX direction 2 lines) */
1978 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
1980 /* Reset CRC Calculation */
1981 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
1983 /* Wait until RXNE flag is set to send data */
1984 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
1986 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1990 tmpreg = hspi->Instance->DR;
1992 /* Wait until RXNE flag is set */
1993 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
1995 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1998 /* Check if CRC error occurred */
1999 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
2001 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2002 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
2006 hspi->RxXferCount = 0;
2007 hspi->State = HAL_SPI_STATE_READY;
2009 /* Check if Errors has been detected during transfer */
2010 if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2012 HAL_SPI_ErrorCallback(hspi);
2016 HAL_SPI_RxCpltCallback(hspi);
2021 HAL_SPI_RxCpltCallback(hspi);
2026 * @brief DMA SPI transmit receive process complete callback
2027 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2028 * the configuration information for the specified DMA module.
2031 static void SPI_DMATransmitReceiveCplt(struct __DMA_HandleTypeDef *hdma)
2033 __IO uint16_t tmpreg = 0;
2035 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2037 if((hdma->Instance->CCR & DMA_CIRCULAR) == 0)
2039 /* Reset CRC Calculation */
2040 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2042 /* Check if CRC is done on going (RXNE flag set) */
2043 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_TIMEOUT_VALUE) == HAL_OK)
2045 /* Wait until RXNE flag is set to send data */
2046 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
2048 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
2052 tmpreg = hspi->Instance->DR;
2054 /* Check if CRC error occurred */
2055 if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
2057 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
2058 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
2062 /* Wait until TXE flag is set to send data */
2063 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_TXE, RESET, SPI_TIMEOUT_VALUE) != HAL_OK)
2065 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
2068 /* Disable Tx DMA Request */
2069 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
2071 /* Wait until Busy flag is reset before disabling SPI */
2072 if(SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_BSY, SET, SPI_TIMEOUT_VALUE) != HAL_OK)
2074 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
2077 /* Disable Rx DMA Request */
2078 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
2080 hspi->TxXferCount = 0;
2081 hspi->RxXferCount = 0;
2083 hspi->State = HAL_SPI_STATE_READY;
2085 /* Check if Errors has been detected during transfer */
2086 if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
2088 HAL_SPI_ErrorCallback(hspi);
2092 HAL_SPI_TxRxCpltCallback(hspi);
2097 HAL_SPI_TxRxCpltCallback(hspi);
2102 * @brief DMA SPI half transmit process complete callback
2103 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2104 * the configuration information for the specified DMA module.
2107 static void SPI_DMAHalfTransmitCplt(struct __DMA_HandleTypeDef *hdma)
2109 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2111 HAL_SPI_TxHalfCpltCallback(hspi);
2115 * @brief DMA SPI half receive process complete callback
2116 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2117 * the configuration information for the specified DMA module.
2120 static void SPI_DMAHalfReceiveCplt(struct __DMA_HandleTypeDef *hdma)
2122 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2124 HAL_SPI_RxHalfCpltCallback(hspi);
2128 * @brief DMA SPI Half transmit receive process complete callback
2129 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2130 * the configuration information for the specified DMA module.
2133 static void SPI_DMAHalfTransmitReceiveCplt(struct __DMA_HandleTypeDef *hdma)
2135 SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2137 HAL_SPI_TxRxHalfCpltCallback(hspi);
2141 * @brief DMA SPI communication error callback
2142 * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
2143 * the configuration information for the specified DMA module.
2146 static void SPI_DMAError(DMA_HandleTypeDef *hdma)
2148 SPI_HandleTypeDef* hspi = (SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
2149 hspi->TxXferCount = 0;
2150 hspi->RxXferCount = 0;
2151 hspi->State= HAL_SPI_STATE_READY;
2152 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2153 HAL_SPI_ErrorCallback(hspi);
2157 * @brief This function handles SPI Communication Timeout.
2158 * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
2159 * the configuration information for SPI module.
2160 * @param Flag: SPI flag to check
2161 * @param Status: Flag status to check: RESET or set
2162 * @param Timeout: Timeout duration
2163 * @retval HAL status
2165 static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(struct __SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
2167 uint32_t tickstart = 0;
2170 tickstart = HAL_GetTick();
2172 /* Wait until flag is set */
2175 while(__HAL_SPI_GET_FLAG(hspi, Flag) == RESET)
2177 if(Timeout != HAL_MAX_DELAY)
2179 if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
2181 /* Disable the SPI and reset the CRC: the CRC value should be cleared
2182 on both master and slave sides in order to resynchronize the master
2183 and slave for their respective CRC calculation */
2185 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
2186 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
2188 /* Disable SPI peripheral */
2189 __HAL_SPI_DISABLE(hspi);
2191 /* Reset CRC Calculation */
2192 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2194 SPI_RESET_CRC(hspi);
2197 hspi->State= HAL_SPI_STATE_READY;
2199 /* Process Unlocked */
2209 while(__HAL_SPI_GET_FLAG(hspi, Flag) != RESET)
2211 if(Timeout != HAL_MAX_DELAY)
2213 if((Timeout == 0) || ((HAL_GetTick() - tickstart ) > Timeout))
2215 /* Disable the SPI and reset the CRC: the CRC value should be cleared
2216 on both master and slave sides in order to resynchronize the master
2217 and slave for their respective CRC calculation */
2219 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
2220 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
2222 /* Disable SPI peripheral */
2223 __HAL_SPI_DISABLE(hspi);
2225 /* Reset CRC Calculation */
2226 if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED)
2228 SPI_RESET_CRC(hspi);
2231 hspi->State= HAL_SPI_STATE_READY;
2233 /* Process Unlocked */
2247 #endif /* HAL_SPI_MODULE_ENABLED */
2256 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/