2 ******************************************************************************
3 * @file stm32g4xx_hal_spi.c
4 * @author MCD Application Team
5 * @brief SPI HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Serial Peripheral Interface (SPI) peripheral:
8 * + Initialization and de-initialization functions
9 * + IO operation functions
10 * + Peripheral Control functions
11 * + Peripheral State functions
14 ==============================================================================
15 ##### How to use this driver #####
16 ==============================================================================
18 The SPI HAL driver can be used as follows:
20 (#) Declare a SPI_HandleTypeDef handle structure, for example:
21 SPI_HandleTypeDef hspi;
23 (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
24 (##) Enable the SPIx interface clock
25 (##) SPI pins configuration
26 (+++) Enable the clock for the SPI GPIOs
27 (+++) Configure these SPI pins as alternate function push-pull
28 (##) NVIC configuration if you need to use interrupt process
29 (+++) Configure the SPIx interrupt priority
30 (+++) Enable the NVIC SPI IRQ handle
31 (##) DMA Configuration if you need to use DMA process
32 (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel
33 (+++) Enable the DMAx clock
34 (+++) Configure the DMA handle parameters
35 (+++) Configure the DMA Tx or Rx Stream/Channel
36 (+++) Associate the initialized hdma_tx(or _rx) handle to the hspi DMA Tx or Rx handle
37 (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream/Channel
39 (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
40 management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
42 (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
43 (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
44 by calling the customized HAL_SPI_MspInit() API.
46 Circular mode restriction:
47 (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
48 (##) Master 2Lines RxOnly
50 (#) The CRC feature is not managed when the DMA circular mode is enabled
51 (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
52 the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
54 Master Receive mode restriction:
55 (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=1) or
56 bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI
57 does not initiate a new transfer the following procedure has to be respected:
61 Callback registration:
63 (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1U
64 allows the user to configure dynamically the driver callbacks.
65 Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback.
67 Function HAL_SPI_RegisterCallback() allows to register following callbacks:
68 (+) TxCpltCallback : SPI Tx Completed callback
69 (+) RxCpltCallback : SPI Rx Completed callback
70 (+) TxRxCpltCallback : SPI TxRx Completed callback
71 (+) TxHalfCpltCallback : SPI Tx Half Completed callback
72 (+) RxHalfCpltCallback : SPI Rx Half Completed callback
73 (+) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
74 (+) ErrorCallback : SPI Error callback
75 (+) AbortCpltCallback : SPI Abort callback
76 (+) MspInitCallback : SPI Msp Init callback
77 (+) MspDeInitCallback : SPI Msp DeInit callback
78 This function takes as parameters the HAL peripheral handle, the Callback ID
79 and a pointer to the user callback function.
82 (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default
84 HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle,
86 This function allows to reset following callbacks:
87 (+) TxCpltCallback : SPI Tx Completed callback
88 (+) RxCpltCallback : SPI Rx Completed callback
89 (+) TxRxCpltCallback : SPI TxRx Completed callback
90 (+) TxHalfCpltCallback : SPI Tx Half Completed callback
91 (+) RxHalfCpltCallback : SPI Rx Half Completed callback
92 (+) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
93 (+) ErrorCallback : SPI Error callback
94 (+) AbortCpltCallback : SPI Abort callback
95 (+) MspInitCallback : SPI Msp Init callback
96 (+) MspDeInitCallback : SPI Msp DeInit callback
98 By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET
99 all callbacks are set to the corresponding weak functions:
100 examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback().
101 Exception done for MspInit and MspDeInit functions that are
102 reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when
103 these callbacks are null (not registered beforehand).
104 If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit()
105 keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
107 Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only.
108 Exception done MspInit/MspDeInit functions that can be registered/unregistered
109 in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state,
110 thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
111 Then, the user first registers the MspInit/MspDeInit user callbacks
112 using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit()
113 or HAL_SPI_Init() function.
115 When The compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or
116 not defined, the callback registering feature is not available
117 and weak (surcharged) callbacks are used.
120 Using the HAL it is not possible to reach all supported SPI frequency with the different SPI Modes,
121 the following table resume the max SPI frequency reached with data size 8bits/16bits,
122 according to frequency of the APBx Peripheral Clock (fPCLK) used by the SPI instance.
128 DataSize = SPI_DATASIZE_8BIT:
129 +----------------------------------------------------------------------------------------------+
130 | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
131 | Process | Tranfert mode |---------------------|----------------------|----------------------|
132 | | | Master | Slave | Master | Slave | Master | Slave |
133 |==============================================================================================|
134 | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
135 | X |----------------|----------|----------|-----------|----------|-----------|----------|
136 | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
137 | R |----------------|----------|----------|-----------|----------|-----------|----------|
138 | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
139 |=========|================|==========|==========|===========|==========|===========|==========|
140 | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
141 | |----------------|----------|----------|-----------|----------|-----------|----------|
142 | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
143 | X |----------------|----------|----------|-----------|----------|-----------|----------|
144 | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
145 |=========|================|==========|==========|===========|==========|===========|==========|
146 | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
147 | |----------------|----------|----------|-----------|----------|-----------|----------|
148 | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
149 | X |----------------|----------|----------|-----------|----------|-----------|----------|
150 | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
151 +----------------------------------------------------------------------------------------------+
153 DataSize = SPI_DATASIZE_16BIT:
154 +----------------------------------------------------------------------------------------------+
155 | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
156 | Process | Tranfert mode |---------------------|----------------------|----------------------|
157 | | | Master | Slave | Master | Slave | Master | Slave |
158 |==============================================================================================|
159 | T | Polling | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
160 | X |----------------|----------|----------|-----------|----------|-----------|----------|
161 | / | Interrupt | Fpclk/4 | Fpclk/16 | NA | NA | NA | NA |
162 | R |----------------|----------|----------|-----------|----------|-----------|----------|
163 | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
164 |=========|================|==========|==========|===========|==========|===========|==========|
165 | | Polling | Fpclk/4 | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 |
166 | |----------------|----------|----------|-----------|----------|-----------|----------|
167 | R | Interrupt | Fpclk/8 | Fpclk/16 | Fpclk/8 | Fpclk/8 | Fpclk/8 | Fpclk/4 |
168 | X |----------------|----------|----------|-----------|----------|-----------|----------|
169 | | DMA | Fpclk/4 | Fpclk/2 | Fpclk/2 | Fpclk/16 | Fpclk/2 | Fpclk/16 |
170 |=========|================|==========|==========|===========|==========|===========|==========|
171 | | Polling | Fpclk/8 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/8 |
172 | |----------------|----------|----------|-----------|----------|-----------|----------|
173 | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/16 | Fpclk/8 |
174 | X |----------------|----------|----------|-----------|----------|-----------|----------|
175 | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/8 | Fpclk/16 |
176 +----------------------------------------------------------------------------------------------+
177 @note The max SPI frequency depend on SPI data size (4bits, 5bits,..., 8bits,...15bits, 16bits),
178 SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA).
180 (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA()
181 (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA()
182 (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA()
184 ******************************************************************************
187 * <h2><center>© Copyright (c) 2017 STMicroelectronics.
188 * All rights reserved.</center></h2>
190 * This software component is licensed by ST under BSD 3-Clause license,
191 * the "License"; You may not use this file except in compliance with the
192 * License. You may obtain a copy of the License at:
193 * opensource.org/licenses/BSD-3-Clause
195 ******************************************************************************
198 /* Includes ------------------------------------------------------------------*/
199 #include "stm32g4xx_hal.h"
201 /** @addtogroup STM32G4xx_HAL_Driver
205 /** @defgroup SPI SPI
206 * @brief SPI HAL module driver
209 #ifdef HAL_SPI_MODULE_ENABLED
211 /* Private typedef -----------------------------------------------------------*/
212 /* Private defines -----------------------------------------------------------*/
213 /** @defgroup SPI_Private_Constants SPI Private Constants
216 #define SPI_DEFAULT_TIMEOUT 100U
221 /* Private macros ------------------------------------------------------------*/
222 /* Private variables ---------------------------------------------------------*/
223 /* Private function prototypes -----------------------------------------------*/
224 /** @defgroup SPI_Private_Functions SPI Private Functions
227 static void SPI_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
228 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
229 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef
*hdma
);
230 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef
*hdma
);
231 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef
*hdma
);
232 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef
*hdma
);
233 static void SPI_DMAError(DMA_HandleTypeDef
*hdma
);
234 static void SPI_DMAAbortOnError(DMA_HandleTypeDef
*hdma
);
235 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
);
236 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
);
237 static HAL_StatusTypeDef
SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef
*hspi
, uint32_t Flag
, FlagStatus State
,
238 uint32_t Timeout
, uint32_t Tickstart
);
239 static HAL_StatusTypeDef
SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef
*hspi
, uint32_t Fifo
, uint32_t State
,
240 uint32_t Timeout
, uint32_t Tickstart
);
241 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
);
242 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
);
243 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
);
244 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
);
245 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
);
246 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
);
247 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
);
248 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
);
249 #if (USE_SPI_CRC != 0U)
250 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef
*hspi
);
251 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef
*hspi
);
252 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef
*hspi
);
253 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef
*hspi
);
254 #endif /* USE_SPI_CRC */
255 static void SPI_AbortRx_ISR(SPI_HandleTypeDef
*hspi
);
256 static void SPI_AbortTx_ISR(SPI_HandleTypeDef
*hspi
);
257 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef
*hspi
);
258 static void SPI_CloseRx_ISR(SPI_HandleTypeDef
*hspi
);
259 static void SPI_CloseTx_ISR(SPI_HandleTypeDef
*hspi
);
260 static HAL_StatusTypeDef
SPI_EndRxTransaction(SPI_HandleTypeDef
*hspi
, uint32_t Timeout
, uint32_t Tickstart
);
261 static HAL_StatusTypeDef
SPI_EndRxTxTransaction(SPI_HandleTypeDef
*hspi
, uint32_t Timeout
, uint32_t Tickstart
);
266 /* Exported functions --------------------------------------------------------*/
267 /** @defgroup SPI_Exported_Functions SPI Exported Functions
271 /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
272 * @brief Initialization and Configuration functions
275 ===============================================================================
276 ##### Initialization and de-initialization functions #####
277 ===============================================================================
278 [..] This subsection provides a set of functions allowing to initialize and
279 de-initialize the SPIx peripheral:
281 (+) User must implement HAL_SPI_MspInit() function in which he configures
282 all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
284 (+) Call the function HAL_SPI_Init() to configure the selected device with
285 the selected configuration:
289 (++) Clock Polarity and Phase
291 (++) BaudRate Prescaler
295 (++) CRC Polynomial if CRC enabled
296 (++) CRC Length, used only with Data8 and Data16
297 (++) FIFO reception threshold
299 (+) Call the function HAL_SPI_DeInit() to restore the default configuration
300 of the selected SPIx peripheral.
307 * @brief Initialize the SPI according to the specified parameters
308 * in the SPI_InitTypeDef and initialize the associated handle.
309 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
310 * the configuration information for SPI module.
313 HAL_StatusTypeDef
HAL_SPI_Init(SPI_HandleTypeDef
*hspi
)
317 /* Check the SPI handle allocation */
323 /* Check the parameters */
324 assert_param(IS_SPI_ALL_INSTANCE(hspi
->Instance
));
325 assert_param(IS_SPI_MODE(hspi
->Init
.Mode
));
326 assert_param(IS_SPI_DIRECTION(hspi
->Init
.Direction
));
327 assert_param(IS_SPI_DATASIZE(hspi
->Init
.DataSize
));
328 assert_param(IS_SPI_NSS(hspi
->Init
.NSS
));
329 assert_param(IS_SPI_NSSP(hspi
->Init
.NSSPMode
));
330 assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi
->Init
.BaudRatePrescaler
));
331 assert_param(IS_SPI_FIRST_BIT(hspi
->Init
.FirstBit
));
332 assert_param(IS_SPI_TIMODE(hspi
->Init
.TIMode
));
333 if (hspi
->Init
.TIMode
== SPI_TIMODE_DISABLE
)
335 assert_param(IS_SPI_CPOL(hspi
->Init
.CLKPolarity
));
336 assert_param(IS_SPI_CPHA(hspi
->Init
.CLKPhase
));
338 #if (USE_SPI_CRC != 0U)
339 assert_param(IS_SPI_CRC_CALCULATION(hspi
->Init
.CRCCalculation
));
340 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
342 assert_param(IS_SPI_CRC_POLYNOMIAL(hspi
->Init
.CRCPolynomial
));
343 assert_param(IS_SPI_CRC_LENGTH(hspi
->Init
.CRCLength
));
346 hspi
->Init
.CRCCalculation
= SPI_CRCCALCULATION_DISABLE
;
347 #endif /* USE_SPI_CRC */
349 if (hspi
->State
== HAL_SPI_STATE_RESET
)
351 /* Allocate lock resource and initialize it */
352 hspi
->Lock
= HAL_UNLOCKED
;
354 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
355 /* Init the SPI Callback settings */
356 hspi
->TxCpltCallback
= HAL_SPI_TxCpltCallback
; /* Legacy weak TxCpltCallback */
357 hspi
->RxCpltCallback
= HAL_SPI_RxCpltCallback
; /* Legacy weak RxCpltCallback */
358 hspi
->TxRxCpltCallback
= HAL_SPI_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
359 hspi
->TxHalfCpltCallback
= HAL_SPI_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
360 hspi
->RxHalfCpltCallback
= HAL_SPI_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
361 hspi
->TxRxHalfCpltCallback
= HAL_SPI_TxRxHalfCpltCallback
; /* Legacy weak TxRxHalfCpltCallback */
362 hspi
->ErrorCallback
= HAL_SPI_ErrorCallback
; /* Legacy weak ErrorCallback */
363 hspi
->AbortCpltCallback
= HAL_SPI_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
365 if (hspi
->MspInitCallback
== NULL
)
367 hspi
->MspInitCallback
= HAL_SPI_MspInit
; /* Legacy weak MspInit */
370 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
371 hspi
->MspInitCallback(hspi
);
373 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
374 HAL_SPI_MspInit(hspi
);
375 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
378 hspi
->State
= HAL_SPI_STATE_BUSY
;
380 /* Disable the selected SPI peripheral */
381 __HAL_SPI_DISABLE(hspi
);
383 /* Align by default the rs fifo threshold on the data size */
384 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
386 frxth
= SPI_RXFIFO_THRESHOLD_HF
;
390 frxth
= SPI_RXFIFO_THRESHOLD_QF
;
393 /* CRC calculation is valid only for 16Bit and 8 Bit */
394 if ((hspi
->Init
.DataSize
!= SPI_DATASIZE_16BIT
) && (hspi
->Init
.DataSize
!= SPI_DATASIZE_8BIT
))
396 /* CRC must be disabled */
397 hspi
->Init
.CRCCalculation
= SPI_CRCCALCULATION_DISABLE
;
400 /* Align the CRC Length on the data size */
401 if (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_DATASIZE
)
403 /* CRC Length aligned on the data size : value set by default */
404 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
406 hspi
->Init
.CRCLength
= SPI_CRC_LENGTH_16BIT
;
410 hspi
->Init
.CRCLength
= SPI_CRC_LENGTH_8BIT
;
414 /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
415 /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
416 Communication speed, First bit and CRC calculation state */
417 WRITE_REG(hspi
->Instance
->CR1
, (hspi
->Init
.Mode
| hspi
->Init
.Direction
|
418 hspi
->Init
.CLKPolarity
| hspi
->Init
.CLKPhase
| (hspi
->Init
.NSS
& SPI_CR1_SSM
) |
419 hspi
->Init
.BaudRatePrescaler
| hspi
->Init
.FirstBit
| hspi
->Init
.CRCCalculation
));
420 #if (USE_SPI_CRC != 0U)
421 /* Configure : CRC Length */
422 if (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
)
424 hspi
->Instance
->CR1
|= SPI_CR1_CRCL
;
426 #endif /* USE_SPI_CRC */
428 /* Configure : NSS management, TI Mode, NSS Pulse, Data size and Rx Fifo threshold */
429 WRITE_REG(hspi
->Instance
->CR2
, (((hspi
->Init
.NSS
>> 16U) & SPI_CR2_SSOE
) | hspi
->Init
.TIMode
|
430 hspi
->Init
.NSSPMode
| hspi
->Init
.DataSize
) | frxth
);
432 #if (USE_SPI_CRC != 0U)
433 /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
434 /* Configure : CRC Polynomial */
435 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
437 WRITE_REG(hspi
->Instance
->CRCPR
, hspi
->Init
.CRCPolynomial
);
439 #endif /* USE_SPI_CRC */
441 #if defined(SPI_I2SCFGR_I2SMOD)
442 /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
443 CLEAR_BIT(hspi
->Instance
->I2SCFGR
, SPI_I2SCFGR_I2SMOD
);
444 #endif /* SPI_I2SCFGR_I2SMOD */
446 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
447 hspi
->State
= HAL_SPI_STATE_READY
;
453 * @brief De-Initialize the SPI peripheral.
454 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
455 * the configuration information for SPI module.
458 HAL_StatusTypeDef
HAL_SPI_DeInit(SPI_HandleTypeDef
*hspi
)
460 /* Check the SPI handle allocation */
466 /* Check SPI Instance parameter */
467 assert_param(IS_SPI_ALL_INSTANCE(hspi
->Instance
));
469 hspi
->State
= HAL_SPI_STATE_BUSY
;
471 /* Disable the SPI Peripheral Clock */
472 __HAL_SPI_DISABLE(hspi
);
474 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
475 if (hspi
->MspDeInitCallback
== NULL
)
477 hspi
->MspDeInitCallback
= HAL_SPI_MspDeInit
; /* Legacy weak MspDeInit */
480 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
481 hspi
->MspDeInitCallback(hspi
);
483 /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
484 HAL_SPI_MspDeInit(hspi
);
485 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
487 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
488 hspi
->State
= HAL_SPI_STATE_RESET
;
497 * @brief Initialize the SPI MSP.
498 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
499 * the configuration information for SPI module.
502 __weak
void HAL_SPI_MspInit(SPI_HandleTypeDef
*hspi
)
504 /* Prevent unused argument(s) compilation warning */
507 /* NOTE : This function should not be modified, when the callback is needed,
508 the HAL_SPI_MspInit should be implemented in the user file
513 * @brief De-Initialize the SPI MSP.
514 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
515 * the configuration information for SPI module.
518 __weak
void HAL_SPI_MspDeInit(SPI_HandleTypeDef
*hspi
)
520 /* Prevent unused argument(s) compilation warning */
523 /* NOTE : This function should not be modified, when the callback is needed,
524 the HAL_SPI_MspDeInit should be implemented in the user file
528 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
530 * @brief Register a User SPI Callback
531 * To be used instead of the weak predefined callback
532 * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
533 * the configuration information for the specified SPI.
534 * @param CallbackID ID of the callback to be registered
535 * @param pCallback pointer to the Callback function
538 HAL_StatusTypeDef
HAL_SPI_RegisterCallback(SPI_HandleTypeDef
*hspi
, HAL_SPI_CallbackIDTypeDef CallbackID
,
539 pSPI_CallbackTypeDef pCallback
)
541 HAL_StatusTypeDef status
= HAL_OK
;
543 if (pCallback
== NULL
)
545 /* Update the error code */
546 hspi
->ErrorCode
|= HAL_SPI_ERROR_INVALID_CALLBACK
;
553 if (HAL_SPI_STATE_READY
== hspi
->State
)
557 case HAL_SPI_TX_COMPLETE_CB_ID
:
558 hspi
->TxCpltCallback
= pCallback
;
561 case HAL_SPI_RX_COMPLETE_CB_ID
:
562 hspi
->RxCpltCallback
= pCallback
;
565 case HAL_SPI_TX_RX_COMPLETE_CB_ID
:
566 hspi
->TxRxCpltCallback
= pCallback
;
569 case HAL_SPI_TX_HALF_COMPLETE_CB_ID
:
570 hspi
->TxHalfCpltCallback
= pCallback
;
573 case HAL_SPI_RX_HALF_COMPLETE_CB_ID
:
574 hspi
->RxHalfCpltCallback
= pCallback
;
577 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID
:
578 hspi
->TxRxHalfCpltCallback
= pCallback
;
581 case HAL_SPI_ERROR_CB_ID
:
582 hspi
->ErrorCallback
= pCallback
;
585 case HAL_SPI_ABORT_CB_ID
:
586 hspi
->AbortCpltCallback
= pCallback
;
589 case HAL_SPI_MSPINIT_CB_ID
:
590 hspi
->MspInitCallback
= pCallback
;
593 case HAL_SPI_MSPDEINIT_CB_ID
:
594 hspi
->MspDeInitCallback
= pCallback
;
598 /* Update the error code */
599 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
601 /* Return error status */
606 else if (HAL_SPI_STATE_RESET
== hspi
->State
)
610 case HAL_SPI_MSPINIT_CB_ID
:
611 hspi
->MspInitCallback
= pCallback
;
614 case HAL_SPI_MSPDEINIT_CB_ID
:
615 hspi
->MspDeInitCallback
= pCallback
;
619 /* Update the error code */
620 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
622 /* Return error status */
629 /* Update the error code */
630 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
632 /* Return error status */
642 * @brief Unregister an SPI Callback
643 * SPI callback is redirected to the weak predefined callback
644 * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
645 * the configuration information for the specified SPI.
646 * @param CallbackID ID of the callback to be unregistered
649 HAL_StatusTypeDef
HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef
*hspi
, HAL_SPI_CallbackIDTypeDef CallbackID
)
651 HAL_StatusTypeDef status
= HAL_OK
;
656 if (HAL_SPI_STATE_READY
== hspi
->State
)
660 case HAL_SPI_TX_COMPLETE_CB_ID
:
661 hspi
->TxCpltCallback
= HAL_SPI_TxCpltCallback
; /* Legacy weak TxCpltCallback */
664 case HAL_SPI_RX_COMPLETE_CB_ID
:
665 hspi
->RxCpltCallback
= HAL_SPI_RxCpltCallback
; /* Legacy weak RxCpltCallback */
668 case HAL_SPI_TX_RX_COMPLETE_CB_ID
:
669 hspi
->TxRxCpltCallback
= HAL_SPI_TxRxCpltCallback
; /* Legacy weak TxRxCpltCallback */
672 case HAL_SPI_TX_HALF_COMPLETE_CB_ID
:
673 hspi
->TxHalfCpltCallback
= HAL_SPI_TxHalfCpltCallback
; /* Legacy weak TxHalfCpltCallback */
676 case HAL_SPI_RX_HALF_COMPLETE_CB_ID
:
677 hspi
->RxHalfCpltCallback
= HAL_SPI_RxHalfCpltCallback
; /* Legacy weak RxHalfCpltCallback */
680 case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID
:
681 hspi
->TxRxHalfCpltCallback
= HAL_SPI_TxRxHalfCpltCallback
; /* Legacy weak TxRxHalfCpltCallback */
684 case HAL_SPI_ERROR_CB_ID
:
685 hspi
->ErrorCallback
= HAL_SPI_ErrorCallback
; /* Legacy weak ErrorCallback */
688 case HAL_SPI_ABORT_CB_ID
:
689 hspi
->AbortCpltCallback
= HAL_SPI_AbortCpltCallback
; /* Legacy weak AbortCpltCallback */
692 case HAL_SPI_MSPINIT_CB_ID
:
693 hspi
->MspInitCallback
= HAL_SPI_MspInit
; /* Legacy weak MspInit */
696 case HAL_SPI_MSPDEINIT_CB_ID
:
697 hspi
->MspDeInitCallback
= HAL_SPI_MspDeInit
; /* Legacy weak MspDeInit */
701 /* Update the error code */
702 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
704 /* Return error status */
709 else if (HAL_SPI_STATE_RESET
== hspi
->State
)
713 case HAL_SPI_MSPINIT_CB_ID
:
714 hspi
->MspInitCallback
= HAL_SPI_MspInit
; /* Legacy weak MspInit */
717 case HAL_SPI_MSPDEINIT_CB_ID
:
718 hspi
->MspDeInitCallback
= HAL_SPI_MspDeInit
; /* Legacy weak MspDeInit */
722 /* Update the error code */
723 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
725 /* Return error status */
732 /* Update the error code */
733 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_INVALID_CALLBACK
);
735 /* Return error status */
743 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
748 /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
749 * @brief Data transfers functions
752 ==============================================================================
753 ##### IO operation functions #####
754 ===============================================================================
756 This subsection provides a set of functions allowing to manage the SPI
759 [..] The SPI supports master and slave mode :
761 (#) There are two modes of transfer:
762 (++) Blocking mode: The communication is performed in polling mode.
763 The HAL status of all data processing is returned by the same function
764 after finishing transfer.
765 (++) No-Blocking mode: The communication is performed using Interrupts
766 or DMA, These APIs return the HAL status.
767 The end of the data processing will be indicated through the
768 dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
770 The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
771 will be executed respectively at the end of the transmit or Receive process
772 The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
774 (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
775 exist for 1Line (simplex) and 2Lines (full duplex) modes.
782 * @brief Transmit an amount of data in blocking mode.
783 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
784 * the configuration information for SPI module.
785 * @param pData pointer to data buffer
786 * @param Size amount of data to be sent
787 * @param Timeout Timeout duration
790 HAL_StatusTypeDef
HAL_SPI_Transmit(SPI_HandleTypeDef
*hspi
, const uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
793 HAL_StatusTypeDef errorcode
= HAL_OK
;
794 uint16_t initial_TxXferCount
;
796 /* Check Direction parameter */
797 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi
->Init
.Direction
));
802 /* Init tickstart for timeout management*/
803 tickstart
= HAL_GetTick();
804 initial_TxXferCount
= Size
;
806 if (hspi
->State
!= HAL_SPI_STATE_READY
)
808 errorcode
= HAL_BUSY
;
812 if ((pData
== NULL
) || (Size
== 0U))
814 errorcode
= HAL_ERROR
;
818 /* Set the transaction information */
819 hspi
->State
= HAL_SPI_STATE_BUSY_TX
;
820 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
821 hspi
->pTxBuffPtr
= (uint8_t *)pData
;
822 hspi
->TxXferSize
= Size
;
823 hspi
->TxXferCount
= Size
;
825 /*Init field not used in handle to zero */
826 hspi
->pRxBuffPtr
= (uint8_t *)NULL
;
827 hspi
->RxXferSize
= 0U;
828 hspi
->RxXferCount
= 0U;
832 /* Configure communication direction : 1Line */
833 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
838 #if (USE_SPI_CRC != 0U)
839 /* Reset CRC Calculation */
840 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
844 #endif /* USE_SPI_CRC */
846 /* Check if the SPI is already enabled */
847 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
849 /* Enable SPI peripheral */
850 __HAL_SPI_ENABLE(hspi
);
853 /* Transmit data in 16 Bit mode */
854 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
856 if ((hspi
->Init
.Mode
== SPI_MODE_SLAVE
) || (initial_TxXferCount
== 0x01U
))
858 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
859 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
862 /* Transmit data in 16 Bit mode */
863 while (hspi
->TxXferCount
> 0U)
865 /* Wait until TXE flag is set to send data */
866 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_TXE
))
868 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
869 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
874 /* Timeout management */
875 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
)) || (Timeout
== 0U))
877 errorcode
= HAL_TIMEOUT
;
883 /* Transmit data in 8 Bit mode */
886 if ((hspi
->Init
.Mode
== SPI_MODE_SLAVE
) || (initial_TxXferCount
== 0x01U
))
888 if (hspi
->TxXferCount
> 1U)
890 /* write on the data register in packing mode */
891 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
892 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
893 hspi
->TxXferCount
-= 2U;
897 *((__IO
uint8_t *)&hspi
->Instance
->DR
) = (*hspi
->pTxBuffPtr
);
902 while (hspi
->TxXferCount
> 0U)
904 /* Wait until TXE flag is set to send data */
905 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_TXE
))
907 if (hspi
->TxXferCount
> 1U)
909 /* write on the data register in packing mode */
910 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
911 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
912 hspi
->TxXferCount
-= 2U;
916 *((__IO
uint8_t *)&hspi
->Instance
->DR
) = (*hspi
->pTxBuffPtr
);
923 /* Timeout management */
924 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
)) || (Timeout
== 0U))
926 errorcode
= HAL_TIMEOUT
;
932 #if (USE_SPI_CRC != 0U)
933 /* Enable CRC Transmission */
934 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
936 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
938 #endif /* USE_SPI_CRC */
940 /* Check the end of the transaction */
941 if (SPI_EndRxTxTransaction(hspi
, Timeout
, tickstart
) != HAL_OK
)
943 hspi
->ErrorCode
= HAL_SPI_ERROR_FLAG
;
946 /* Clear overrun flag in 2 Lines communication mode because received is not read */
947 if (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
)
949 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
952 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
954 errorcode
= HAL_ERROR
;
958 hspi
->State
= HAL_SPI_STATE_READY
;
959 /* Process Unlocked */
965 * @brief Receive an amount of data in blocking mode.
966 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
967 * the configuration information for SPI module.
968 * @param pData pointer to data buffer
969 * @param Size amount of data to be received
970 * @param Timeout Timeout duration
973 HAL_StatusTypeDef
HAL_SPI_Receive(SPI_HandleTypeDef
*hspi
, uint8_t *pData
, uint16_t Size
, uint32_t Timeout
)
976 HAL_StatusTypeDef errorcode
= HAL_OK
;
978 if ((hspi
->Init
.Mode
== SPI_MODE_MASTER
) && (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
))
980 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
981 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
982 return HAL_SPI_TransmitReceive(hspi
, pData
, pData
, Size
, Timeout
);
988 /* Init tickstart for timeout management*/
989 tickstart
= HAL_GetTick();
991 if (hspi
->State
!= HAL_SPI_STATE_READY
)
993 errorcode
= HAL_BUSY
;
997 if ((pData
== NULL
) || (Size
== 0U))
999 errorcode
= HAL_ERROR
;
1003 /* Set the transaction information */
1004 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
1005 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1006 hspi
->pRxBuffPtr
= (uint8_t *)pData
;
1007 hspi
->RxXferSize
= Size
;
1008 hspi
->RxXferCount
= Size
;
1010 /*Init field not used in handle to zero */
1011 hspi
->pTxBuffPtr
= (uint8_t *)NULL
;
1012 hspi
->TxXferSize
= 0U;
1013 hspi
->TxXferCount
= 0U;
1017 #if (USE_SPI_CRC != 0U)
1018 /* Reset CRC Calculation */
1019 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1021 SPI_RESET_CRC(hspi
);
1022 /* this is done to handle the CRCNEXT before the latest data */
1023 hspi
->RxXferCount
--;
1025 #endif /* USE_SPI_CRC */
1027 /* Set the Rx Fifo threshold */
1028 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1030 /* Set RX Fifo threshold according the reception data length: 16bit */
1031 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1035 /* Set RX Fifo threshold according the reception data length: 8bit */
1036 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1039 /* Configure communication direction: 1Line */
1040 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
1045 /* Check if the SPI is already enabled */
1046 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1048 /* Enable SPI peripheral */
1049 __HAL_SPI_ENABLE(hspi
);
1052 /* Receive data in 8 Bit mode */
1053 if (hspi
->Init
.DataSize
<= SPI_DATASIZE_8BIT
)
1056 while (hspi
->RxXferCount
> 0U)
1058 /* Check the RXNE flag */
1059 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_RXNE
))
1061 /* read the received data */
1062 (* (uint8_t *)hspi
->pRxBuffPtr
) = *(__IO
uint8_t *)&hspi
->Instance
->DR
;
1063 hspi
->pRxBuffPtr
+= sizeof(uint8_t);
1064 hspi
->RxXferCount
--;
1068 /* Timeout management */
1069 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
)) || (Timeout
== 0U))
1071 errorcode
= HAL_TIMEOUT
;
1080 while (hspi
->RxXferCount
> 0U)
1082 /* Check the RXNE flag */
1083 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_RXNE
))
1085 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)hspi
->Instance
->DR
;
1086 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
1087 hspi
->RxXferCount
--;
1091 /* Timeout management */
1092 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
)) || (Timeout
== 0U))
1094 errorcode
= HAL_TIMEOUT
;
1101 #if (USE_SPI_CRC != 0U)
1102 /* Handle the CRC Transmission */
1103 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1105 /* freeze the CRC before the latest data */
1106 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
1108 /* Read the latest data */
1109 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, Timeout
, tickstart
) != HAL_OK
)
1111 /* the latest data has not been received */
1112 errorcode
= HAL_TIMEOUT
;
1116 /* Receive last data in 16 Bit mode */
1117 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1119 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)hspi
->Instance
->DR
;
1121 /* Receive last data in 8 Bit mode */
1124 (*(uint8_t *)hspi
->pRxBuffPtr
) = *(__IO
uint8_t *)&hspi
->Instance
->DR
;
1127 /* Wait the CRC data */
1128 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, Timeout
, tickstart
) != HAL_OK
)
1130 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1131 errorcode
= HAL_TIMEOUT
;
1135 /* Read CRC to Flush DR and RXNE flag */
1136 if (hspi
->Init
.DataSize
== SPI_DATASIZE_16BIT
)
1138 /* Read 16bit CRC */
1139 READ_REG(hspi
->Instance
->DR
);
1144 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
1146 if ((hspi
->Init
.DataSize
== SPI_DATASIZE_8BIT
) && (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
))
1148 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, Timeout
, tickstart
) != HAL_OK
)
1150 /* Error on the CRC reception */
1151 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1152 errorcode
= HAL_TIMEOUT
;
1155 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
1156 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
1160 #endif /* USE_SPI_CRC */
1162 /* Check the end of the transaction */
1163 if (SPI_EndRxTransaction(hspi
, Timeout
, tickstart
) != HAL_OK
)
1165 hspi
->ErrorCode
= HAL_SPI_ERROR_FLAG
;
1168 #if (USE_SPI_CRC != 0U)
1169 /* Check if CRC error occurred */
1170 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
))
1172 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1173 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
1175 #endif /* USE_SPI_CRC */
1177 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
1179 errorcode
= HAL_ERROR
;
1183 hspi
->State
= HAL_SPI_STATE_READY
;
1189 * @brief Transmit and Receive an amount of data in blocking mode.
1190 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1191 * the configuration information for SPI module.
1192 * @param pTxData pointer to transmission data buffer
1193 * @param pRxData pointer to reception data buffer
1194 * @param Size amount of data to be sent and received
1195 * @param Timeout Timeout duration
1196 * @retval HAL status
1198 HAL_StatusTypeDef
HAL_SPI_TransmitReceive(SPI_HandleTypeDef
*hspi
, const uint8_t *pTxData
, uint8_t *pRxData
, uint16_t Size
,
1201 uint16_t initial_TxXferCount
;
1202 uint16_t initial_RxXferCount
;
1204 HAL_SPI_StateTypeDef tmp_state
;
1206 #if (USE_SPI_CRC != 0U)
1209 #endif /* USE_SPI_CRC */
1211 /* Variable used to alternate Rx and Tx during transfer */
1212 uint32_t txallowed
= 1U;
1213 HAL_StatusTypeDef errorcode
= HAL_OK
;
1215 /* Check Direction parameter */
1216 assert_param(IS_SPI_DIRECTION_2LINES(hspi
->Init
.Direction
));
1218 /* Process Locked */
1221 /* Init tickstart for timeout management*/
1222 tickstart
= HAL_GetTick();
1224 /* Init temporary variables */
1225 tmp_state
= hspi
->State
;
1226 tmp_mode
= hspi
->Init
.Mode
;
1227 initial_TxXferCount
= Size
;
1228 initial_RxXferCount
= Size
;
1229 #if (USE_SPI_CRC != 0U)
1230 spi_cr1
= READ_REG(hspi
->Instance
->CR1
);
1231 spi_cr2
= READ_REG(hspi
->Instance
->CR2
);
1232 #endif /* USE_SPI_CRC */
1234 if (!((tmp_state
== HAL_SPI_STATE_READY
) || \
1235 ((tmp_mode
== SPI_MODE_MASTER
) && (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
) && (tmp_state
== HAL_SPI_STATE_BUSY_RX
))))
1237 errorcode
= HAL_BUSY
;
1241 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1243 errorcode
= HAL_ERROR
;
1247 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1248 if (hspi
->State
!= HAL_SPI_STATE_BUSY_RX
)
1250 hspi
->State
= HAL_SPI_STATE_BUSY_TX_RX
;
1253 /* Set the transaction information */
1254 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1255 hspi
->pRxBuffPtr
= (uint8_t *)pRxData
;
1256 hspi
->RxXferCount
= Size
;
1257 hspi
->RxXferSize
= Size
;
1258 hspi
->pTxBuffPtr
= (uint8_t *)pTxData
;
1259 hspi
->TxXferCount
= Size
;
1260 hspi
->TxXferSize
= Size
;
1262 /*Init field not used in handle to zero */
1266 #if (USE_SPI_CRC != 0U)
1267 /* Reset CRC Calculation */
1268 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1270 SPI_RESET_CRC(hspi
);
1272 #endif /* USE_SPI_CRC */
1274 /* Set the Rx Fifo threshold */
1275 if ((hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
) || (initial_RxXferCount
> 1U))
1277 /* Set fiforxthreshold according the reception data length: 16bit */
1278 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1282 /* Set fiforxthreshold according the reception data length: 8bit */
1283 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1286 /* Check if the SPI is already enabled */
1287 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1289 /* Enable SPI peripheral */
1290 __HAL_SPI_ENABLE(hspi
);
1293 /* Transmit and Receive data in 16 Bit mode */
1294 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1296 if ((hspi
->Init
.Mode
== SPI_MODE_SLAVE
) || (initial_TxXferCount
== 0x01U
))
1298 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
1299 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
1300 hspi
->TxXferCount
--;
1302 while ((hspi
->TxXferCount
> 0U) || (hspi
->RxXferCount
> 0U))
1304 /* Check TXE flag */
1305 if ((__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_TXE
)) && (hspi
->TxXferCount
> 0U) && (txallowed
== 1U))
1307 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
1308 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
1309 hspi
->TxXferCount
--;
1310 /* Next Data is a reception (Rx). Tx not allowed */
1313 #if (USE_SPI_CRC != 0U)
1314 /* Enable CRC Transmission */
1315 if ((hspi
->TxXferCount
== 0U) && (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
))
1317 /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
1318 if ((READ_BIT(spi_cr1
, SPI_CR1_MSTR
) == 0U) && (READ_BIT(spi_cr2
, SPI_CR2_NSSP
) == SPI_CR2_NSSP
))
1320 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_SSM
);
1322 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
1324 #endif /* USE_SPI_CRC */
1327 /* Check RXNE flag */
1328 if ((__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_RXNE
)) && (hspi
->RxXferCount
> 0U))
1330 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)hspi
->Instance
->DR
;
1331 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
1332 hspi
->RxXferCount
--;
1333 /* Next Data is a Transmission (Tx). Tx is allowed */
1336 if (((HAL_GetTick() - tickstart
) >= Timeout
) && (Timeout
!= HAL_MAX_DELAY
))
1338 errorcode
= HAL_TIMEOUT
;
1343 /* Transmit and Receive data in 8 Bit mode */
1346 if ((hspi
->Init
.Mode
== SPI_MODE_SLAVE
) || (initial_TxXferCount
== 0x01U
))
1348 if (hspi
->TxXferCount
> 1U)
1350 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
1351 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
1352 hspi
->TxXferCount
-= 2U;
1356 *(__IO
uint8_t *)&hspi
->Instance
->DR
= (*hspi
->pTxBuffPtr
);
1358 hspi
->TxXferCount
--;
1361 while ((hspi
->TxXferCount
> 0U) || (hspi
->RxXferCount
> 0U))
1363 /* Check TXE flag */
1364 if ((__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_TXE
)) && (hspi
->TxXferCount
> 0U) && (txallowed
== 1U))
1366 if (hspi
->TxXferCount
> 1U)
1368 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
1369 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
1370 hspi
->TxXferCount
-= 2U;
1374 *(__IO
uint8_t *)&hspi
->Instance
->DR
= (*hspi
->pTxBuffPtr
);
1376 hspi
->TxXferCount
--;
1378 /* Next Data is a reception (Rx). Tx not allowed */
1381 #if (USE_SPI_CRC != 0U)
1382 /* Enable CRC Transmission */
1383 if ((hspi
->TxXferCount
== 0U) && (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
))
1385 /* Set NSS Soft to received correctly the CRC on slave mode with NSS pulse activated */
1386 if ((READ_BIT(spi_cr1
, SPI_CR1_MSTR
) == 0U) && (READ_BIT(spi_cr2
, SPI_CR2_NSSP
) == SPI_CR2_NSSP
))
1388 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_SSM
);
1390 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
1392 #endif /* USE_SPI_CRC */
1395 /* Wait until RXNE flag is reset */
1396 if ((__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_RXNE
)) && (hspi
->RxXferCount
> 0U))
1398 if (hspi
->RxXferCount
> 1U)
1400 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)hspi
->Instance
->DR
;
1401 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
1402 hspi
->RxXferCount
-= 2U;
1403 if (hspi
->RxXferCount
<= 1U)
1405 /* Set RX Fifo threshold before to switch on 8 bit data size */
1406 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1411 (*(uint8_t *)hspi
->pRxBuffPtr
) = *(__IO
uint8_t *)&hspi
->Instance
->DR
;
1413 hspi
->RxXferCount
--;
1415 /* Next Data is a Transmission (Tx). Tx is allowed */
1418 if ((((HAL_GetTick() - tickstart
) >= Timeout
) && ((Timeout
!= HAL_MAX_DELAY
))) || (Timeout
== 0U))
1420 errorcode
= HAL_TIMEOUT
;
1426 #if (USE_SPI_CRC != 0U)
1427 /* Read CRC from DR to close CRC calculation process */
1428 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1430 /* Wait until TXE flag */
1431 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, Timeout
, tickstart
) != HAL_OK
)
1433 /* Error on the CRC reception */
1434 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1435 errorcode
= HAL_TIMEOUT
;
1439 if (hspi
->Init
.DataSize
== SPI_DATASIZE_16BIT
)
1441 /* Read 16bit CRC */
1442 READ_REG(hspi
->Instance
->DR
);
1447 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
1449 if (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
)
1451 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, Timeout
, tickstart
) != HAL_OK
)
1453 /* Error on the CRC reception */
1454 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1455 errorcode
= HAL_TIMEOUT
;
1458 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
1459 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
1464 /* Check if CRC error occurred */
1465 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
))
1467 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
1468 /* Clear CRC Flag */
1469 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
1471 errorcode
= HAL_ERROR
;
1473 #endif /* USE_SPI_CRC */
1475 /* Check the end of the transaction */
1476 if (SPI_EndRxTxTransaction(hspi
, Timeout
, tickstart
) != HAL_OK
)
1478 errorcode
= HAL_ERROR
;
1479 hspi
->ErrorCode
= HAL_SPI_ERROR_FLAG
;
1483 hspi
->State
= HAL_SPI_STATE_READY
;
1489 * @brief Transmit an amount of data in non-blocking mode with Interrupt.
1490 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1491 * the configuration information for SPI module.
1492 * @param pData pointer to data buffer
1493 * @param Size amount of data to be sent
1494 * @retval HAL status
1496 HAL_StatusTypeDef
HAL_SPI_Transmit_IT(SPI_HandleTypeDef
*hspi
, uint8_t *pData
, uint16_t Size
)
1498 HAL_StatusTypeDef errorcode
= HAL_OK
;
1500 /* Check Direction parameter */
1501 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi
->Init
.Direction
));
1503 /* Process Locked */
1506 if ((pData
== NULL
) || (Size
== 0U))
1508 errorcode
= HAL_ERROR
;
1512 if (hspi
->State
!= HAL_SPI_STATE_READY
)
1514 errorcode
= HAL_BUSY
;
1518 /* Set the transaction information */
1519 hspi
->State
= HAL_SPI_STATE_BUSY_TX
;
1520 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1521 hspi
->pTxBuffPtr
= (uint8_t *)pData
;
1522 hspi
->TxXferSize
= Size
;
1523 hspi
->TxXferCount
= Size
;
1525 /* Init field not used in handle to zero */
1526 hspi
->pRxBuffPtr
= (uint8_t *)NULL
;
1527 hspi
->RxXferSize
= 0U;
1528 hspi
->RxXferCount
= 0U;
1531 /* Set the function for IT treatment */
1532 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1534 hspi
->TxISR
= SPI_TxISR_16BIT
;
1538 hspi
->TxISR
= SPI_TxISR_8BIT
;
1541 /* Configure communication direction : 1Line */
1542 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
1547 #if (USE_SPI_CRC != 0U)
1548 /* Reset CRC Calculation */
1549 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1551 SPI_RESET_CRC(hspi
);
1553 #endif /* USE_SPI_CRC */
1555 /* Enable TXE and ERR interrupt */
1556 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_TXE
| SPI_IT_ERR
));
1559 /* Check if the SPI is already enabled */
1560 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1562 /* Enable SPI peripheral */
1563 __HAL_SPI_ENABLE(hspi
);
1572 * @brief Receive an amount of data in non-blocking mode with Interrupt.
1573 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1574 * the configuration information for SPI module.
1575 * @param pData pointer to data buffer
1576 * @param Size amount of data to be sent
1577 * @retval HAL status
1579 HAL_StatusTypeDef
HAL_SPI_Receive_IT(SPI_HandleTypeDef
*hspi
, uint8_t *pData
, uint16_t Size
)
1581 HAL_StatusTypeDef errorcode
= HAL_OK
;
1583 if ((hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
) && (hspi
->Init
.Mode
== SPI_MODE_MASTER
))
1585 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
1586 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1587 return HAL_SPI_TransmitReceive_IT(hspi
, pData
, pData
, Size
);
1590 /* Process Locked */
1593 if (hspi
->State
!= HAL_SPI_STATE_READY
)
1595 errorcode
= HAL_BUSY
;
1599 if ((pData
== NULL
) || (Size
== 0U))
1601 errorcode
= HAL_ERROR
;
1605 /* Set the transaction information */
1606 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
1607 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1608 hspi
->pRxBuffPtr
= (uint8_t *)pData
;
1609 hspi
->RxXferSize
= Size
;
1610 hspi
->RxXferCount
= Size
;
1612 /* Init field not used in handle to zero */
1613 hspi
->pTxBuffPtr
= (uint8_t *)NULL
;
1614 hspi
->TxXferSize
= 0U;
1615 hspi
->TxXferCount
= 0U;
1618 /* Check the data size to adapt Rx threshold and the set the function for IT treatment */
1619 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1621 /* Set RX Fifo threshold according the reception data length: 16 bit */
1622 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1623 hspi
->RxISR
= SPI_RxISR_16BIT
;
1627 /* Set RX Fifo threshold according the reception data length: 8 bit */
1628 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1629 hspi
->RxISR
= SPI_RxISR_8BIT
;
1632 /* Configure communication direction : 1Line */
1633 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
1638 #if (USE_SPI_CRC != 0U)
1639 /* Reset CRC Calculation */
1640 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1643 if ((hspi
->Init
.DataSize
<= SPI_DATASIZE_8BIT
) && (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
))
1647 SPI_RESET_CRC(hspi
);
1653 #endif /* USE_SPI_CRC */
1655 /* Enable TXE and ERR interrupt */
1656 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_RXNE
| SPI_IT_ERR
));
1658 /* Note : The SPI must be enabled after unlocking current process
1659 to avoid the risk of SPI interrupt handle execution before current
1662 /* Check if the SPI is already enabled */
1663 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1665 /* Enable SPI peripheral */
1666 __HAL_SPI_ENABLE(hspi
);
1670 /* Process Unlocked */
1676 * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt.
1677 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1678 * the configuration information for SPI module.
1679 * @param pTxData pointer to transmission data buffer
1680 * @param pRxData pointer to reception data buffer
1681 * @param Size amount of data to be sent and received
1682 * @retval HAL status
1684 HAL_StatusTypeDef
HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef
*hspi
, uint8_t *pTxData
, uint8_t *pRxData
, uint16_t Size
)
1687 HAL_SPI_StateTypeDef tmp_state
;
1688 HAL_StatusTypeDef errorcode
= HAL_OK
;
1690 /* Check Direction parameter */
1691 assert_param(IS_SPI_DIRECTION_2LINES(hspi
->Init
.Direction
));
1693 /* Process locked */
1696 /* Init temporary variables */
1697 tmp_state
= hspi
->State
;
1698 tmp_mode
= hspi
->Init
.Mode
;
1700 if (!((tmp_state
== HAL_SPI_STATE_READY
) || \
1701 ((tmp_mode
== SPI_MODE_MASTER
) && (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
) && (tmp_state
== HAL_SPI_STATE_BUSY_RX
))))
1703 errorcode
= HAL_BUSY
;
1707 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
1709 errorcode
= HAL_ERROR
;
1713 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
1714 if (hspi
->State
!= HAL_SPI_STATE_BUSY_RX
)
1716 hspi
->State
= HAL_SPI_STATE_BUSY_TX_RX
;
1719 /* Set the transaction information */
1720 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1721 hspi
->pTxBuffPtr
= (uint8_t *)pTxData
;
1722 hspi
->TxXferSize
= Size
;
1723 hspi
->TxXferCount
= Size
;
1724 hspi
->pRxBuffPtr
= (uint8_t *)pRxData
;
1725 hspi
->RxXferSize
= Size
;
1726 hspi
->RxXferCount
= Size
;
1728 /* Set the function for IT treatment */
1729 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1731 hspi
->RxISR
= SPI_2linesRxISR_16BIT
;
1732 hspi
->TxISR
= SPI_2linesTxISR_16BIT
;
1736 hspi
->RxISR
= SPI_2linesRxISR_8BIT
;
1737 hspi
->TxISR
= SPI_2linesTxISR_8BIT
;
1740 #if (USE_SPI_CRC != 0U)
1741 /* Reset CRC Calculation */
1742 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1745 if ((hspi
->Init
.DataSize
<= SPI_DATASIZE_8BIT
) && (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
))
1749 SPI_RESET_CRC(hspi
);
1755 #endif /* USE_SPI_CRC */
1757 /* Check if packing mode is enabled and if there is more than 2 data to receive */
1758 if ((hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
) || (Size
>= 2U))
1760 /* Set RX Fifo threshold according the reception data length: 16 bit */
1761 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1765 /* Set RX Fifo threshold according the reception data length: 8 bit */
1766 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1769 /* Enable TXE, RXNE and ERR interrupt */
1770 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_TXE
| SPI_IT_RXNE
| SPI_IT_ERR
));
1772 /* Check if the SPI is already enabled */
1773 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1775 /* Enable SPI peripheral */
1776 __HAL_SPI_ENABLE(hspi
);
1780 /* Process Unlocked */
1786 * @brief Transmit an amount of data in non-blocking mode with DMA.
1787 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1788 * the configuration information for SPI module.
1789 * @param pData pointer to data buffer
1790 * @param Size amount of data to be sent
1791 * @retval HAL status
1793 HAL_StatusTypeDef
HAL_SPI_Transmit_DMA(SPI_HandleTypeDef
*hspi
, uint8_t *pData
, uint16_t Size
)
1795 HAL_StatusTypeDef errorcode
= HAL_OK
;
1797 /* Check tx dma handle */
1798 assert_param(IS_SPI_DMA_HANDLE(hspi
->hdmatx
));
1800 /* Check Direction parameter */
1801 assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi
->Init
.Direction
));
1803 /* Process Locked */
1806 if (hspi
->State
!= HAL_SPI_STATE_READY
)
1808 errorcode
= HAL_BUSY
;
1812 if ((pData
== NULL
) || (Size
== 0U))
1814 errorcode
= HAL_ERROR
;
1818 /* Set the transaction information */
1819 hspi
->State
= HAL_SPI_STATE_BUSY_TX
;
1820 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1821 hspi
->pTxBuffPtr
= (uint8_t *)pData
;
1822 hspi
->TxXferSize
= Size
;
1823 hspi
->TxXferCount
= Size
;
1825 /* Init field not used in handle to zero */
1826 hspi
->pRxBuffPtr
= (uint8_t *)NULL
;
1829 hspi
->RxXferSize
= 0U;
1830 hspi
->RxXferCount
= 0U;
1832 /* Configure communication direction : 1Line */
1833 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
1838 #if (USE_SPI_CRC != 0U)
1839 /* Reset CRC Calculation */
1840 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1842 SPI_RESET_CRC(hspi
);
1844 #endif /* USE_SPI_CRC */
1846 /* Set the SPI TxDMA Half transfer complete callback */
1847 hspi
->hdmatx
->XferHalfCpltCallback
= SPI_DMAHalfTransmitCplt
;
1849 /* Set the SPI TxDMA transfer complete callback */
1850 hspi
->hdmatx
->XferCpltCallback
= SPI_DMATransmitCplt
;
1852 /* Set the DMA error callback */
1853 hspi
->hdmatx
->XferErrorCallback
= SPI_DMAError
;
1855 /* Set the DMA AbortCpltCallback */
1856 hspi
->hdmatx
->XferAbortCallback
= NULL
;
1858 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
);
1859 /* Packing mode is enabled only if the DMA setting is HALWORD */
1860 if ((hspi
->Init
.DataSize
<= SPI_DATASIZE_8BIT
) && (hspi
->hdmatx
->Init
.MemDataAlignment
== DMA_MDATAALIGN_HALFWORD
))
1862 /* Check the even/odd of the data size + crc if enabled */
1863 if ((hspi
->TxXferCount
& 0x1U
) == 0U)
1865 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
);
1866 hspi
->TxXferCount
= (hspi
->TxXferCount
>> 1U);
1870 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
);
1871 hspi
->TxXferCount
= (hspi
->TxXferCount
>> 1U) + 1U;
1875 /* Enable the Tx DMA Stream/Channel */
1876 if (HAL_OK
!= HAL_DMA_Start_IT(hspi
->hdmatx
, (uint32_t)hspi
->pTxBuffPtr
, (uint32_t)&hspi
->Instance
->DR
,
1879 /* Update SPI error code */
1880 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
1881 errorcode
= HAL_ERROR
;
1883 hspi
->State
= HAL_SPI_STATE_READY
;
1887 /* Check if the SPI is already enabled */
1888 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
1890 /* Enable SPI peripheral */
1891 __HAL_SPI_ENABLE(hspi
);
1894 /* Enable the SPI Error Interrupt Bit */
1895 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_ERR
));
1897 /* Enable Tx DMA Request */
1898 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
1901 /* Process Unlocked */
1907 * @brief Receive an amount of data in non-blocking mode with DMA.
1908 * @note In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined.
1909 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
1910 * the configuration information for SPI module.
1911 * @param pData pointer to data buffer
1912 * @note When the CRC feature is enabled the pData Length must be Size + 1.
1913 * @param Size amount of data to be sent
1914 * @retval HAL status
1916 HAL_StatusTypeDef
HAL_SPI_Receive_DMA(SPI_HandleTypeDef
*hspi
, uint8_t *pData
, uint16_t Size
)
1918 HAL_StatusTypeDef errorcode
= HAL_OK
;
1920 /* Check rx dma handle */
1921 assert_param(IS_SPI_DMA_HANDLE(hspi
->hdmarx
));
1923 if ((hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
) && (hspi
->Init
.Mode
== SPI_MODE_MASTER
))
1925 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
1927 /* Check tx dma handle */
1928 assert_param(IS_SPI_DMA_HANDLE(hspi
->hdmatx
));
1930 /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
1931 return HAL_SPI_TransmitReceive_DMA(hspi
, pData
, pData
, Size
);
1934 /* Process Locked */
1937 if (hspi
->State
!= HAL_SPI_STATE_READY
)
1939 errorcode
= HAL_BUSY
;
1943 if ((pData
== NULL
) || (Size
== 0U))
1945 errorcode
= HAL_ERROR
;
1949 /* Set the transaction information */
1950 hspi
->State
= HAL_SPI_STATE_BUSY_RX
;
1951 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
1952 hspi
->pRxBuffPtr
= (uint8_t *)pData
;
1953 hspi
->RxXferSize
= Size
;
1954 hspi
->RxXferCount
= Size
;
1956 /*Init field not used in handle to zero */
1959 hspi
->TxXferSize
= 0U;
1960 hspi
->TxXferCount
= 0U;
1962 /* Configure communication direction : 1Line */
1963 if (hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
1968 #if (USE_SPI_CRC != 0U)
1969 /* Reset CRC Calculation */
1970 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
1972 SPI_RESET_CRC(hspi
);
1974 #endif /* USE_SPI_CRC */
1977 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMARX
);
1978 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
1980 /* Set RX Fifo threshold according the reception data length: 16bit */
1981 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1985 /* Set RX Fifo threshold according the reception data length: 8bit */
1986 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1988 if (hspi
->hdmarx
->Init
.MemDataAlignment
== DMA_MDATAALIGN_HALFWORD
)
1990 /* Set RX Fifo threshold according the reception data length: 16bit */
1991 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
1993 if ((hspi
->RxXferCount
& 0x1U
) == 0x0U
)
1995 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMARX
);
1996 hspi
->RxXferCount
= hspi
->RxXferCount
>> 1U;
2000 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMARX
);
2001 hspi
->RxXferCount
= (hspi
->RxXferCount
>> 1U) + 1U;
2006 /* Set the SPI RxDMA Half transfer complete callback */
2007 hspi
->hdmarx
->XferHalfCpltCallback
= SPI_DMAHalfReceiveCplt
;
2009 /* Set the SPI Rx DMA transfer complete callback */
2010 hspi
->hdmarx
->XferCpltCallback
= SPI_DMAReceiveCplt
;
2012 /* Set the DMA error callback */
2013 hspi
->hdmarx
->XferErrorCallback
= SPI_DMAError
;
2015 /* Set the DMA AbortCpltCallback */
2016 hspi
->hdmarx
->XferAbortCallback
= NULL
;
2018 /* Enable the Rx DMA Stream/Channel */
2019 if (HAL_OK
!= HAL_DMA_Start_IT(hspi
->hdmarx
, (uint32_t)&hspi
->Instance
->DR
, (uint32_t)hspi
->pRxBuffPtr
,
2022 /* Update SPI error code */
2023 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
2024 errorcode
= HAL_ERROR
;
2026 hspi
->State
= HAL_SPI_STATE_READY
;
2030 /* Check if the SPI is already enabled */
2031 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
2033 /* Enable SPI peripheral */
2034 __HAL_SPI_ENABLE(hspi
);
2037 /* Enable the SPI Error Interrupt Bit */
2038 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_ERR
));
2040 /* Enable Rx DMA Request */
2041 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
2044 /* Process Unlocked */
2050 * @brief Transmit and Receive an amount of data in non-blocking mode with DMA.
2051 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2052 * the configuration information for SPI module.
2053 * @param pTxData pointer to transmission data buffer
2054 * @param pRxData pointer to reception data buffer
2055 * @note When the CRC feature is enabled the pRxData Length must be Size + 1
2056 * @param Size amount of data to be sent
2057 * @retval HAL status
2059 HAL_StatusTypeDef
HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef
*hspi
, uint8_t *pTxData
, uint8_t *pRxData
,
2063 HAL_SPI_StateTypeDef tmp_state
;
2064 HAL_StatusTypeDef errorcode
= HAL_OK
;
2066 /* Check rx & tx dma handles */
2067 assert_param(IS_SPI_DMA_HANDLE(hspi
->hdmarx
));
2068 assert_param(IS_SPI_DMA_HANDLE(hspi
->hdmatx
));
2070 /* Check Direction parameter */
2071 assert_param(IS_SPI_DIRECTION_2LINES(hspi
->Init
.Direction
));
2073 /* Process locked */
2076 /* Init temporary variables */
2077 tmp_state
= hspi
->State
;
2078 tmp_mode
= hspi
->Init
.Mode
;
2080 if (!((tmp_state
== HAL_SPI_STATE_READY
) ||
2081 ((tmp_mode
== SPI_MODE_MASTER
) && (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
) && (tmp_state
== HAL_SPI_STATE_BUSY_RX
))))
2083 errorcode
= HAL_BUSY
;
2087 if ((pTxData
== NULL
) || (pRxData
== NULL
) || (Size
== 0U))
2089 errorcode
= HAL_ERROR
;
2093 /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
2094 if (hspi
->State
!= HAL_SPI_STATE_BUSY_RX
)
2096 hspi
->State
= HAL_SPI_STATE_BUSY_TX_RX
;
2099 /* Set the transaction information */
2100 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
2101 hspi
->pTxBuffPtr
= (uint8_t *)pTxData
;
2102 hspi
->TxXferSize
= Size
;
2103 hspi
->TxXferCount
= Size
;
2104 hspi
->pRxBuffPtr
= (uint8_t *)pRxData
;
2105 hspi
->RxXferSize
= Size
;
2106 hspi
->RxXferCount
= Size
;
2108 /* Init field not used in handle to zero */
2112 #if (USE_SPI_CRC != 0U)
2113 /* Reset CRC Calculation */
2114 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
2116 SPI_RESET_CRC(hspi
);
2118 #endif /* USE_SPI_CRC */
2120 /* Reset the threshold bit */
2121 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
| SPI_CR2_LDMARX
);
2123 /* The packing mode management is enabled by the DMA settings according the spi data size */
2124 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
2126 /* Set fiforxthreshold according the reception data length: 16bit */
2127 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
2131 /* Set RX Fifo threshold according the reception data length: 8bit */
2132 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
2134 if (hspi
->hdmatx
->Init
.MemDataAlignment
== DMA_MDATAALIGN_HALFWORD
)
2136 if ((hspi
->TxXferSize
& 0x1U
) == 0x0U
)
2138 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
);
2139 hspi
->TxXferCount
= hspi
->TxXferCount
>> 1U;
2143 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMATX
);
2144 hspi
->TxXferCount
= (hspi
->TxXferCount
>> 1U) + 1U;
2148 if (hspi
->hdmarx
->Init
.MemDataAlignment
== DMA_MDATAALIGN_HALFWORD
)
2150 /* Set RX Fifo threshold according the reception data length: 16bit */
2151 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
2153 if ((hspi
->RxXferCount
& 0x1U
) == 0x0U
)
2155 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMARX
);
2156 hspi
->RxXferCount
= hspi
->RxXferCount
>> 1U;
2160 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_LDMARX
);
2161 hspi
->RxXferCount
= (hspi
->RxXferCount
>> 1U) + 1U;
2166 /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
2167 if (hspi
->State
== HAL_SPI_STATE_BUSY_RX
)
2169 /* Set the SPI Rx DMA Half transfer complete callback */
2170 hspi
->hdmarx
->XferHalfCpltCallback
= SPI_DMAHalfReceiveCplt
;
2171 hspi
->hdmarx
->XferCpltCallback
= SPI_DMAReceiveCplt
;
2175 /* Set the SPI Tx/Rx DMA Half transfer complete callback */
2176 hspi
->hdmarx
->XferHalfCpltCallback
= SPI_DMAHalfTransmitReceiveCplt
;
2177 hspi
->hdmarx
->XferCpltCallback
= SPI_DMATransmitReceiveCplt
;
2180 /* Set the DMA error callback */
2181 hspi
->hdmarx
->XferErrorCallback
= SPI_DMAError
;
2183 /* Set the DMA AbortCpltCallback */
2184 hspi
->hdmarx
->XferAbortCallback
= NULL
;
2186 /* Enable the Rx DMA Stream/Channel */
2187 if (HAL_OK
!= HAL_DMA_Start_IT(hspi
->hdmarx
, (uint32_t)&hspi
->Instance
->DR
, (uint32_t)hspi
->pRxBuffPtr
,
2190 /* Update SPI error code */
2191 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
2192 errorcode
= HAL_ERROR
;
2194 hspi
->State
= HAL_SPI_STATE_READY
;
2198 /* Enable Rx DMA Request */
2199 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
2201 /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
2202 is performed in DMA reception complete callback */
2203 hspi
->hdmatx
->XferHalfCpltCallback
= NULL
;
2204 hspi
->hdmatx
->XferCpltCallback
= NULL
;
2205 hspi
->hdmatx
->XferErrorCallback
= NULL
;
2206 hspi
->hdmatx
->XferAbortCallback
= NULL
;
2208 /* Enable the Tx DMA Stream/Channel */
2209 if (HAL_OK
!= HAL_DMA_Start_IT(hspi
->hdmatx
, (uint32_t)hspi
->pTxBuffPtr
, (uint32_t)&hspi
->Instance
->DR
,
2212 /* Update SPI error code */
2213 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
2214 errorcode
= HAL_ERROR
;
2216 hspi
->State
= HAL_SPI_STATE_READY
;
2220 /* Check if the SPI is already enabled */
2221 if ((hspi
->Instance
->CR1
& SPI_CR1_SPE
) != SPI_CR1_SPE
)
2223 /* Enable SPI peripheral */
2224 __HAL_SPI_ENABLE(hspi
);
2226 /* Enable the SPI Error Interrupt Bit */
2227 __HAL_SPI_ENABLE_IT(hspi
, (SPI_IT_ERR
));
2229 /* Enable Tx DMA Request */
2230 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
2233 /* Process Unlocked */
2239 * @brief Abort ongoing transfer (blocking mode).
2240 * @param hspi SPI handle.
2241 * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
2242 * started in Interrupt or DMA mode.
2243 * This procedure performs following operations :
2244 * - Disable SPI Interrupts (depending of transfer direction)
2245 * - Disable the DMA transfer in the peripheral register (if enabled)
2246 * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
2247 * - Set handle State to READY
2248 * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
2249 * @retval HAL status
2251 HAL_StatusTypeDef
HAL_SPI_Abort(SPI_HandleTypeDef
*hspi
)
2253 HAL_StatusTypeDef errorcode
;
2254 __IO
uint32_t count
;
2255 __IO
uint32_t resetcount
;
2257 /* Initialized local variable */
2259 resetcount
= SPI_DEFAULT_TIMEOUT
* (SystemCoreClock
/ 24U / 1000U);
2262 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2263 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_ERRIE
);
2265 /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
2266 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXEIE
))
2268 hspi
->TxISR
= SPI_AbortTx_ISR
;
2269 /* Wait HAL_SPI_STATE_ABORT state */
2274 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2278 } while (hspi
->State
!= HAL_SPI_STATE_ABORT
);
2279 /* Reset Timeout Counter */
2283 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXNEIE
))
2285 hspi
->RxISR
= SPI_AbortRx_ISR
;
2286 /* Wait HAL_SPI_STATE_ABORT state */
2291 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2295 } while (hspi
->State
!= HAL_SPI_STATE_ABORT
);
2296 /* Reset Timeout Counter */
2300 /* Disable the SPI DMA Tx request if enabled */
2301 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
))
2303 /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
2304 if (hspi
->hdmatx
!= NULL
)
2306 /* Set the SPI DMA Abort callback :
2307 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2308 hspi
->hdmatx
->XferAbortCallback
= NULL
;
2310 /* Abort DMA Tx Handle linked to SPI Peripheral */
2311 if (HAL_DMA_Abort(hspi
->hdmatx
) != HAL_OK
)
2313 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2316 /* Disable Tx DMA Request */
2317 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_TXDMAEN
));
2319 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
2321 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2324 /* Disable SPI Peripheral */
2325 __HAL_SPI_DISABLE(hspi
);
2327 /* Empty the FRLVL fifo */
2328 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
2330 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2335 /* Disable the SPI DMA Rx request if enabled */
2336 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
))
2338 /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
2339 if (hspi
->hdmarx
!= NULL
)
2341 /* Set the SPI DMA Abort callback :
2342 will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
2343 hspi
->hdmarx
->XferAbortCallback
= NULL
;
2345 /* Abort DMA Rx Handle linked to SPI Peripheral */
2346 if (HAL_DMA_Abort(hspi
->hdmarx
) != HAL_OK
)
2348 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2351 /* Disable peripheral */
2352 __HAL_SPI_DISABLE(hspi
);
2354 /* Control the BSY flag */
2355 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
2357 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2360 /* Empty the FRLVL fifo */
2361 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
2363 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2366 /* Disable Rx DMA Request */
2367 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_RXDMAEN
));
2370 /* Reset Tx and Rx transfer counters */
2371 hspi
->RxXferCount
= 0U;
2372 hspi
->TxXferCount
= 0U;
2374 /* Check error during Abort procedure */
2375 if (hspi
->ErrorCode
== HAL_SPI_ERROR_ABORT
)
2377 /* return HAL_Error in case of error during Abort procedure */
2378 errorcode
= HAL_ERROR
;
2382 /* Reset errorCode */
2383 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
2386 /* Clear the Error flags in the SR register */
2387 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
2388 __HAL_SPI_CLEAR_FREFLAG(hspi
);
2390 /* Restore hspi->state to ready */
2391 hspi
->State
= HAL_SPI_STATE_READY
;
2397 * @brief Abort ongoing transfer (Interrupt mode).
2398 * @param hspi SPI handle.
2399 * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
2400 * started in Interrupt or DMA mode.
2401 * This procedure performs following operations :
2402 * - Disable SPI Interrupts (depending of transfer direction)
2403 * - Disable the DMA transfer in the peripheral register (if enabled)
2404 * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2405 * - Set handle State to READY
2406 * - At abort completion, call user abort complete callback
2407 * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
2408 * considered as completed only when user abort complete callback is executed (not when exiting function).
2409 * @retval HAL status
2411 HAL_StatusTypeDef
HAL_SPI_Abort_IT(SPI_HandleTypeDef
*hspi
)
2413 HAL_StatusTypeDef errorcode
;
2414 uint32_t abortcplt
;
2415 __IO
uint32_t count
;
2416 __IO
uint32_t resetcount
;
2418 /* Initialized local variable */
2421 resetcount
= SPI_DEFAULT_TIMEOUT
* (SystemCoreClock
/ 24U / 1000U);
2424 /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
2425 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_ERRIE
);
2427 /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
2428 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXEIE
))
2430 hspi
->TxISR
= SPI_AbortTx_ISR
;
2431 /* Wait HAL_SPI_STATE_ABORT state */
2436 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2440 } while (hspi
->State
!= HAL_SPI_STATE_ABORT
);
2441 /* Reset Timeout Counter */
2445 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXNEIE
))
2447 hspi
->RxISR
= SPI_AbortRx_ISR
;
2448 /* Wait HAL_SPI_STATE_ABORT state */
2453 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2457 } while (hspi
->State
!= HAL_SPI_STATE_ABORT
);
2458 /* Reset Timeout Counter */
2462 /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
2463 before any call to DMA Abort functions */
2464 /* DMA Tx Handle is valid */
2465 if (hspi
->hdmatx
!= NULL
)
2467 /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
2468 Otherwise, set it to NULL */
2469 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
))
2471 hspi
->hdmatx
->XferAbortCallback
= SPI_DMATxAbortCallback
;
2475 hspi
->hdmatx
->XferAbortCallback
= NULL
;
2478 /* DMA Rx Handle is valid */
2479 if (hspi
->hdmarx
!= NULL
)
2481 /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
2482 Otherwise, set it to NULL */
2483 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
))
2485 hspi
->hdmarx
->XferAbortCallback
= SPI_DMARxAbortCallback
;
2489 hspi
->hdmarx
->XferAbortCallback
= NULL
;
2493 /* Disable the SPI DMA Tx request if enabled */
2494 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
))
2496 /* Abort the SPI DMA Tx Stream/Channel */
2497 if (hspi
->hdmatx
!= NULL
)
2499 /* Abort DMA Tx Handle linked to SPI Peripheral */
2500 if (HAL_DMA_Abort_IT(hspi
->hdmatx
) != HAL_OK
)
2502 hspi
->hdmatx
->XferAbortCallback
= NULL
;
2503 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2511 /* Disable the SPI DMA Rx request if enabled */
2512 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
))
2514 /* Abort the SPI DMA Rx Stream/Channel */
2515 if (hspi
->hdmarx
!= NULL
)
2517 /* Abort DMA Rx Handle linked to SPI Peripheral */
2518 if (HAL_DMA_Abort_IT(hspi
->hdmarx
) != HAL_OK
)
2520 hspi
->hdmarx
->XferAbortCallback
= NULL
;
2521 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
2530 if (abortcplt
== 1U)
2532 /* Reset Tx and Rx transfer counters */
2533 hspi
->RxXferCount
= 0U;
2534 hspi
->TxXferCount
= 0U;
2536 /* Check error during Abort procedure */
2537 if (hspi
->ErrorCode
== HAL_SPI_ERROR_ABORT
)
2539 /* return HAL_Error in case of error during Abort procedure */
2540 errorcode
= HAL_ERROR
;
2544 /* Reset errorCode */
2545 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
2548 /* Clear the Error flags in the SR register */
2549 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
2550 __HAL_SPI_CLEAR_FREFLAG(hspi
);
2552 /* Restore hspi->State to Ready */
2553 hspi
->State
= HAL_SPI_STATE_READY
;
2555 /* As no DMA to be aborted, call directly user Abort complete callback */
2556 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2557 hspi
->AbortCpltCallback(hspi
);
2559 HAL_SPI_AbortCpltCallback(hspi
);
2560 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2567 * @brief Pause the DMA Transfer.
2568 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2569 * the configuration information for the specified SPI module.
2570 * @retval HAL status
2572 HAL_StatusTypeDef
HAL_SPI_DMAPause(SPI_HandleTypeDef
*hspi
)
2574 /* Process Locked */
2577 /* Disable the SPI DMA Tx & Rx requests */
2578 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
2580 /* Process Unlocked */
2587 * @brief Resume the DMA Transfer.
2588 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2589 * the configuration information for the specified SPI module.
2590 * @retval HAL status
2592 HAL_StatusTypeDef
HAL_SPI_DMAResume(SPI_HandleTypeDef
*hspi
)
2594 /* Process Locked */
2597 /* Enable the SPI DMA Tx & Rx requests */
2598 SET_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
2600 /* Process Unlocked */
2607 * @brief Stop the DMA Transfer.
2608 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2609 * the configuration information for the specified SPI module.
2610 * @retval HAL status
2612 HAL_StatusTypeDef
HAL_SPI_DMAStop(SPI_HandleTypeDef
*hspi
)
2614 HAL_StatusTypeDef errorcode
= HAL_OK
;
2615 /* The Lock is not implemented on this API to allow the user application
2616 to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
2617 when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
2618 and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
2621 /* Abort the SPI DMA tx Stream/Channel */
2622 if (hspi
->hdmatx
!= NULL
)
2624 if (HAL_OK
!= HAL_DMA_Abort(hspi
->hdmatx
))
2626 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
2627 errorcode
= HAL_ERROR
;
2630 /* Abort the SPI DMA rx Stream/Channel */
2631 if (hspi
->hdmarx
!= NULL
)
2633 if (HAL_OK
!= HAL_DMA_Abort(hspi
->hdmarx
))
2635 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
2636 errorcode
= HAL_ERROR
;
2640 /* Disable the SPI DMA Tx & Rx requests */
2641 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
2642 hspi
->State
= HAL_SPI_STATE_READY
;
2647 * @brief Handle SPI interrupt request.
2648 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2649 * the configuration information for the specified SPI module.
2652 void HAL_SPI_IRQHandler(SPI_HandleTypeDef
*hspi
)
2654 uint32_t itsource
= hspi
->Instance
->CR2
;
2655 uint32_t itflag
= hspi
->Instance
->SR
;
2657 /* SPI in mode Receiver ----------------------------------------------------*/
2658 if ((SPI_CHECK_FLAG(itflag
, SPI_FLAG_OVR
) == RESET
) &&
2659 (SPI_CHECK_FLAG(itflag
, SPI_FLAG_RXNE
) != RESET
) && (SPI_CHECK_IT_SOURCE(itsource
, SPI_IT_RXNE
) != RESET
))
2665 /* SPI in mode Transmitter -------------------------------------------------*/
2666 if ((SPI_CHECK_FLAG(itflag
, SPI_FLAG_TXE
) != RESET
) && (SPI_CHECK_IT_SOURCE(itsource
, SPI_IT_TXE
) != RESET
))
2672 /* SPI in Error Treatment --------------------------------------------------*/
2673 if (((SPI_CHECK_FLAG(itflag
, SPI_FLAG_MODF
) != RESET
) || (SPI_CHECK_FLAG(itflag
, SPI_FLAG_OVR
) != RESET
)
2674 || (SPI_CHECK_FLAG(itflag
, SPI_FLAG_FRE
) != RESET
)) && (SPI_CHECK_IT_SOURCE(itsource
, SPI_IT_ERR
) != RESET
))
2676 /* SPI Overrun error interrupt occurred ----------------------------------*/
2677 if (SPI_CHECK_FLAG(itflag
, SPI_FLAG_OVR
) != RESET
)
2679 if (hspi
->State
!= HAL_SPI_STATE_BUSY_TX
)
2681 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_OVR
);
2682 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
2686 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
2691 /* SPI Mode Fault error interrupt occurred -------------------------------*/
2692 if (SPI_CHECK_FLAG(itflag
, SPI_FLAG_MODF
) != RESET
)
2694 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_MODF
);
2695 __HAL_SPI_CLEAR_MODFFLAG(hspi
);
2698 /* SPI Frame error interrupt occurred ------------------------------------*/
2699 if (SPI_CHECK_FLAG(itflag
, SPI_FLAG_FRE
) != RESET
)
2701 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FRE
);
2702 __HAL_SPI_CLEAR_FREFLAG(hspi
);
2705 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
2707 /* Disable all interrupts */
2708 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_RXNE
| SPI_IT_TXE
| SPI_IT_ERR
);
2710 hspi
->State
= HAL_SPI_STATE_READY
;
2711 /* Disable the SPI DMA requests if enabled */
2712 if ((HAL_IS_BIT_SET(itsource
, SPI_CR2_TXDMAEN
)) || (HAL_IS_BIT_SET(itsource
, SPI_CR2_RXDMAEN
)))
2714 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
));
2716 /* Abort the SPI DMA Rx channel */
2717 if (hspi
->hdmarx
!= NULL
)
2719 /* Set the SPI DMA Abort callback :
2720 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2721 hspi
->hdmarx
->XferAbortCallback
= SPI_DMAAbortOnError
;
2722 if (HAL_OK
!= HAL_DMA_Abort_IT(hspi
->hdmarx
))
2724 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2727 /* Abort the SPI DMA Tx channel */
2728 if (hspi
->hdmatx
!= NULL
)
2730 /* Set the SPI DMA Abort callback :
2731 will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
2732 hspi
->hdmatx
->XferAbortCallback
= SPI_DMAAbortOnError
;
2733 if (HAL_OK
!= HAL_DMA_Abort_IT(hspi
->hdmatx
))
2735 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
2741 /* Call user error callback */
2742 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2743 hspi
->ErrorCallback(hspi
);
2745 HAL_SPI_ErrorCallback(hspi
);
2746 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2754 * @brief Tx Transfer completed callback.
2755 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2756 * the configuration information for SPI module.
2759 __weak
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef
*hspi
)
2761 /* Prevent unused argument(s) compilation warning */
2764 /* NOTE : This function should not be modified, when the callback is needed,
2765 the HAL_SPI_TxCpltCallback should be implemented in the user file
2770 * @brief Rx Transfer completed callback.
2771 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2772 * the configuration information for SPI module.
2775 __weak
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef
*hspi
)
2777 /* Prevent unused argument(s) compilation warning */
2780 /* NOTE : This function should not be modified, when the callback is needed,
2781 the HAL_SPI_RxCpltCallback should be implemented in the user file
2786 * @brief Tx and Rx Transfer completed callback.
2787 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2788 * the configuration information for SPI module.
2791 __weak
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef
*hspi
)
2793 /* Prevent unused argument(s) compilation warning */
2796 /* NOTE : This function should not be modified, when the callback is needed,
2797 the HAL_SPI_TxRxCpltCallback should be implemented in the user file
2802 * @brief Tx Half Transfer completed callback.
2803 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2804 * the configuration information for SPI module.
2807 __weak
void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef
*hspi
)
2809 /* Prevent unused argument(s) compilation warning */
2812 /* NOTE : This function should not be modified, when the callback is needed,
2813 the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
2818 * @brief Rx Half Transfer completed callback.
2819 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2820 * the configuration information for SPI module.
2823 __weak
void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef
*hspi
)
2825 /* Prevent unused argument(s) compilation warning */
2828 /* NOTE : This function should not be modified, when the callback is needed,
2829 the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
2834 * @brief Tx and Rx Half Transfer callback.
2835 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2836 * the configuration information for SPI module.
2839 __weak
void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef
*hspi
)
2841 /* Prevent unused argument(s) compilation warning */
2844 /* NOTE : This function should not be modified, when the callback is needed,
2845 the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
2850 * @brief SPI error callback.
2851 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2852 * the configuration information for SPI module.
2855 __weak
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef
*hspi
)
2857 /* Prevent unused argument(s) compilation warning */
2860 /* NOTE : This function should not be modified, when the callback is needed,
2861 the HAL_SPI_ErrorCallback should be implemented in the user file
2863 /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
2864 and user can use HAL_SPI_GetError() API to check the latest error occurred
2869 * @brief SPI Abort Complete callback.
2870 * @param hspi SPI handle.
2873 __weak
void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef
*hspi
)
2875 /* Prevent unused argument(s) compilation warning */
2878 /* NOTE : This function should not be modified, when the callback is needed,
2879 the HAL_SPI_AbortCpltCallback can be implemented in the user file.
2887 /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
2888 * @brief SPI control functions
2891 ===============================================================================
2892 ##### Peripheral State and Errors functions #####
2893 ===============================================================================
2895 This subsection provides a set of functions allowing to control the SPI.
2896 (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
2897 (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
2903 * @brief Return the SPI handle state.
2904 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2905 * the configuration information for SPI module.
2908 HAL_SPI_StateTypeDef
HAL_SPI_GetState(SPI_HandleTypeDef
*hspi
)
2910 /* Return SPI handle state */
2915 * @brief Return the SPI error code.
2916 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
2917 * the configuration information for SPI module.
2918 * @retval SPI error code in bitmap format
2920 uint32_t HAL_SPI_GetError(SPI_HandleTypeDef
*hspi
)
2922 /* Return SPI ErrorCode */
2923 return hspi
->ErrorCode
;
2934 /** @addtogroup SPI_Private_Functions
2935 * @brief Private functions
2940 * @brief DMA SPI transmit process complete callback.
2941 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2942 * the configuration information for the specified DMA module.
2945 static void SPI_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2947 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
2950 /* Init tickstart for timeout management*/
2951 tickstart
= HAL_GetTick();
2953 /* DMA Normal Mode */
2954 if ((hdma
->Instance
->CCR
& DMA_CCR_CIRC
) != DMA_CCR_CIRC
)
2956 /* Disable ERR interrupt */
2957 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_ERR
);
2959 /* Disable Tx DMA Request */
2960 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
2962 /* Check the end of the transaction */
2963 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
2965 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
2968 /* Clear overrun flag in 2 Lines communication mode because received data is not read */
2969 if (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
)
2971 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
2974 hspi
->TxXferCount
= 0U;
2975 hspi
->State
= HAL_SPI_STATE_READY
;
2977 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
2979 /* Call user error callback */
2980 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2981 hspi
->ErrorCallback(hspi
);
2983 HAL_SPI_ErrorCallback(hspi
);
2984 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2988 /* Call user Tx complete callback */
2989 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
2990 hspi
->TxCpltCallback(hspi
);
2992 HAL_SPI_TxCpltCallback(hspi
);
2993 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2997 * @brief DMA SPI receive process complete callback.
2998 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
2999 * the configuration information for the specified DMA module.
3002 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
3004 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3007 /* Init tickstart for timeout management*/
3008 tickstart
= HAL_GetTick();
3010 /* DMA Normal Mode */
3011 if ((hdma
->Instance
->CCR
& DMA_CCR_CIRC
) != DMA_CCR_CIRC
)
3013 /* Disable ERR interrupt */
3014 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_ERR
);
3016 #if (USE_SPI_CRC != 0U)
3018 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3020 /* Wait until RXNE flag */
3021 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3023 /* Error on the CRC reception */
3024 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3027 if (hspi
->Init
.DataSize
> SPI_DATASIZE_8BIT
)
3029 /* Read 16bit CRC */
3030 READ_REG(hspi
->Instance
->DR
);
3035 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3037 if (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_16BIT
)
3039 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_RXNE
, SET
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3041 /* Error on the CRC reception */
3042 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3044 /* Read 8bit CRC again in case of 16bit CRC in 8bit Data mode */
3045 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3049 #endif /* USE_SPI_CRC */
3051 /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
3052 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
3054 /* Check the end of the transaction */
3055 if (SPI_EndRxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3057 hspi
->ErrorCode
= HAL_SPI_ERROR_FLAG
;
3060 hspi
->RxXferCount
= 0U;
3061 hspi
->State
= HAL_SPI_STATE_READY
;
3063 #if (USE_SPI_CRC != 0U)
3064 /* Check if CRC error occurred */
3065 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
))
3067 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3068 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
3070 #endif /* USE_SPI_CRC */
3072 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
3074 /* Call user error callback */
3075 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3076 hspi
->ErrorCallback(hspi
);
3078 HAL_SPI_ErrorCallback(hspi
);
3079 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3083 /* Call user Rx complete callback */
3084 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3085 hspi
->RxCpltCallback(hspi
);
3087 HAL_SPI_RxCpltCallback(hspi
);
3088 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3092 * @brief DMA SPI transmit receive process complete callback.
3093 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
3094 * the configuration information for the specified DMA module.
3097 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef
*hdma
)
3099 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3102 /* Init tickstart for timeout management*/
3103 tickstart
= HAL_GetTick();
3105 /* DMA Normal Mode */
3106 if ((hdma
->Instance
->CCR
& DMA_CCR_CIRC
) != DMA_CCR_CIRC
)
3108 /* Disable ERR interrupt */
3109 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_ERR
);
3111 #if (USE_SPI_CRC != 0U)
3113 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3115 if ((hspi
->Init
.DataSize
== SPI_DATASIZE_8BIT
) && (hspi
->Init
.CRCLength
== SPI_CRC_LENGTH_8BIT
))
3117 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_QUARTER_FULL
, SPI_DEFAULT_TIMEOUT
,
3118 tickstart
) != HAL_OK
)
3120 /* Error on the CRC reception */
3121 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3123 /* Read CRC to Flush DR and RXNE flag */
3124 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3128 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_HALF_FULL
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3130 /* Error on the CRC reception */
3131 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3133 /* Read CRC to Flush DR and RXNE flag */
3134 READ_REG(hspi
->Instance
->DR
);
3137 #endif /* USE_SPI_CRC */
3139 /* Check the end of the transaction */
3140 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3142 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3145 /* Disable Rx/Tx DMA Request */
3146 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
3148 hspi
->TxXferCount
= 0U;
3149 hspi
->RxXferCount
= 0U;
3150 hspi
->State
= HAL_SPI_STATE_READY
;
3152 #if (USE_SPI_CRC != 0U)
3153 /* Check if CRC error occurred */
3154 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
))
3156 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3157 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
3159 #endif /* USE_SPI_CRC */
3161 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
3163 /* Call user error callback */
3164 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3165 hspi
->ErrorCallback(hspi
);
3167 HAL_SPI_ErrorCallback(hspi
);
3168 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3172 /* Call user TxRx complete callback */
3173 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3174 hspi
->TxRxCpltCallback(hspi
);
3176 HAL_SPI_TxRxCpltCallback(hspi
);
3177 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3181 * @brief DMA SPI half transmit process complete callback.
3182 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
3183 * the configuration information for the specified DMA module.
3186 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef
*hdma
)
3188 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3190 /* Call user Tx half complete callback */
3191 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3192 hspi
->TxHalfCpltCallback(hspi
);
3194 HAL_SPI_TxHalfCpltCallback(hspi
);
3195 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3199 * @brief DMA SPI half receive process complete callback
3200 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
3201 * the configuration information for the specified DMA module.
3204 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef
*hdma
)
3206 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3208 /* Call user Rx half complete callback */
3209 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3210 hspi
->RxHalfCpltCallback(hspi
);
3212 HAL_SPI_RxHalfCpltCallback(hspi
);
3213 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3217 * @brief DMA SPI half transmit receive process complete callback.
3218 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
3219 * the configuration information for the specified DMA module.
3222 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef
*hdma
)
3224 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3226 /* Call user TxRx half complete callback */
3227 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3228 hspi
->TxRxHalfCpltCallback(hspi
);
3230 HAL_SPI_TxRxHalfCpltCallback(hspi
);
3231 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3235 * @brief DMA SPI communication error callback.
3236 * @param hdma pointer to a DMA_HandleTypeDef structure that contains
3237 * the configuration information for the specified DMA module.
3240 static void SPI_DMAError(DMA_HandleTypeDef
*hdma
)
3242 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3244 /* Stop the disable DMA transfer on SPI side */
3245 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
| SPI_CR2_RXDMAEN
);
3247 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_DMA
);
3248 hspi
->State
= HAL_SPI_STATE_READY
;
3249 /* Call user error callback */
3250 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3251 hspi
->ErrorCallback(hspi
);
3253 HAL_SPI_ErrorCallback(hspi
);
3254 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3258 * @brief DMA SPI communication abort callback, when initiated by HAL services on Error
3259 * (To be called at end of DMA Abort procedure following error occurrence).
3260 * @param hdma DMA handle.
3263 static void SPI_DMAAbortOnError(DMA_HandleTypeDef
*hdma
)
3265 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3266 hspi
->RxXferCount
= 0U;
3267 hspi
->TxXferCount
= 0U;
3269 /* Call user error callback */
3270 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3271 hspi
->ErrorCallback(hspi
);
3273 HAL_SPI_ErrorCallback(hspi
);
3274 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3278 * @brief DMA SPI Tx communication abort callback, when initiated by user
3279 * (To be called at end of DMA Tx Abort procedure following user abort request).
3280 * @note When this callback is executed, User Abort complete call back is called only if no
3281 * Abort still ongoing for Rx DMA Handle.
3282 * @param hdma DMA handle.
3285 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef
*hdma
)
3287 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3289 hspi
->hdmatx
->XferAbortCallback
= NULL
;
3291 /* Disable Tx DMA Request */
3292 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_TXDMAEN
);
3294 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
3296 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
3299 /* Disable SPI Peripheral */
3300 __HAL_SPI_DISABLE(hspi
);
3302 /* Empty the FRLVL fifo */
3303 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
3305 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
3308 /* Check if an Abort process is still ongoing */
3309 if (hspi
->hdmarx
!= NULL
)
3311 if (hspi
->hdmarx
->XferAbortCallback
!= NULL
)
3317 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
3318 hspi
->RxXferCount
= 0U;
3319 hspi
->TxXferCount
= 0U;
3321 /* Check no error during Abort procedure */
3322 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_ABORT
)
3324 /* Reset errorCode */
3325 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
3328 /* Clear the Error flags in the SR register */
3329 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
3330 __HAL_SPI_CLEAR_FREFLAG(hspi
);
3332 /* Restore hspi->State to Ready */
3333 hspi
->State
= HAL_SPI_STATE_READY
;
3335 /* Call user Abort complete callback */
3336 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3337 hspi
->AbortCpltCallback(hspi
);
3339 HAL_SPI_AbortCpltCallback(hspi
);
3340 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3344 * @brief DMA SPI Rx communication abort callback, when initiated by user
3345 * (To be called at end of DMA Rx Abort procedure following user abort request).
3346 * @note When this callback is executed, User Abort complete call back is called only if no
3347 * Abort still ongoing for Tx DMA Handle.
3348 * @param hdma DMA handle.
3351 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef
*hdma
)
3353 SPI_HandleTypeDef
*hspi
= (SPI_HandleTypeDef
*)(((DMA_HandleTypeDef
*)hdma
)->Parent
); /* Derogation MISRAC2012-Rule-11.5 */
3355 /* Disable SPI Peripheral */
3356 __HAL_SPI_DISABLE(hspi
);
3358 hspi
->hdmarx
->XferAbortCallback
= NULL
;
3360 /* Disable Rx DMA Request */
3361 CLEAR_BIT(hspi
->Instance
->CR2
, SPI_CR2_RXDMAEN
);
3363 /* Control the BSY flag */
3364 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
3366 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
3369 /* Empty the FRLVL fifo */
3370 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
3372 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
3375 /* Check if an Abort process is still ongoing */
3376 if (hspi
->hdmatx
!= NULL
)
3378 if (hspi
->hdmatx
->XferAbortCallback
!= NULL
)
3384 /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
3385 hspi
->RxXferCount
= 0U;
3386 hspi
->TxXferCount
= 0U;
3388 /* Check no error during Abort procedure */
3389 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_ABORT
)
3391 /* Reset errorCode */
3392 hspi
->ErrorCode
= HAL_SPI_ERROR_NONE
;
3395 /* Clear the Error flags in the SR register */
3396 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
3397 __HAL_SPI_CLEAR_FREFLAG(hspi
);
3399 /* Restore hspi->State to Ready */
3400 hspi
->State
= HAL_SPI_STATE_READY
;
3402 /* Call user Abort complete callback */
3403 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3404 hspi
->AbortCpltCallback(hspi
);
3406 HAL_SPI_AbortCpltCallback(hspi
);
3407 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3411 * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
3412 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3413 * the configuration information for SPI module.
3416 static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
)
3418 /* Receive data in packing mode */
3419 if (hspi
->RxXferCount
> 1U)
3421 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)(hspi
->Instance
->DR
);
3422 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
3423 hspi
->RxXferCount
-= 2U;
3424 if (hspi
->RxXferCount
== 1U)
3426 /* Set RX Fifo threshold according the reception data length: 8bit */
3427 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
3430 /* Receive data in 8 Bit mode */
3433 *hspi
->pRxBuffPtr
= *((__IO
uint8_t *)&hspi
->Instance
->DR
);
3435 hspi
->RxXferCount
--;
3438 /* Check end of the reception */
3439 if (hspi
->RxXferCount
== 0U)
3441 #if (USE_SPI_CRC != 0U)
3442 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3444 SET_BIT(hspi
->Instance
->CR2
, SPI_RXFIFO_THRESHOLD
);
3445 hspi
->RxISR
= SPI_2linesRxISR_8BITCRC
;
3448 #endif /* USE_SPI_CRC */
3450 /* Disable RXNE and ERR interrupt */
3451 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_RXNE
| SPI_IT_ERR
));
3453 if (hspi
->TxXferCount
== 0U)
3455 SPI_CloseRxTx_ISR(hspi
);
3460 #if (USE_SPI_CRC != 0U)
3462 * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
3463 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3464 * the configuration information for SPI module.
3467 static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef
*hspi
)
3469 /* Read 8bit CRC to flush Data Regsiter */
3470 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3474 /* Check end of the reception */
3475 if (hspi
->CRCSize
== 0U)
3477 /* Disable RXNE and ERR interrupt */
3478 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_RXNE
| SPI_IT_ERR
));
3480 if (hspi
->TxXferCount
== 0U)
3482 SPI_CloseRxTx_ISR(hspi
);
3486 #endif /* USE_SPI_CRC */
3489 * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode.
3490 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3491 * the configuration information for SPI module.
3494 static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
)
3496 /* Transmit data in packing Bit mode */
3497 if (hspi
->TxXferCount
>= 2U)
3499 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
3500 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
3501 hspi
->TxXferCount
-= 2U;
3503 /* Transmit data in 8 Bit mode */
3506 *(__IO
uint8_t *)&hspi
->Instance
->DR
= (*hspi
->pTxBuffPtr
);
3508 hspi
->TxXferCount
--;
3511 /* Check the end of the transmission */
3512 if (hspi
->TxXferCount
== 0U)
3514 #if (USE_SPI_CRC != 0U)
3515 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3517 /* Set CRC Next Bit to send CRC */
3518 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3519 /* Disable TXE interrupt */
3520 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_TXE
);
3523 #endif /* USE_SPI_CRC */
3525 /* Disable TXE interrupt */
3526 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_TXE
);
3528 if (hspi
->RxXferCount
== 0U)
3530 SPI_CloseRxTx_ISR(hspi
);
3536 * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode.
3537 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3538 * the configuration information for SPI module.
3541 static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
)
3543 /* Receive data in 16 Bit mode */
3544 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)(hspi
->Instance
->DR
);
3545 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
3546 hspi
->RxXferCount
--;
3548 if (hspi
->RxXferCount
== 0U)
3550 #if (USE_SPI_CRC != 0U)
3551 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3553 hspi
->RxISR
= SPI_2linesRxISR_16BITCRC
;
3556 #endif /* USE_SPI_CRC */
3558 /* Disable RXNE interrupt */
3559 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_RXNE
);
3561 if (hspi
->TxXferCount
== 0U)
3563 SPI_CloseRxTx_ISR(hspi
);
3568 #if (USE_SPI_CRC != 0U)
3570 * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode.
3571 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3572 * the configuration information for SPI module.
3575 static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef
*hspi
)
3577 /* Read 16bit CRC to flush Data Regsiter */
3578 READ_REG(hspi
->Instance
->DR
);
3580 /* Disable RXNE interrupt */
3581 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_RXNE
);
3583 SPI_CloseRxTx_ISR(hspi
);
3585 #endif /* USE_SPI_CRC */
3588 * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode.
3589 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3590 * the configuration information for SPI module.
3593 static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
)
3595 /* Transmit data in 16 Bit mode */
3596 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
3597 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
3598 hspi
->TxXferCount
--;
3600 /* Enable CRC Transmission */
3601 if (hspi
->TxXferCount
== 0U)
3603 #if (USE_SPI_CRC != 0U)
3604 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3606 /* Set CRC Next Bit to send CRC */
3607 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3608 /* Disable TXE interrupt */
3609 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_TXE
);
3612 #endif /* USE_SPI_CRC */
3614 /* Disable TXE interrupt */
3615 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_TXE
);
3617 if (hspi
->RxXferCount
== 0U)
3619 SPI_CloseRxTx_ISR(hspi
);
3624 #if (USE_SPI_CRC != 0U)
3626 * @brief Manage the CRC 8-bit receive in Interrupt context.
3627 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3628 * the configuration information for SPI module.
3631 static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef
*hspi
)
3633 /* Read 8bit CRC to flush Data Register */
3634 READ_REG(*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3638 if (hspi
->CRCSize
== 0U)
3640 SPI_CloseRx_ISR(hspi
);
3643 #endif /* USE_SPI_CRC */
3646 * @brief Manage the receive 8-bit in Interrupt context.
3647 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3648 * the configuration information for SPI module.
3651 static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
)
3653 *hspi
->pRxBuffPtr
= (*(__IO
uint8_t *)&hspi
->Instance
->DR
);
3655 hspi
->RxXferCount
--;
3657 #if (USE_SPI_CRC != 0U)
3658 /* Enable CRC Transmission */
3659 if ((hspi
->RxXferCount
== 1U) && (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
))
3661 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3663 #endif /* USE_SPI_CRC */
3665 if (hspi
->RxXferCount
== 0U)
3667 #if (USE_SPI_CRC != 0U)
3668 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3670 hspi
->RxISR
= SPI_RxISR_8BITCRC
;
3673 #endif /* USE_SPI_CRC */
3674 SPI_CloseRx_ISR(hspi
);
3678 #if (USE_SPI_CRC != 0U)
3680 * @brief Manage the CRC 16-bit receive in Interrupt context.
3681 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3682 * the configuration information for SPI module.
3685 static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef
*hspi
)
3687 /* Read 16bit CRC to flush Data Register */
3688 READ_REG(hspi
->Instance
->DR
);
3690 /* Disable RXNE and ERR interrupt */
3691 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_RXNE
| SPI_IT_ERR
));
3693 SPI_CloseRx_ISR(hspi
);
3695 #endif /* USE_SPI_CRC */
3698 * @brief Manage the 16-bit receive in Interrupt context.
3699 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3700 * the configuration information for SPI module.
3703 static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
)
3705 *((uint16_t *)hspi
->pRxBuffPtr
) = (uint16_t)(hspi
->Instance
->DR
);
3706 hspi
->pRxBuffPtr
+= sizeof(uint16_t);
3707 hspi
->RxXferCount
--;
3709 #if (USE_SPI_CRC != 0U)
3710 /* Enable CRC Transmission */
3711 if ((hspi
->RxXferCount
== 1U) && (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
))
3713 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3715 #endif /* USE_SPI_CRC */
3717 if (hspi
->RxXferCount
== 0U)
3719 #if (USE_SPI_CRC != 0U)
3720 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3722 hspi
->RxISR
= SPI_RxISR_16BITCRC
;
3725 #endif /* USE_SPI_CRC */
3726 SPI_CloseRx_ISR(hspi
);
3731 * @brief Handle the data 8-bit transmit in Interrupt mode.
3732 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3733 * the configuration information for SPI module.
3736 static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef
*hspi
)
3738 *(__IO
uint8_t *)&hspi
->Instance
->DR
= (*hspi
->pTxBuffPtr
);
3740 hspi
->TxXferCount
--;
3742 if (hspi
->TxXferCount
== 0U)
3744 #if (USE_SPI_CRC != 0U)
3745 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3747 /* Enable CRC Transmission */
3748 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3750 #endif /* USE_SPI_CRC */
3751 SPI_CloseTx_ISR(hspi
);
3756 * @brief Handle the data 16-bit transmit in Interrupt mode.
3757 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3758 * the configuration information for SPI module.
3761 static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef
*hspi
)
3763 /* Transmit data in 16 Bit mode */
3764 hspi
->Instance
->DR
= *((uint16_t *)hspi
->pTxBuffPtr
);
3765 hspi
->pTxBuffPtr
+= sizeof(uint16_t);
3766 hspi
->TxXferCount
--;
3768 if (hspi
->TxXferCount
== 0U)
3770 #if (USE_SPI_CRC != 0U)
3771 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3773 /* Enable CRC Transmission */
3774 SET_BIT(hspi
->Instance
->CR1
, SPI_CR1_CRCNEXT
);
3776 #endif /* USE_SPI_CRC */
3777 SPI_CloseTx_ISR(hspi
);
3782 * @brief Handle SPI Communication Timeout.
3783 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3784 * the configuration information for SPI module.
3785 * @param Flag SPI flag to check
3786 * @param State flag state to check
3787 * @param Timeout Timeout duration
3788 * @param Tickstart tick start value
3789 * @retval HAL status
3791 static HAL_StatusTypeDef
SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef
*hspi
, uint32_t Flag
, FlagStatus State
,
3792 uint32_t Timeout
, uint32_t Tickstart
)
3794 while ((__HAL_SPI_GET_FLAG(hspi
, Flag
) ? SET
: RESET
) != State
)
3796 if (Timeout
!= HAL_MAX_DELAY
)
3798 if (((HAL_GetTick() - Tickstart
) >= Timeout
) || (Timeout
== 0U))
3800 /* Disable the SPI and reset the CRC: the CRC value should be cleared
3801 on both master and slave sides in order to resynchronize the master
3802 and slave for their respective CRC calculation */
3804 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
3805 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_TXE
| SPI_IT_RXNE
| SPI_IT_ERR
));
3807 if ((hspi
->Init
.Mode
== SPI_MODE_MASTER
) && ((hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
3808 || (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES_RXONLY
)))
3810 /* Disable SPI peripheral */
3811 __HAL_SPI_DISABLE(hspi
);
3814 /* Reset CRC Calculation */
3815 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3817 SPI_RESET_CRC(hspi
);
3820 hspi
->State
= HAL_SPI_STATE_READY
;
3822 /* Process Unlocked */
3834 * @brief Handle SPI FIFO Communication Timeout.
3835 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3836 * the configuration information for SPI module.
3837 * @param Fifo Fifo to check
3838 * @param State Fifo state to check
3839 * @param Timeout Timeout duration
3840 * @param Tickstart tick start value
3841 * @retval HAL status
3843 static HAL_StatusTypeDef
SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef
*hspi
, uint32_t Fifo
, uint32_t State
,
3844 uint32_t Timeout
, uint32_t Tickstart
)
3846 while ((hspi
->Instance
->SR
& Fifo
) != State
)
3848 if ((Fifo
== SPI_SR_FRLVL
) && (State
== SPI_FRLVL_EMPTY
))
3850 /* Read 8bit CRC to flush Data Register */
3851 READ_REG(*((__IO
uint8_t *)&hspi
->Instance
->DR
));
3854 if (Timeout
!= HAL_MAX_DELAY
)
3856 if (((HAL_GetTick() - Tickstart
) >= Timeout
) || (Timeout
== 0U))
3858 /* Disable the SPI and reset the CRC: the CRC value should be cleared
3859 on both master and slave sides in order to resynchronize the master
3860 and slave for their respective CRC calculation */
3862 /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
3863 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_TXE
| SPI_IT_RXNE
| SPI_IT_ERR
));
3865 if ((hspi
->Init
.Mode
== SPI_MODE_MASTER
) && ((hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
3866 || (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES_RXONLY
)))
3868 /* Disable SPI peripheral */
3869 __HAL_SPI_DISABLE(hspi
);
3872 /* Reset CRC Calculation */
3873 if (hspi
->Init
.CRCCalculation
== SPI_CRCCALCULATION_ENABLE
)
3875 SPI_RESET_CRC(hspi
);
3878 hspi
->State
= HAL_SPI_STATE_READY
;
3880 /* Process Unlocked */
3892 * @brief Handle the check of the RX transaction complete.
3893 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3894 * the configuration information for SPI module.
3895 * @param Timeout Timeout duration
3896 * @param Tickstart tick start value
3897 * @retval HAL status
3899 static HAL_StatusTypeDef
SPI_EndRxTransaction(SPI_HandleTypeDef
*hspi
, uint32_t Timeout
, uint32_t Tickstart
)
3901 if ((hspi
->Init
.Mode
== SPI_MODE_MASTER
) && ((hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
3902 || (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES_RXONLY
)))
3904 /* Disable SPI peripheral */
3905 __HAL_SPI_DISABLE(hspi
);
3908 /* Control the BSY flag */
3909 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, Timeout
, Tickstart
) != HAL_OK
)
3911 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3915 if ((hspi
->Init
.Mode
== SPI_MODE_MASTER
) && ((hspi
->Init
.Direction
== SPI_DIRECTION_1LINE
)
3916 || (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES_RXONLY
)))
3918 /* Empty the FRLVL fifo */
3919 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, Timeout
, Tickstart
) != HAL_OK
)
3921 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3929 * @brief Handle the check of the RXTX or TX transaction complete.
3930 * @param hspi SPI handle
3931 * @param Timeout Timeout duration
3932 * @param Tickstart tick start value
3933 * @retval HAL status
3935 static HAL_StatusTypeDef
SPI_EndRxTxTransaction(SPI_HandleTypeDef
*hspi
, uint32_t Timeout
, uint32_t Tickstart
)
3937 /* Control if the TX fifo is empty */
3938 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FTLVL
, SPI_FTLVL_EMPTY
, Timeout
, Tickstart
) != HAL_OK
)
3940 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3944 /* Control the BSY flag */
3945 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, Timeout
, Tickstart
) != HAL_OK
)
3947 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3951 /* Control if the RX fifo is empty */
3952 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, Timeout
, Tickstart
) != HAL_OK
)
3954 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3962 * @brief Handle the end of the RXTX transaction.
3963 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
3964 * the configuration information for SPI module.
3967 static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef
*hspi
)
3971 /* Init tickstart for timeout managment*/
3972 tickstart
= HAL_GetTick();
3974 /* Disable ERR interrupt */
3975 __HAL_SPI_DISABLE_IT(hspi
, SPI_IT_ERR
);
3977 /* Check the end of the transaction */
3978 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
3980 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
3983 #if (USE_SPI_CRC != 0U)
3984 /* Check if CRC error occurred */
3985 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
) != RESET
)
3987 hspi
->State
= HAL_SPI_STATE_READY
;
3988 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
3989 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
3990 /* Call user error callback */
3991 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
3992 hspi
->ErrorCallback(hspi
);
3994 HAL_SPI_ErrorCallback(hspi
);
3995 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3999 #endif /* USE_SPI_CRC */
4000 if (hspi
->ErrorCode
== HAL_SPI_ERROR_NONE
)
4002 if (hspi
->State
== HAL_SPI_STATE_BUSY_RX
)
4004 hspi
->State
= HAL_SPI_STATE_READY
;
4005 /* Call user Rx complete callback */
4006 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4007 hspi
->RxCpltCallback(hspi
);
4009 HAL_SPI_RxCpltCallback(hspi
);
4010 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4014 hspi
->State
= HAL_SPI_STATE_READY
;
4015 /* Call user TxRx complete callback */
4016 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4017 hspi
->TxRxCpltCallback(hspi
);
4019 HAL_SPI_TxRxCpltCallback(hspi
);
4020 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4025 hspi
->State
= HAL_SPI_STATE_READY
;
4026 /* Call user error callback */
4027 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4028 hspi
->ErrorCallback(hspi
);
4030 HAL_SPI_ErrorCallback(hspi
);
4031 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4033 #if (USE_SPI_CRC != 0U)
4035 #endif /* USE_SPI_CRC */
4039 * @brief Handle the end of the RX transaction.
4040 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
4041 * the configuration information for SPI module.
4044 static void SPI_CloseRx_ISR(SPI_HandleTypeDef
*hspi
)
4046 /* Disable RXNE and ERR interrupt */
4047 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_RXNE
| SPI_IT_ERR
));
4049 /* Check the end of the transaction */
4050 if (SPI_EndRxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4052 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
4054 hspi
->State
= HAL_SPI_STATE_READY
;
4056 #if (USE_SPI_CRC != 0U)
4057 /* Check if CRC error occurred */
4058 if (__HAL_SPI_GET_FLAG(hspi
, SPI_FLAG_CRCERR
) != RESET
)
4060 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_CRC
);
4061 __HAL_SPI_CLEAR_CRCERRFLAG(hspi
);
4062 /* Call user error callback */
4063 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4064 hspi
->ErrorCallback(hspi
);
4066 HAL_SPI_ErrorCallback(hspi
);
4067 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4071 #endif /* USE_SPI_CRC */
4072 if (hspi
->ErrorCode
== HAL_SPI_ERROR_NONE
)
4074 /* Call user Rx complete callback */
4075 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4076 hspi
->RxCpltCallback(hspi
);
4078 HAL_SPI_RxCpltCallback(hspi
);
4079 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4083 /* Call user error callback */
4084 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4085 hspi
->ErrorCallback(hspi
);
4087 HAL_SPI_ErrorCallback(hspi
);
4088 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4090 #if (USE_SPI_CRC != 0U)
4092 #endif /* USE_SPI_CRC */
4096 * @brief Handle the end of the TX transaction.
4097 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
4098 * the configuration information for SPI module.
4101 static void SPI_CloseTx_ISR(SPI_HandleTypeDef
*hspi
)
4105 /* Init tickstart for timeout management*/
4106 tickstart
= HAL_GetTick();
4108 /* Disable TXE and ERR interrupt */
4109 __HAL_SPI_DISABLE_IT(hspi
, (SPI_IT_TXE
| SPI_IT_ERR
));
4111 /* Check the end of the transaction */
4112 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, tickstart
) != HAL_OK
)
4114 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_FLAG
);
4117 /* Clear overrun flag in 2 Lines communication mode because received is not read */
4118 if (hspi
->Init
.Direction
== SPI_DIRECTION_2LINES
)
4120 __HAL_SPI_CLEAR_OVRFLAG(hspi
);
4123 hspi
->State
= HAL_SPI_STATE_READY
;
4124 if (hspi
->ErrorCode
!= HAL_SPI_ERROR_NONE
)
4126 /* Call user error callback */
4127 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4128 hspi
->ErrorCallback(hspi
);
4130 HAL_SPI_ErrorCallback(hspi
);
4131 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4135 /* Call user Rx complete callback */
4136 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
4137 hspi
->TxCpltCallback(hspi
);
4139 HAL_SPI_TxCpltCallback(hspi
);
4140 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
4145 * @brief Handle abort a Rx transaction.
4146 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
4147 * the configuration information for SPI module.
4150 static void SPI_AbortRx_ISR(SPI_HandleTypeDef
*hspi
)
4152 __IO
uint32_t count
;
4154 /* Disable SPI Peripheral */
4155 __HAL_SPI_DISABLE(hspi
);
4157 count
= SPI_DEFAULT_TIMEOUT
* (SystemCoreClock
/ 24U / 1000U);
4159 /* Disable RXNEIE interrupt */
4160 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_RXNEIE
));
4162 /* Check RXNEIE is disabled */
4167 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
4171 } while (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXNEIE
));
4173 /* Control the BSY flag */
4174 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4176 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4179 /* Empty the FRLVL fifo */
4180 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4182 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4185 hspi
->State
= HAL_SPI_STATE_ABORT
;
4189 * @brief Handle abort a Tx or Rx/Tx transaction.
4190 * @param hspi pointer to a SPI_HandleTypeDef structure that contains
4191 * the configuration information for SPI module.
4194 static void SPI_AbortTx_ISR(SPI_HandleTypeDef
*hspi
)
4196 __IO
uint32_t count
;
4198 count
= SPI_DEFAULT_TIMEOUT
* (SystemCoreClock
/ 24U / 1000U);
4200 /* Disable TXEIE interrupt */
4201 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_TXEIE
));
4203 /* Check TXEIE is disabled */
4208 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
4212 } while (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_TXEIE
));
4214 if (SPI_EndRxTxTransaction(hspi
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4216 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4219 /* Disable SPI Peripheral */
4220 __HAL_SPI_DISABLE(hspi
);
4222 /* Empty the FRLVL fifo */
4223 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4225 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4228 /* Check case of Full-Duplex Mode and disable directly RXNEIE interrupt */
4229 if (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXNEIE
))
4231 /* Disable RXNEIE interrupt */
4232 CLEAR_BIT(hspi
->Instance
->CR2
, (SPI_CR2_RXNEIE
));
4234 /* Check RXNEIE is disabled */
4239 SET_BIT(hspi
->ErrorCode
, HAL_SPI_ERROR_ABORT
);
4243 } while (HAL_IS_BIT_SET(hspi
->Instance
->CR2
, SPI_CR2_RXNEIE
));
4245 /* Control the BSY flag */
4246 if (SPI_WaitFlagStateUntilTimeout(hspi
, SPI_FLAG_BSY
, RESET
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4248 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4251 /* Empty the FRLVL fifo */
4252 if (SPI_WaitFifoStateUntilTimeout(hspi
, SPI_FLAG_FRLVL
, SPI_FRLVL_EMPTY
, SPI_DEFAULT_TIMEOUT
, HAL_GetTick()) != HAL_OK
)
4254 hspi
->ErrorCode
= HAL_SPI_ERROR_ABORT
;
4257 hspi
->State
= HAL_SPI_STATE_ABORT
;
4264 #endif /* HAL_SPI_MODULE_ENABLED */
4274 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/