2 ******************************************************************************
\r
3 * @file stm32l4xx_hal_spi.c
\r
4 * @author MCD Application Team
\r
5 * @brief SPI HAL module driver.
\r
6 * This file provides firmware functions to manage the following
\r
7 * functionalities of the Serial Peripheral Interface (SPI) peripheral:
\r
8 * + Initialization and de-initialization functions
\r
9 * + IO operation functions
\r
10 * + Peripheral Control functions
\r
11 * + Peripheral State functions
\r
14 ==============================================================================
\r
15 ##### How to use this driver #####
\r
16 ==============================================================================
\r
18 The SPI HAL driver can be used as follows:
\r
20 (#) Declare a SPI_HandleTypeDef handle structure, for example:
\r
21 SPI_HandleTypeDef hspi;
\r
23 (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
\r
24 (##) Enable the SPIx interface clock
\r
25 (##) SPI pins configuration
\r
26 (+++) Enable the clock for the SPI GPIOs
\r
27 (+++) Configure these SPI pins as alternate function push-pull
\r
28 (##) NVIC configuration if you need to use interrupt process
\r
29 (+++) Configure the SPIx interrupt priority
\r
30 (+++) Enable the NVIC SPI IRQ handle
\r
31 (##) DMA Configuration if you need to use DMA process
\r
32 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel
\r
33 (+++) Enable the DMAx clock
\r
34 (+++) Configure the DMA handle parameters
\r
35 (+++) Configure the DMA Tx or Rx Stream/Channel
\r
36 (+++) Associate the initialized hdma_tx(or _rx) handle to the hspi DMA Tx or Rx handle
\r
37 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream/Channel
\r
39 (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
\r
40 management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
\r
42 (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
\r
43 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
\r
44 by calling the customized HAL_SPI_MspInit() API.
\r
46 Circular mode restriction:
\r
47 (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
\r
48 (##) Master 2Lines RxOnly
\r
49 (##) Master 1Line Rx
\r
50 (#) The CRC feature is not managed when the DMA circular mode is enabled
\r
51 (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
\r
52 the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
\r
54 Master Receive mode restriction:
\r
55 (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=1) or
\r
56 bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI
\r
57 does not initiate a new transfer the following procedure has to be respected:
\r
58 (##) HAL_SPI_DeInit()
\r
61 Callback registration:
\r
63 (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1U
\r
64 allows the user to configure dynamically the driver callbacks.
\r
65 Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback.
\r
67 Function HAL_SPI_RegisterCallback() allows to register following callbacks:
\r
68 (+) TxCpltCallback : SPI Tx Completed callback
\r
69 (+) RxCpltCallback : SPI Rx Completed callback
\r
70 (+) TxRxCpltCallback : SPI TxRx Completed callback
\r
71 (+) TxHalfCpltCallback : SPI Tx Half Completed callback
\r
72 (+) RxHalfCpltCallback : SPI Rx Half Completed callback
\r
73 (+) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
\r
74 (+) ErrorCallback : SPI Error callback
\r
75 (+) AbortCpltCallback : SPI Abort callback
\r
76 (+) MspInitCallback : SPI Msp Init callback
\r
77 (+) MspDeInitCallback : SPI Msp DeInit callback
\r
78 This function takes as parameters the HAL peripheral handle, the Callback ID
\r
79 and a pointer to the user callback function.
\r
82 (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default
\r
84 HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle,
\r
85 and the Callback ID.
\r
86 This function allows to reset following callbacks:
\r
87 (+) TxCpltCallback : SPI Tx Completed callback
\r
88 (+) RxCpltCallback : SPI Rx Completed callback
\r
89 (+) TxRxCpltCallback : SPI TxRx Completed callback
\r
90 (+) TxHalfCpltCallback : SPI Tx Half Completed callback
\r
91 (+) RxHalfCpltCallback : SPI Rx Half Completed callback
\r
92 (+) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
\r
93 (+) ErrorCallback : SPI Error callback
\r
94 (+) AbortCpltCallback : SPI Abort callback
\r
95 (+) MspInitCallback : SPI Msp Init callback
\r
96 (+) MspDeInitCallback : SPI Msp DeInit callback
\r
98 By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET
\r
99 all callbacks are set to the corresponding weak functions:
\r
100 examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback().
\r
101 Exception done for MspInit and MspDeInit functions that are
\r
102 reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when
\r
103 these callbacks are null (not registered beforehand).
\r
104 If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit()
\r
105 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
\r
107 Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only.
\r
108 Exception done MspInit/MspDeInit functions that can be registered/unregistered
\r
109 in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state,
\r
110 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
\r
111 Then, the user first registers the MspInit/MspDeInit user callbacks
\r
112 using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit()
\r
113 or HAL_SPI_Init() function.
\r
115 When The compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or
\r
116 not defined, the callback registering feature is not available
\r
117 and weak (surcharged) callbacks are used.
\r
120 Using the HAL it is not possible to reach all supported SPI frequency with the different SPI Modes,
\r
121 the following table resume the max SPI frequency reached with data size 8bits/16bits,
\r
122 according to frequency of the APBx Peripheral Clock (fPCLK) used by the SPI instance.
\r
128 DataSize = SPI_DATASIZE_8BIT:
\r
129 +----------------------------------------------------------------------------------------------+
\r
130 | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
\r
131 | Process | Tranfert mode |---------------------|----------------------|----------------------|
\r
132 | | | Master | Slave | Master | Slave | Master | Slave |
\r
133 |==============================================================================================|
\r
134 | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
\r
135 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
136 | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
\r
137 | R |----------------|----------|----------|-----------|----------|-----------|----------|
\r
138 | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
\r
139 |=========|================|==========|==========|===========|==========|===========|==========|
\r
140 | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
\r
141 | |----------------|----------|----------|-----------|----------|-----------|----------|
\r
142 | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
\r
143 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
144 | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
\r
145 |=========|================|==========|==========|===========|==========|===========|==========|
\r
146 | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
\r
147 | |----------------|----------|----------|-----------|----------|-----------|----------|
\r
148 | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
\r
149 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
150 | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
\r
151 +----------------------------------------------------------------------------------------------+
\r
153 DataSize = SPI_DATASIZE_16BIT:
\r
154 +----------------------------------------------------------------------------------------------+
\r
155 | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
\r
156 | Process | Tranfert mode |---------------------|----------------------|----------------------|
\r
157 | | | Master | Slave | Master | Slave | Master | Slave |
\r
158 |==============================================================================================|
\r
159 | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
\r
160 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
161 | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
\r
162 | R |----------------|----------|----------|-----------|----------|-----------|----------|
\r
163 | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
\r
164 |=========|================|==========|==========|===========|==========|===========|==========|
\r
165 | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
\r
166 | |----------------|----------|----------|-----------|----------|-----------|----------|
\r
167 | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
\r
168 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
169 | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
\r
170 |=========|================|==========|==========|===========|==========|===========|==========|
\r
171 | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
\r
172 | |----------------|----------|----------|-----------|----------|-----------|----------|
\r
173 | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
\r
174 | X |----------------|----------|----------|-----------|----------|-----------|----------|
\r
175 | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
\r
176 +----------------------------------------------------------------------------------------------+
\r
177 @note The max SPI frequency depend on SPI data size (4bits, 5bits,..., 8bits,...15bits, 16bits),
\r
178 SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA).
\r
180 (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA()
\r
181 (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA()
\r
182 (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA()
\r
184 ******************************************************************************
\r
187 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
\r
188 * All rights reserved.</center></h2>
\r
190 * This software component is licensed by ST under BSD 3-Clause license,
\r
191 * the "License"; You may not use this file except in compliance with the
\r
192 * License. You may obtain a copy of the License at:
\r
193 * opensource.org/licenses/BSD-3-Clause
\r
195 ******************************************************************************
\r
198 /* Includes ------------------------------------------------------------------*/
\r
199 #include "stm32l4xx_hal.h"
\r
201 /** @addtogroup STM32L4xx_HAL_Driver
\r
205 /** @defgroup SPI SPI
\r
206 * @brief SPI HAL module driver
\r
209 #ifdef HAL_SPI_MODULE_ENABLED
\r
211 /* Private typedef -----------------------------------------------------------*/
\r
212 /* Private defines -----------------------------------------------------------*/
\r
213 /** @defgroup SPI_Private_Constants SPI Private Constants
\r
216 #define SPI_DEFAULT_TIMEOUT 100U
\r
221 /* Private macros ------------------------------------------------------------*/
\r
222 /* Private variables ---------------------------------------------------------*/
\r
223 /* Private function prototypes -----------------------------------------------*/
\r
224 /** @defgroup SPI_Private_Functions SPI Private Functions
\r
227 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
\r
228 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
\r
229 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
\r
230 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
\r
231 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
\r
232 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
\r
233 static void SPI_DMAError(DMA_HandleTypeDef *hdma);
\r
234 static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma);
\r
235 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
\r
236 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
\r
237 static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
\r
238 uint32_t Timeout, uint32_t Tickstart);
\r
239 static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State,
\r
240 uint32_t Timeout, uint32_t Tickstart);
\r
241 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
\r
242 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
\r
243 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
\r
244 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
\r
245 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
\r
246 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
\r
247 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
\r
248 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
\r
249 #if (USE_SPI_CRC != 0U)
\r
250 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
\r
251 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
\r
252 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
\r
253 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
\r
254 #endif /* USE_SPI_CRC */
\r
255 static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi);
\r
256 static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi);
\r
257 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
\r
258 static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
\r
259 static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
\r
260 static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
\r
261 static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
\r
266 /* Exported functions --------------------------------------------------------*/
\r
267 /** @defgroup SPI_Exported_Functions SPI Exported Functions
\r
271 /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
\r
272 * @brief Initialization and Configuration functions
\r
275 ===============================================================================
\r
276 ##### Initialization and de-initialization functions #####
\r
277 ===============================================================================
\r
278 [..] This subsection provides a set of functions allowing to initialize and
\r
279 de-initialize the SPIx peripheral:
\r
281 (+) User must implement HAL_SPI_MspInit() function in which he configures
\r
282 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
\r
284 (+) Call the function HAL_SPI_Init() to configure the selected device with
\r
285 the selected configuration:
\r
289 (++) Clock Polarity and Phase
\r
290 (++) NSS Management
\r
291 (++) BaudRate Prescaler
\r
294 (++) CRC Calculation
\r
295 (++) CRC Polynomial if CRC enabled
\r
296 (++) CRC Length, used only with Data8 and Data16
\r
297 (++) FIFO reception threshold
\r
299 (+) Call the function HAL_SPI_DeInit() to restore the default configuration
\r
300 of the selected SPIx peripheral.
\r
307 * @brief Initialize the SPI according to the specified parameters
\r
308 * in the SPI_InitTypeDef and initialize the associated handle.
\r
309 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
310 * the configuration information for SPI module.
\r
311 * @retval HAL status
\r
313 HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
\r
317 /* Check the SPI handle allocation */
\r
323 /* Check the parameters */
\r
324 assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
\r
325 assert_param(IS_SPI_MODE(hspi->Init.Mode));
\r
326 assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
\r
327 assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
\r
328 assert_param(IS_SPI_NSS(hspi->Init.NSS));
\r
329 assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
\r
330 assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
\r
331 assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
\r
332 assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
\r
333 if (hspi->Init.TIMode == SPI_TIMODE_DISABLE)
\r
335 assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
\r
336 assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
\r
338 #if (USE_SPI_CRC != 0U)
\r
339 assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
\r
340 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
342 assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
\r
343 assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
\r
346 hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
\r
347 #endif /* USE_SPI_CRC */
\r
349 if (hspi->State == HAL_SPI_STATE_RESET)
\r
351 /* Allocate lock resource and initialize it */
\r
352 hspi->Lock = HAL_UNLOCKED;
\r
354 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
355 /* Init the SPI Callback settings */
\r
356 hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
\r
357 hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
\r
358 hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
\r
359 hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
\r
360 hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
\r
361 hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
\r
362 hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
\r
363 hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
365 if (hspi->MspInitCallback == NULL)
\r
367 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
\r
370 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
\r
371 hspi->MspInitCallback(hspi);
\r
373 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
\r
374 HAL_SPI_MspInit(hspi);
\r
375 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
378 hspi->State = HAL_SPI_STATE_BUSY;
\r
380 /* Disable the selected SPI peripheral */
\r
381 __HAL_SPI_DISABLE(hspi);
\r
383 /* Align by default the rs fifo threshold on the data size */
\r
384 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
386 frxth = SPI_RXFIFO_THRESHOLD_HF;
\r
390 frxth = SPI_RXFIFO_THRESHOLD_QF;
\r
393 /* CRC calculation is valid only for 16Bit and 8 Bit */
\r
394 if ((hspi->Init.DataSize != SPI_DATASIZE_16BIT) && (hspi->Init.DataSize != SPI_DATASIZE_8BIT))
\r
396 /* CRC must be disabled */
\r
397 hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
\r
400 /* Align the CRC Length on the data size */
\r
401 if (hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
\r
403 /* CRC Length aligned on the data size : value set by default */
\r
404 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
406 hspi->Init.CRCLength = SPI_CRC_LENGTH_16BIT;
\r
410 hspi->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
\r
414 /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
\r
415 /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
\r
416 Communication speed, First bit and CRC calculation state */
\r
417 WRITE_REG(hspi->Instance->CR1, (hspi->Init.Mode | hspi->Init.Direction |
\r
418 hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
\r
419 hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation));
\r
420 #if (USE_SPI_CRC != 0U)
\r
421 /* Configure : CRC Length */
\r
422 if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
\r
424 hspi->Instance->CR1 |= SPI_CR1_CRCL;
\r
426 #endif /* USE_SPI_CRC */
\r
428 /* Configure : NSS management, TI Mode, NSS Pulse, Data size and Rx Fifo threshold */
\r
429 WRITE_REG(hspi->Instance->CR2, (((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE) | hspi->Init.TIMode |
\r
430 hspi->Init.NSSPMode | hspi->Init.DataSize) | frxth);
\r
432 #if (USE_SPI_CRC != 0U)
\r
433 /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
\r
434 /* Configure : CRC Polynomial */
\r
435 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
437 WRITE_REG(hspi->Instance->CRCPR, hspi->Init.CRCPolynomial);
\r
439 #endif /* USE_SPI_CRC */
\r
441 #if defined(SPI_I2SCFGR_I2SMOD)
\r
442 /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
\r
443 CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
\r
444 #endif /* SPI_I2SCFGR_I2SMOD */
\r
446 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
447 hspi->State = HAL_SPI_STATE_READY;
\r
453 * @brief De-Initialize the SPI peripheral.
\r
454 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
455 * the configuration information for SPI module.
\r
456 * @retval HAL status
\r
458 HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
\r
460 /* Check the SPI handle allocation */
\r
466 /* Check SPI Instance parameter */
\r
467 assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
\r
469 hspi->State = HAL_SPI_STATE_BUSY;
\r
471 /* Disable the SPI Peripheral Clock */
\r
472 __HAL_SPI_DISABLE(hspi);
\r
474 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
475 if (hspi->MspDeInitCallback == NULL)
\r
477 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
\r
480 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
\r
481 hspi->MspDeInitCallback(hspi);
\r
483 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
\r
484 HAL_SPI_MspDeInit(hspi);
\r
485 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
487 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
488 hspi->State = HAL_SPI_STATE_RESET;
\r
491 __HAL_UNLOCK(hspi);
\r
497 * @brief Initialize the SPI MSP.
\r
498 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
499 * the configuration information for SPI module.
\r
502 __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
\r
504 /* Prevent unused argument(s) compilation warning */
\r
507 /* NOTE : This function should not be modified, when the callback is needed,
\r
508 the HAL_SPI_MspInit should be implemented in the user file
\r
513 * @brief De-Initialize the SPI MSP.
\r
514 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
515 * the configuration information for SPI module.
\r
518 __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
\r
520 /* Prevent unused argument(s) compilation warning */
\r
523 /* NOTE : This function should not be modified, when the callback is needed,
\r
524 the HAL_SPI_MspDeInit should be implemented in the user file
\r
528 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
530 * @brief Register a User SPI Callback
\r
531 * To be used instead of the weak predefined callback
\r
532 * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
\r
533 * the configuration information for the specified SPI.
\r
534 * @param CallbackID ID of the callback to be registered
\r
535 * @param pCallback pointer to the Callback function
\r
536 * @retval HAL status
\r
538 HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID, pSPI_CallbackTypeDef pCallback)
\r
540 HAL_StatusTypeDef status = HAL_OK;
\r
542 if (pCallback == NULL)
\r
544 /* Update the error code */
\r
545 hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK;
\r
549 /* Process locked */
\r
552 if (HAL_SPI_STATE_READY == hspi->State)
\r
554 switch (CallbackID)
\r
556 case HAL_SPI_TX_COMPLETE_CB_ID :
\r
557 hspi->TxCpltCallback = pCallback;
\r
560 case HAL_SPI_RX_COMPLETE_CB_ID :
\r
561 hspi->RxCpltCallback = pCallback;
\r
564 case HAL_SPI_TX_RX_COMPLETE_CB_ID :
\r
565 hspi->TxRxCpltCallback = pCallback;
\r
568 case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
\r
569 hspi->TxHalfCpltCallback = pCallback;
\r
572 case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
\r
573 hspi->RxHalfCpltCallback = pCallback;
\r
576 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
\r
577 hspi->TxRxHalfCpltCallback = pCallback;
\r
580 case HAL_SPI_ERROR_CB_ID :
\r
581 hspi->ErrorCallback = pCallback;
\r
584 case HAL_SPI_ABORT_CB_ID :
\r
585 hspi->AbortCpltCallback = pCallback;
\r
588 case HAL_SPI_MSPINIT_CB_ID :
\r
589 hspi->MspInitCallback = pCallback;
\r
592 case HAL_SPI_MSPDEINIT_CB_ID :
\r
593 hspi->MspDeInitCallback = pCallback;
\r
597 /* Update the error code */
\r
598 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
600 /* Return error status */
\r
601 status = HAL_ERROR;
\r
605 else if (HAL_SPI_STATE_RESET == hspi->State)
\r
607 switch (CallbackID)
\r
609 case HAL_SPI_MSPINIT_CB_ID :
\r
610 hspi->MspInitCallback = pCallback;
\r
613 case HAL_SPI_MSPDEINIT_CB_ID :
\r
614 hspi->MspDeInitCallback = pCallback;
\r
618 /* Update the error code */
\r
619 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
621 /* Return error status */
\r
622 status = HAL_ERROR;
\r
628 /* Update the error code */
\r
629 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
631 /* Return error status */
\r
632 status = HAL_ERROR;
\r
636 __HAL_UNLOCK(hspi);
\r
641 * @brief Unregister an SPI Callback
\r
642 * SPI callback is redirected to the weak predefined callback
\r
643 * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
\r
644 * the configuration information for the specified SPI.
\r
645 * @param CallbackID ID of the callback to be unregistered
\r
646 * @retval HAL status
\r
648 HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID)
\r
650 HAL_StatusTypeDef status = HAL_OK;
\r
652 /* Process locked */
\r
655 if (HAL_SPI_STATE_READY == hspi->State)
\r
657 switch (CallbackID)
\r
659 case HAL_SPI_TX_COMPLETE_CB_ID :
\r
660 hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
\r
663 case HAL_SPI_RX_COMPLETE_CB_ID :
\r
664 hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
\r
667 case HAL_SPI_TX_RX_COMPLETE_CB_ID :
\r
668 hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
\r
671 case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
\r
672 hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
\r
675 case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
\r
676 hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
\r
679 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
\r
680 hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
\r
683 case HAL_SPI_ERROR_CB_ID :
\r
684 hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
\r
687 case HAL_SPI_ABORT_CB_ID :
\r
688 hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
\r
691 case HAL_SPI_MSPINIT_CB_ID :
\r
692 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
\r
695 case HAL_SPI_MSPDEINIT_CB_ID :
\r
696 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
\r
700 /* Update the error code */
\r
701 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
703 /* Return error status */
\r
704 status = HAL_ERROR;
\r
708 else if (HAL_SPI_STATE_RESET == hspi->State)
\r
710 switch (CallbackID)
\r
712 case HAL_SPI_MSPINIT_CB_ID :
\r
713 hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
\r
716 case HAL_SPI_MSPDEINIT_CB_ID :
\r
717 hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
\r
721 /* Update the error code */
\r
722 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
724 /* Return error status */
\r
725 status = HAL_ERROR;
\r
731 /* Update the error code */
\r
732 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
\r
734 /* Return error status */
\r
735 status = HAL_ERROR;
\r
739 __HAL_UNLOCK(hspi);
\r
742 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
747 /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
\r
748 * @brief Data transfers functions
\r
751 ==============================================================================
\r
752 ##### IO operation functions #####
\r
753 ===============================================================================
\r
755 This subsection provides a set of functions allowing to manage the SPI
\r
758 [..] The SPI supports master and slave mode :
\r
760 (#) There are two modes of transfer:
\r
761 (++) Blocking mode: The communication is performed in polling mode.
\r
762 The HAL status of all data processing is returned by the same function
\r
763 after finishing transfer.
\r
764 (++) No-Blocking mode: The communication is performed using Interrupts
\r
765 or DMA, These APIs return the HAL status.
\r
766 The end of the data processing will be indicated through the
\r
767 dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
\r
769 The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
\r
770 will be executed respectively at the end of the transmit or Receive process
\r
771 The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
\r
773 (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
\r
774 exist for 1Line (simplex) and 2Lines (full duplex) modes.
\r
781 * @brief Transmit an amount of data in blocking mode.
\r
782 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
783 * the configuration information for SPI module.
\r
784 * @param pData pointer to data buffer
\r
785 * @param Size amount of data to be sent
\r
786 * @param Timeout Timeout duration
\r
787 * @retval HAL status
\r
789 HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
791 uint32_t tickstart;
\r
792 HAL_StatusTypeDef errorcode = HAL_OK;
\r
793 uint16_t initial_TxXferCount;
\r
795 /* Check Direction parameter */
\r
796 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
\r
798 /* Process Locked */
\r
801 /* Init tickstart for timeout management*/
\r
802 tickstart = HAL_GetTick();
\r
803 initial_TxXferCount = Size;
\r
805 if (hspi->State != HAL_SPI_STATE_READY)
\r
807 errorcode = HAL_BUSY;
\r
811 if ((pData == NULL) || (Size == 0U))
\r
813 errorcode = HAL_ERROR;
\r
817 /* Set the transaction information */
\r
818 hspi->State = HAL_SPI_STATE_BUSY_TX;
\r
819 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
820 hspi->pTxBuffPtr = (uint8_t *)pData;
\r
821 hspi->TxXferSize = Size;
\r
822 hspi->TxXferCount = Size;
\r
824 /*Init field not used in handle to zero */
\r
825 hspi->pRxBuffPtr = (uint8_t *)NULL;
\r
826 hspi->RxXferSize = 0U;
\r
827 hspi->RxXferCount = 0U;
\r
828 hspi->TxISR = NULL;
\r
829 hspi->RxISR = NULL;
\r
831 /* Configure communication direction : 1Line */
\r
832 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
834 SPI_1LINE_TX(hspi);
\r
837 #if (USE_SPI_CRC != 0U)
\r
838 /* Reset CRC Calculation */
\r
839 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
841 SPI_RESET_CRC(hspi);
\r
843 #endif /* USE_SPI_CRC */
\r
845 /* Check if the SPI is already enabled */
\r
846 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
848 /* Enable SPI peripheral */
\r
849 __HAL_SPI_ENABLE(hspi);
\r
852 /* Transmit data in 16 Bit mode */
\r
853 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
855 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
\r
857 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
858 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
859 hspi->TxXferCount--;
\r
861 /* Transmit data in 16 Bit mode */
\r
862 while (hspi->TxXferCount > 0U)
\r
864 /* Wait until TXE flag is set to send data */
\r
865 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
\r
867 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
868 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
869 hspi->TxXferCount--;
\r
873 /* Timeout management */
\r
874 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
\r
876 errorcode = HAL_TIMEOUT;
\r
882 /* Transmit data in 8 Bit mode */
\r
885 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
\r
887 if (hspi->TxXferCount > 1U)
\r
889 /* write on the data register in packing mode */
\r
890 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
891 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
892 hspi->TxXferCount -= 2U;
\r
896 *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
\r
897 hspi->pTxBuffPtr ++;
\r
898 hspi->TxXferCount--;
\r
901 while (hspi->TxXferCount > 0U)
\r
903 /* Wait until TXE flag is set to send data */
\r
904 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
\r
906 if (hspi->TxXferCount > 1U)
\r
908 /* write on the data register in packing mode */
\r
909 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
910 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
911 hspi->TxXferCount -= 2U;
\r
915 *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
\r
916 hspi->pTxBuffPtr++;
\r
917 hspi->TxXferCount--;
\r
922 /* Timeout management */
\r
923 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
\r
925 errorcode = HAL_TIMEOUT;
\r
931 #if (USE_SPI_CRC != 0U)
\r
932 /* Enable CRC Transmission */
\r
933 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
935 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
937 #endif /* USE_SPI_CRC */
\r
939 /* Check the end of the transaction */
\r
940 if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
\r
942 hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
\r
945 /* Clear overrun flag in 2 Lines communication mode because received is not read */
\r
946 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
\r
948 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
951 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
953 errorcode = HAL_ERROR;
\r
957 hspi->State = HAL_SPI_STATE_READY;
\r
958 /* Process Unlocked */
\r
959 __HAL_UNLOCK(hspi);
\r
964 * @brief Receive an amount of data in blocking mode.
\r
965 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
966 * the configuration information for SPI module.
\r
967 * @param pData pointer to data buffer
\r
968 * @param Size amount of data to be received
\r
969 * @param Timeout Timeout duration
\r
970 * @retval HAL status
\r
972 HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
\r
974 uint32_t tickstart;
\r
975 HAL_StatusTypeDef errorcode = HAL_OK;
\r
977 if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
\r
979 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
980 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
\r
981 return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
\r
984 /* Process Locked */
\r
987 /* Init tickstart for timeout management*/
\r
988 tickstart = HAL_GetTick();
\r
990 if (hspi->State != HAL_SPI_STATE_READY)
\r
992 errorcode = HAL_BUSY;
\r
996 if ((pData == NULL) || (Size == 0U))
\r
998 errorcode = HAL_ERROR;
\r
1002 /* Set the transaction information */
\r
1003 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
1004 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1005 hspi->pRxBuffPtr = (uint8_t *)pData;
\r
1006 hspi->RxXferSize = Size;
\r
1007 hspi->RxXferCount = Size;
\r
1009 /*Init field not used in handle to zero */
\r
1010 hspi->pTxBuffPtr = (uint8_t *)NULL;
\r
1011 hspi->TxXferSize = 0U;
\r
1012 hspi->TxXferCount = 0U;
\r
1013 hspi->RxISR = NULL;
\r
1014 hspi->TxISR = NULL;
\r
1016 #if (USE_SPI_CRC != 0U)
\r
1017 /* Reset CRC Calculation */
\r
1018 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1020 SPI_RESET_CRC(hspi);
\r
1021 /* this is done to handle the CRCNEXT before the latest data */
\r
1022 hspi->RxXferCount--;
\r
1024 #endif /* USE_SPI_CRC */
\r
1026 /* Set the Rx Fifo threshold */
\r
1027 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1029 /* Set RX Fifo threshold according the reception data length: 16bit */
\r
1030 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1034 /* Set RX Fifo threshold according the reception data length: 8bit */
\r
1035 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1038 /* Configure communication direction: 1Line */
\r
1039 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
1041 SPI_1LINE_RX(hspi);
\r
1044 /* Check if the SPI is already enabled */
\r
1045 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1047 /* Enable SPI peripheral */
\r
1048 __HAL_SPI_ENABLE(hspi);
\r
1051 /* Receive data in 8 Bit mode */
\r
1052 if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
\r
1054 /* Transfer loop */
\r
1055 while (hspi->RxXferCount > 0U)
\r
1057 /* Check the RXNE flag */
\r
1058 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
\r
1060 /* read the received data */
\r
1061 (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
\r
1062 hspi->pRxBuffPtr += sizeof(uint8_t);
\r
1063 hspi->RxXferCount--;
\r
1067 /* Timeout management */
\r
1068 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
\r
1070 errorcode = HAL_TIMEOUT;
\r
1078 /* Transfer loop */
\r
1079 while (hspi->RxXferCount > 0U)
\r
1081 /* Check the RXNE flag */
\r
1082 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
\r
1084 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
\r
1085 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
1086 hspi->RxXferCount--;
\r
1090 /* Timeout management */
\r
1091 if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
\r
1093 errorcode = HAL_TIMEOUT;
\r
1100 #if (USE_SPI_CRC != 0U)
\r
1101 /* Handle the CRC Transmission */
\r
1102 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1104 /* freeze the CRC before the latest data */
\r
1105 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
1107 /* Read the latest data */
\r
1108 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
\r
1110 /* the latest data has not been received */
\r
1111 errorcode = HAL_TIMEOUT;
\r
1115 /* Receive last data in 16 Bit mode */
\r
1116 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1118 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
\r
1120 /* Receive last data in 8 Bit mode */
\r
1123 (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
\r
1126 /* Wait the CRC data */
\r
1127 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
\r
1129 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1130 errorcode = HAL_TIMEOUT;
\r
1134 /* Read CRC to Flush DR and RXNE flag */
\r
1135 if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
\r
1137 /* Read 16bit CRC */
\r
1138 READ_REG(hspi->Instance->DR);
\r
1142 /* Read 8bit CRC */
\r
1143 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
1145 if ((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
\r
1147 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
\r
1149 /* Error on the CRC reception */
\r
1150 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1151 errorcode = HAL_TIMEOUT;
\r
1154 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
\r
1155 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
1159 #endif /* USE_SPI_CRC */
\r
1161 /* Check the end of the transaction */
\r
1162 if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)
\r
1164 hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
\r
1167 #if (USE_SPI_CRC != 0U)
\r
1168 /* Check if CRC error occurred */
\r
1169 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
\r
1171 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1172 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
1174 #endif /* USE_SPI_CRC */
\r
1176 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
1178 errorcode = HAL_ERROR;
\r
1182 hspi->State = HAL_SPI_STATE_READY;
\r
1183 __HAL_UNLOCK(hspi);
\r
1188 * @brief Transmit and Receive an amount of data in blocking mode.
\r
1189 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1190 * the configuration information for SPI module.
\r
1191 * @param pTxData pointer to transmission data buffer
\r
1192 * @param pRxData pointer to reception data buffer
\r
1193 * @param Size amount of data to be sent and received
\r
1194 * @param Timeout Timeout duration
\r
1195 * @retval HAL status
\r
1197 HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
\r
1200 uint16_t initial_TxXferCount;
\r
1201 uint16_t initial_RxXferCount;
\r
1202 uint32_t tmp_mode;
\r
1203 HAL_SPI_StateTypeDef tmp_state;
\r
1204 uint32_t tickstart;
\r
1205 #if (USE_SPI_CRC != 0U)
\r
1208 #endif /* USE_SPI_CRC */
\r
1210 /* Variable used to alternate Rx and Tx during transfer */
\r
1211 uint32_t txallowed = 1U;
\r
1212 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1214 /* Check Direction parameter */
\r
1215 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
\r
1217 /* Process Locked */
\r
1220 /* Init tickstart for timeout management*/
\r
1221 tickstart = HAL_GetTick();
\r
1223 /* Init temporary variables */
\r
1224 tmp_state = hspi->State;
\r
1225 tmp_mode = hspi->Init.Mode;
\r
1226 initial_TxXferCount = Size;
\r
1227 initial_RxXferCount = Size;
\r
1228 #if (USE_SPI_CRC != 0U)
\r
1229 spi_cr1 = READ_REG(hspi->Instance->CR1);
\r
1230 spi_cr2 = READ_REG(hspi->Instance->CR2);
\r
1231 #endif /* USE_SPI_CRC */
\r
1233 if (!((tmp_state == HAL_SPI_STATE_READY) || \
\r
1234 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
\r
1236 errorcode = HAL_BUSY;
\r
1240 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
\r
1242 errorcode = HAL_ERROR;
\r
1246 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
\r
1247 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
\r
1249 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
\r
1252 /* Set the transaction information */
\r
1253 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1254 hspi->pRxBuffPtr = (uint8_t *)pRxData;
\r
1255 hspi->RxXferCount = Size;
\r
1256 hspi->RxXferSize = Size;
\r
1257 hspi->pTxBuffPtr = (uint8_t *)pTxData;
\r
1258 hspi->TxXferCount = Size;
\r
1259 hspi->TxXferSize = Size;
\r
1261 /*Init field not used in handle to zero */
\r
1262 hspi->RxISR = NULL;
\r
1263 hspi->TxISR = NULL;
\r
1265 #if (USE_SPI_CRC != 0U)
\r
1266 /* Reset CRC Calculation */
\r
1267 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1269 SPI_RESET_CRC(hspi);
\r
1271 #endif /* USE_SPI_CRC */
\r
1273 /* Set the Rx Fifo threshold */
\r
1274 if ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (initial_RxXferCount > 1U))
\r
1276 /* Set fiforxthreshold according the reception data length: 16bit */
\r
1277 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1281 /* Set fiforxthreshold according the reception data length: 8bit */
\r
1282 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1285 /* Check if the SPI is already enabled */
\r
1286 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1288 /* Enable SPI peripheral */
\r
1289 __HAL_SPI_ENABLE(hspi);
\r
1292 /* Transmit and Receive data in 16 Bit mode */
\r
1293 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1295 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
\r
1297 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
1298 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
1299 hspi->TxXferCount--;
\r
1301 while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
\r
1303 /* Check TXE flag */
\r
1304 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
\r
1306 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
1307 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
1308 hspi->TxXferCount--;
\r
1309 /* Next Data is a reception (Rx). Tx not allowed */
\r
1312 #if (USE_SPI_CRC != 0U)
\r
1313 /* Enable CRC Transmission */
\r
1314 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
\r
1316 /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
\r
1317 if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
\r
1319 SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
\r
1321 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
1323 #endif /* USE_SPI_CRC */
\r
1326 /* Check RXNE flag */
\r
1327 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
\r
1329 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
\r
1330 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
1331 hspi->RxXferCount--;
\r
1332 /* Next Data is a Transmission (Tx). Tx is allowed */
\r
1335 if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY))
\r
1337 errorcode = HAL_TIMEOUT;
\r
1342 /* Transmit and Receive data in 8 Bit mode */
\r
1345 if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
\r
1347 if (hspi->TxXferCount > 1U)
\r
1349 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
1350 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
1351 hspi->TxXferCount -= 2U;
\r
1355 *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
\r
1356 hspi->pTxBuffPtr++;
\r
1357 hspi->TxXferCount--;
\r
1360 while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
\r
1362 /* Check TXE flag */
\r
1363 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
\r
1365 if (hspi->TxXferCount > 1U)
\r
1367 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
1368 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
1369 hspi->TxXferCount -= 2U;
\r
1373 *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
\r
1374 hspi->pTxBuffPtr++;
\r
1375 hspi->TxXferCount--;
\r
1377 /* Next Data is a reception (Rx). Tx not allowed */
\r
1380 #if (USE_SPI_CRC != 0U)
\r
1381 /* Enable CRC Transmission */
\r
1382 if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
\r
1384 /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
\r
1385 if ((READ_BIT(spi_cr1, SPI_CR1_MSTR) == 0U) && (READ_BIT(spi_cr2, SPI_CR2_NSSP) == SPI_CR2_NSSP))
\r
1387 SET_BIT(hspi->Instance->CR1, SPI_CR1_SSM);
\r
1389 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
1391 #endif /* USE_SPI_CRC */
\r
1394 /* Wait until RXNE flag is reset */
\r
1395 if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
\r
1397 if (hspi->RxXferCount > 1U)
\r
1399 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
\r
1400 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
1401 hspi->RxXferCount -= 2U;
\r
1402 if (hspi->RxXferCount <= 1U)
\r
1404 /* Set RX Fifo threshold before to switch on 8 bit data size */
\r
1405 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1410 (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
\r
1411 hspi->pRxBuffPtr++;
\r
1412 hspi->RxXferCount--;
\r
1414 /* Next Data is a Transmission (Tx). Tx is allowed */
\r
1417 if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
\r
1419 errorcode = HAL_TIMEOUT;
\r
1425 #if (USE_SPI_CRC != 0U)
\r
1426 /* Read CRC from DR to close CRC calculation process */
\r
1427 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1429 /* Wait until TXE flag */
\r
1430 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
\r
1432 /* Error on the CRC reception */
\r
1433 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1434 errorcode = HAL_TIMEOUT;
\r
1438 if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
\r
1440 /* Read 16bit CRC */
\r
1441 READ_REG(hspi->Instance->DR);
\r
1445 /* Read 8bit CRC */
\r
1446 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
1448 if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
\r
1450 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
\r
1452 /* Error on the CRC reception */
\r
1453 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1454 errorcode = HAL_TIMEOUT;
\r
1457 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
\r
1458 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
1463 /* Check if CRC error occurred */
\r
1464 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
\r
1466 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
1467 /* Clear CRC Flag */
\r
1468 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
1470 errorcode = HAL_ERROR;
\r
1472 #endif /* USE_SPI_CRC */
\r
1474 /* Check the end of the transaction */
\r
1475 if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
\r
1477 errorcode = HAL_ERROR;
\r
1478 hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
\r
1482 hspi->State = HAL_SPI_STATE_READY;
\r
1483 __HAL_UNLOCK(hspi);
\r
1488 * @brief Transmit an amount of data in non-blocking mode with Interrupt.
\r
1489 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1490 * the configuration information for SPI module.
\r
1491 * @param pData pointer to data buffer
\r
1492 * @param Size amount of data to be sent
\r
1493 * @retval HAL status
\r
1495 HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
\r
1497 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1499 /* Check Direction parameter */
\r
1500 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
\r
1502 /* Process Locked */
\r
1505 if ((pData == NULL) || (Size == 0U))
\r
1507 errorcode = HAL_ERROR;
\r
1511 if (hspi->State != HAL_SPI_STATE_READY)
\r
1513 errorcode = HAL_BUSY;
\r
1517 /* Set the transaction information */
\r
1518 hspi->State = HAL_SPI_STATE_BUSY_TX;
\r
1519 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1520 hspi->pTxBuffPtr = (uint8_t *)pData;
\r
1521 hspi->TxXferSize = Size;
\r
1522 hspi->TxXferCount = Size;
\r
1524 /* Init field not used in handle to zero */
\r
1525 hspi->pRxBuffPtr = (uint8_t *)NULL;
\r
1526 hspi->RxXferSize = 0U;
\r
1527 hspi->RxXferCount = 0U;
\r
1528 hspi->RxISR = NULL;
\r
1530 /* Set the function for IT treatment */
\r
1531 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1533 hspi->TxISR = SPI_TxISR_16BIT;
\r
1537 hspi->TxISR = SPI_TxISR_8BIT;
\r
1540 /* Configure communication direction : 1Line */
\r
1541 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
1543 SPI_1LINE_TX(hspi);
\r
1546 #if (USE_SPI_CRC != 0U)
\r
1547 /* Reset CRC Calculation */
\r
1548 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1550 SPI_RESET_CRC(hspi);
\r
1552 #endif /* USE_SPI_CRC */
\r
1554 /* Enable TXE and ERR interrupt */
\r
1555 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
\r
1558 /* Check if the SPI is already enabled */
\r
1559 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1561 /* Enable SPI peripheral */
\r
1562 __HAL_SPI_ENABLE(hspi);
\r
1566 __HAL_UNLOCK(hspi);
\r
1571 * @brief Receive an amount of data in non-blocking mode with Interrupt.
\r
1572 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1573 * the configuration information for SPI module.
\r
1574 * @param pData pointer to data buffer
\r
1575 * @param Size amount of data to be sent
\r
1576 * @retval HAL status
\r
1578 HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
\r
1580 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1582 if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
\r
1584 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
1585 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
\r
1586 return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
\r
1589 /* Process Locked */
\r
1592 if (hspi->State != HAL_SPI_STATE_READY)
\r
1594 errorcode = HAL_BUSY;
\r
1598 if ((pData == NULL) || (Size == 0U))
\r
1600 errorcode = HAL_ERROR;
\r
1604 /* Set the transaction information */
\r
1605 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
1606 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1607 hspi->pRxBuffPtr = (uint8_t *)pData;
\r
1608 hspi->RxXferSize = Size;
\r
1609 hspi->RxXferCount = Size;
\r
1611 /* Init field not used in handle to zero */
\r
1612 hspi->pTxBuffPtr = (uint8_t *)NULL;
\r
1613 hspi->TxXferSize = 0U;
\r
1614 hspi->TxXferCount = 0U;
\r
1615 hspi->TxISR = NULL;
\r
1617 /* Check the data size to adapt Rx threshold and the set the function for IT treatment */
\r
1618 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1620 /* Set RX Fifo threshold according the reception data length: 16 bit */
\r
1621 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1622 hspi->RxISR = SPI_RxISR_16BIT;
\r
1626 /* Set RX Fifo threshold according the reception data length: 8 bit */
\r
1627 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1628 hspi->RxISR = SPI_RxISR_8BIT;
\r
1631 /* Configure communication direction : 1Line */
\r
1632 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
1634 SPI_1LINE_RX(hspi);
\r
1637 #if (USE_SPI_CRC != 0U)
\r
1638 /* Reset CRC Calculation */
\r
1639 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1641 hspi->CRCSize = 1U;
\r
1642 if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
\r
1644 hspi->CRCSize = 2U;
\r
1646 SPI_RESET_CRC(hspi);
\r
1650 hspi->CRCSize = 0U;
\r
1652 #endif /* USE_SPI_CRC */
\r
1654 /* Enable TXE and ERR interrupt */
\r
1655 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
\r
1657 /* Note : The SPI must be enabled after unlocking current process
\r
1658 to avoid the risk of SPI interrupt handle execution before current
\r
1661 /* Check if the SPI is already enabled */
\r
1662 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1664 /* Enable SPI peripheral */
\r
1665 __HAL_SPI_ENABLE(hspi);
\r
1669 /* Process Unlocked */
\r
1670 __HAL_UNLOCK(hspi);
\r
1675 * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt.
\r
1676 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1677 * the configuration information for SPI module.
\r
1678 * @param pTxData pointer to transmission data buffer
\r
1679 * @param pRxData pointer to reception data buffer
\r
1680 * @param Size amount of data to be sent and received
\r
1681 * @retval HAL status
\r
1683 HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
\r
1685 uint32_t tmp_mode;
\r
1686 HAL_SPI_StateTypeDef tmp_state;
\r
1687 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1689 /* Check Direction parameter */
\r
1690 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
\r
1692 /* Process locked */
\r
1695 /* Init temporary variables */
\r
1696 tmp_state = hspi->State;
\r
1697 tmp_mode = hspi->Init.Mode;
\r
1699 if (!((tmp_state == HAL_SPI_STATE_READY) || \
\r
1700 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
\r
1702 errorcode = HAL_BUSY;
\r
1706 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
\r
1708 errorcode = HAL_ERROR;
\r
1712 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
\r
1713 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
\r
1715 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
\r
1718 /* Set the transaction information */
\r
1719 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1720 hspi->pTxBuffPtr = (uint8_t *)pTxData;
\r
1721 hspi->TxXferSize = Size;
\r
1722 hspi->TxXferCount = Size;
\r
1723 hspi->pRxBuffPtr = (uint8_t *)pRxData;
\r
1724 hspi->RxXferSize = Size;
\r
1725 hspi->RxXferCount = Size;
\r
1727 /* Set the function for IT treatment */
\r
1728 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1730 hspi->RxISR = SPI_2linesRxISR_16BIT;
\r
1731 hspi->TxISR = SPI_2linesTxISR_16BIT;
\r
1735 hspi->RxISR = SPI_2linesRxISR_8BIT;
\r
1736 hspi->TxISR = SPI_2linesTxISR_8BIT;
\r
1739 #if (USE_SPI_CRC != 0U)
\r
1740 /* Reset CRC Calculation */
\r
1741 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1743 hspi->CRCSize = 1U;
\r
1744 if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
\r
1746 hspi->CRCSize = 2U;
\r
1748 SPI_RESET_CRC(hspi);
\r
1752 hspi->CRCSize = 0U;
\r
1754 #endif /* USE_SPI_CRC */
\r
1756 /* Check if packing mode is enabled and if there is more than 2 data to receive */
\r
1757 if ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (Size >= 2U))
\r
1759 /* Set RX Fifo threshold according the reception data length: 16 bit */
\r
1760 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1764 /* Set RX Fifo threshold according the reception data length: 8 bit */
\r
1765 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1768 /* Enable TXE, RXNE and ERR interrupt */
\r
1769 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
\r
1771 /* Check if the SPI is already enabled */
\r
1772 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1774 /* Enable SPI peripheral */
\r
1775 __HAL_SPI_ENABLE(hspi);
\r
1779 /* Process Unlocked */
\r
1780 __HAL_UNLOCK(hspi);
\r
1785 * @brief Transmit an amount of data in non-blocking mode with DMA.
\r
1786 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1787 * the configuration information for SPI module.
\r
1788 * @param pData pointer to data buffer
\r
1789 * @param Size amount of data to be sent
\r
1790 * @retval HAL status
\r
1792 HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
\r
1794 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1796 /* Check tx dma handle */
\r
1797 assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
\r
1799 /* Check Direction parameter */
\r
1800 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
\r
1802 /* Process Locked */
\r
1805 if (hspi->State != HAL_SPI_STATE_READY)
\r
1807 errorcode = HAL_BUSY;
\r
1811 if ((pData == NULL) || (Size == 0U))
\r
1813 errorcode = HAL_ERROR;
\r
1817 /* Set the transaction information */
\r
1818 hspi->State = HAL_SPI_STATE_BUSY_TX;
\r
1819 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1820 hspi->pTxBuffPtr = (uint8_t *)pData;
\r
1821 hspi->TxXferSize = Size;
\r
1822 hspi->TxXferCount = Size;
\r
1824 /* Init field not used in handle to zero */
\r
1825 hspi->pRxBuffPtr = (uint8_t *)NULL;
\r
1826 hspi->TxISR = NULL;
\r
1827 hspi->RxISR = NULL;
\r
1828 hspi->RxXferSize = 0U;
\r
1829 hspi->RxXferCount = 0U;
\r
1831 /* Configure communication direction : 1Line */
\r
1832 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
1834 SPI_1LINE_TX(hspi);
\r
1837 #if (USE_SPI_CRC != 0U)
\r
1838 /* Reset CRC Calculation */
\r
1839 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1841 SPI_RESET_CRC(hspi);
\r
1843 #endif /* USE_SPI_CRC */
\r
1845 /* Set the SPI TxDMA Half transfer complete callback */
\r
1846 hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
\r
1848 /* Set the SPI TxDMA transfer complete callback */
\r
1849 hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
\r
1851 /* Set the DMA error callback */
\r
1852 hspi->hdmatx->XferErrorCallback = SPI_DMAError;
\r
1854 /* Set the DMA AbortCpltCallback */
\r
1855 hspi->hdmatx->XferAbortCallback = NULL;
\r
1857 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
\r
1858 /* Packing mode is enabled only if the DMA setting is HALWORD */
\r
1859 if ((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
\r
1861 /* Check the even/odd of the data size + crc if enabled */
\r
1862 if ((hspi->TxXferCount & 0x1U) == 0U)
\r
1864 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
\r
1865 hspi->TxXferCount = (hspi->TxXferCount >> 1U);
\r
1869 SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
\r
1870 hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
\r
1874 /* Enable the Tx DMA Stream/Channel */
\r
1875 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount))
\r
1877 /* Update SPI error code */
\r
1878 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
1879 errorcode = HAL_ERROR;
\r
1881 hspi->State = HAL_SPI_STATE_READY;
\r
1885 /* Check if the SPI is already enabled */
\r
1886 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
1888 /* Enable SPI peripheral */
\r
1889 __HAL_SPI_ENABLE(hspi);
\r
1892 /* Enable the SPI Error Interrupt Bit */
\r
1893 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
\r
1895 /* Enable Tx DMA Request */
\r
1896 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
\r
1899 /* Process Unlocked */
\r
1900 __HAL_UNLOCK(hspi);
\r
1905 * @brief Receive an amount of data in non-blocking mode with DMA.
\r
1906 * @note In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined.
\r
1907 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
1908 * the configuration information for SPI module.
\r
1909 * @param pData pointer to data buffer
\r
1910 * @note When the CRC feature is enabled the pData Length must be Size + 1.
\r
1911 * @param Size amount of data to be sent
\r
1912 * @retval HAL status
\r
1914 HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
\r
1916 HAL_StatusTypeDef errorcode = HAL_OK;
\r
1918 /* Check rx dma handle */
\r
1919 assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
\r
1921 if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
\r
1923 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
1925 /* Check tx dma handle */
\r
1926 assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
\r
1928 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
\r
1929 return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
\r
1932 /* Process Locked */
\r
1935 if (hspi->State != HAL_SPI_STATE_READY)
\r
1937 errorcode = HAL_BUSY;
\r
1941 if ((pData == NULL) || (Size == 0U))
\r
1943 errorcode = HAL_ERROR;
\r
1947 /* Set the transaction information */
\r
1948 hspi->State = HAL_SPI_STATE_BUSY_RX;
\r
1949 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
1950 hspi->pRxBuffPtr = (uint8_t *)pData;
\r
1951 hspi->RxXferSize = Size;
\r
1952 hspi->RxXferCount = Size;
\r
1954 /*Init field not used in handle to zero */
\r
1955 hspi->RxISR = NULL;
\r
1956 hspi->TxISR = NULL;
\r
1957 hspi->TxXferSize = 0U;
\r
1958 hspi->TxXferCount = 0U;
\r
1960 /* Configure communication direction : 1Line */
\r
1961 if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
1963 SPI_1LINE_RX(hspi);
\r
1966 #if (USE_SPI_CRC != 0U)
\r
1967 /* Reset CRC Calculation */
\r
1968 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
1970 SPI_RESET_CRC(hspi);
\r
1972 #endif /* USE_SPI_CRC */
\r
1975 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
\r
1976 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
1978 /* Set RX Fifo threshold according the reception data length: 16bit */
\r
1979 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1983 /* Set RX Fifo threshold according the reception data length: 8bit */
\r
1984 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1986 if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
\r
1988 /* Set RX Fifo threshold according the reception data length: 16bit */
\r
1989 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
1991 if ((hspi->RxXferCount & 0x1U) == 0x0U)
\r
1993 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
\r
1994 hspi->RxXferCount = hspi->RxXferCount >> 1U;
\r
1998 SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
\r
1999 hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
\r
2004 /* Set the SPI RxDMA Half transfer complete callback */
\r
2005 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
\r
2007 /* Set the SPI Rx DMA transfer complete callback */
\r
2008 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
\r
2010 /* Set the DMA error callback */
\r
2011 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
\r
2013 /* Set the DMA AbortCpltCallback */
\r
2014 hspi->hdmarx->XferAbortCallback = NULL;
\r
2016 /* Enable the Rx DMA Stream/Channel */
\r
2017 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount))
\r
2019 /* Update SPI error code */
\r
2020 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
2021 errorcode = HAL_ERROR;
\r
2023 hspi->State = HAL_SPI_STATE_READY;
\r
2027 /* Check if the SPI is already enabled */
\r
2028 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
2030 /* Enable SPI peripheral */
\r
2031 __HAL_SPI_ENABLE(hspi);
\r
2034 /* Enable the SPI Error Interrupt Bit */
\r
2035 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
\r
2037 /* Enable Rx DMA Request */
\r
2038 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
\r
2041 /* Process Unlocked */
\r
2042 __HAL_UNLOCK(hspi);
\r
2047 * @brief Transmit and Receive an amount of data in non-blocking mode with DMA.
\r
2048 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2049 * the configuration information for SPI module.
\r
2050 * @param pTxData pointer to transmission data buffer
\r
2051 * @param pRxData pointer to reception data buffer
\r
2052 * @note When the CRC feature is enabled the pRxData Length must be Size + 1
\r
2053 * @param Size amount of data to be sent
\r
2054 * @retval HAL status
\r
2056 HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
\r
2059 uint32_t tmp_mode;
\r
2060 HAL_SPI_StateTypeDef tmp_state;
\r
2061 HAL_StatusTypeDef errorcode = HAL_OK;
\r
2063 /* Check rx & tx dma handles */
\r
2064 assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
\r
2065 assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
\r
2067 /* Check Direction parameter */
\r
2068 assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
\r
2070 /* Process locked */
\r
2073 /* Init temporary variables */
\r
2074 tmp_state = hspi->State;
\r
2075 tmp_mode = hspi->Init.Mode;
\r
2077 if (!((tmp_state == HAL_SPI_STATE_READY) ||
\r
2078 ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
\r
2080 errorcode = HAL_BUSY;
\r
2084 if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
\r
2086 errorcode = HAL_ERROR;
\r
2090 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
\r
2091 if (hspi->State != HAL_SPI_STATE_BUSY_RX)
\r
2093 hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
\r
2096 /* Set the transaction information */
\r
2097 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
2098 hspi->pTxBuffPtr = (uint8_t *)pTxData;
\r
2099 hspi->TxXferSize = Size;
\r
2100 hspi->TxXferCount = Size;
\r
2101 hspi->pRxBuffPtr = (uint8_t *)pRxData;
\r
2102 hspi->RxXferSize = Size;
\r
2103 hspi->RxXferCount = Size;
\r
2105 /* Init field not used in handle to zero */
\r
2106 hspi->RxISR = NULL;
\r
2107 hspi->TxISR = NULL;
\r
2109 #if (USE_SPI_CRC != 0U)
\r
2110 /* Reset CRC Calculation */
\r
2111 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
2113 SPI_RESET_CRC(hspi);
\r
2115 #endif /* USE_SPI_CRC */
\r
2117 /* Reset the threshold bit */
\r
2118 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX | SPI_CR2_LDMARX);
\r
2120 /* The packing mode management is enabled by the DMA settings according the spi data size */
\r
2121 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
2123 /* Set fiforxthreshold according the reception data length: 16bit */
\r
2124 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
2128 /* Set RX Fifo threshold according the reception data length: 8bit */
\r
2129 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
2131 if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
\r
2133 if ((hspi->TxXferSize & 0x1U) == 0x0U)
\r
2135 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
\r
2136 hspi->TxXferCount = hspi->TxXferCount >> 1U;
\r
2140 SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
\r
2141 hspi->TxXferCount = (hspi->TxXferCount >> 1U) + 1U;
\r
2145 if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
\r
2147 /* Set RX Fifo threshold according the reception data length: 16bit */
\r
2148 CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
2150 if ((hspi->RxXferCount & 0x1U) == 0x0U)
\r
2152 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
\r
2153 hspi->RxXferCount = hspi->RxXferCount >> 1U;
\r
2157 SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
\r
2158 hspi->RxXferCount = (hspi->RxXferCount >> 1U) + 1U;
\r
2163 /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
\r
2164 if (hspi->State == HAL_SPI_STATE_BUSY_RX)
\r
2166 /* Set the SPI Rx DMA Half transfer complete callback */
\r
2167 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
\r
2168 hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
\r
2172 /* Set the SPI Tx/Rx DMA Half transfer complete callback */
\r
2173 hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
\r
2174 hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
\r
2177 /* Set the DMA error callback */
\r
2178 hspi->hdmarx->XferErrorCallback = SPI_DMAError;
\r
2180 /* Set the DMA AbortCpltCallback */
\r
2181 hspi->hdmarx->XferAbortCallback = NULL;
\r
2183 /* Enable the Rx DMA Stream/Channel */
\r
2184 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount))
\r
2186 /* Update SPI error code */
\r
2187 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
2188 errorcode = HAL_ERROR;
\r
2190 hspi->State = HAL_SPI_STATE_READY;
\r
2194 /* Enable Rx DMA Request */
\r
2195 SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
\r
2197 /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
\r
2198 is performed in DMA reception complete callback */
\r
2199 hspi->hdmatx->XferHalfCpltCallback = NULL;
\r
2200 hspi->hdmatx->XferCpltCallback = NULL;
\r
2201 hspi->hdmatx->XferErrorCallback = NULL;
\r
2202 hspi->hdmatx->XferAbortCallback = NULL;
\r
2204 /* Enable the Tx DMA Stream/Channel */
\r
2205 if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount))
\r
2207 /* Update SPI error code */
\r
2208 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
2209 errorcode = HAL_ERROR;
\r
2211 hspi->State = HAL_SPI_STATE_READY;
\r
2215 /* Check if the SPI is already enabled */
\r
2216 if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
\r
2218 /* Enable SPI peripheral */
\r
2219 __HAL_SPI_ENABLE(hspi);
\r
2221 /* Enable the SPI Error Interrupt Bit */
\r
2222 __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
\r
2224 /* Enable Tx DMA Request */
\r
2225 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
\r
2228 /* Process Unlocked */
\r
2229 __HAL_UNLOCK(hspi);
\r
2234 * @brief Abort ongoing transfer (blocking mode).
\r
2235 * @param hspi SPI handle.
\r
2236 * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
\r
2237 * started in Interrupt or DMA mode.
\r
2238 * This procedure performs following operations :
\r
2239 * - Disable SPI Interrupts (depending of transfer direction)
\r
2240 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
2241 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
\r
2242 * - Set handle State to READY
\r
2243 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
\r
2244 * @retval HAL status
\r
2246 HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)
\r
2248 HAL_StatusTypeDef errorcode;
\r
2249 __IO uint32_t count, resetcount;
\r
2251 /* Initialized local variable */
\r
2252 errorcode = HAL_OK;
\r
2253 resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
\r
2254 count = resetcount;
\r
2256 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
\r
2257 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
\r
2259 /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
\r
2260 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
\r
2262 hspi->TxISR = SPI_AbortTx_ISR;
\r
2263 /* Wait HAL_SPI_STATE_ABORT state */
\r
2268 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2273 while (hspi->State != HAL_SPI_STATE_ABORT);
\r
2274 /* Reset Timeout Counter */
\r
2275 count = resetcount;
\r
2278 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
\r
2280 hspi->RxISR = SPI_AbortRx_ISR;
\r
2281 /* Wait HAL_SPI_STATE_ABORT state */
\r
2286 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2291 while (hspi->State != HAL_SPI_STATE_ABORT);
\r
2292 /* Reset Timeout Counter */
\r
2293 count = resetcount;
\r
2296 /* Disable the SPI DMA Tx request if enabled */
\r
2297 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
\r
2299 /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
\r
2300 if (hspi->hdmatx != NULL)
\r
2302 /* Set the SPI DMA Abort callback :
\r
2303 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
\r
2304 hspi->hdmatx->XferAbortCallback = NULL;
\r
2306 /* Abort DMA Tx Handle linked to SPI Peripheral */
\r
2307 if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
\r
2309 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2312 /* Disable Tx DMA Request */
\r
2313 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN));
\r
2315 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
2317 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2320 /* Disable SPI Peripheral */
\r
2321 __HAL_SPI_DISABLE(hspi);
\r
2323 /* Empty the FRLVL fifo */
\r
2324 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
2326 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2331 /* Disable the SPI DMA Rx request if enabled */
\r
2332 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
\r
2334 /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
\r
2335 if (hspi->hdmarx != NULL)
\r
2337 /* Set the SPI DMA Abort callback :
\r
2338 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
\r
2339 hspi->hdmarx->XferAbortCallback = NULL;
\r
2341 /* Abort DMA Rx Handle linked to SPI Peripheral */
\r
2342 if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
\r
2344 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2347 /* Disable peripheral */
\r
2348 __HAL_SPI_DISABLE(hspi);
\r
2350 /* Control the BSY flag */
\r
2351 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
2353 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2356 /* Empty the FRLVL fifo */
\r
2357 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
2359 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2362 /* Disable Rx DMA Request */
\r
2363 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN));
\r
2366 /* Reset Tx and Rx transfer counters */
\r
2367 hspi->RxXferCount = 0U;
\r
2368 hspi->TxXferCount = 0U;
\r
2370 /* Check error during Abort procedure */
\r
2371 if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
\r
2373 /* return HAL_Error in case of error during Abort procedure */
\r
2374 errorcode = HAL_ERROR;
\r
2378 /* Reset errorCode */
\r
2379 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
2382 /* Clear the Error flags in the SR register */
\r
2383 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
2384 __HAL_SPI_CLEAR_FREFLAG(hspi);
\r
2386 /* Restore hspi->state to ready */
\r
2387 hspi->State = HAL_SPI_STATE_READY;
\r
2393 * @brief Abort ongoing transfer (Interrupt mode).
\r
2394 * @param hspi SPI handle.
\r
2395 * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
\r
2396 * started in Interrupt or DMA mode.
\r
2397 * This procedure performs following operations :
\r
2398 * - Disable SPI Interrupts (depending of transfer direction)
\r
2399 * - Disable the DMA transfer in the peripheral register (if enabled)
\r
2400 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
\r
2401 * - Set handle State to READY
\r
2402 * - At abort completion, call user abort complete callback
\r
2403 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
\r
2404 * considered as completed only when user abort complete callback is executed (not when exiting function).
\r
2405 * @retval HAL status
\r
2407 HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi)
\r
2409 HAL_StatusTypeDef errorcode;
\r
2410 uint32_t abortcplt ;
\r
2411 __IO uint32_t count, resetcount;
\r
2413 /* Initialized local variable */
\r
2414 errorcode = HAL_OK;
\r
2416 resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
\r
2417 count = resetcount;
\r
2419 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
\r
2420 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
\r
2422 /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
\r
2423 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
\r
2425 hspi->TxISR = SPI_AbortTx_ISR;
\r
2426 /* Wait HAL_SPI_STATE_ABORT state */
\r
2431 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2436 while (hspi->State != HAL_SPI_STATE_ABORT);
\r
2437 /* Reset Timeout Counter */
\r
2438 count = resetcount;
\r
2441 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
\r
2443 hspi->RxISR = SPI_AbortRx_ISR;
\r
2444 /* Wait HAL_SPI_STATE_ABORT state */
\r
2449 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2454 while (hspi->State != HAL_SPI_STATE_ABORT);
\r
2455 /* Reset Timeout Counter */
\r
2456 count = resetcount;
\r
2459 /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
\r
2460 before any call to DMA Abort functions */
\r
2461 /* DMA Tx Handle is valid */
\r
2462 if (hspi->hdmatx != NULL)
\r
2464 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
\r
2465 Otherwise, set it to NULL */
\r
2466 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
\r
2468 hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback;
\r
2472 hspi->hdmatx->XferAbortCallback = NULL;
\r
2475 /* DMA Rx Handle is valid */
\r
2476 if (hspi->hdmarx != NULL)
\r
2478 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
\r
2479 Otherwise, set it to NULL */
\r
2480 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
\r
2482 hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback;
\r
2486 hspi->hdmarx->XferAbortCallback = NULL;
\r
2490 /* Disable the SPI DMA Tx request if enabled */
\r
2491 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
\r
2493 /* Abort the SPI DMA Tx Stream/Channel */
\r
2494 if (hspi->hdmatx != NULL)
\r
2496 /* Abort DMA Tx Handle linked to SPI Peripheral */
\r
2497 if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
\r
2499 hspi->hdmatx->XferAbortCallback = NULL;
\r
2500 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2508 /* Disable the SPI DMA Rx request if enabled */
\r
2509 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
\r
2511 /* Abort the SPI DMA Rx Stream/Channel */
\r
2512 if (hspi->hdmarx != NULL)
\r
2514 /* Abort DMA Rx Handle linked to SPI Peripheral */
\r
2515 if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
\r
2517 hspi->hdmarx->XferAbortCallback = NULL;
\r
2518 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
2527 if (abortcplt == 1U)
\r
2529 /* Reset Tx and Rx transfer counters */
\r
2530 hspi->RxXferCount = 0U;
\r
2531 hspi->TxXferCount = 0U;
\r
2533 /* Check error during Abort procedure */
\r
2534 if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
\r
2536 /* return HAL_Error in case of error during Abort procedure */
\r
2537 errorcode = HAL_ERROR;
\r
2541 /* Reset errorCode */
\r
2542 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
2545 /* Clear the Error flags in the SR register */
\r
2546 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
2547 __HAL_SPI_CLEAR_FREFLAG(hspi);
\r
2549 /* Restore hspi->State to Ready */
\r
2550 hspi->State = HAL_SPI_STATE_READY;
\r
2552 /* As no DMA to be aborted, call directly user Abort complete callback */
\r
2553 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
2554 hspi->AbortCpltCallback(hspi);
\r
2556 HAL_SPI_AbortCpltCallback(hspi);
\r
2557 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
2564 * @brief Pause the DMA Transfer.
\r
2565 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2566 * the configuration information for the specified SPI module.
\r
2567 * @retval HAL status
\r
2569 HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
\r
2571 /* Process Locked */
\r
2574 /* Disable the SPI DMA Tx & Rx requests */
\r
2575 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
2577 /* Process Unlocked */
\r
2578 __HAL_UNLOCK(hspi);
\r
2584 * @brief Resume the DMA Transfer.
\r
2585 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2586 * the configuration information for the specified SPI module.
\r
2587 * @retval HAL status
\r
2589 HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
\r
2591 /* Process Locked */
\r
2594 /* Enable the SPI DMA Tx & Rx requests */
\r
2595 SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
2597 /* Process Unlocked */
\r
2598 __HAL_UNLOCK(hspi);
\r
2604 * @brief Stop the DMA Transfer.
\r
2605 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2606 * the configuration information for the specified SPI module.
\r
2607 * @retval HAL status
\r
2609 HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
\r
2611 HAL_StatusTypeDef errorcode = HAL_OK;
\r
2612 /* The Lock is not implemented on this API to allow the user application
\r
2613 to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
\r
2614 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
\r
2615 and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
\r
2618 /* Abort the SPI DMA tx Stream/Channel */
\r
2619 if (hspi->hdmatx != NULL)
\r
2621 if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx))
\r
2623 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
2624 errorcode = HAL_ERROR;
\r
2627 /* Abort the SPI DMA rx Stream/Channel */
\r
2628 if (hspi->hdmarx != NULL)
\r
2630 if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx))
\r
2632 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
2633 errorcode = HAL_ERROR;
\r
2637 /* Disable the SPI DMA Tx & Rx requests */
\r
2638 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
2639 hspi->State = HAL_SPI_STATE_READY;
\r
2644 * @brief Handle SPI interrupt request.
\r
2645 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2646 * the configuration information for the specified SPI module.
\r
2649 void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
\r
2651 uint32_t itsource = hspi->Instance->CR2;
\r
2652 uint32_t itflag = hspi->Instance->SR;
\r
2654 /* SPI in mode Receiver ----------------------------------------------------*/
\r
2655 if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) &&
\r
2656 (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET))
\r
2658 hspi->RxISR(hspi);
\r
2662 /* SPI in mode Transmitter -------------------------------------------------*/
\r
2663 if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET))
\r
2665 hspi->TxISR(hspi);
\r
2669 /* SPI in Error Treatment --------------------------------------------------*/
\r
2670 if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET))
\r
2672 /* SPI Overrun error interrupt occurred ----------------------------------*/
\r
2673 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
\r
2675 if (hspi->State != HAL_SPI_STATE_BUSY_TX)
\r
2677 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
\r
2678 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
2682 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
2687 /* SPI Mode Fault error interrupt occurred -------------------------------*/
\r
2688 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET)
\r
2690 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
\r
2691 __HAL_SPI_CLEAR_MODFFLAG(hspi);
\r
2694 /* SPI Frame error interrupt occurred ------------------------------------*/
\r
2695 if (SPI_CHECK_FLAG(itflag, SPI_FLAG_FRE) != RESET)
\r
2697 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
\r
2698 __HAL_SPI_CLEAR_FREFLAG(hspi);
\r
2701 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
2703 /* Disable all interrupts */
\r
2704 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
\r
2706 hspi->State = HAL_SPI_STATE_READY;
\r
2707 /* Disable the SPI DMA requests if enabled */
\r
2708 if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN)))
\r
2710 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
\r
2712 /* Abort the SPI DMA Rx channel */
\r
2713 if (hspi->hdmarx != NULL)
\r
2715 /* Set the SPI DMA Abort callback :
\r
2716 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
\r
2717 hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError;
\r
2718 if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
\r
2720 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2723 /* Abort the SPI DMA Tx channel */
\r
2724 if (hspi->hdmatx != NULL)
\r
2726 /* Set the SPI DMA Abort callback :
\r
2727 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
\r
2728 hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError;
\r
2729 if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
\r
2731 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
2737 /* Call user error callback */
\r
2738 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
2739 hspi->ErrorCallback(hspi);
\r
2741 HAL_SPI_ErrorCallback(hspi);
\r
2742 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
2750 * @brief Tx Transfer completed callback.
\r
2751 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2752 * the configuration information for SPI module.
\r
2755 __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
\r
2757 /* Prevent unused argument(s) compilation warning */
\r
2760 /* NOTE : This function should not be modified, when the callback is needed,
\r
2761 the HAL_SPI_TxCpltCallback should be implemented in the user file
\r
2766 * @brief Rx Transfer completed callback.
\r
2767 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2768 * the configuration information for SPI module.
\r
2771 __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
\r
2773 /* Prevent unused argument(s) compilation warning */
\r
2776 /* NOTE : This function should not be modified, when the callback is needed,
\r
2777 the HAL_SPI_RxCpltCallback should be implemented in the user file
\r
2782 * @brief Tx and Rx Transfer completed callback.
\r
2783 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2784 * the configuration information for SPI module.
\r
2787 __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
\r
2789 /* Prevent unused argument(s) compilation warning */
\r
2792 /* NOTE : This function should not be modified, when the callback is needed,
\r
2793 the HAL_SPI_TxRxCpltCallback should be implemented in the user file
\r
2798 * @brief Tx Half Transfer completed callback.
\r
2799 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2800 * the configuration information for SPI module.
\r
2803 __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
\r
2805 /* Prevent unused argument(s) compilation warning */
\r
2808 /* NOTE : This function should not be modified, when the callback is needed,
\r
2809 the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
\r
2814 * @brief Rx Half Transfer completed callback.
\r
2815 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2816 * the configuration information for SPI module.
\r
2819 __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
\r
2821 /* Prevent unused argument(s) compilation warning */
\r
2824 /* NOTE : This function should not be modified, when the callback is needed,
\r
2825 the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
\r
2830 * @brief Tx and Rx Half Transfer callback.
\r
2831 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2832 * the configuration information for SPI module.
\r
2835 __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
\r
2837 /* Prevent unused argument(s) compilation warning */
\r
2840 /* NOTE : This function should not be modified, when the callback is needed,
\r
2841 the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
\r
2846 * @brief SPI error callback.
\r
2847 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2848 * the configuration information for SPI module.
\r
2851 __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
\r
2853 /* Prevent unused argument(s) compilation warning */
\r
2856 /* NOTE : This function should not be modified, when the callback is needed,
\r
2857 the HAL_SPI_ErrorCallback should be implemented in the user file
\r
2859 /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
\r
2860 and user can use HAL_SPI_GetError() API to check the latest error occurred
\r
2865 * @brief SPI Abort Complete callback.
\r
2866 * @param hspi SPI handle.
\r
2869 __weak void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi)
\r
2871 /* Prevent unused argument(s) compilation warning */
\r
2874 /* NOTE : This function should not be modified, when the callback is needed,
\r
2875 the HAL_SPI_AbortCpltCallback can be implemented in the user file.
\r
2883 /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
\r
2884 * @brief SPI control functions
\r
2887 ===============================================================================
\r
2888 ##### Peripheral State and Errors functions #####
\r
2889 ===============================================================================
\r
2891 This subsection provides a set of functions allowing to control the SPI.
\r
2892 (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
\r
2893 (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
\r
2899 * @brief Return the SPI handle state.
\r
2900 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2901 * the configuration information for SPI module.
\r
2902 * @retval SPI state
\r
2904 HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
\r
2906 /* Return SPI handle state */
\r
2907 return hspi->State;
\r
2911 * @brief Return the SPI error code.
\r
2912 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
2913 * the configuration information for SPI module.
\r
2914 * @retval SPI error code in bitmap format
\r
2916 uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
\r
2918 /* Return SPI ErrorCode */
\r
2919 return hspi->ErrorCode;
\r
2930 /** @addtogroup SPI_Private_Functions
\r
2931 * @brief Private functions
\r
2936 * @brief DMA SPI transmit process complete callback.
\r
2937 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
2938 * the configuration information for the specified DMA module.
\r
2941 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
\r
2943 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
2944 uint32_t tickstart;
\r
2946 /* Init tickstart for timeout management*/
\r
2947 tickstart = HAL_GetTick();
\r
2949 /* DMA Normal Mode */
\r
2950 if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
\r
2952 /* Disable ERR interrupt */
\r
2953 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
\r
2955 /* Disable Tx DMA Request */
\r
2956 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
\r
2958 /* Check the end of the transaction */
\r
2959 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
2961 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
2964 /* Clear overrun flag in 2 Lines communication mode because received data is not read */
\r
2965 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
\r
2967 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
2970 hspi->TxXferCount = 0U;
\r
2971 hspi->State = HAL_SPI_STATE_READY;
\r
2973 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
2975 /* Call user error callback */
\r
2976 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
2977 hspi->ErrorCallback(hspi);
\r
2979 HAL_SPI_ErrorCallback(hspi);
\r
2980 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
2984 /* Call user Tx complete callback */
\r
2985 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
2986 hspi->TxCpltCallback(hspi);
\r
2988 HAL_SPI_TxCpltCallback(hspi);
\r
2989 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
2993 * @brief DMA SPI receive process complete callback.
\r
2994 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
2995 * the configuration information for the specified DMA module.
\r
2998 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
\r
3000 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3001 uint32_t tickstart;
\r
3003 /* Init tickstart for timeout management*/
\r
3004 tickstart = HAL_GetTick();
\r
3006 /* DMA Normal Mode */
\r
3007 if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
\r
3009 /* Disable ERR interrupt */
\r
3010 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
\r
3012 #if (USE_SPI_CRC != 0U)
\r
3013 /* CRC handling */
\r
3014 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3016 /* Wait until RXNE flag */
\r
3017 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3019 /* Error on the CRC reception */
\r
3020 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3023 if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
\r
3025 /* Read 16bit CRC */
\r
3026 READ_REG(hspi->Instance->DR);
\r
3030 /* Read 8bit CRC */
\r
3031 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
3033 if (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
\r
3035 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3037 /* Error on the CRC reception */
\r
3038 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3040 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
\r
3041 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
3045 #endif /* USE_SPI_CRC */
\r
3047 /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
\r
3048 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
3050 /* Check the end of the transaction */
\r
3051 if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3053 hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
\r
3056 hspi->RxXferCount = 0U;
\r
3057 hspi->State = HAL_SPI_STATE_READY;
\r
3059 #if (USE_SPI_CRC != 0U)
\r
3060 /* Check if CRC error occurred */
\r
3061 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
\r
3063 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3064 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
3066 #endif /* USE_SPI_CRC */
\r
3068 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
3070 /* Call user error callback */
\r
3071 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3072 hspi->ErrorCallback(hspi);
\r
3074 HAL_SPI_ErrorCallback(hspi);
\r
3075 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3079 /* Call user Rx complete callback */
\r
3080 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3081 hspi->RxCpltCallback(hspi);
\r
3083 HAL_SPI_RxCpltCallback(hspi);
\r
3084 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3088 * @brief DMA SPI transmit receive process complete callback.
\r
3089 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
3090 * the configuration information for the specified DMA module.
\r
3093 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
\r
3095 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3096 uint32_t tickstart;
\r
3098 /* Init tickstart for timeout management*/
\r
3099 tickstart = HAL_GetTick();
\r
3101 /* DMA Normal Mode */
\r
3102 if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
\r
3104 /* Disable ERR interrupt */
\r
3105 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
\r
3107 #if (USE_SPI_CRC != 0U)
\r
3108 /* CRC handling */
\r
3109 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3111 if ((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_8BIT))
\r
3113 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_QUARTER_FULL, SPI_DEFAULT_TIMEOUT,
\r
3114 tickstart) != HAL_OK)
\r
3116 /* Error on the CRC reception */
\r
3117 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3119 /* Read CRC to Flush DR and RXNE flag */
\r
3120 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
3124 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_HALF_FULL, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3126 /* Error on the CRC reception */
\r
3127 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3129 /* Read CRC to Flush DR and RXNE flag */
\r
3130 READ_REG(hspi->Instance->DR);
\r
3133 #endif /* USE_SPI_CRC */
\r
3135 /* Check the end of the transaction */
\r
3136 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3138 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3141 /* Disable Rx/Tx DMA Request */
\r
3142 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
3144 hspi->TxXferCount = 0U;
\r
3145 hspi->RxXferCount = 0U;
\r
3146 hspi->State = HAL_SPI_STATE_READY;
\r
3148 #if (USE_SPI_CRC != 0U)
\r
3149 /* Check if CRC error occurred */
\r
3150 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR))
\r
3152 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3153 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
3155 #endif /* USE_SPI_CRC */
\r
3157 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
3159 /* Call user error callback */
\r
3160 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3161 hspi->ErrorCallback(hspi);
\r
3163 HAL_SPI_ErrorCallback(hspi);
\r
3164 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3168 /* Call user TxRx complete callback */
\r
3169 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3170 hspi->TxRxCpltCallback(hspi);
\r
3172 HAL_SPI_TxRxCpltCallback(hspi);
\r
3173 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3177 * @brief DMA SPI half transmit process complete callback.
\r
3178 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
3179 * the configuration information for the specified DMA module.
\r
3182 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
\r
3184 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3186 /* Call user Tx half complete callback */
\r
3187 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3188 hspi->TxHalfCpltCallback(hspi);
\r
3190 HAL_SPI_TxHalfCpltCallback(hspi);
\r
3191 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3195 * @brief DMA SPI half receive process complete callback
\r
3196 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
3197 * the configuration information for the specified DMA module.
\r
3200 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
\r
3202 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3204 /* Call user Rx half complete callback */
\r
3205 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3206 hspi->RxHalfCpltCallback(hspi);
\r
3208 HAL_SPI_RxHalfCpltCallback(hspi);
\r
3209 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3213 * @brief DMA SPI half transmit receive process complete callback.
\r
3214 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
3215 * the configuration information for the specified DMA module.
\r
3218 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
\r
3220 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3222 /* Call user TxRx half complete callback */
\r
3223 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3224 hspi->TxRxHalfCpltCallback(hspi);
\r
3226 HAL_SPI_TxRxHalfCpltCallback(hspi);
\r
3227 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3231 * @brief DMA SPI communication error callback.
\r
3232 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
\r
3233 * the configuration information for the specified DMA module.
\r
3236 static void SPI_DMAError(DMA_HandleTypeDef *hdma)
\r
3238 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3240 /* Stop the disable DMA transfer on SPI side */
\r
3241 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
\r
3243 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
\r
3244 hspi->State = HAL_SPI_STATE_READY;
\r
3245 /* Call user error callback */
\r
3246 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3247 hspi->ErrorCallback(hspi);
\r
3249 HAL_SPI_ErrorCallback(hspi);
\r
3250 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3254 * @brief DMA SPI communication abort callback, when initiated by HAL services on Error
\r
3255 * (To be called at end of DMA Abort procedure following error occurrence).
\r
3256 * @param hdma DMA handle.
\r
3259 static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
\r
3261 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3262 hspi->RxXferCount = 0U;
\r
3263 hspi->TxXferCount = 0U;
\r
3265 /* Call user error callback */
\r
3266 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3267 hspi->ErrorCallback(hspi);
\r
3269 HAL_SPI_ErrorCallback(hspi);
\r
3270 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3274 * @brief DMA SPI Tx communication abort callback, when initiated by user
\r
3275 * (To be called at end of DMA Tx Abort procedure following user abort request).
\r
3276 * @note When this callback is executed, User Abort complete call back is called only if no
\r
3277 * Abort still ongoing for Rx DMA Handle.
\r
3278 * @param hdma DMA handle.
\r
3281 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
\r
3283 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3285 hspi->hdmatx->XferAbortCallback = NULL;
\r
3287 /* Disable Tx DMA Request */
\r
3288 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
\r
3290 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
3292 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
3295 /* Disable SPI Peripheral */
\r
3296 __HAL_SPI_DISABLE(hspi);
\r
3298 /* Empty the FRLVL fifo */
\r
3299 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
3301 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
3304 /* Check if an Abort process is still ongoing */
\r
3305 if (hspi->hdmarx != NULL)
\r
3307 if (hspi->hdmarx->XferAbortCallback != NULL)
\r
3313 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
\r
3314 hspi->RxXferCount = 0U;
\r
3315 hspi->TxXferCount = 0U;
\r
3317 /* Check no error during Abort procedure */
\r
3318 if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
\r
3320 /* Reset errorCode */
\r
3321 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
3324 /* Clear the Error flags in the SR register */
\r
3325 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
3326 __HAL_SPI_CLEAR_FREFLAG(hspi);
\r
3328 /* Restore hspi->State to Ready */
\r
3329 hspi->State = HAL_SPI_STATE_READY;
\r
3331 /* Call user Abort complete callback */
\r
3332 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3333 hspi->AbortCpltCallback(hspi);
\r
3335 HAL_SPI_AbortCpltCallback(hspi);
\r
3336 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3340 * @brief DMA SPI Rx communication abort callback, when initiated by user
\r
3341 * (To be called at end of DMA Rx Abort procedure following user abort request).
\r
3342 * @note When this callback is executed, User Abort complete call back is called only if no
\r
3343 * Abort still ongoing for Tx DMA Handle.
\r
3344 * @param hdma DMA handle.
\r
3347 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
\r
3349 SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
\r
3351 /* Disable SPI Peripheral */
\r
3352 __HAL_SPI_DISABLE(hspi);
\r
3354 hspi->hdmarx->XferAbortCallback = NULL;
\r
3356 /* Disable Rx DMA Request */
\r
3357 CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
\r
3359 /* Control the BSY flag */
\r
3360 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
3362 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
3365 /* Empty the FRLVL fifo */
\r
3366 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
3368 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
3371 /* Check if an Abort process is still ongoing */
\r
3372 if (hspi->hdmatx != NULL)
\r
3374 if (hspi->hdmatx->XferAbortCallback != NULL)
\r
3380 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
\r
3381 hspi->RxXferCount = 0U;
\r
3382 hspi->TxXferCount = 0U;
\r
3384 /* Check no error during Abort procedure */
\r
3385 if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
\r
3387 /* Reset errorCode */
\r
3388 hspi->ErrorCode = HAL_SPI_ERROR_NONE;
\r
3391 /* Clear the Error flags in the SR register */
\r
3392 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
3393 __HAL_SPI_CLEAR_FREFLAG(hspi);
\r
3395 /* Restore hspi->State to Ready */
\r
3396 hspi->State = HAL_SPI_STATE_READY;
\r
3398 /* Call user Abort complete callback */
\r
3399 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3400 hspi->AbortCpltCallback(hspi);
\r
3402 HAL_SPI_AbortCpltCallback(hspi);
\r
3403 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3407 * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
\r
3408 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3409 * the configuration information for SPI module.
\r
3412 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
\r
3414 /* Receive data in packing mode */
\r
3415 if (hspi->RxXferCount > 1U)
\r
3417 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
\r
3418 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
3419 hspi->RxXferCount -= 2U;
\r
3420 if (hspi->RxXferCount == 1U)
\r
3422 /* Set RX Fifo threshold according the reception data length: 8bit */
\r
3423 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
3426 /* Receive data in 8 Bit mode */
\r
3429 *hspi->pRxBuffPtr = *((__IO uint8_t *)&hspi->Instance->DR);
\r
3430 hspi->pRxBuffPtr++;
\r
3431 hspi->RxXferCount--;
\r
3434 /* Check end of the reception */
\r
3435 if (hspi->RxXferCount == 0U)
\r
3437 #if (USE_SPI_CRC != 0U)
\r
3438 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3440 SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
\r
3441 hspi->RxISR = SPI_2linesRxISR_8BITCRC;
\r
3444 #endif /* USE_SPI_CRC */
\r
3446 /* Disable RXNE and ERR interrupt */
\r
3447 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
\r
3449 if (hspi->TxXferCount == 0U)
\r
3451 SPI_CloseRxTx_ISR(hspi);
\r
3456 #if (USE_SPI_CRC != 0U)
\r
3458 * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
\r
3459 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3460 * the configuration information for SPI module.
\r
3463 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
\r
3465 /* Read 8bit CRC to flush Data Regsiter */
\r
3466 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
3470 /* Check end of the reception */
\r
3471 if (hspi->CRCSize == 0U)
\r
3473 /* Disable RXNE and ERR interrupt */
\r
3474 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
\r
3476 if (hspi->TxXferCount == 0U)
\r
3478 SPI_CloseRxTx_ISR(hspi);
\r
3482 #endif /* USE_SPI_CRC */
\r
3485 * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode.
\r
3486 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3487 * the configuration information for SPI module.
\r
3490 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
\r
3492 /* Transmit data in packing Bit mode */
\r
3493 if (hspi->TxXferCount >= 2U)
\r
3495 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
3496 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
3497 hspi->TxXferCount -= 2U;
\r
3499 /* Transmit data in 8 Bit mode */
\r
3502 *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
\r
3503 hspi->pTxBuffPtr++;
\r
3504 hspi->TxXferCount--;
\r
3507 /* Check the end of the transmission */
\r
3508 if (hspi->TxXferCount == 0U)
\r
3510 #if (USE_SPI_CRC != 0U)
\r
3511 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3513 /* Set CRC Next Bit to send CRC */
\r
3514 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3515 /* Disable TXE interrupt */
\r
3516 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
\r
3519 #endif /* USE_SPI_CRC */
\r
3521 /* Disable TXE interrupt */
\r
3522 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
\r
3524 if (hspi->RxXferCount == 0U)
\r
3526 SPI_CloseRxTx_ISR(hspi);
\r
3532 * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode.
\r
3533 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3534 * the configuration information for SPI module.
\r
3537 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
\r
3539 /* Receive data in 16 Bit mode */
\r
3540 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
\r
3541 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
3542 hspi->RxXferCount--;
\r
3544 if (hspi->RxXferCount == 0U)
\r
3546 #if (USE_SPI_CRC != 0U)
\r
3547 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3549 hspi->RxISR = SPI_2linesRxISR_16BITCRC;
\r
3552 #endif /* USE_SPI_CRC */
\r
3554 /* Disable RXNE interrupt */
\r
3555 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
\r
3557 if (hspi->TxXferCount == 0U)
\r
3559 SPI_CloseRxTx_ISR(hspi);
\r
3564 #if (USE_SPI_CRC != 0U)
\r
3566 * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode.
\r
3567 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3568 * the configuration information for SPI module.
\r
3571 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
\r
3573 /* Read 16bit CRC to flush Data Regsiter */
\r
3574 READ_REG(hspi->Instance->DR);
\r
3576 /* Disable RXNE interrupt */
\r
3577 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
\r
3579 SPI_CloseRxTx_ISR(hspi);
\r
3581 #endif /* USE_SPI_CRC */
\r
3584 * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode.
\r
3585 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3586 * the configuration information for SPI module.
\r
3589 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
\r
3591 /* Transmit data in 16 Bit mode */
\r
3592 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
3593 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
3594 hspi->TxXferCount--;
\r
3596 /* Enable CRC Transmission */
\r
3597 if (hspi->TxXferCount == 0U)
\r
3599 #if (USE_SPI_CRC != 0U)
\r
3600 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3602 /* Set CRC Next Bit to send CRC */
\r
3603 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3604 /* Disable TXE interrupt */
\r
3605 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
\r
3608 #endif /* USE_SPI_CRC */
\r
3610 /* Disable TXE interrupt */
\r
3611 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
\r
3613 if (hspi->RxXferCount == 0U)
\r
3615 SPI_CloseRxTx_ISR(hspi);
\r
3620 #if (USE_SPI_CRC != 0U)
\r
3622 * @brief Manage the CRC 8-bit receive in Interrupt context.
\r
3623 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3624 * the configuration information for SPI module.
\r
3627 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
\r
3629 /* Read 8bit CRC to flush Data Register */
\r
3630 READ_REG(*(__IO uint8_t *)&hspi->Instance->DR);
\r
3634 if (hspi->CRCSize == 0U)
\r
3636 SPI_CloseRx_ISR(hspi);
\r
3639 #endif /* USE_SPI_CRC */
\r
3642 * @brief Manage the receive 8-bit in Interrupt context.
\r
3643 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3644 * the configuration information for SPI module.
\r
3647 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
\r
3649 *hspi->pRxBuffPtr = (*(__IO uint8_t *)&hspi->Instance->DR);
\r
3650 hspi->pRxBuffPtr++;
\r
3651 hspi->RxXferCount--;
\r
3653 #if (USE_SPI_CRC != 0U)
\r
3654 /* Enable CRC Transmission */
\r
3655 if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
\r
3657 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3659 #endif /* USE_SPI_CRC */
\r
3661 if (hspi->RxXferCount == 0U)
\r
3663 #if (USE_SPI_CRC != 0U)
\r
3664 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3666 hspi->RxISR = SPI_RxISR_8BITCRC;
\r
3669 #endif /* USE_SPI_CRC */
\r
3670 SPI_CloseRx_ISR(hspi);
\r
3674 #if (USE_SPI_CRC != 0U)
\r
3676 * @brief Manage the CRC 16-bit receive in Interrupt context.
\r
3677 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3678 * the configuration information for SPI module.
\r
3681 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
\r
3683 /* Read 16bit CRC to flush Data Register */
\r
3684 READ_REG(hspi->Instance->DR);
\r
3686 /* Disable RXNE and ERR interrupt */
\r
3687 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
\r
3689 SPI_CloseRx_ISR(hspi);
\r
3691 #endif /* USE_SPI_CRC */
\r
3694 * @brief Manage the 16-bit receive in Interrupt context.
\r
3695 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3696 * the configuration information for SPI module.
\r
3699 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
\r
3701 *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
\r
3702 hspi->pRxBuffPtr += sizeof(uint16_t);
\r
3703 hspi->RxXferCount--;
\r
3705 #if (USE_SPI_CRC != 0U)
\r
3706 /* Enable CRC Transmission */
\r
3707 if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
\r
3709 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3711 #endif /* USE_SPI_CRC */
\r
3713 if (hspi->RxXferCount == 0U)
\r
3715 #if (USE_SPI_CRC != 0U)
\r
3716 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3718 hspi->RxISR = SPI_RxISR_16BITCRC;
\r
3721 #endif /* USE_SPI_CRC */
\r
3722 SPI_CloseRx_ISR(hspi);
\r
3727 * @brief Handle the data 8-bit transmit in Interrupt mode.
\r
3728 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3729 * the configuration information for SPI module.
\r
3732 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
\r
3734 *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
\r
3735 hspi->pTxBuffPtr++;
\r
3736 hspi->TxXferCount--;
\r
3738 if (hspi->TxXferCount == 0U)
\r
3740 #if (USE_SPI_CRC != 0U)
\r
3741 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3743 /* Enable CRC Transmission */
\r
3744 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3746 #endif /* USE_SPI_CRC */
\r
3747 SPI_CloseTx_ISR(hspi);
\r
3752 * @brief Handle the data 16-bit transmit in Interrupt mode.
\r
3753 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3754 * the configuration information for SPI module.
\r
3757 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
\r
3759 /* Transmit data in 16 Bit mode */
\r
3760 hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
\r
3761 hspi->pTxBuffPtr += sizeof(uint16_t);
\r
3762 hspi->TxXferCount--;
\r
3764 if (hspi->TxXferCount == 0U)
\r
3766 #if (USE_SPI_CRC != 0U)
\r
3767 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3769 /* Enable CRC Transmission */
\r
3770 SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
\r
3772 #endif /* USE_SPI_CRC */
\r
3773 SPI_CloseTx_ISR(hspi);
\r
3778 * @brief Handle SPI Communication Timeout.
\r
3779 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3780 * the configuration information for SPI module.
\r
3781 * @param Flag SPI flag to check
\r
3782 * @param State flag state to check
\r
3783 * @param Timeout Timeout duration
\r
3784 * @param Tickstart tick start value
\r
3785 * @retval HAL status
\r
3787 static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
\r
3788 uint32_t Timeout, uint32_t Tickstart)
\r
3790 while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) != State)
\r
3792 if (Timeout != HAL_MAX_DELAY)
\r
3794 if (((HAL_GetTick() - Tickstart) >= Timeout) || (Timeout == 0U))
\r
3796 /* Disable the SPI and reset the CRC: the CRC value should be cleared
\r
3797 on both master and slave sides in order to resynchronize the master
\r
3798 and slave for their respective CRC calculation */
\r
3800 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
\r
3801 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
\r
3803 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
3804 || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
\r
3806 /* Disable SPI peripheral */
\r
3807 __HAL_SPI_DISABLE(hspi);
\r
3810 /* Reset CRC Calculation */
\r
3811 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3813 SPI_RESET_CRC(hspi);
\r
3816 hspi->State = HAL_SPI_STATE_READY;
\r
3818 /* Process Unlocked */
\r
3819 __HAL_UNLOCK(hspi);
\r
3821 return HAL_TIMEOUT;
\r
3830 * @brief Handle SPI FIFO Communication Timeout.
\r
3831 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3832 * the configuration information for SPI module.
\r
3833 * @param Fifo Fifo to check
\r
3834 * @param State Fifo state to check
\r
3835 * @param Timeout Timeout duration
\r
3836 * @param Tickstart tick start value
\r
3837 * @retval HAL status
\r
3839 static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State,
\r
3840 uint32_t Timeout, uint32_t Tickstart)
\r
3842 while ((hspi->Instance->SR & Fifo) != State)
\r
3844 if ((Fifo == SPI_SR_FRLVL) && (State == SPI_FRLVL_EMPTY))
\r
3846 /* Read 8bit CRC to flush Data Register */
\r
3847 READ_REG(*((__IO uint8_t *)&hspi->Instance->DR));
\r
3850 if (Timeout != HAL_MAX_DELAY)
\r
3852 if (((HAL_GetTick() - Tickstart) >= Timeout) || (Timeout == 0U))
\r
3854 /* Disable the SPI and reset the CRC: the CRC value should be cleared
\r
3855 on both master and slave sides in order to resynchronize the master
\r
3856 and slave for their respective CRC calculation */
\r
3858 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
\r
3859 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
\r
3861 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
3862 || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
\r
3864 /* Disable SPI peripheral */
\r
3865 __HAL_SPI_DISABLE(hspi);
\r
3868 /* Reset CRC Calculation */
\r
3869 if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
\r
3871 SPI_RESET_CRC(hspi);
\r
3874 hspi->State = HAL_SPI_STATE_READY;
\r
3876 /* Process Unlocked */
\r
3877 __HAL_UNLOCK(hspi);
\r
3879 return HAL_TIMEOUT;
\r
3888 * @brief Handle the check of the RX transaction complete.
\r
3889 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3890 * the configuration information for SPI module.
\r
3891 * @param Timeout Timeout duration
\r
3892 * @param Tickstart tick start value
\r
3893 * @retval HAL status
\r
3895 static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
\r
3897 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
3898 || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
\r
3900 /* Disable SPI peripheral */
\r
3901 __HAL_SPI_DISABLE(hspi);
\r
3904 /* Control the BSY flag */
\r
3905 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
\r
3907 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3908 return HAL_TIMEOUT;
\r
3911 if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
\r
3912 || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
\r
3914 /* Empty the FRLVL fifo */
\r
3915 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
\r
3917 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3918 return HAL_TIMEOUT;
\r
3925 * @brief Handle the check of the RXTX or TX transaction complete.
\r
3926 * @param hspi SPI handle
\r
3927 * @param Timeout Timeout duration
\r
3928 * @param Tickstart tick start value
\r
3929 * @retval HAL status
\r
3931 static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
\r
3933 /* Control if the TX fifo is empty */
\r
3934 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
\r
3936 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3937 return HAL_TIMEOUT;
\r
3940 /* Control the BSY flag */
\r
3941 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
\r
3943 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3944 return HAL_TIMEOUT;
\r
3947 /* Control if the RX fifo is empty */
\r
3948 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout, Tickstart) != HAL_OK)
\r
3950 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3951 return HAL_TIMEOUT;
\r
3958 * @brief Handle the end of the RXTX transaction.
\r
3959 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
3960 * the configuration information for SPI module.
\r
3963 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
\r
3965 uint32_t tickstart;
\r
3967 /* Init tickstart for timeout managment*/
\r
3968 tickstart = HAL_GetTick();
\r
3970 /* Disable ERR interrupt */
\r
3971 __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
\r
3973 /* Check the end of the transaction */
\r
3974 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
3976 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
3979 #if (USE_SPI_CRC != 0U)
\r
3980 /* Check if CRC error occurred */
\r
3981 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
\r
3983 hspi->State = HAL_SPI_STATE_READY;
\r
3984 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
3985 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
3986 /* Call user error callback */
\r
3987 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
3988 hspi->ErrorCallback(hspi);
\r
3990 HAL_SPI_ErrorCallback(hspi);
\r
3991 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
3995 #endif /* USE_SPI_CRC */
\r
3996 if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
\r
3998 if (hspi->State == HAL_SPI_STATE_BUSY_RX)
\r
4000 hspi->State = HAL_SPI_STATE_READY;
\r
4001 /* Call user Rx complete callback */
\r
4002 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4003 hspi->RxCpltCallback(hspi);
\r
4005 HAL_SPI_RxCpltCallback(hspi);
\r
4006 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4010 hspi->State = HAL_SPI_STATE_READY;
\r
4011 /* Call user TxRx complete callback */
\r
4012 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4013 hspi->TxRxCpltCallback(hspi);
\r
4015 HAL_SPI_TxRxCpltCallback(hspi);
\r
4016 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4021 hspi->State = HAL_SPI_STATE_READY;
\r
4022 /* Call user error callback */
\r
4023 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4024 hspi->ErrorCallback(hspi);
\r
4026 HAL_SPI_ErrorCallback(hspi);
\r
4027 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4029 #if (USE_SPI_CRC != 0U)
\r
4031 #endif /* USE_SPI_CRC */
\r
4035 * @brief Handle the end of the RX transaction.
\r
4036 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
4037 * the configuration information for SPI module.
\r
4040 static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
\r
4042 /* Disable RXNE and ERR interrupt */
\r
4043 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
\r
4045 /* Check the end of the transaction */
\r
4046 if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4048 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
4050 hspi->State = HAL_SPI_STATE_READY;
\r
4052 #if (USE_SPI_CRC != 0U)
\r
4053 /* Check if CRC error occurred */
\r
4054 if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
\r
4056 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
\r
4057 __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
\r
4058 /* Call user error callback */
\r
4059 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4060 hspi->ErrorCallback(hspi);
\r
4062 HAL_SPI_ErrorCallback(hspi);
\r
4063 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4067 #endif /* USE_SPI_CRC */
\r
4068 if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
\r
4070 /* Call user Rx complete callback */
\r
4071 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4072 hspi->RxCpltCallback(hspi);
\r
4074 HAL_SPI_RxCpltCallback(hspi);
\r
4075 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4079 /* Call user error callback */
\r
4080 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4081 hspi->ErrorCallback(hspi);
\r
4083 HAL_SPI_ErrorCallback(hspi);
\r
4084 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4086 #if (USE_SPI_CRC != 0U)
\r
4088 #endif /* USE_SPI_CRC */
\r
4092 * @brief Handle the end of the TX transaction.
\r
4093 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
4094 * the configuration information for SPI module.
\r
4097 static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
\r
4099 uint32_t tickstart;
\r
4101 /* Init tickstart for timeout management*/
\r
4102 tickstart = HAL_GetTick();
\r
4104 /* Disable TXE and ERR interrupt */
\r
4105 __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
\r
4107 /* Check the end of the transaction */
\r
4108 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
\r
4110 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
\r
4113 /* Clear overrun flag in 2 Lines communication mode because received is not read */
\r
4114 if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
\r
4116 __HAL_SPI_CLEAR_OVRFLAG(hspi);
\r
4119 hspi->State = HAL_SPI_STATE_READY;
\r
4120 if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
\r
4122 /* Call user error callback */
\r
4123 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4124 hspi->ErrorCallback(hspi);
\r
4126 HAL_SPI_ErrorCallback(hspi);
\r
4127 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4131 /* Call user Rx complete callback */
\r
4132 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
\r
4133 hspi->TxCpltCallback(hspi);
\r
4135 HAL_SPI_TxCpltCallback(hspi);
\r
4136 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
\r
4141 * @brief Handle abort a Rx transaction.
\r
4142 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
4143 * the configuration information for SPI module.
\r
4146 static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
\r
4148 __IO uint32_t count;
\r
4150 /* Disable SPI Peripheral */
\r
4151 __HAL_SPI_DISABLE(hspi);
\r
4153 count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
\r
4155 /* Disable RXNEIE interrupt */
\r
4156 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXNEIE));
\r
4158 /* Check RXNEIE is disabled */
\r
4163 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
4168 while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE));
\r
4170 /* Control the BSY flag */
\r
4171 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4173 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4176 /* Empty the FRLVL fifo */
\r
4177 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4179 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4182 hspi->State = HAL_SPI_STATE_ABORT;
\r
4186 * @brief Handle abort a Tx or Rx/Tx transaction.
\r
4187 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
\r
4188 * the configuration information for SPI module.
\r
4191 static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
\r
4193 __IO uint32_t count;
\r
4195 count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
\r
4197 /* Disable TXEIE interrupt */
\r
4198 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE));
\r
4200 /* Check TXEIE is disabled */
\r
4205 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
4210 while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE));
\r
4212 if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4214 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4217 /* Disable SPI Peripheral */
\r
4218 __HAL_SPI_DISABLE(hspi);
\r
4220 /* Empty the FRLVL fifo */
\r
4221 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4223 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4226 /* Check case of Full-Duplex Mode and disable directly RXNEIE interrupt */
\r
4227 if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
\r
4229 /* Disable RXNEIE interrupt */
\r
4230 CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXNEIE));
\r
4232 /* Check RXNEIE is disabled */
\r
4237 SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
\r
4242 while (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE));
\r
4244 /* Control the BSY flag */
\r
4245 if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4247 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4250 /* Empty the FRLVL fifo */
\r
4251 if (SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
\r
4253 hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
\r
4256 hspi->State = HAL_SPI_STATE_ABORT;
\r
4263 #endif /* HAL_SPI_MODULE_ENABLED */
\r
4273 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
\r