Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:09

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_spi.c
0004   * @author  MCD Application Team
0005   * @brief   SPI HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Serial Peripheral Interface (SPI) peripheral:
0008   *           + Initialization and de-initialization functions
0009   *           + IO operation functions
0010   *           + Peripheral Control functions
0011   *           + Peripheral State functions
0012   *
0013   ******************************************************************************
0014   * @attention
0015   *
0016   * Copyright (c) 2017 STMicroelectronics.
0017   * All rights reserved.
0018   *
0019   * This software is licensed under terms that can be found in the LICENSE file
0020   * in the root directory of this software component.
0021   * If no LICENSE file comes with this software, it is provided AS-IS.
0022   *
0023   ******************************************************************************
0024   @verbatim
0025   ==============================================================================
0026                         ##### How to use this driver #####
0027   ==============================================================================
0028     [..]
0029       The SPI HAL driver can be used as follows:
0030 
0031       (#) Declare a SPI_HandleTypeDef handle structure, for example:
0032           SPI_HandleTypeDef  hspi;
0033 
0034       (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
0035           (##) Enable the SPIx interface clock
0036           (##) SPI pins configuration
0037               (+++) Enable the clock for the SPI GPIOs
0038               (+++) Configure these SPI pins as alternate function push-pull
0039           (##) NVIC configuration if you need to use interrupt process or DMA process
0040               (+++) Configure the SPIx interrupt priority
0041               (+++) Enable the NVIC SPI IRQ handle
0042           (##) DMA Configuration if you need to use DMA process
0043               (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel
0044               (+++) Enable the DMAx clock
0045               (+++) Configure the DMA handle parameters
0046               (+++) Configure the DMA Tx or Rx Stream/Channel
0047               (+++) Associate the initialized hdma_tx handle to the hspi DMA Tx or Rx handle
0048               (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx
0049                     or Rx Stream/Channel
0050 
0051       (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
0052           management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
0053 
0054       (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
0055           (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
0056               by calling the customized HAL_SPI_MspInit() API.
0057      [..]
0058        Callback registration:
0059 
0060       (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1UL
0061           allows the user to configure dynamically the driver callbacks.
0062           Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback.
0063 
0064           Function HAL_SPI_RegisterCallback() allows to register following callbacks:
0065             (+) TxCpltCallback        : SPI Tx Completed callback
0066             (+) RxCpltCallback        : SPI Rx Completed callback
0067             (+) TxRxCpltCallback      : SPI TxRx Completed callback
0068             (+) TxHalfCpltCallback    : SPI Tx Half Completed callback
0069             (+) RxHalfCpltCallback    : SPI Rx Half Completed callback
0070             (+) TxRxHalfCpltCallback  : SPI TxRx Half Completed callback
0071             (+) ErrorCallback         : SPI Error callback
0072             (+) AbortCpltCallback     : SPI Abort callback
0073             (+) SuspendCallback       : SPI Suspend callback
0074             (+) MspInitCallback       : SPI Msp Init callback
0075             (+) MspDeInitCallback     : SPI Msp DeInit callback
0076           This function takes as parameters the HAL peripheral handle, the Callback ID
0077           and a pointer to the user callback function.
0078 
0079 
0080       (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default
0081           weak function.
0082           HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle,
0083           and the Callback ID.
0084           This function allows to reset following callbacks:
0085             (+) TxCpltCallback        : SPI Tx Completed callback
0086             (+) RxCpltCallback        : SPI Rx Completed callback
0087             (+) TxRxCpltCallback      : SPI TxRx Completed callback
0088             (+) TxHalfCpltCallback    : SPI Tx Half Completed callback
0089             (+) RxHalfCpltCallback    : SPI Rx Half Completed callback
0090             (+) TxRxHalfCpltCallback  : SPI TxRx Half Completed callback
0091             (+) ErrorCallback         : SPI Error callback
0092             (+) AbortCpltCallback     : SPI Abort callback
0093             (+) SuspendCallback       : SPI Suspend callback
0094             (+) MspInitCallback       : SPI Msp Init callback
0095             (+) MspDeInitCallback     : SPI Msp DeInit callback
0096 
0097        By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET
0098        all callbacks are set to the corresponding weak functions:
0099        examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback().
0100        Exception done for MspInit and MspDeInit functions that are
0101        reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when
0102        these callbacks are null (not registered beforehand).
0103        If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit()
0104        keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
0105 
0106        Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only.
0107        Exception done MspInit/MspDeInit functions that can be registered/unregistered
0108        in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state,
0109        thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
0110        Then, the user first registers the MspInit/MspDeInit user callbacks
0111        using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit()
0112        or HAL_SPI_Init() function.
0113 
0114        When The compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or not defined,
0115        the callback registering feature is not available and weak callbacks are used.
0116 
0117        SuspendCallback restriction:
0118            SuspendCallback is called only when MasterReceiverAutoSusp is enabled and
0119        EOT interrupt is activated. SuspendCallback is used in relation with functions
0120        HAL_SPI_Transmit_IT, HAL_SPI_Receive_IT and HAL_SPI_TransmitReceive_IT.
0121 
0122     [..]
0123       Circular mode restriction:
0124       (+) The DMA circular mode cannot be used when the SPI is configured in these modes:
0125           (++) Master 2Lines RxOnly
0126           (++) Master 1Line Rx
0127       (+) The CRC feature is not managed when the DMA circular mode is enabled
0128       (+) The functions HAL_SPI_DMAPause()/ HAL_SPI_DMAResume() are not supported. Return always
0129           HAL_ERROR with ErrorCode set to HAL_SPI_ERROR_NOT_SUPPORTED.
0130           Those functions are maintained for backward compatibility reasons.
0131 
0132   @endverbatim
0133   */
0134 
0135 /* Includes ------------------------------------------------------------------*/
0136 #include "stm32h7xx_hal.h"
0137 
0138 /** @addtogroup STM32H7xx_HAL_Driver
0139   * @{
0140   */
0141 
0142 /** @defgroup SPI SPI
0143   * @ingroup RTEMSBSPsARMSTM32H7
0144   * @brief SPI HAL module driver
0145   * @{
0146   */
0147 #ifdef HAL_SPI_MODULE_ENABLED
0148 
0149 /* Private typedef -----------------------------------------------------------*/
0150 /* Private defines -----------------------------------------------------------*/
0151 /** @defgroup SPI_Private_Constants SPI Private Constants
0152   * @ingroup RTEMSBSPsARMSTM32H7
0153   * @{
0154   */
0155 #define SPI_DEFAULT_TIMEOUT 100UL
0156 /**
0157   * @}
0158   */
0159 
0160 /* Private macros ------------------------------------------------------------*/
0161 /* Private variables ---------------------------------------------------------*/
0162 /* Private function prototypes -----------------------------------------------*/
0163 /** @defgroup SPI_Private_Functions SPI Private Functions
0164   * @ingroup RTEMSBSPsARMSTM32H7
0165   * @{
0166   */
0167 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
0168 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
0169 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
0170 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
0171 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
0172 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
0173 static void SPI_DMAError(DMA_HandleTypeDef *hdma);
0174 static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma);
0175 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
0176 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
0177 static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(const SPI_HandleTypeDef *hspi, uint32_t Flag,
0178                                                     FlagStatus FlagStatus, uint32_t Timeout, uint32_t Tickstart);
0179 static void SPI_TxISR_8BIT(SPI_HandleTypeDef *hspi);
0180 static void SPI_TxISR_16BIT(SPI_HandleTypeDef *hspi);
0181 static void SPI_TxISR_32BIT(SPI_HandleTypeDef *hspi);
0182 static void SPI_RxISR_8BIT(SPI_HandleTypeDef *hspi);
0183 static void SPI_RxISR_16BIT(SPI_HandleTypeDef *hspi);
0184 static void SPI_RxISR_32BIT(SPI_HandleTypeDef *hspi);
0185 static void SPI_AbortTransfer(SPI_HandleTypeDef *hspi);
0186 static void SPI_CloseTransfer(SPI_HandleTypeDef *hspi);
0187 static uint32_t SPI_GetPacketSize(const SPI_HandleTypeDef *hspi);
0188 
0189 
0190 /**
0191   * @}
0192   */
0193 
0194 /* Exported functions --------------------------------------------------------*/
0195 /** @defgroup SPI_Exported_Functions SPI Exported Functions
0196   * @ingroup RTEMSBSPsARMSTM32H7
0197   * @{
0198   */
0199 
0200 /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
0201   * @ingroup RTEMSBSPsARMSTM32H7
0202   *  @brief    Initialization and Configuration functions
0203   *
0204 @verbatim
0205  ===============================================================================
0206               ##### Initialization and de-initialization functions #####
0207  ===============================================================================
0208     [..]  This subsection provides a set of functions allowing to initialize and
0209           de-initialize the SPIx peripheral:
0210 
0211       (+) User must implement HAL_SPI_MspInit() function in which he configures
0212           all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
0213 
0214       (+) Call the function HAL_SPI_Init() to configure the selected device with
0215           the selected configuration:
0216         (++) Mode
0217         (++) Direction
0218         (++) Data Size
0219         (++) Clock Polarity and Phase
0220         (++) NSS Management
0221         (++) BaudRate Prescaler
0222         (++) FirstBit
0223         (++) TIMode
0224         (++) CRC Calculation
0225         (++) CRC Polynomial if CRC enabled
0226         (++) CRC Length, used only with Data8 and Data16
0227         (++) FIFO reception threshold
0228         (++) FIFO transmission threshold
0229 
0230       (+) Call the function HAL_SPI_DeInit() to restore the default configuration
0231           of the selected SPIx peripheral.
0232 
0233 @endverbatim
0234   * @{
0235   */
0236 
0237 /**
0238   * @brief  Initialize the SPI according to the specified parameters
0239   *         in the SPI_InitTypeDef and initialize the associated handle.
0240   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
0241   *               the configuration information for SPI module.
0242   * @retval HAL status
0243   */
0244 HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
0245 {
0246   uint32_t crc_length;
0247   uint32_t packet_length;
0248 #if (USE_SPI_CRC != 0UL)
0249   uint32_t crc_poly_msb_mask;
0250 #endif /* USE_SPI_CRC */
0251 
0252   /* Check the SPI handle allocation */
0253   if (hspi == NULL)
0254   {
0255     return HAL_ERROR;
0256   }
0257 
0258   /* Check the parameters */
0259   assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
0260   assert_param(IS_SPI_MODE(hspi->Init.Mode));
0261   assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
0262   assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
0263   assert_param(IS_SPI_FIFOTHRESHOLD(hspi->Init.FifoThreshold));
0264   assert_param(IS_SPI_NSS(hspi->Init.NSS));
0265   assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
0266   assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
0267   assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
0268   assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
0269   if (hspi->Init.TIMode == SPI_TIMODE_DISABLE)
0270   {
0271     assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
0272     assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
0273   }
0274 #if (USE_SPI_CRC != 0UL)
0275   assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
0276   if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
0277   {
0278     assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
0279     assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
0280     assert_param(IS_SPI_CRC_INITIALIZATION_PATTERN(hspi->Init.TxCRCInitializationPattern));
0281     assert_param(IS_SPI_CRC_INITIALIZATION_PATTERN(hspi->Init.RxCRCInitializationPattern));
0282   }
0283 #else
0284   hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
0285 #endif /* USE_SPI_CRC */
0286 
0287   /* Verify that the SPI instance supports Data Size higher than 16bits */
0288   if ((!IS_SPI_HIGHEND_INSTANCE(hspi->Instance)) && (hspi->Init.DataSize > SPI_DATASIZE_16BIT))
0289   {
0290     return HAL_ERROR;
0291   }
0292 
0293   /* Verify that the SPI instance supports requested data packing */
0294   packet_length = SPI_GetPacketSize(hspi);
0295   if (((!IS_SPI_HIGHEND_INSTANCE(hspi->Instance)) && (packet_length > SPI_LOWEND_FIFO_SIZE)) ||
0296       ((IS_SPI_HIGHEND_INSTANCE(hspi->Instance)) && (packet_length > SPI_HIGHEND_FIFO_SIZE)))
0297   {
0298     return HAL_ERROR;
0299   }
0300 
0301 #if (USE_SPI_CRC != 0UL)
0302   if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
0303   {
0304     /* Verify that the SPI instance supports CRC Length higher than 16bits */
0305     if ((!IS_SPI_HIGHEND_INSTANCE(hspi->Instance)) && (hspi->Init.CRCLength > SPI_CRC_LENGTH_16BIT))
0306     {
0307       return HAL_ERROR;
0308     }
0309 
0310     /* Align the CRC Length on the data size */
0311     if (hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
0312     {
0313       crc_length = (hspi->Init.DataSize >> SPI_CFG1_DSIZE_Pos) << SPI_CFG1_CRCSIZE_Pos;
0314     }
0315     else
0316     {
0317       crc_length = hspi->Init.CRCLength;
0318     }
0319 
0320     /* Verify the correctness of polynom size */
0321     assert_param(IS_SPI_CRC_POLYNOMIAL_SIZE(hspi->Init.CRCPolynomial, crc_length));
0322 
0323     /* Verify that the CRC Length is higher than DataSize */
0324     if ((hspi->Init.DataSize >> SPI_CFG1_DSIZE_Pos) > (crc_length >> SPI_CFG1_CRCSIZE_Pos))
0325     {
0326       return HAL_ERROR;
0327     }
0328   }
0329   else
0330   {
0331     crc_length = hspi->Init.DataSize << SPI_CFG1_CRCSIZE_Pos;
0332   }
0333 #endif /* USE_SPI_CRC */
0334 
0335   if (hspi->State == HAL_SPI_STATE_RESET)
0336   {
0337     /* Allocate lock resource and initialize it */
0338     hspi->Lock = HAL_UNLOCKED;
0339 
0340 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
0341     /* Init the SPI Callback settings */
0342     hspi->TxCpltCallback       = HAL_SPI_TxCpltCallback;       /* Legacy weak TxCpltCallback       */
0343     hspi->RxCpltCallback       = HAL_SPI_RxCpltCallback;       /* Legacy weak RxCpltCallback       */
0344     hspi->TxRxCpltCallback     = HAL_SPI_TxRxCpltCallback;     /* Legacy weak TxRxCpltCallback     */
0345     hspi->TxHalfCpltCallback   = HAL_SPI_TxHalfCpltCallback;   /* Legacy weak TxHalfCpltCallback   */
0346     hspi->RxHalfCpltCallback   = HAL_SPI_RxHalfCpltCallback;   /* Legacy weak RxHalfCpltCallback   */
0347     hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
0348     hspi->ErrorCallback        = HAL_SPI_ErrorCallback;        /* Legacy weak ErrorCallback        */
0349     hspi->AbortCpltCallback    = HAL_SPI_AbortCpltCallback;    /* Legacy weak AbortCpltCallback    */
0350     hspi->SuspendCallback      = HAL_SPI_SuspendCallback;      /* Legacy weak SuspendCallback      */
0351 
0352     if (hspi->MspInitCallback == NULL)
0353     {
0354       hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit  */
0355     }
0356 
0357     /* Init the low level hardware : GPIO, CLOCK, NVIC... */
0358     hspi->MspInitCallback(hspi);
0359 #else
0360     /* Init the low level hardware : GPIO, CLOCK, NVIC... */
0361     HAL_SPI_MspInit(hspi);
0362 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
0363   }
0364 
0365   hspi->State = HAL_SPI_STATE_BUSY;
0366 
0367   /* Disable the selected SPI peripheral */
0368   __HAL_SPI_DISABLE(hspi);
0369 
0370 #if (USE_SPI_CRC == 0)
0371   /* Keep the default value of CRCSIZE in case of CRC is not used */
0372   crc_length = hspi->Instance->CFG1 & SPI_CFG1_CRCSIZE;
0373 #endif /* USE_SPI_CRC */
0374 
0375   /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
0376   /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
0377   Communication speed, First bit, CRC calculation state, CRC Length */
0378 
0379   /* SPIx NSS Software Management Configuration */
0380   if ((hspi->Init.NSS == SPI_NSS_SOFT) && (((hspi->Init.Mode == SPI_MODE_MASTER) &&  \
0381                                             (hspi->Init.NSSPolarity == SPI_NSS_POLARITY_LOW)) || \
0382                                            ((hspi->Init.Mode == SPI_MODE_SLAVE) && \
0383                                             (hspi->Init.NSSPolarity == SPI_NSS_POLARITY_HIGH))))
0384   {
0385     SET_BIT(hspi->Instance->CR1, SPI_CR1_SSI);
0386   }
0387 
0388   /* SPIx Master Rx Auto Suspend Configuration */
0389   if (((hspi->Init.Mode & SPI_MODE_MASTER) == SPI_MODE_MASTER) && (hspi->Init.DataSize >= SPI_DATASIZE_8BIT))
0390   {
0391     MODIFY_REG(hspi->Instance->CR1, SPI_CR1_MASRX, hspi->Init.MasterReceiverAutoSusp);
0392   }
0393   else
0394   {
0395     CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_MASRX);
0396   }
0397 
0398   /* SPIx CFG1 Configuration */
0399   WRITE_REG(hspi->Instance->CFG1, (hspi->Init.BaudRatePrescaler | hspi->Init.CRCCalculation | crc_length |
0400                                    hspi->Init.FifoThreshold     | hspi->Init.DataSize));
0401 
0402   /* SPIx CFG2 Configuration */
0403   WRITE_REG(hspi->Instance->CFG2, (hspi->Init.NSSPMode                | hspi->Init.TIMode    |
0404                                    hspi->Init.NSSPolarity             | hspi->Init.NSS       |
0405                                    hspi->Init.CLKPolarity             | hspi->Init.CLKPhase  |
0406                                    hspi->Init.FirstBit                | hspi->Init.Mode      |
0407                                    hspi->Init.MasterInterDataIdleness | hspi->Init.Direction |
0408                                    hspi->Init.MasterSSIdleness        | hspi->Init.IOSwap));
0409 
0410 #if (USE_SPI_CRC != 0UL)
0411   /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
0412   /* Configure : CRC Polynomial */
0413   if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
0414   {
0415     /* Initialize TXCRC Pattern Initial Value */
0416     if (hspi->Init.TxCRCInitializationPattern == SPI_CRC_INITIALIZATION_ALL_ONE_PATTERN)
0417     {
0418       SET_BIT(hspi->Instance->CR1, SPI_CR1_TCRCINI);
0419     }
0420     else
0421     {
0422       CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_TCRCINI);
0423     }
0424 
0425     /* Initialize RXCRC Pattern Initial Value */
0426     if (hspi->Init.RxCRCInitializationPattern == SPI_CRC_INITIALIZATION_ALL_ONE_PATTERN)
0427     {
0428       SET_BIT(hspi->Instance->CR1, SPI_CR1_RCRCINI);
0429     }
0430     else
0431     {
0432       CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_RCRCINI);
0433     }
0434 
0435     /* Enable 33/17 bits CRC computation */
0436     if (((!IS_SPI_HIGHEND_INSTANCE(hspi->Instance)) && (crc_length == SPI_CRC_LENGTH_16BIT)) ||
0437         ((IS_SPI_HIGHEND_INSTANCE(hspi->Instance))  && (crc_length == SPI_CRC_LENGTH_32BIT)))
0438     {
0439       /* Set SPI_CR1_CRC33_17 bit */
0440       SET_BIT(hspi->Instance->CR1, SPI_CR1_CRC33_17);
0441       /* Write CRC polynomial in SPI Register */
0442       WRITE_REG(hspi->Instance->CRCPOLY, hspi->Init.CRCPolynomial);
0443     }
0444     else
0445     {
0446       /* Clear SPI_CR1_CRC33_17 bit */
0447       CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRC33_17);
0448 
0449       /* Write CRC polynomial and set MSB bit at 1 in SPI Register */
0450       /* Set MSB is mandatory for a correct CRC computation        */
0451       crc_poly_msb_mask = (0x1UL << ((crc_length >> SPI_CFG1_CRCSIZE_Pos) + 0x1U));
0452       WRITE_REG(hspi->Instance->CRCPOLY, (hspi->Init.CRCPolynomial) | crc_poly_msb_mask);
0453     }
0454   }
0455 #endif /* USE_SPI_CRC */
0456 
0457   /* Insure that Underrun configuration is managed only by Salve */
0458   if (hspi->Init.Mode == SPI_MODE_SLAVE)
0459   {
0460     /* Set Default Underrun configuration */
0461 #if (USE_SPI_CRC != 0UL)
0462     if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_DISABLE)
0463 #endif /* USE_SPI_CRC */
0464     {
0465       MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRDET, SPI_CFG1_UDRDET_0);
0466     }
0467     MODIFY_REG(hspi->Instance->CFG1, SPI_CFG1_UDRCFG, SPI_CFG1_UDRCFG_1);
0468   }
0469 
0470 #if defined(SPI_I2SCFGR_I2SMOD)
0471   /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
0472   CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
0473 #endif /* SPI_I2SCFGR_I2SMOD */
0474 
0475   /* Insure that AFCNTR is managed only by Master */
0476   if ((hspi->Init.Mode & SPI_MODE_MASTER) == SPI_MODE_MASTER)
0477   {
0478     /* Alternate function GPIOs control */
0479     MODIFY_REG(hspi->Instance->CFG2, SPI_CFG2_AFCNTR, (hspi->Init.MasterKeepIOState));
0480   }
0481 
0482   hspi->ErrorCode = HAL_SPI_ERROR_NONE;
0483   hspi->State     = HAL_SPI_STATE_READY;
0484 
0485   return HAL_OK;
0486 }
0487 
0488 /**
0489   * @brief  De-Initialize the SPI peripheral.
0490   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
0491   *               the configuration information for SPI module.
0492   * @retval HAL status
0493   */
0494 HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
0495 {
0496   /* Check the SPI handle allocation */
0497   if (hspi == NULL)
0498   {
0499     return HAL_ERROR;
0500   }
0501 
0502   /* Check SPI Instance parameter */
0503   assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
0504 
0505   hspi->State = HAL_SPI_STATE_BUSY;
0506 
0507   /* Disable the SPI Peripheral Clock */
0508   __HAL_SPI_DISABLE(hspi);
0509 
0510 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
0511   if (hspi->MspDeInitCallback == NULL)
0512   {
0513     hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit  */
0514   }
0515 
0516   /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
0517   hspi->MspDeInitCallback(hspi);
0518 #else
0519   /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
0520   HAL_SPI_MspDeInit(hspi);
0521 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
0522 
0523   hspi->ErrorCode = HAL_SPI_ERROR_NONE;
0524   hspi->State = HAL_SPI_STATE_RESET;
0525 
0526   /* Release Lock */
0527   __HAL_UNLOCK(hspi);
0528 
0529   return HAL_OK;
0530 }
0531 
0532 #ifndef __rtems__
0533 /**
0534   * @brief  Initialize the SPI MSP.
0535   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
0536   *               the configuration information for SPI module.
0537   * @retval None
0538   */
0539 __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
0540 {
0541   /* Prevent unused argument(s) compilation warning */
0542   UNUSED(hspi);
0543 
0544   /* NOTE : This function should not be modified, when the callback is needed,
0545             the HAL_SPI_MspInit should be implemented in the user file
0546    */
0547 }
0548 #endif
0549 
0550 /**
0551   * @brief  De-Initialize the SPI MSP.
0552   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
0553   *               the configuration information for SPI module.
0554   * @retval None
0555   */
0556 __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
0557 {
0558   /* Prevent unused argument(s) compilation warning */
0559   UNUSED(hspi);
0560 
0561   /* NOTE : This function should not be modified, when the callback is needed,
0562             the HAL_SPI_MspDeInit should be implemented in the user file
0563    */
0564 }
0565 
0566 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
0567 /**
0568   * @brief  Register a User SPI Callback
0569   *         To be used instead of the weak predefined callback
0570   * @param  hspi Pointer to a SPI_HandleTypeDef structure that contains
0571   *                the configuration information for the specified SPI.
0572   * @param  CallbackID ID of the callback to be registered
0573   * @param  pCallback pointer to the Callback function
0574   * @note   The HAL_SPI_RegisterCallback() may be called before HAL_SPI_Init() in HAL_SPI_STATE_RESET
0575   *         to register callbacks for HAL_SPI_MSPINIT_CB_ID and HAL_SPI_MSPDEINIT_CB_ID
0576   * @retval HAL status
0577   */
0578 HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID,
0579                                            pSPI_CallbackTypeDef pCallback)
0580 {
0581   HAL_StatusTypeDef status = HAL_OK;
0582 
0583   if (pCallback == NULL)
0584   {
0585     /* Update the error code */
0586     hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK;
0587 
0588     return HAL_ERROR;
0589   }
0590 
0591   if (HAL_SPI_STATE_READY == hspi->State)
0592   {
0593     switch (CallbackID)
0594     {
0595       case HAL_SPI_TX_COMPLETE_CB_ID :
0596         hspi->TxCpltCallback = pCallback;
0597         break;
0598 
0599       case HAL_SPI_RX_COMPLETE_CB_ID :
0600         hspi->RxCpltCallback = pCallback;
0601         break;
0602 
0603       case HAL_SPI_TX_RX_COMPLETE_CB_ID :
0604         hspi->TxRxCpltCallback = pCallback;
0605         break;
0606 
0607       case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
0608         hspi->TxHalfCpltCallback = pCallback;
0609         break;
0610 
0611       case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
0612         hspi->RxHalfCpltCallback = pCallback;
0613         break;
0614 
0615       case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
0616         hspi->TxRxHalfCpltCallback = pCallback;
0617         break;
0618 
0619       case HAL_SPI_ERROR_CB_ID :
0620         hspi->ErrorCallback = pCallback;
0621         break;
0622 
0623       case HAL_SPI_ABORT_CB_ID :
0624         hspi->AbortCpltCallback = pCallback;
0625         break;
0626 
0627       case HAL_SPI_SUSPEND_CB_ID :
0628         hspi->SuspendCallback = pCallback;
0629         break;
0630 
0631       case HAL_SPI_MSPINIT_CB_ID :
0632         hspi->MspInitCallback = pCallback;
0633         break;
0634 
0635       case HAL_SPI_MSPDEINIT_CB_ID :
0636         hspi->MspDeInitCallback = pCallback;
0637         break;
0638 
0639       default :
0640         /* Update the error code */
0641         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0642 
0643         /* Return error status */
0644         status =  HAL_ERROR;
0645         break;
0646     }
0647   }
0648   else if (HAL_SPI_STATE_RESET == hspi->State)
0649   {
0650     switch (CallbackID)
0651     {
0652       case HAL_SPI_MSPINIT_CB_ID :
0653         hspi->MspInitCallback = pCallback;
0654         break;
0655 
0656       case HAL_SPI_MSPDEINIT_CB_ID :
0657         hspi->MspDeInitCallback = pCallback;
0658         break;
0659 
0660       default :
0661         /* Update the error code */
0662         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0663 
0664         /* Return error status */
0665         status =  HAL_ERROR;
0666         break;
0667     }
0668   }
0669   else
0670   {
0671     /* Update the error code */
0672     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0673 
0674     /* Return error status */
0675     status =  HAL_ERROR;
0676   }
0677 
0678   return status;
0679 }
0680 
0681 /**
0682   * @brief  Unregister an SPI Callback
0683   *         SPI callback is redirected to the weak predefined callback
0684   * @param  hspi Pointer to a SPI_HandleTypeDef structure that contains
0685   *                the configuration information for the specified SPI.
0686   * @param  CallbackID ID of the callback to be unregistered
0687   * @note   The HAL_SPI_UnRegisterCallback() may be called before HAL_SPI_Init() in HAL_SPI_STATE_RESET
0688   *         to un-register callbacks for HAL_SPI_MSPINIT_CB_ID and HAL_SPI_MSPDEINIT_CB_ID
0689   * @retval HAL status
0690   */
0691 HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID)
0692 {
0693   HAL_StatusTypeDef status = HAL_OK;
0694 
0695   if (HAL_SPI_STATE_READY == hspi->State)
0696   {
0697     switch (CallbackID)
0698     {
0699       case HAL_SPI_TX_COMPLETE_CB_ID :
0700         hspi->TxCpltCallback = HAL_SPI_TxCpltCallback;             /* Legacy weak TxCpltCallback       */
0701         break;
0702 
0703       case HAL_SPI_RX_COMPLETE_CB_ID :
0704         hspi->RxCpltCallback = HAL_SPI_RxCpltCallback;             /* Legacy weak RxCpltCallback       */
0705         break;
0706 
0707       case HAL_SPI_TX_RX_COMPLETE_CB_ID :
0708         hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback;         /* Legacy weak TxRxCpltCallback     */
0709         break;
0710 
0711       case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
0712         hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback;     /* Legacy weak TxHalfCpltCallback   */
0713         break;
0714 
0715       case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
0716         hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback;     /* Legacy weak RxHalfCpltCallback   */
0717         break;
0718 
0719       case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
0720         hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
0721         break;
0722 
0723       case HAL_SPI_ERROR_CB_ID :
0724         hspi->ErrorCallback = HAL_SPI_ErrorCallback;               /* Legacy weak ErrorCallback        */
0725         break;
0726 
0727       case HAL_SPI_ABORT_CB_ID :
0728         hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback;       /* Legacy weak AbortCpltCallback    */
0729         break;
0730 
0731       case HAL_SPI_SUSPEND_CB_ID :
0732         hspi->SuspendCallback = HAL_SPI_SuspendCallback;           /* Legacy weak SuspendCallback      */
0733         break;
0734 
0735       case HAL_SPI_MSPINIT_CB_ID :
0736         hspi->MspInitCallback = HAL_SPI_MspInit;                   /* Legacy weak MspInit              */
0737         break;
0738 
0739       case HAL_SPI_MSPDEINIT_CB_ID :
0740         hspi->MspDeInitCallback = HAL_SPI_MspDeInit;               /* Legacy weak MspDeInit            */
0741         break;
0742 
0743       default :
0744         /* Update the error code */
0745         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0746 
0747         /* Return error status */
0748         status =  HAL_ERROR;
0749         break;
0750     }
0751   }
0752   else if (HAL_SPI_STATE_RESET == hspi->State)
0753   {
0754     switch (CallbackID)
0755     {
0756       case HAL_SPI_MSPINIT_CB_ID :
0757         hspi->MspInitCallback = HAL_SPI_MspInit;                   /* Legacy weak MspInit              */
0758         break;
0759 
0760       case HAL_SPI_MSPDEINIT_CB_ID :
0761         hspi->MspDeInitCallback = HAL_SPI_MspDeInit;               /* Legacy weak MspDeInit            */
0762         break;
0763 
0764       default :
0765         /* Update the error code */
0766         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0767 
0768         /* Return error status */
0769         status =  HAL_ERROR;
0770         break;
0771     }
0772   }
0773   else
0774   {
0775     /* Update the error code */
0776     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
0777 
0778     /* Return error status */
0779     status =  HAL_ERROR;
0780   }
0781 
0782   return status;
0783 }
0784 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
0785 /**
0786   * @}
0787   */
0788 
0789 /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
0790   * @ingroup RTEMSBSPsARMSTM32H7
0791   *  @brief   Data transfers functions
0792   *
0793 @verbatim
0794   ==============================================================================
0795                       ##### IO operation functions #####
0796  ===============================================================================
0797  [..]
0798     This subsection provides a set of functions allowing to manage the SPI
0799     data transfers.
0800 
0801     [..] The SPI supports master and slave mode :
0802 
0803     (#) There are two modes of transfer:
0804        (##) Blocking mode: The communication is performed in polling mode.
0805             The HAL status of all data processing is returned by the same function
0806             after finishing transfer.
0807        (##) No-Blocking mode: The communication is performed using Interrupts
0808             or DMA, These APIs return the HAL status.
0809             The end of the data processing will be indicated through the
0810             dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
0811             using DMA mode.
0812             The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
0813             will be executed respectively at the end of the transmit or Receive process
0814             The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
0815 
0816     (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
0817         exist for 1Line (simplex) and 2Lines (full duplex) modes.
0818 
0819 @endverbatim
0820   * @{
0821   */
0822 
0823 /**
0824   * @brief  Transmit an amount of data in blocking mode.
0825   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
0826   *                  the configuration information for SPI module.
0827   * @param  pData  : pointer to data buffer
0828   * @param  Size   : amount of data to be sent
0829   * @param  Timeout: Timeout duration
0830   * @retval HAL status
0831   */
0832 HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
0833 {
0834 #if defined (__GNUC__)
0835   __IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
0836 #endif /* __GNUC__ */
0837 
0838   uint32_t tickstart;
0839 
0840   /* Check Direction parameter */
0841   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_TXONLY(hspi->Init.Direction));
0842 
0843   /* Init tickstart for timeout management*/
0844   tickstart = HAL_GetTick();
0845 
0846   if (hspi->State != HAL_SPI_STATE_READY)
0847   {
0848     return HAL_BUSY;
0849   }
0850 
0851   if ((pData == NULL) || (Size == 0UL))
0852   {
0853     return HAL_ERROR;
0854   }
0855 
0856   /* Lock the process */
0857   __HAL_LOCK(hspi);
0858 
0859   /* Set the transaction information */
0860   hspi->State       = HAL_SPI_STATE_BUSY_TX;
0861   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
0862   hspi->pTxBuffPtr  = (const uint8_t *)pData;
0863   hspi->TxXferSize  = Size;
0864   hspi->TxXferCount = Size;
0865 
0866   /*Init field not used in handle to zero */
0867   hspi->pRxBuffPtr  = NULL;
0868   hspi->RxXferSize  = (uint16_t) 0UL;
0869   hspi->RxXferCount = (uint16_t) 0UL;
0870   hspi->TxISR       = NULL;
0871   hspi->RxISR       = NULL;
0872 
0873   /* Configure communication direction : 1Line */
0874   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
0875   {
0876     SPI_1LINE_TX(hspi);
0877   }
0878   else
0879   {
0880     SPI_2LINES_TX(hspi);
0881   }
0882 
0883   /* Set the number of data at current transfer */
0884   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
0885 
0886   /* Enable SPI peripheral */
0887   __HAL_SPI_ENABLE(hspi);
0888 
0889   if (hspi->Init.Mode == SPI_MODE_MASTER)
0890   {
0891     /* Master transfer start */
0892     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
0893   }
0894 
0895   /* Transmit data in 32 Bit mode */
0896   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
0897   {
0898     /* Transmit data in 32 Bit mode */
0899     while (hspi->TxXferCount > 0UL)
0900     {
0901       /* Wait until TXP flag is set to send data */
0902       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP))
0903       {
0904         *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
0905         hspi->pTxBuffPtr += sizeof(uint32_t);
0906         hspi->TxXferCount--;
0907       }
0908       else
0909       {
0910         /* Timeout management */
0911         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
0912         {
0913           /* Call standard close procedure with error check */
0914           SPI_CloseTransfer(hspi);
0915 
0916           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
0917           hspi->State = HAL_SPI_STATE_READY;
0918 
0919           /* Unlock the process */
0920           __HAL_UNLOCK(hspi);
0921 
0922           return HAL_TIMEOUT;
0923         }
0924       }
0925     }
0926   }
0927   /* Transmit data in 16 Bit mode */
0928   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
0929   {
0930     /* Transmit data in 16 Bit mode */
0931     while (hspi->TxXferCount > 0UL)
0932     {
0933       /* Wait until TXP flag is set to send data */
0934       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP))
0935       {
0936         if ((hspi->TxXferCount > 1UL) && (hspi->Init.FifoThreshold > SPI_FIFO_THRESHOLD_01DATA))
0937         {
0938           *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
0939           hspi->pTxBuffPtr += sizeof(uint32_t);
0940           hspi->TxXferCount -= (uint16_t)2UL;
0941         }
0942         else
0943         {
0944 #if defined (__GNUC__)
0945           *ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
0946 #else
0947           *((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
0948 #endif /* __GNUC__ */
0949           hspi->pTxBuffPtr += sizeof(uint16_t);
0950           hspi->TxXferCount--;
0951         }
0952       }
0953       else
0954       {
0955         /* Timeout management */
0956         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
0957         {
0958           /* Call standard close procedure with error check */
0959           SPI_CloseTransfer(hspi);
0960 
0961           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
0962           hspi->State = HAL_SPI_STATE_READY;
0963 
0964           /* Unlock the process */
0965           __HAL_UNLOCK(hspi);
0966 
0967           return HAL_TIMEOUT;
0968         }
0969       }
0970     }
0971   }
0972   /* Transmit data in 8 Bit mode */
0973   else
0974   {
0975     while (hspi->TxXferCount > 0UL)
0976     {
0977       /* Wait until TXP flag is set to send data */
0978       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP))
0979       {
0980         if ((hspi->TxXferCount > 3UL) && (hspi->Init.FifoThreshold > SPI_FIFO_THRESHOLD_03DATA))
0981         {
0982           *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
0983           hspi->pTxBuffPtr += sizeof(uint32_t);
0984           hspi->TxXferCount -= (uint16_t)4UL;
0985         }
0986         else if ((hspi->TxXferCount > 1UL) && (hspi->Init.FifoThreshold > SPI_FIFO_THRESHOLD_01DATA))
0987         {
0988 #if defined (__GNUC__)
0989           *ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
0990 #else
0991           *((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
0992 #endif /* __GNUC__ */
0993           hspi->pTxBuffPtr += sizeof(uint16_t);
0994           hspi->TxXferCount -= (uint16_t)2UL;
0995         }
0996         else
0997         {
0998           *((__IO uint8_t *)&hspi->Instance->TXDR) = *((const uint8_t *)hspi->pTxBuffPtr);
0999           hspi->pTxBuffPtr += sizeof(uint8_t);
1000           hspi->TxXferCount--;
1001         }
1002       }
1003       else
1004       {
1005         /* Timeout management */
1006         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1007         {
1008           /* Call standard close procedure with error check */
1009           SPI_CloseTransfer(hspi);
1010 
1011           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1012           hspi->State = HAL_SPI_STATE_READY;
1013 
1014           /* Unlock the process */
1015           __HAL_UNLOCK(hspi);
1016 
1017           return HAL_TIMEOUT;
1018         }
1019       }
1020     }
1021   }
1022 
1023   /* Wait for Tx (and CRC) data to be sent */
1024   if (SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_EOT, RESET, Timeout, tickstart) != HAL_OK)
1025   {
1026     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1027   }
1028 
1029   /* Call standard close procedure with error check */
1030   SPI_CloseTransfer(hspi);
1031 
1032   hspi->State = HAL_SPI_STATE_READY;
1033 
1034   /* Unlock the process */
1035   __HAL_UNLOCK(hspi);
1036 
1037   if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1038   {
1039     return HAL_ERROR;
1040   }
1041   else
1042   {
1043     return HAL_OK;
1044   }
1045 }
1046 
1047 /**
1048   * @brief  Receive an amount of data in blocking mode.
1049   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
1050   *                  the configuration information for SPI module.
1051   * @param  pData  : pointer to data buffer
1052   * @param  Size   : amount of data to be received
1053   * @param  Timeout: Timeout duration
1054   * @retval HAL status
1055   */
1056 HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1057 {
1058   uint32_t tickstart;
1059   uint32_t temp_sr_reg;
1060   uint16_t init_max_data_in_fifo;
1061   init_max_data_in_fifo = (((uint16_t)(hspi->Init.FifoThreshold >> 5U) + 1U));
1062 #if defined (__GNUC__)
1063   __IO uint16_t *prxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->RXDR));
1064 #endif /* __GNUC__ */
1065 
1066   /* Check Direction parameter */
1067   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_RXONLY(hspi->Init.Direction));
1068 
1069   /* Init tickstart for timeout management*/
1070   tickstart = HAL_GetTick();
1071 
1072   if (hspi->State != HAL_SPI_STATE_READY)
1073   {
1074     return HAL_BUSY;
1075   }
1076 
1077   if ((pData == NULL) || (Size == 0UL))
1078   {
1079     return HAL_ERROR;
1080   }
1081 
1082   /* Lock the process */
1083   __HAL_LOCK(hspi);
1084 
1085   /* Set the transaction information */
1086   hspi->State       = HAL_SPI_STATE_BUSY_RX;
1087   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1088   hspi->pRxBuffPtr  = (uint8_t *)pData;
1089   hspi->RxXferSize  = Size;
1090   hspi->RxXferCount = Size;
1091 
1092   /*Init field not used in handle to zero */
1093   hspi->pTxBuffPtr  = NULL;
1094   hspi->TxXferSize  = (uint16_t) 0UL;
1095   hspi->TxXferCount = (uint16_t) 0UL;
1096   hspi->RxISR       = NULL;
1097   hspi->TxISR       = NULL;
1098 
1099   /* Configure communication direction: 1Line */
1100   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1101   {
1102     SPI_1LINE_RX(hspi);
1103   }
1104   else
1105   {
1106     SPI_2LINES_RX(hspi);
1107   }
1108 
1109   /* Set the number of data at current transfer */
1110   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
1111 
1112   /* Enable SPI peripheral */
1113   __HAL_SPI_ENABLE(hspi);
1114 
1115   if (hspi->Init.Mode == SPI_MODE_MASTER)
1116   {
1117     /* Master transfer start */
1118     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
1119   }
1120 
1121   /* Receive data in 32 Bit mode */
1122   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1123   {
1124     /* Transfer loop */
1125     while (hspi->RxXferCount > 0UL)
1126     {
1127       /* Evaluate state of SR register */
1128       temp_sr_reg = hspi->Instance->SR;
1129 
1130       /* Check the RXP flag */
1131       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1132       {
1133         *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
1134         hspi->pRxBuffPtr += sizeof(uint32_t);
1135         hspi->RxXferCount--;
1136       }
1137       /* Check RXWNE flag if RXP cannot be reached */
1138       else if ((hspi->RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1139       {
1140         *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
1141         hspi->pRxBuffPtr += sizeof(uint32_t);
1142         hspi->RxXferCount--;
1143       }
1144       else
1145       {
1146         /* Timeout management */
1147         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1148         {
1149           /* Call standard close procedure with error check */
1150           SPI_CloseTransfer(hspi);
1151 
1152           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1153           hspi->State = HAL_SPI_STATE_READY;
1154 
1155           /* Unlock the process */
1156           __HAL_UNLOCK(hspi);
1157 
1158           return HAL_TIMEOUT;
1159         }
1160       }
1161     }
1162   }
1163   /* Receive data in 16 Bit mode */
1164   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1165   {
1166     /* Transfer loop */
1167     while (hspi->RxXferCount > 0UL)
1168     {
1169       /* Evaluate state of SR register */
1170       temp_sr_reg = hspi->Instance->SR;
1171 
1172       /* Check the RXP flag */
1173       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1174       {
1175 #if defined (__GNUC__)
1176         *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1177 #else
1178         *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1179 #endif /* __GNUC__ */
1180         hspi->pRxBuffPtr += sizeof(uint16_t);
1181         hspi->RxXferCount--;
1182       }
1183       /* Check RXWNE flag if RXP cannot be reached */
1184       else if ((hspi->RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1185       {
1186 #if defined (__GNUC__)
1187         *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1188 #else
1189         *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1190 #endif /* __GNUC__ */
1191         hspi->pRxBuffPtr += sizeof(uint16_t);
1192 #if defined (__GNUC__)
1193         *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1194 #else
1195         *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1196 #endif /* __GNUC__ */
1197         hspi->pRxBuffPtr += sizeof(uint16_t);
1198         hspi->RxXferCount -= (uint16_t)2UL;
1199       }
1200       /* Check RXPLVL flags when RXWNE cannot be reached */
1201       else if ((hspi->RxXferCount == 1UL) && ((temp_sr_reg & SPI_SR_RXPLVL_0) != 0UL))
1202       {
1203 #if defined (__GNUC__)
1204         *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1205 #else
1206         *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1207 #endif /* __GNUC__ */
1208         hspi->pRxBuffPtr += sizeof(uint16_t);
1209         hspi->RxXferCount--;
1210       }
1211       else
1212       {
1213         /* Timeout management */
1214         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1215         {
1216           /* Call standard close procedure with error check */
1217           SPI_CloseTransfer(hspi);
1218 
1219           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1220           hspi->State = HAL_SPI_STATE_READY;
1221 
1222           /* Unlock the process */
1223           __HAL_UNLOCK(hspi);
1224 
1225           return HAL_TIMEOUT;
1226         }
1227       }
1228     }
1229   }
1230   /* Receive data in 8 Bit mode */
1231   else
1232   {
1233     /* Transfer loop */
1234     while (hspi->RxXferCount > 0UL)
1235     {
1236       /* Evaluate state of SR register */
1237       temp_sr_reg = hspi->Instance->SR;
1238 
1239       /* Check the RXP flag */
1240       if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1241       {
1242         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1243         hspi->pRxBuffPtr += sizeof(uint8_t);
1244         hspi->RxXferCount--;
1245       }
1246       /* Check RXWNE flag if RXP cannot be reached */
1247       else if ((hspi->RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1248       {
1249         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1250         hspi->pRxBuffPtr += sizeof(uint8_t);
1251         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1252         hspi->pRxBuffPtr += sizeof(uint8_t);
1253         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1254         hspi->pRxBuffPtr += sizeof(uint8_t);
1255         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1256         hspi->pRxBuffPtr += sizeof(uint8_t);
1257         hspi->RxXferCount -= (uint16_t)4UL;
1258       }
1259       /* Check RXPLVL flags when RXWNE cannot be reached */
1260       else if ((hspi->RxXferCount < 4UL) && ((temp_sr_reg & SPI_SR_RXPLVL_Msk) != 0UL))
1261       {
1262         *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1263         hspi->pRxBuffPtr += sizeof(uint8_t);
1264         hspi->RxXferCount--;
1265       }
1266       else
1267       {
1268         /* Timeout management */
1269         if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1270         {
1271           /* Call standard close procedure with error check */
1272           SPI_CloseTransfer(hspi);
1273 
1274           SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1275           hspi->State = HAL_SPI_STATE_READY;
1276 
1277           /* Unlock the process */
1278           __HAL_UNLOCK(hspi);
1279 
1280           return HAL_TIMEOUT;
1281         }
1282       }
1283     }
1284   }
1285 
1286 #if (USE_SPI_CRC != 0UL)
1287   if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
1288   {
1289     /* Wait for crc data to be received */
1290     if (SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_EOT, RESET, Timeout, tickstart) != HAL_OK)
1291     {
1292       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1293     }
1294   }
1295 #endif /* USE_SPI_CRC */
1296 
1297   /* Call standard close procedure with error check */
1298   SPI_CloseTransfer(hspi);
1299 
1300   hspi->State = HAL_SPI_STATE_READY;
1301 
1302   /* Unlock the process */
1303   __HAL_UNLOCK(hspi);
1304 
1305 
1306   if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1307   {
1308     return HAL_ERROR;
1309   }
1310   else
1311   {
1312     return HAL_OK;
1313   }
1314 }
1315 
1316 /**
1317   * @brief  Transmit and Receive an amount of data in blocking mode.
1318   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
1319   *                  the configuration information for SPI module.
1320   * @param  pTxData: pointer to transmission data buffer
1321   * @param  pRxData: pointer to reception data buffer
1322   * @param  Size   : amount of data to be sent and received
1323   * @param  Timeout: Timeout duration
1324   * @retval HAL status
1325   */
1326 HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
1327                                           uint16_t Size, uint32_t Timeout)
1328 {
1329 #if defined (__GNUC__)
1330   __IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
1331   __IO uint16_t *prxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->RXDR));
1332 #endif /* __GNUC__ */
1333 
1334   uint32_t   tickstart;
1335   uint32_t   fifo_length;
1336   uint32_t   temp_sr_reg;
1337   uint16_t   initial_TxXferCount;
1338   uint16_t   initial_RxXferCount;
1339   uint16_t   init_max_data_in_fifo;
1340   init_max_data_in_fifo = (((uint16_t)(hspi->Init.FifoThreshold >> 5U) + 1U));
1341 
1342   /* Check Direction parameter */
1343   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1344 
1345   /* Init tickstart for timeout management*/
1346   tickstart = HAL_GetTick();
1347 
1348   initial_TxXferCount = Size;
1349   initial_RxXferCount = Size;
1350 
1351   if (hspi->State != HAL_SPI_STATE_READY)
1352   {
1353     return HAL_BUSY;
1354   }
1355 
1356   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0UL))
1357   {
1358     return HAL_ERROR;
1359   }
1360 
1361   /* Lock the process */
1362   __HAL_LOCK(hspi);
1363 
1364   /* Set the transaction information */
1365   hspi->State       = HAL_SPI_STATE_BUSY_TX_RX;
1366   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1367   hspi->pRxBuffPtr  = (uint8_t *)pRxData;
1368   hspi->RxXferCount = Size;
1369   hspi->RxXferSize  = Size;
1370   hspi->pTxBuffPtr  = (const uint8_t *)pTxData;
1371   hspi->TxXferCount = Size;
1372   hspi->TxXferSize  = Size;
1373 
1374   /*Init field not used in handle to zero */
1375   hspi->RxISR       = NULL;
1376   hspi->TxISR       = NULL;
1377 
1378   /* Set Full-Duplex mode */
1379   SPI_2LINES(hspi);
1380 
1381   /* Initialize FIFO length */
1382   if (IS_SPI_HIGHEND_INSTANCE(hspi->Instance))
1383   {
1384     fifo_length = SPI_HIGHEND_FIFO_SIZE;
1385   }
1386   else
1387   {
1388     fifo_length = SPI_LOWEND_FIFO_SIZE;
1389   }
1390 
1391   /* Set the number of data at current transfer */
1392   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
1393 
1394   __HAL_SPI_ENABLE(hspi);
1395 
1396   if (hspi->Init.Mode == SPI_MODE_MASTER)
1397   {
1398     /* Master transfer start */
1399     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
1400   }
1401 
1402   /* Transmit and Receive data in 32 Bit mode */
1403   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1404   {
1405     /* Adapt fifo length to 32bits data width */
1406     fifo_length = (fifo_length / 4UL);
1407 
1408     while ((initial_TxXferCount > 0UL) || (initial_RxXferCount > 0UL))
1409     {
1410       /* Check TXP flag */
1411       if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)) && (initial_TxXferCount > 0UL) &&
1412           (initial_RxXferCount  < (initial_TxXferCount + fifo_length)))
1413       {
1414         *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
1415         hspi->pTxBuffPtr += sizeof(uint32_t);
1416         hspi->TxXferCount --;
1417         initial_TxXferCount = hspi->TxXferCount;
1418       }
1419 
1420       /* Evaluate state of SR register */
1421       temp_sr_reg = hspi->Instance->SR;
1422 
1423       if (initial_RxXferCount > 0UL)
1424       {
1425         /* Check the RXP flag */
1426         if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1427         {
1428           *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
1429           hspi->pRxBuffPtr += sizeof(uint32_t);
1430           hspi->RxXferCount--;
1431           initial_RxXferCount = hspi->RxXferCount;
1432         }
1433         /* Check RXWNE flag if RXP cannot be reached */
1434         else if ((initial_RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1435         {
1436           *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
1437           hspi->pRxBuffPtr += sizeof(uint32_t);
1438           hspi->RxXferCount--;
1439           initial_RxXferCount = hspi->RxXferCount;
1440         }
1441         else
1442         {
1443           /* Timeout management */
1444           if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1445           {
1446             /* Call standard close procedure with error check */
1447             SPI_CloseTransfer(hspi);
1448 
1449             SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1450             hspi->State = HAL_SPI_STATE_READY;
1451 
1452             /* Unlock the process */
1453             __HAL_UNLOCK(hspi);
1454 
1455             return HAL_TIMEOUT;
1456           }
1457         }
1458       }
1459     }
1460   }
1461   /* Transmit and Receive data in 16 Bit mode */
1462   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1463   {
1464     /* Adapt fifo length to 16bits data width */
1465     fifo_length = (fifo_length / 2UL);
1466 
1467     while ((initial_TxXferCount > 0UL) || (initial_RxXferCount > 0UL))
1468     {
1469       /* Check the TXP flag */
1470       if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)) && (initial_TxXferCount > 0UL) &&
1471           (initial_RxXferCount  < (initial_TxXferCount + fifo_length)))
1472       {
1473 #if defined (__GNUC__)
1474         *ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
1475 #else
1476         *((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
1477 #endif /* __GNUC__ */
1478         hspi->pTxBuffPtr += sizeof(uint16_t);
1479         hspi->TxXferCount--;
1480         initial_TxXferCount = hspi->TxXferCount;
1481       }
1482 
1483       /* Evaluate state of SR register */
1484       temp_sr_reg = hspi->Instance->SR;
1485 
1486       if (initial_RxXferCount > 0UL)
1487       {
1488         /* Check the RXP flag */
1489         if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1490         {
1491 #if defined (__GNUC__)
1492           *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1493 #else
1494           *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1495 #endif /* __GNUC__ */
1496           hspi->pRxBuffPtr += sizeof(uint16_t);
1497           hspi->RxXferCount--;
1498           initial_RxXferCount = hspi->RxXferCount;
1499         }
1500         /* Check RXWNE flag if RXP cannot be reached */
1501         else if ((initial_RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1502         {
1503 #if defined (__GNUC__)
1504           *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1505 #else
1506           *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1507 #endif /* __GNUC__ */
1508           hspi->pRxBuffPtr += sizeof(uint16_t);
1509 #if defined (__GNUC__)
1510           *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1511 #else
1512           *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1513 #endif /* __GNUC__ */
1514           hspi->pRxBuffPtr += sizeof(uint16_t);
1515           hspi->RxXferCount -= (uint16_t)2UL;
1516           initial_RxXferCount = hspi->RxXferCount;
1517         }
1518         /* Check RXPLVL flags when RXWNE cannot be reached */
1519         else if ((initial_RxXferCount == 1UL) && ((temp_sr_reg & SPI_SR_RXPLVL_0) != 0UL))
1520         {
1521 #if defined (__GNUC__)
1522           *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
1523 #else
1524           *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
1525 #endif /* __GNUC__ */
1526           hspi->pRxBuffPtr += sizeof(uint16_t);
1527           hspi->RxXferCount--;
1528           initial_RxXferCount = hspi->RxXferCount;
1529         }
1530         else
1531         {
1532           /* Timeout management */
1533           if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1534           {
1535             /* Call standard close procedure with error check */
1536             SPI_CloseTransfer(hspi);
1537 
1538             SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1539             hspi->State = HAL_SPI_STATE_READY;
1540 
1541             /* Unlock the process */
1542             __HAL_UNLOCK(hspi);
1543 
1544             return HAL_TIMEOUT;
1545           }
1546         }
1547       }
1548     }
1549   }
1550   /* Transmit and Receive data in 8 Bit mode */
1551   else
1552   {
1553     while ((initial_TxXferCount > 0UL) || (initial_RxXferCount > 0UL))
1554     {
1555       /* Check the TXP flag */
1556       if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)) && (initial_TxXferCount > 0UL) &&
1557           (initial_RxXferCount  < (initial_TxXferCount + fifo_length)))
1558       {
1559         *((__IO uint8_t *)&hspi->Instance->TXDR) = *((const uint8_t *)hspi->pTxBuffPtr);
1560         hspi->pTxBuffPtr += sizeof(uint8_t);
1561         hspi->TxXferCount--;
1562         initial_TxXferCount = hspi->TxXferCount;
1563       }
1564 
1565       /* Evaluate state of SR register */
1566       temp_sr_reg = hspi->Instance->SR;
1567 
1568       if (initial_RxXferCount > 0UL)
1569       {
1570         /* Check the RXP flag */
1571         if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXP))
1572         {
1573           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1574           hspi->pRxBuffPtr += sizeof(uint8_t);
1575           hspi->RxXferCount--;
1576           initial_RxXferCount = hspi->RxXferCount;
1577         }
1578         /* Check RXWNE flag if RXP cannot be reached */
1579         else if ((initial_RxXferCount < init_max_data_in_fifo) && ((temp_sr_reg & SPI_SR_RXWNE_Msk) != 0UL))
1580         {
1581           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1582           hspi->pRxBuffPtr += sizeof(uint8_t);
1583           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1584           hspi->pRxBuffPtr += sizeof(uint8_t);
1585           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1586           hspi->pRxBuffPtr += sizeof(uint8_t);
1587           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1588           hspi->pRxBuffPtr += sizeof(uint8_t);
1589           hspi->RxXferCount -= (uint16_t)4UL;
1590           initial_RxXferCount = hspi->RxXferCount;
1591         }
1592         /* Check RXPLVL flags when RXWNE cannot be reached */
1593         else if ((initial_RxXferCount < 4UL) && ((temp_sr_reg & SPI_SR_RXPLVL_Msk) != 0UL))
1594         {
1595           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
1596           hspi->pRxBuffPtr += sizeof(uint8_t);
1597           hspi->RxXferCount--;
1598           initial_RxXferCount = hspi->RxXferCount;
1599         }
1600         else
1601         {
1602           /* Timeout management */
1603           if ((((HAL_GetTick() - tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
1604           {
1605             /* Call standard close procedure with error check */
1606             SPI_CloseTransfer(hspi);
1607 
1608             SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);
1609             hspi->State = HAL_SPI_STATE_READY;
1610 
1611             /* Unlock the process */
1612             __HAL_UNLOCK(hspi);
1613 
1614             return HAL_TIMEOUT;
1615           }
1616         }
1617       }
1618     }
1619   }
1620 
1621   /* Wait for Tx/Rx (and CRC) data to be sent/received */
1622   if (SPI_WaitOnFlagUntilTimeout(hspi, SPI_FLAG_EOT, RESET, Timeout, tickstart) != HAL_OK)
1623   {
1624     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
1625   }
1626 
1627   /* Call standard close procedure with error check */
1628   SPI_CloseTransfer(hspi);
1629 
1630   hspi->State = HAL_SPI_STATE_READY;
1631 
1632   /* Unlock the process */
1633   __HAL_UNLOCK(hspi);
1634 
1635   if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
1636   {
1637     return HAL_ERROR;
1638   }
1639   else
1640   {
1641     return HAL_OK;
1642   }
1643 }
1644 
1645 /**
1646   * @brief  Transmit an amount of data in non-blocking mode with Interrupt.
1647   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
1648   *                the configuration information for SPI module.
1649   * @param  pData: pointer to data buffer
1650   * @param  Size : amount of data to be sent
1651   * @retval HAL status
1652   */
1653 HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
1654 {
1655   /* Check Direction parameter */
1656   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_TXONLY(hspi->Init.Direction));
1657 
1658   if ((pData == NULL) || (Size == 0UL))
1659   {
1660     return HAL_ERROR;
1661   }
1662 
1663   if (hspi->State != HAL_SPI_STATE_READY)
1664   {
1665     return HAL_BUSY;
1666   }
1667 
1668   /* Lock the process */
1669   __HAL_LOCK(hspi);
1670 
1671   /* Set the transaction information */
1672   hspi->State       = HAL_SPI_STATE_BUSY_TX;
1673   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1674   hspi->pTxBuffPtr  = (const uint8_t *)pData;
1675   hspi->TxXferSize  = Size;
1676   hspi->TxXferCount = Size;
1677 
1678   /* Init field not used in handle to zero */
1679   hspi->pRxBuffPtr  = NULL;
1680   hspi->RxXferSize  = (uint16_t) 0UL;
1681   hspi->RxXferCount = (uint16_t) 0UL;
1682   hspi->RxISR       = NULL;
1683 
1684 #if defined(USE_SPI_RELOAD_TRANSFER)
1685   hspi->Reload.Requested   = 0UL;
1686   hspi->Reload.pTxBuffPtr  = NULL;
1687   hspi->Reload.TxXferSize  = NULL;
1688 #endif /* USE_SPI_RELOAD_TRANSFER */
1689 
1690   /* Set the function for IT treatment */
1691   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1692   {
1693     hspi->TxISR = SPI_TxISR_32BIT;
1694   }
1695   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1696   {
1697     hspi->TxISR = SPI_TxISR_16BIT;
1698   }
1699   else
1700   {
1701     hspi->TxISR = SPI_TxISR_8BIT;
1702   }
1703 
1704   /* Configure communication direction : 1Line */
1705   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1706   {
1707     SPI_1LINE_TX(hspi);
1708   }
1709   else
1710   {
1711     SPI_2LINES_TX(hspi);
1712   }
1713 
1714   /* Set the number of data at current transfer */
1715   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
1716 
1717   /* Enable SPI peripheral */
1718   __HAL_SPI_ENABLE(hspi);
1719 
1720   /* Unlock the process */
1721   __HAL_UNLOCK(hspi);
1722 
1723   /* Enable EOT, TXP, FRE, MODF, UDR and TSERF interrupts */
1724   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_TXP | SPI_IT_UDR | SPI_IT_FRE | SPI_IT_MODF | SPI_IT_TSERF));
1725 
1726   if (hspi->Init.Mode == SPI_MODE_MASTER)
1727   {
1728     /* Master transfer start */
1729     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
1730   }
1731 
1732   return HAL_OK;
1733 }
1734 
1735 /**
1736   * @brief  Receive an amount of data in non-blocking mode with Interrupt.
1737   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
1738   *                the configuration information for SPI module.
1739   * @param  pData: pointer to data buffer
1740   * @param  Size : amount of data to be sent
1741   * @retval HAL status
1742   */
1743 HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
1744 {
1745   /* Check Direction parameter */
1746   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_RXONLY(hspi->Init.Direction));
1747 
1748   if (hspi->State != HAL_SPI_STATE_READY)
1749   {
1750     return HAL_BUSY;
1751   }
1752 
1753   if ((pData == NULL) || (Size == 0UL))
1754   {
1755     return HAL_ERROR;
1756   }
1757 
1758   /* Lock the process */
1759   __HAL_LOCK(hspi);
1760 
1761   /* Set the transaction information */
1762   hspi->State       = HAL_SPI_STATE_BUSY_RX;
1763   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1764   hspi->pRxBuffPtr  = (uint8_t *)pData;
1765   hspi->RxXferSize  = Size;
1766   hspi->RxXferCount = Size;
1767 
1768   /* Init field not used in handle to zero */
1769   hspi->pTxBuffPtr  = NULL;
1770   hspi->TxXferSize  = (uint16_t) 0UL;
1771   hspi->TxXferCount = (uint16_t) 0UL;
1772   hspi->TxISR       = NULL;
1773 
1774 #if defined(USE_SPI_RELOAD_TRANSFER)
1775   hspi->Reload.Requested   = 0UL;
1776   hspi->Reload.pRxBuffPtr  = NULL;
1777   hspi->Reload.RxXferSize  = NULL;
1778 #endif /* USE_SPI_RELOAD_TRANSFER */
1779 
1780   /* Set the function for IT treatment */
1781   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1782   {
1783     hspi->RxISR = SPI_RxISR_32BIT;
1784   }
1785   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1786   {
1787     hspi->RxISR = SPI_RxISR_16BIT;
1788   }
1789   else
1790   {
1791     hspi->RxISR = SPI_RxISR_8BIT;
1792   }
1793 
1794   /* Configure communication direction : 1Line */
1795   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
1796   {
1797     SPI_1LINE_RX(hspi);
1798   }
1799   else
1800   {
1801     SPI_2LINES_RX(hspi);
1802   }
1803 
1804   /* Note : The SPI must be enabled after unlocking current process
1805             to avoid the risk of SPI interrupt handle execution before current
1806             process unlock */
1807 
1808   /* Set the number of data at current transfer */
1809   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
1810 
1811   /* Enable SPI peripheral */
1812   __HAL_SPI_ENABLE(hspi);
1813 
1814   /* Unlock the process */
1815   __HAL_UNLOCK(hspi);
1816 
1817   /* Enable EOT, RXP, OVR, FRE, MODF and TSERF interrupts */
1818   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_RXP | SPI_IT_OVR | SPI_IT_FRE | SPI_IT_MODF | SPI_IT_TSERF));
1819 
1820   if (hspi->Init.Mode == SPI_MODE_MASTER)
1821   {
1822     /* Master transfer start */
1823     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
1824   }
1825 
1826   return HAL_OK;
1827 }
1828 
1829 /**
1830   * @brief  Transmit and Receive an amount of data in non-blocking mode with Interrupt.
1831   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
1832   *                  the configuration information for SPI module.
1833   * @param  pTxData: pointer to transmission data buffer
1834   * @param  pRxData: pointer to reception data buffer
1835   * @param  Size   : amount of data to be sent and received
1836   * @retval HAL status
1837   */
1838 HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
1839                                              uint16_t Size)
1840 {
1841   uint32_t tmp_TxXferCount;
1842 #if defined (__GNUC__)
1843   __IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
1844 #endif /* __GNUC__ */
1845 
1846   /* Check Direction parameter */
1847   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
1848 
1849   if (hspi->State != HAL_SPI_STATE_READY)
1850   {
1851     return HAL_BUSY;
1852   }
1853 
1854   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0UL))
1855   {
1856     return HAL_ERROR;
1857   }
1858 
1859   /* Lock the process */
1860   __HAL_LOCK(hspi);
1861 
1862   /* Set the transaction information */
1863   hspi->State       = HAL_SPI_STATE_BUSY_TX_RX;
1864   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
1865   hspi->pTxBuffPtr  = (const uint8_t *)pTxData;
1866   hspi->TxXferSize  = Size;
1867   hspi->TxXferCount = Size;
1868   hspi->pRxBuffPtr  = (uint8_t *)pRxData;
1869   hspi->RxXferSize  = Size;
1870   hspi->RxXferCount = Size;
1871   tmp_TxXferCount   = hspi->TxXferCount;
1872 
1873 #if defined(USE_SPI_RELOAD_TRANSFER)
1874   hspi->Reload.Requested   = 0UL;
1875   hspi->Reload.pRxBuffPtr  = NULL;
1876   hspi->Reload.RxXferSize  = NULL;
1877   hspi->Reload.pTxBuffPtr  = NULL;
1878   hspi->Reload.TxXferSize  = NULL;
1879 #endif /* USE_SPI_RELOAD_TRANSFER */
1880 
1881   /* Set the function for IT treatment */
1882   if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1883   {
1884     hspi->TxISR     = SPI_TxISR_32BIT;
1885     hspi->RxISR     = SPI_RxISR_32BIT;
1886   }
1887   else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1888   {
1889     hspi->RxISR     = SPI_RxISR_16BIT;
1890     hspi->TxISR     = SPI_TxISR_16BIT;
1891   }
1892   else
1893   {
1894     hspi->RxISR     = SPI_RxISR_8BIT;
1895     hspi->TxISR     = SPI_TxISR_8BIT;
1896   }
1897 
1898   /* Set Full-Duplex mode */
1899   SPI_2LINES(hspi);
1900 
1901   /* Set the number of data at current transfer */
1902   MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
1903 
1904   /* Enable SPI peripheral */
1905   __HAL_SPI_ENABLE(hspi);
1906 
1907   /* Fill in the TxFIFO */
1908   while ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXP)) && (tmp_TxXferCount != 0UL))
1909   {
1910     /* Transmit data in 32 Bit mode */
1911     if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
1912     {
1913       *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
1914       hspi->pTxBuffPtr += sizeof(uint32_t);
1915       hspi->TxXferCount--;
1916       tmp_TxXferCount = hspi->TxXferCount;
1917     }
1918     /* Transmit data in 16 Bit mode */
1919     else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
1920     {
1921 #if defined (__GNUC__)
1922       *ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
1923 #else
1924       *((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
1925 #endif /* __GNUC__ */
1926       hspi->pTxBuffPtr += sizeof(uint16_t);
1927       hspi->TxXferCount--;
1928       tmp_TxXferCount = hspi->TxXferCount;
1929     }
1930     /* Transmit data in 8 Bit mode */
1931     else
1932     {
1933       *((__IO uint8_t *)&hspi->Instance->TXDR) = *((const uint8_t *)hspi->pTxBuffPtr);
1934       hspi->pTxBuffPtr += sizeof(uint8_t);
1935       hspi->TxXferCount--;
1936       tmp_TxXferCount = hspi->TxXferCount;
1937     }
1938   }
1939 
1940   /* Unlock the process */
1941   __HAL_UNLOCK(hspi);
1942 
1943   /* Enable EOT, DXP, UDR, OVR, FRE, MODF and TSERF interrupts */
1944   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_DXP | SPI_IT_UDR | SPI_IT_OVR |
1945                              SPI_IT_FRE | SPI_IT_MODF | SPI_IT_TSERF));
1946 
1947   if (hspi->Init.Mode == SPI_MODE_MASTER)
1948   {
1949     /* Start Master transfer */
1950     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
1951   }
1952 
1953   return HAL_OK;
1954 }
1955 
1956 #if defined(USE_SPI_RELOAD_TRANSFER)
1957 /**
1958   * @brief  Transmit an additional amount of data in blocking mode.
1959   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
1960   *                the configuration information for SPI module.
1961   * @param  pData: pointer to data buffer
1962   * @param  Size : amount of data to be sent
1963   * @retval HAL status
1964   */
1965 HAL_StatusTypeDef HAL_SPI_Reload_Transmit_IT(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
1966 {
1967   /* check if there is already a request to reload */
1968   if ((hspi->Reload.Requested == 1UL) || (pData == NULL) || (Size == 0UL))
1969   {
1970     return HAL_ERROR;
1971   }
1972 
1973   if (hspi->State == HAL_SPI_STATE_BUSY_TX)
1974   {
1975     /* Lock the process */
1976     __HAL_LOCK(hspi);
1977 
1978     /* Insert the new number of data to be sent just after the current one */
1979     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSER, (Size & 0xFFFFFFFFUL) << 16UL);
1980 
1981     /* Set the transaction information */
1982     hspi->Reload.Requested   = 1UL;
1983     hspi->Reload.pTxBuffPtr  = (const uint8_t *)pData;
1984     hspi->Reload.TxXferSize  = Size;
1985 
1986     /* Unlock the process */
1987     __HAL_UNLOCK(hspi);
1988 
1989     return HAL_OK;
1990   }
1991   else
1992   {
1993     return HAL_ERROR;
1994   }
1995 }
1996 #endif /* USE_SPI_RELOAD_TRANSFER */
1997 
1998 #if defined(USE_SPI_RELOAD_TRANSFER)
1999 /**
2000   * @brief  Receive an additional amount of data in blocking mode.
2001   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
2002   *                the configuration information for SPI module.
2003   * @param  pData: pointer to data buffer
2004   * @param  Size : amount of data to be sent
2005   * @retval HAL status
2006   */
2007 HAL_StatusTypeDef HAL_SPI_Reload_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
2008 {
2009   /* check if there is already a request to reload */
2010   if ((hspi->Reload.Requested == 1UL) || (pData == NULL) || (Size == 0UL))
2011   {
2012     return HAL_ERROR;
2013   }
2014 
2015   if (hspi->State == HAL_SPI_STATE_BUSY_RX)
2016   {
2017     /* Lock the process */
2018     __HAL_LOCK(hspi);
2019 
2020     /* Insert the new number of data that will be received just after the current one */
2021     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSER, (Size & 0xFFFFFFFFUL) << 16UL);
2022 
2023     /* Set the transaction information */
2024     hspi->Reload.Requested   = 1UL;
2025     hspi->Reload.pRxBuffPtr  = (uint8_t *)pData;
2026     hspi->Reload.RxXferSize  = Size;
2027 
2028     /* Unlock the process */
2029     __HAL_UNLOCK(hspi);
2030 
2031     return HAL_OK;
2032   }
2033   else
2034   {
2035     return HAL_ERROR;
2036   }
2037 }
2038 #endif /* USE_SPI_RELOAD_TRANSFER */
2039 
2040 #if defined(USE_SPI_RELOAD_TRANSFER)
2041 /**
2042   * @brief  Transmit and receive an additional amount of data in blocking mode.
2043   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
2044   *                  the configuration information for SPI module.
2045   * @param  pTxData: pointer to transmission data buffer
2046   * @param  pRxData: pointer to reception data buffer
2047   * @param  Size   : amount of data to be sent and received
2048   * @retval HAL status
2049   */
2050 HAL_StatusTypeDef HAL_SPI_Reload_TransmitReceive_IT(SPI_HandleTypeDef *hspi, const uint8_t *pTxData,
2051                                                     uint8_t *pRxData, uint16_t Size)
2052 {
2053   /* check if there is already a request to reload */
2054   if ((hspi->Reload.Requested == 1UL) || (pTxData == NULL) || (pRxData == NULL) || (Size == 0UL))
2055   {
2056     return HAL_ERROR;
2057   }
2058 
2059   if (hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
2060   {
2061     /* Lock the process */
2062     __HAL_LOCK(hspi);
2063 
2064     /* Insert the new number of data that will be sent and received just after the current one */
2065     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSER, (Size & 0xFFFFFFFFUL) << 16UL);
2066 
2067     /* Set the transaction information */
2068     hspi->Reload.Requested   = 1UL;
2069     hspi->Reload.pTxBuffPtr  = (const uint8_t *)pTxData;
2070     hspi->Reload.TxXferSize  = Size;
2071     hspi->Reload.pRxBuffPtr  = (uint8_t *)pRxData;
2072     hspi->Reload.RxXferSize  = Size;
2073 
2074     /* Unlock the process */
2075     __HAL_UNLOCK(hspi);
2076 
2077     return HAL_OK;
2078   }
2079   else
2080   {
2081     return HAL_ERROR;
2082   }
2083 }
2084 #endif /* USE_SPI_RELOAD_TRANSFER */
2085 
2086 /**
2087   * @brief  Transmit an amount of data in non-blocking mode with DMA.
2088   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
2089   *                the configuration information for SPI module.
2090   * @param  pData: pointer to data buffer
2091   * @param  Size : amount of data to be sent
2092   * @retval HAL status
2093   */
2094 HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pData, uint16_t Size)
2095 {
2096 
2097   /* Check Direction parameter */
2098   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_TXONLY(hspi->Init.Direction));
2099 
2100   if (hspi->State != HAL_SPI_STATE_READY)
2101   {
2102     return HAL_BUSY;
2103   }
2104 
2105   if ((pData == NULL) || (Size == 0UL))
2106   {
2107     return HAL_ERROR;
2108   }
2109 
2110   /* Lock the process */
2111   __HAL_LOCK(hspi);
2112 
2113   /* Set the transaction information */
2114   hspi->State       = HAL_SPI_STATE_BUSY_TX;
2115   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
2116   hspi->pTxBuffPtr  = (const uint8_t *)pData;
2117   hspi->TxXferSize  = Size;
2118   hspi->TxXferCount = Size;
2119 
2120   /* Init field not used in handle to zero */
2121   hspi->pRxBuffPtr  = NULL;
2122   hspi->TxISR       = NULL;
2123   hspi->RxISR       = NULL;
2124   hspi->RxXferSize  = (uint16_t)0UL;
2125   hspi->RxXferCount = (uint16_t)0UL;
2126 
2127   /* Configure communication direction : 1Line */
2128   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
2129   {
2130     SPI_1LINE_TX(hspi);
2131   }
2132   else
2133   {
2134     SPI_2LINES_TX(hspi);
2135   }
2136 
2137   /* Packing mode management is enabled by the DMA settings */
2138   if (((hspi->Init.DataSize > SPI_DATASIZE_16BIT) && (hspi->hdmatx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))    || \
2139       ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) && ((hspi->hdmatx->Init.MemDataAlignment != DMA_MDATAALIGN_HALFWORD) && \
2140                                                      (hspi->hdmatx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))))
2141   {
2142     /* Restriction the DMA data received is not allowed in this mode */
2143     __HAL_UNLOCK(hspi);
2144     return HAL_ERROR;
2145   }
2146 
2147   /* Adjust XferCount according to DMA alignment / Data size */
2148   if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
2149   {
2150     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2151     {
2152       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 1UL) >> 1UL;
2153     }
2154     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2155     {
2156       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 3UL) >> 2UL;
2157     }
2158   }
2159   else if (hspi->Init.DataSize <= SPI_DATASIZE_16BIT)
2160   {
2161     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2162     {
2163       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 1UL) >> 1UL;
2164     }
2165   }
2166   else
2167   {
2168     /* Adjustment done */
2169   }
2170 
2171   /* Set the SPI TxDMA Half transfer complete callback */
2172   hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
2173 
2174   /* Set the SPI TxDMA transfer complete callback */
2175   hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
2176 
2177   /* Set the DMA error callback */
2178   hspi->hdmatx->XferErrorCallback = SPI_DMAError;
2179 
2180   /* Set the DMA AbortCpltCallback */
2181   hspi->hdmatx->XferAbortCallback = NULL;
2182 
2183   /* Clear TXDMAEN bit*/
2184   CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN);
2185 
2186   /* Enable the Tx DMA Stream/Channel */
2187   if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->TXDR,
2188                                  hspi->TxXferCount))
2189   {
2190     /* Update SPI error code */
2191     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2192     hspi->State = HAL_SPI_STATE_READY;
2193 
2194     /* Unlock the process */
2195     __HAL_UNLOCK(hspi);
2196 
2197     return HAL_ERROR;
2198   }
2199 
2200   /* Set the number of data at current transfer */
2201   if (hspi->hdmatx->Init.Mode == DMA_CIRCULAR)
2202   {
2203     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, 0UL);
2204   }
2205   else
2206   {
2207     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
2208   }
2209 
2210   /* Enable Tx DMA Request */
2211   SET_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN);
2212 
2213   /* Enable the SPI Error Interrupt Bit */
2214   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_UDR | SPI_IT_FRE | SPI_IT_MODF));
2215 
2216   /* Enable SPI peripheral */
2217   __HAL_SPI_ENABLE(hspi);
2218 
2219   if (hspi->Init.Mode == SPI_MODE_MASTER)
2220   {
2221     /* Master transfer start */
2222     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
2223   }
2224 
2225   /* Unlock the process */
2226   __HAL_UNLOCK(hspi);
2227 
2228   return HAL_OK;
2229 }
2230 
2231 /**
2232   * @brief  Receive an amount of data in non-blocking mode with DMA.
2233   * @param  hspi : pointer to a SPI_HandleTypeDef structure that contains
2234   *                the configuration information for SPI module.
2235   * @param  pData: pointer to data buffer
2236   * @param  Size : amount of data to be sent
2237   * @note   When the CRC feature is enabled the pData Length must be Size + 1.
2238   * @retval HAL status
2239   */
2240 HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
2241 {
2242 
2243   /* Check Direction parameter */
2244   assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE_2LINES_RXONLY(hspi->Init.Direction));
2245 
2246 
2247   if (hspi->State != HAL_SPI_STATE_READY)
2248   {
2249     __HAL_UNLOCK(hspi);
2250     return HAL_BUSY;
2251   }
2252 
2253   if ((pData == NULL) || (Size == 0UL))
2254   {
2255     __HAL_UNLOCK(hspi);
2256     return HAL_ERROR;
2257   }
2258 
2259   /* Lock the process */
2260   __HAL_LOCK(hspi);
2261 
2262   /* Set the transaction information */
2263   hspi->State       = HAL_SPI_STATE_BUSY_RX;
2264   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
2265   hspi->pRxBuffPtr  = (uint8_t *)pData;
2266   hspi->RxXferSize  = Size;
2267   hspi->RxXferCount = Size;
2268 
2269   /*Init field not used in handle to zero */
2270   hspi->RxISR       = NULL;
2271   hspi->TxISR       = NULL;
2272   hspi->TxXferSize  = (uint16_t) 0UL;
2273   hspi->TxXferCount = (uint16_t) 0UL;
2274 
2275   /* Configure communication direction : 1Line */
2276   if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
2277   {
2278     SPI_1LINE_RX(hspi);
2279   }
2280   else
2281   {
2282     SPI_2LINES_RX(hspi);
2283   }
2284 
2285   /* Packing mode management is enabled by the DMA settings */
2286   if (((hspi->Init.DataSize > SPI_DATASIZE_16BIT) && (hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))    || \
2287       ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) && ((hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_HALFWORD) && \
2288                                                      (hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))))
2289   {
2290     /* Restriction the DMA data received is not allowed in this mode */
2291     __HAL_UNLOCK(hspi);
2292     return HAL_ERROR;
2293   }
2294 
2295   /* Clear RXDMAEN bit */
2296   CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_RXDMAEN);
2297 
2298   /* Adjust XferCount according to DMA alignment / Data size */
2299   if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
2300   {
2301     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2302     {
2303       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 1UL) >> 1UL;
2304     }
2305     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2306     {
2307       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 3UL) >> 2UL;
2308     }
2309   }
2310   else if (hspi->Init.DataSize <= SPI_DATASIZE_16BIT)
2311   {
2312     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2313     {
2314       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 1UL) >> 1UL;
2315     }
2316   }
2317   else
2318   {
2319     /* Adjustment done */
2320   }
2321 
2322   /* Set the SPI RxDMA Half transfer complete callback */
2323   hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
2324 
2325   /* Set the SPI Rx DMA transfer complete callback */
2326   hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
2327 
2328   /* Set the DMA error callback */
2329   hspi->hdmarx->XferErrorCallback = SPI_DMAError;
2330 
2331   /* Set the DMA AbortCpltCallback */
2332   hspi->hdmarx->XferAbortCallback = NULL;
2333 
2334   /* Enable the Rx DMA Stream/Channel  */
2335   if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->RXDR, (uint32_t)hspi->pRxBuffPtr,
2336                                  hspi->RxXferCount))
2337   {
2338     /* Update SPI error code */
2339     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2340     hspi->State = HAL_SPI_STATE_READY;
2341 
2342     /* Unlock the process */
2343     __HAL_UNLOCK(hspi);
2344 
2345     return HAL_ERROR;
2346   }
2347 
2348   /* Set the number of data at current transfer */
2349   if (hspi->hdmarx->Init.Mode == DMA_CIRCULAR)
2350   {
2351     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, 0UL);
2352   }
2353   else
2354   {
2355     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
2356   }
2357 
2358   /* Enable Rx DMA Request */
2359   SET_BIT(hspi->Instance->CFG1, SPI_CFG1_RXDMAEN);
2360 
2361   /* Enable the SPI Error Interrupt Bit */
2362   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_OVR | SPI_IT_FRE | SPI_IT_MODF));
2363 
2364   /* Enable SPI peripheral */
2365   __HAL_SPI_ENABLE(hspi);
2366 
2367   if (hspi->Init.Mode == SPI_MODE_MASTER)
2368   {
2369     /* Master transfer start */
2370     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
2371   }
2372 
2373   /* Unlock the process */
2374   __HAL_UNLOCK(hspi);
2375 
2376   return HAL_OK;
2377 }
2378 
2379 /**
2380   * @brief  Transmit and Receive an amount of data in non-blocking mode with DMA.
2381   * @param  hspi   : pointer to a SPI_HandleTypeDef structure that contains
2382   *                  the configuration information for SPI module.
2383   * @param  pTxData: pointer to transmission data buffer
2384   * @param  pRxData: pointer to reception data buffer
2385   * @param  Size   : amount of data to be sent
2386   * @note   When the CRC feature is enabled the pRxData Length must be Size + 1
2387   * @retval HAL status
2388   */
2389 HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, const uint8_t *pTxData, uint8_t *pRxData,
2390                                               uint16_t Size)
2391 {
2392   /* Check Direction parameter */
2393   assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
2394 
2395   if (hspi->State != HAL_SPI_STATE_READY)
2396   {
2397     return HAL_BUSY;
2398   }
2399 
2400   if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0UL))
2401   {
2402     return HAL_ERROR;
2403   }
2404 
2405   /* Lock the process */
2406   __HAL_LOCK(hspi);
2407 
2408   /* Set the transaction information */
2409   hspi->State       = HAL_SPI_STATE_BUSY_TX_RX;
2410   hspi->ErrorCode   = HAL_SPI_ERROR_NONE;
2411   hspi->pTxBuffPtr  = (const uint8_t *)pTxData;
2412   hspi->TxXferSize  = Size;
2413   hspi->TxXferCount = Size;
2414   hspi->pRxBuffPtr  = (uint8_t *)pRxData;
2415   hspi->RxXferSize  = Size;
2416   hspi->RxXferCount = Size;
2417 
2418   /* Init field not used in handle to zero */
2419   hspi->RxISR       = NULL;
2420   hspi->TxISR       = NULL;
2421 
2422   /* Set Full-Duplex mode */
2423   SPI_2LINES(hspi);
2424 
2425   /* Reset the Tx/Rx DMA bits */
2426   CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN);
2427 
2428   /* Packing mode management is enabled by the DMA settings */
2429   if (((hspi->Init.DataSize > SPI_DATASIZE_16BIT) && (hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))    || \
2430       ((hspi->Init.DataSize > SPI_DATASIZE_8BIT) && ((hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_HALFWORD) && \
2431                                                      (hspi->hdmarx->Init.MemDataAlignment != DMA_MDATAALIGN_WORD))))
2432   {
2433     /* Restriction the DMA data received is not allowed in this mode */
2434     /* Unlock the process */
2435     __HAL_UNLOCK(hspi);
2436     return HAL_ERROR;
2437   }
2438 
2439   /* Adjust XferCount according to DMA alignment / Data size */
2440   if (hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
2441   {
2442     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2443     {
2444       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 1UL) >> 1UL;
2445     }
2446     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2447     {
2448       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 3UL) >> 2UL;
2449     }
2450     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
2451     {
2452       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 1UL) >> 1UL;
2453     }
2454     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2455     {
2456       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 3UL) >> 2UL;
2457     }
2458   }
2459   else if (hspi->Init.DataSize <= SPI_DATASIZE_16BIT)
2460   {
2461     if (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2462     {
2463       hspi->TxXferCount = (hspi->TxXferCount + (uint16_t) 1UL) >> 1UL;
2464     }
2465     if (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_WORD)
2466     {
2467       hspi->RxXferCount = (hspi->RxXferCount + (uint16_t) 1UL) >> 1UL;
2468     }
2469   }
2470   else
2471   {
2472     /* Adjustment done */
2473   }
2474 
2475   /* Set the SPI Tx/Rx DMA Half transfer complete callback */
2476   hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
2477   hspi->hdmarx->XferCpltCallback     = SPI_DMATransmitReceiveCplt;
2478 
2479   /* Set the DMA error callback */
2480   hspi->hdmarx->XferErrorCallback = SPI_DMAError;
2481 
2482   /* Set the DMA AbortCallback */
2483   hspi->hdmarx->XferAbortCallback = NULL;
2484 
2485   /* Enable the Rx DMA Stream/Channel  */
2486   if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->RXDR, (uint32_t)hspi->pRxBuffPtr,
2487                                  hspi->RxXferCount))
2488   {
2489     /* Update SPI error code */
2490     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2491     hspi->State = HAL_SPI_STATE_READY;
2492 
2493     /* Unlock the process */
2494     __HAL_UNLOCK(hspi);
2495 
2496     return HAL_ERROR;
2497   }
2498 
2499   /* Enable Rx DMA Request */
2500   SET_BIT(hspi->Instance->CFG1, SPI_CFG1_RXDMAEN);
2501 
2502   /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
2503   is performed in DMA reception complete callback  */
2504   hspi->hdmatx->XferHalfCpltCallback = NULL;
2505   hspi->hdmatx->XferCpltCallback     = NULL;
2506   hspi->hdmatx->XferAbortCallback    = NULL;
2507 
2508   /* Set the DMA error callback */
2509   hspi->hdmatx->XferErrorCallback    = SPI_DMAError;
2510 
2511   /* Enable the Tx DMA Stream/Channel  */
2512   if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->TXDR,
2513                                  hspi->TxXferCount))
2514   {
2515     /* Abort Rx DMA Channel already started */
2516     (void)HAL_DMA_Abort(hspi->hdmarx);
2517 
2518     /* Update SPI error code */
2519     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
2520     hspi->State = HAL_SPI_STATE_READY;
2521 
2522     /* Unlock the process */
2523     __HAL_UNLOCK(hspi);
2524 
2525     return HAL_ERROR;
2526   }
2527 
2528   if (hspi->hdmatx->Init.Mode == DMA_CIRCULAR)
2529   {
2530     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, 0UL);
2531   }
2532   else
2533   {
2534     MODIFY_REG(hspi->Instance->CR2, SPI_CR2_TSIZE, Size);
2535   }
2536 
2537   /* Enable Tx DMA Request */
2538   SET_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN);
2539 
2540   /* Enable the SPI Error Interrupt Bit */
2541   __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_OVR | SPI_IT_UDR | SPI_IT_FRE | SPI_IT_MODF));
2542 
2543   /* Enable SPI peripheral */
2544   __HAL_SPI_ENABLE(hspi);
2545 
2546   if (hspi->Init.Mode == SPI_MODE_MASTER)
2547   {
2548     /* Master transfer start */
2549     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSTART);
2550   }
2551 
2552   /* Unlock the process */
2553   __HAL_UNLOCK(hspi);
2554 
2555   return HAL_OK;
2556 }
2557 
2558 /**
2559   * @brief  Abort ongoing transfer (blocking mode).
2560   * @param  hspi SPI handle.
2561   * @note   This procedure could be used for aborting any ongoing transfer (Tx and Rx),
2562   *         started in Interrupt or DMA mode.
2563   * @note   This procedure performs following operations :
2564   *          + Disable SPI Interrupts (depending of transfer direction)
2565   *          + Disable the DMA transfer in the peripheral register (if enabled)
2566   *          + Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
2567   *          + Set handle State to READY.
2568   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
2569   * @retval HAL status
2570   */
2571 HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)
2572 {
2573   HAL_StatusTypeDef errorcode;
2574 
2575   __IO uint32_t count;
2576 
2577   /* Lock the process */
2578   __HAL_LOCK(hspi);
2579 
2580   /* Set hspi->state to aborting to avoid any interaction */
2581   hspi->State = HAL_SPI_STATE_ABORT;
2582 
2583   /* Initialized local variable  */
2584   errorcode = HAL_OK;
2585   count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24UL / 1000UL);
2586 
2587   /* If master communication on going, make sure current frame is done before closing the connection */
2588   if (HAL_IS_BIT_SET(hspi->Instance->CR1, SPI_CR1_CSTART))
2589   {
2590     /* Disable EOT interrupt */
2591     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_EOT);
2592     do
2593     {
2594       count--;
2595       if (count == 0UL)
2596       {
2597         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2598         break;
2599       }
2600     } while (HAL_IS_BIT_SET(hspi->Instance->IER, SPI_IT_EOT));
2601 
2602     /* Request a Suspend transfer */
2603     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSUSP);
2604     do
2605     {
2606       count--;
2607       if (count == 0UL)
2608       {
2609         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2610         break;
2611       }
2612     } while (HAL_IS_BIT_SET(hspi->Instance->CR1, SPI_CR1_CSTART));
2613 
2614     /* Clear SUSP flag */
2615     __HAL_SPI_CLEAR_SUSPFLAG(hspi);
2616     do
2617     {
2618       count--;
2619       if (count == 0UL)
2620       {
2621         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2622         break;
2623       }
2624     } while (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_SUSP));
2625   }
2626 
2627   /* Disable the SPI DMA Tx request if enabled */
2628   if (HAL_IS_BIT_SET(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN))
2629   {
2630     if (hspi->hdmatx != NULL)
2631     {
2632       /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
2633       hspi->hdmatx->XferAbortCallback = NULL;
2634 
2635       /* Abort DMA Tx Handle linked to SPI Peripheral */
2636       if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
2637       {
2638         if (HAL_DMA_GetError(hspi->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
2639         {
2640           hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2641         }
2642       }
2643     }
2644   }
2645 
2646   /* Disable the SPI DMA Rx request if enabled */
2647   if (HAL_IS_BIT_SET(hspi->Instance->CFG1, SPI_CFG1_RXDMAEN))
2648   {
2649     if (hspi->hdmarx != NULL)
2650     {
2651       /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
2652       hspi->hdmarx->XferAbortCallback = NULL;
2653 
2654       /* Abort DMA Rx Handle linked to SPI Peripheral */
2655       if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
2656       {
2657         if (HAL_DMA_GetError(hspi->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
2658         {
2659           hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
2660         }
2661       }
2662     }
2663   }
2664 
2665   /* Proceed with abort procedure */
2666   SPI_AbortTransfer(hspi);
2667 
2668   /* Check error during Abort procedure */
2669   if (HAL_IS_BIT_SET(hspi->ErrorCode, HAL_SPI_ERROR_ABORT))
2670   {
2671     /* return HAL_Error in case of error during Abort procedure */
2672     errorcode = HAL_ERROR;
2673   }
2674   else
2675   {
2676     /* Reset errorCode */
2677     hspi->ErrorCode = HAL_SPI_ERROR_NONE;
2678   }
2679 
2680   /* Restore hspi->state to ready */
2681   hspi->State = HAL_SPI_STATE_READY;
2682 
2683   /* Unlock the process */
2684   __HAL_UNLOCK(hspi);
2685 
2686   return errorcode;
2687 }
2688 
2689 /**
2690   * @brief  Abort ongoing transfer (Interrupt mode).
2691   * @param  hspi SPI handle.
2692   * @note   This procedure could be used for aborting any ongoing transfer (Tx and Rx),
2693   *         started in Interrupt or DMA mode.
2694   * @note   This procedure performs following operations :
2695   *          + Disable SPI Interrupts (depending of transfer direction)
2696   *          + Disable the DMA transfer in the peripheral register (if enabled)
2697   *          + Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2698   *          + Set handle State to READY
2699   *          + At abort completion, call user abort complete callback.
2700   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
2701   *         considered as completed only when user abort complete callback is executed (not when exiting function).
2702   * @retval HAL status
2703   */
2704 HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi)
2705 {
2706   HAL_StatusTypeDef errorcode;
2707   __IO uint32_t count;
2708   uint32_t dma_tx_abort_done = 1UL;
2709   uint32_t dma_rx_abort_done = 1UL;
2710 
2711   /* Set hspi->state to aborting to avoid any interaction */
2712   hspi->State = HAL_SPI_STATE_ABORT;
2713 
2714   /* Initialized local variable  */
2715   errorcode = HAL_OK;
2716   count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24UL / 1000UL);
2717 
2718   /* If master communication on going, make sure current frame is done before closing the connection */
2719   if (HAL_IS_BIT_SET(hspi->Instance->CR1, SPI_CR1_CSTART))
2720   {
2721     /* Disable EOT interrupt */
2722     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_EOT);
2723     do
2724     {
2725       count--;
2726       if (count == 0UL)
2727       {
2728         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2729         break;
2730       }
2731     } while (HAL_IS_BIT_SET(hspi->Instance->IER, SPI_IT_EOT));
2732 
2733     /* Request a Suspend transfer */
2734     SET_BIT(hspi->Instance->CR1, SPI_CR1_CSUSP);
2735     do
2736     {
2737       count--;
2738       if (count == 0UL)
2739       {
2740         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2741         break;
2742       }
2743     } while (HAL_IS_BIT_SET(hspi->Instance->CR1, SPI_CR1_CSTART));
2744 
2745     /* Clear SUSP flag */
2746     __HAL_SPI_CLEAR_SUSPFLAG(hspi);
2747     do
2748     {
2749       count--;
2750       if (count == 0UL)
2751       {
2752         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
2753         break;
2754       }
2755     } while (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_SUSP));
2756   }
2757 
2758   /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialized
2759      before any call to DMA Abort functions */
2760 
2761   if (hspi->hdmatx != NULL)
2762   {
2763     if (HAL_IS_BIT_SET(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN))
2764     {
2765       /* Set DMA Abort Complete callback if SPI DMA Tx request if enabled */
2766       hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback;
2767 
2768       dma_tx_abort_done = 0UL;
2769 
2770       /* Abort DMA Tx Handle linked to SPI Peripheral */
2771       if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
2772       {
2773         if (HAL_DMA_GetError(hspi->hdmatx) == HAL_DMA_ERROR_NO_XFER)
2774         {
2775           dma_tx_abort_done = 1UL;
2776           hspi->hdmatx->XferAbortCallback = NULL;
2777         }
2778       }
2779     }
2780     else
2781     {
2782       hspi->hdmatx->XferAbortCallback = NULL;
2783     }
2784   }
2785 
2786   if (hspi->hdmarx != NULL)
2787   {
2788     if (HAL_IS_BIT_SET(hspi->Instance->CFG1, SPI_CFG1_RXDMAEN))
2789     {
2790       /* Set DMA Abort Complete callback if SPI DMA Rx request if enabled */
2791       hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback;
2792 
2793       dma_rx_abort_done = 0UL;
2794 
2795       /* Abort DMA Rx Handle linked to SPI Peripheral */
2796       if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
2797       {
2798         if (HAL_DMA_GetError(hspi->hdmarx) == HAL_DMA_ERROR_NO_XFER)
2799         {
2800           dma_rx_abort_done = 1UL;
2801           hspi->hdmarx->XferAbortCallback = NULL;
2802         }
2803       }
2804     }
2805     else
2806     {
2807       hspi->hdmarx->XferAbortCallback = NULL;
2808     }
2809   }
2810 
2811   /* If no running DMA transfer, finish cleanup and call callbacks */
2812   if ((dma_tx_abort_done == 1UL) && (dma_rx_abort_done == 1UL))
2813   {
2814     /* Proceed with abort procedure */
2815     SPI_AbortTransfer(hspi);
2816 
2817     /* Check error during Abort procedure */
2818     if (HAL_IS_BIT_SET(hspi->ErrorCode, HAL_SPI_ERROR_ABORT))
2819     {
2820       /* return HAL_Error in case of error during Abort procedure */
2821       errorcode = HAL_ERROR;
2822     }
2823     else
2824     {
2825       /* Reset errorCode */
2826       hspi->ErrorCode = HAL_SPI_ERROR_NONE;
2827     }
2828 
2829     /* Restore hspi->state to ready */
2830     hspi->State = HAL_SPI_STATE_READY;
2831 
2832     /* Call user Abort complete callback */
2833 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
2834     hspi->AbortCpltCallback(hspi);
2835 #else
2836     HAL_SPI_AbortCpltCallback(hspi);
2837 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2838   }
2839 
2840   return errorcode;
2841 }
2842 
2843 /**
2844   * @brief  Pause the DMA Transfer.
2845   *         This API is not supported, it is maintained for backward compatibility.
2846   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
2847   *               the configuration information for the specified SPI module.
2848   * @retval HAL_ERROR
2849   */
2850 HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
2851 {
2852   /* Set error code to not supported */
2853   SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_NOT_SUPPORTED);
2854 
2855   return HAL_ERROR;
2856 }
2857 
2858 /**
2859   * @brief  Resume the DMA Transfer.
2860   *         This API is not supported, it is maintained for backward compatibility.
2861   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
2862   *               the configuration information for the specified SPI module.
2863   * @retval HAL_ERROR
2864   */
2865 HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
2866 {
2867   /* Set error code to not supported */
2868   SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_NOT_SUPPORTED);
2869 
2870   return HAL_ERROR;
2871 }
2872 
2873 /**
2874   * @brief  Stop the DMA Transfer.
2875   *         This API is not supported, it is maintained for backward compatibility.
2876   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
2877   *               the configuration information for the specified SPI module.
2878   * @retval HAL_ERROR
2879   */
2880 HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
2881 {
2882   /* Set error code to not supported */
2883   SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_NOT_SUPPORTED);
2884 
2885   return HAL_ERROR;
2886 }
2887 
2888 /**
2889   * @brief  Handle SPI interrupt request.
2890   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
2891   *               the configuration information for the specified SPI module.
2892   * @retval None
2893   */
2894 void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
2895 {
2896   uint32_t itsource = hspi->Instance->IER;
2897   uint32_t itflag   = hspi->Instance->SR;
2898   uint32_t trigger  = itsource & itflag;
2899   uint32_t cfg1     = hspi->Instance->CFG1;
2900   uint32_t handled  = 0UL;
2901 
2902   HAL_SPI_StateTypeDef State = hspi->State;
2903 #if defined (__GNUC__)
2904   __IO uint16_t *prxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->RXDR));
2905 #endif /* __GNUC__ */
2906 
2907   /* SPI in SUSPEND mode  ----------------------------------------------------*/
2908   if (HAL_IS_BIT_SET(itflag, SPI_FLAG_SUSP) && HAL_IS_BIT_SET(itsource, SPI_FLAG_EOT))
2909   {
2910     /* Clear the Suspend flag */
2911     __HAL_SPI_CLEAR_SUSPFLAG(hspi);
2912 
2913     /* Suspend on going, Call the Suspend callback */
2914 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
2915     hspi->SuspendCallback(hspi);
2916 #else
2917     HAL_SPI_SuspendCallback(hspi);
2918 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
2919     return;
2920   }
2921 
2922   /* SPI in mode Transmitter and Receiver ------------------------------------*/
2923   if (HAL_IS_BIT_CLR(trigger, SPI_FLAG_OVR) && HAL_IS_BIT_CLR(trigger, SPI_FLAG_UDR) && \
2924       HAL_IS_BIT_SET(trigger, SPI_FLAG_DXP))
2925   {
2926     hspi->TxISR(hspi);
2927     hspi->RxISR(hspi);
2928     handled = 1UL;
2929   }
2930 
2931   /* SPI in mode Receiver ----------------------------------------------------*/
2932   if (HAL_IS_BIT_CLR(trigger, SPI_FLAG_OVR) && HAL_IS_BIT_SET(trigger, SPI_FLAG_RXP) && \
2933       HAL_IS_BIT_CLR(trigger, SPI_FLAG_DXP))
2934   {
2935     hspi->RxISR(hspi);
2936     handled = 1UL;
2937   }
2938 
2939   /* SPI in mode Transmitter -------------------------------------------------*/
2940   if (HAL_IS_BIT_CLR(trigger, SPI_FLAG_UDR) && HAL_IS_BIT_SET(trigger, SPI_FLAG_TXP) && \
2941       HAL_IS_BIT_CLR(trigger, SPI_FLAG_DXP))
2942   {
2943     hspi->TxISR(hspi);
2944     handled = 1UL;
2945   }
2946 
2947 #if defined(USE_SPI_RELOAD_TRANSFER)
2948   /* SPI Reload  -------------------------------------------------*/
2949   if (HAL_IS_BIT_SET(trigger, SPI_FLAG_TSERF))
2950   {
2951     __HAL_SPI_CLEAR_TSERFFLAG(hspi);
2952   }
2953 #endif /* USE_SPI_RELOAD_TRANSFER */
2954 
2955   if (handled != 0UL)
2956   {
2957     return;
2958   }
2959 
2960   /* SPI End Of Transfer: DMA or IT based transfer */
2961   if (HAL_IS_BIT_SET(trigger, SPI_FLAG_EOT))
2962   {
2963     /* Clear EOT/TXTF/SUSP flag */
2964     __HAL_SPI_CLEAR_EOTFLAG(hspi);
2965     __HAL_SPI_CLEAR_TXTFFLAG(hspi);
2966     __HAL_SPI_CLEAR_SUSPFLAG(hspi);
2967 
2968     /* Disable EOT interrupt */
2969     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_EOT);
2970 
2971     /* For the IT based receive extra polling maybe required for last packet */
2972     if (HAL_IS_BIT_CLR(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN))
2973     {
2974       /* Pooling remaining data */
2975       while (hspi->RxXferCount != 0UL)
2976       {
2977         /* Receive data in 32 Bit mode */
2978         if (hspi->Init.DataSize > SPI_DATASIZE_16BIT)
2979         {
2980           *((uint32_t *)hspi->pRxBuffPtr) = *((__IO uint32_t *)&hspi->Instance->RXDR);
2981           hspi->pRxBuffPtr += sizeof(uint32_t);
2982         }
2983         /* Receive data in 16 Bit mode */
2984         else if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
2985         {
2986 #if defined (__GNUC__)
2987           *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
2988 #else
2989           *((uint16_t *)hspi->pRxBuffPtr) = *((__IO uint16_t *)&hspi->Instance->RXDR);
2990 #endif /* __GNUC__ */
2991           hspi->pRxBuffPtr += sizeof(uint16_t);
2992         }
2993         /* Receive data in 8 Bit mode */
2994         else
2995         {
2996           *((uint8_t *)hspi->pRxBuffPtr) = *((__IO uint8_t *)&hspi->Instance->RXDR);
2997           hspi->pRxBuffPtr += sizeof(uint8_t);
2998         }
2999 
3000         hspi->RxXferCount--;
3001       }
3002     }
3003 
3004     /* Call SPI Standard close procedure */
3005     SPI_CloseTransfer(hspi);
3006 
3007     hspi->State = HAL_SPI_STATE_READY;
3008     if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
3009     {
3010 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3011       hspi->ErrorCallback(hspi);
3012 #else
3013       HAL_SPI_ErrorCallback(hspi);
3014 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3015       return;
3016     }
3017 
3018 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3019     /* Call appropriate user callback */
3020     if (State == HAL_SPI_STATE_BUSY_TX_RX)
3021     {
3022       hspi->TxRxCpltCallback(hspi);
3023     }
3024     else if (State == HAL_SPI_STATE_BUSY_RX)
3025     {
3026       hspi->RxCpltCallback(hspi);
3027     }
3028     else if (State == HAL_SPI_STATE_BUSY_TX)
3029     {
3030       hspi->TxCpltCallback(hspi);
3031     }
3032 #else
3033     /* Call appropriate user callback */
3034     if (State == HAL_SPI_STATE_BUSY_TX_RX)
3035     {
3036       HAL_SPI_TxRxCpltCallback(hspi);
3037     }
3038     else if (State == HAL_SPI_STATE_BUSY_RX)
3039     {
3040       HAL_SPI_RxCpltCallback(hspi);
3041     }
3042     else if (State == HAL_SPI_STATE_BUSY_TX)
3043     {
3044       HAL_SPI_TxCpltCallback(hspi);
3045     }
3046 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3047     else
3048     {
3049       /* End of the appropriate call */
3050     }
3051 
3052     return;
3053   }
3054 
3055   /* SPI in Error Treatment --------------------------------------------------*/
3056   if ((trigger & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE | SPI_FLAG_UDR)) != 0UL)
3057   {
3058     /* SPI Overrun error interrupt occurred ----------------------------------*/
3059     if ((trigger & SPI_FLAG_OVR) != 0UL)
3060     {
3061       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
3062       __HAL_SPI_CLEAR_OVRFLAG(hspi);
3063     }
3064 
3065     /* SPI Mode Fault error interrupt occurred -------------------------------*/
3066     if ((trigger & SPI_FLAG_MODF) != 0UL)
3067     {
3068       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
3069       __HAL_SPI_CLEAR_MODFFLAG(hspi);
3070     }
3071 
3072     /* SPI Frame error interrupt occurred ------------------------------------*/
3073     if ((trigger & SPI_FLAG_FRE) != 0UL)
3074     {
3075       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
3076       __HAL_SPI_CLEAR_FREFLAG(hspi);
3077     }
3078 
3079     /* SPI Underrun error interrupt occurred ------------------------------------*/
3080     if ((trigger & SPI_FLAG_UDR) != 0UL)
3081     {
3082       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_UDR);
3083       __HAL_SPI_CLEAR_UDRFLAG(hspi);
3084     }
3085 
3086     if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
3087     {
3088       /* Disable SPI peripheral */
3089       __HAL_SPI_DISABLE(hspi);
3090 
3091       /* Disable all interrupts */
3092       __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_RXP | SPI_IT_TXP | SPI_IT_MODF |
3093                                   SPI_IT_OVR | SPI_IT_FRE | SPI_IT_UDR));
3094 
3095       /* Disable the SPI DMA requests if enabled */
3096       if (HAL_IS_BIT_SET(cfg1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN))
3097       {
3098         /* Disable the SPI DMA requests */
3099         CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN);
3100 
3101         /* Abort the SPI DMA Rx channel */
3102         if (hspi->hdmarx != NULL)
3103         {
3104           /* Set the SPI DMA Abort callback :
3105           will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
3106           hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError;
3107           if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
3108           {
3109             SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
3110           }
3111         }
3112         /* Abort the SPI DMA Tx channel */
3113         if (hspi->hdmatx != NULL)
3114         {
3115           /* Set the SPI DMA Abort callback :
3116           will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
3117           hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError;
3118           if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
3119           {
3120             SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
3121           }
3122         }
3123       }
3124       else
3125       {
3126         /* Restore hspi->State to Ready */
3127         hspi->State = HAL_SPI_STATE_READY;
3128 
3129         /* Call user error callback */
3130 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3131         hspi->ErrorCallback(hspi);
3132 #else
3133         HAL_SPI_ErrorCallback(hspi);
3134 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3135       }
3136     }
3137     return;
3138   }
3139 }
3140 
3141 #ifndef __rtems__
3142 /**
3143   * @brief Tx Transfer completed callback.
3144   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3145   *               the configuration information for SPI module.
3146   * @retval None
3147   */
3148 __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3149 {
3150   /* Prevent unused argument(s) compilation warning */
3151   UNUSED(hspi);
3152 
3153   /* NOTE : This function should not be modified, when the callback is needed,
3154             the HAL_SPI_TxCpltCallback should be implemented in the user file
3155    */
3156 }
3157 
3158 /**
3159   * @brief Rx Transfer completed callback.
3160   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3161   *               the configuration information for SPI module.
3162   * @retval None
3163   */
3164 __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3165 {
3166   /* Prevent unused argument(s) compilation warning */
3167   UNUSED(hspi);
3168 
3169   /* NOTE : This function should not be modified, when the callback is needed,
3170             the HAL_SPI_RxCpltCallback should be implemented in the user file
3171    */
3172 }
3173 
3174 /**
3175   * @brief Tx and Rx Transfer completed callback.
3176   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3177   *               the configuration information for SPI module.
3178   * @retval None
3179   */
3180 __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3181 {
3182   /* Prevent unused argument(s) compilation warning */
3183   UNUSED(hspi);
3184 
3185   /* NOTE : This function should not be modified, when the callback is needed,
3186             the HAL_SPI_TxRxCpltCallback should be implemented in the user file
3187    */
3188 }
3189 #endif
3190 
3191 /**
3192   * @brief Tx Half Transfer completed callback.
3193   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3194   *               the configuration information for SPI module.
3195   * @retval None
3196   */
3197 __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3198 {
3199   /* Prevent unused argument(s) compilation warning */
3200   UNUSED(hspi);
3201 
3202   /* NOTE : This function should not be modified, when the callback is needed,
3203             the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
3204    */
3205 }
3206 
3207 /**
3208   * @brief Rx Half Transfer completed callback.
3209   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3210   *               the configuration information for SPI module.
3211   * @retval None
3212   */
3213 __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3214 {
3215   /* Prevent unused argument(s) compilation warning */
3216   UNUSED(hspi);
3217 
3218   /* NOTE : This function should not be modified, when the callback is needed,
3219             the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
3220    */
3221 }
3222 
3223 /**
3224   * @brief Tx and Rx Half Transfer callback.
3225   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3226   *               the configuration information for SPI module.
3227   * @retval None
3228   */
3229 __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3230 {
3231   /* Prevent unused argument(s) compilation warning */
3232   UNUSED(hspi);
3233 
3234   /* NOTE : This function should not be modified, when the callback is needed,
3235             the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
3236    */
3237 }
3238 
3239 #ifndef __rtems__
3240 /**
3241   * @brief SPI error callback.
3242   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3243   *               the configuration information for SPI module.
3244   * @retval None
3245   */
3246 __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3247 {
3248   /* Prevent unused argument(s) compilation warning */
3249   UNUSED(hspi);
3250 
3251   /* NOTE : This function should not be modified, when the callback is needed,
3252             the HAL_SPI_ErrorCallback should be implemented in the user file
3253    */
3254   /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
3255             and user can use HAL_SPI_GetError() API to check the latest error occurred
3256    */
3257 }
3258 #endif
3259 
3260 /**
3261   * @brief  SPI Abort Complete callback.
3262   * @param  hspi SPI handle.
3263   * @retval None
3264   */
3265 __weak void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3266 {
3267   /* Prevent unused argument(s) compilation warning */
3268   UNUSED(hspi);
3269 
3270   /* NOTE : This function should not be modified, when the callback is needed,
3271             the HAL_SPI_AbortCpltCallback can be implemented in the user file.
3272    */
3273 }
3274 
3275 /**
3276   * @brief  SPI Suspend callback.
3277   * @param  hspi SPI handle.
3278   * @retval None
3279   */
3280 __weak void HAL_SPI_SuspendCallback(SPI_HandleTypeDef *hspi) /* Derogation MISRAC2012-Rule-8.13 */
3281 {
3282   /* Prevent unused argument(s) compilation warning */
3283   UNUSED(hspi);
3284 
3285   /* NOTE : This function should not be modified, when the callback is needed,
3286             the HAL_SPI_SuspendCallback can be implemented in the user file.
3287    */
3288 }
3289 
3290 /**
3291   * @}
3292   */
3293 
3294 /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
3295   * @ingroup RTEMSBSPsARMSTM32H7
3296   * @brief   SPI control functions
3297   *
3298 @verbatim
3299  ===============================================================================
3300                       ##### Peripheral State and Errors functions #####
3301  ===============================================================================
3302     [..]
3303     This subsection provides a set of functions allowing to control the SPI.
3304      (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
3305      (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
3306 @endverbatim
3307   * @{
3308   */
3309 
3310 /**
3311   * @brief  Return the SPI handle state.
3312   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3313   *               the configuration information for SPI module.
3314   * @retval SPI state
3315   */
3316 HAL_SPI_StateTypeDef HAL_SPI_GetState(const SPI_HandleTypeDef *hspi)
3317 {
3318   /* Return SPI handle state */
3319   return hspi->State;
3320 }
3321 
3322 /**
3323   * @brief  Return the SPI error code.
3324   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3325   *               the configuration information for SPI module.
3326   * @retval SPI error code in bitmap format
3327   */
3328 uint32_t HAL_SPI_GetError(const SPI_HandleTypeDef *hspi)
3329 {
3330   /* Return SPI ErrorCode */
3331   return hspi->ErrorCode;
3332 }
3333 
3334 /**
3335   * @}
3336   */
3337 
3338 /**
3339   * @}
3340   */
3341 
3342 /** @addtogroup SPI_Private_Functions
3343   * @brief   Private functions
3344   * @{
3345   */
3346 
3347 /**
3348   * @brief DMA SPI transmit process complete callback.
3349   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3350   *               the configuration information for the specified DMA module.
3351   * @retval None
3352   */
3353 static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3354 {
3355   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3356 
3357   if (hspi->State != HAL_SPI_STATE_ABORT)
3358   {
3359     if (hspi->hdmatx->Init.Mode == DMA_CIRCULAR)
3360     {
3361 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3362       hspi->TxCpltCallback(hspi);
3363 #else
3364       HAL_SPI_TxCpltCallback(hspi);
3365 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3366     }
3367     else
3368     {
3369       /* Enable EOT interrupt */
3370       __HAL_SPI_ENABLE_IT(hspi, SPI_IT_EOT);
3371     }
3372   }
3373 }
3374 
3375 /**
3376   * @brief DMA SPI receive process complete callback.
3377   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3378   *               the configuration information for the specified DMA module.
3379   * @retval None
3380   */
3381 static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3382 {
3383   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3384 
3385   if (hspi->State != HAL_SPI_STATE_ABORT)
3386   {
3387     if (hspi->hdmarx->Init.Mode == DMA_CIRCULAR)
3388     {
3389 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3390       hspi->RxCpltCallback(hspi);
3391 #else
3392       HAL_SPI_RxCpltCallback(hspi);
3393 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3394     }
3395     else
3396     {
3397       /* Enable EOT interrupt */
3398       __HAL_SPI_ENABLE_IT(hspi, SPI_IT_EOT);
3399     }
3400   }
3401 }
3402 
3403 /**
3404   * @brief  DMA SPI transmit receive process complete callback.
3405   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3406   *               the configuration information for the specified DMA module.
3407   * @retval None
3408   */
3409 static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
3410 {
3411   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3412 
3413   if (hspi->State != HAL_SPI_STATE_ABORT)
3414   {
3415     if (hspi->hdmatx->Init.Mode == DMA_CIRCULAR)
3416     {
3417 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3418       hspi->TxRxCpltCallback(hspi);
3419 #else
3420       HAL_SPI_TxRxCpltCallback(hspi);
3421 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3422     }
3423     else
3424     {
3425       /* Enable EOT interrupt */
3426       __HAL_SPI_ENABLE_IT(hspi, SPI_IT_EOT);
3427     }
3428   }
3429 }
3430 
3431 /**
3432   * @brief  DMA SPI half transmit process complete callback.
3433   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3434   *               the configuration information for the specified DMA module.
3435   * @retval None
3436   */
3437 static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma) /* Derogation MISRAC2012-Rule-8.13 */
3438 {
3439   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)
3440                             ((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-8.13 */
3441 
3442 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3443   hspi->TxHalfCpltCallback(hspi);
3444 #else
3445   HAL_SPI_TxHalfCpltCallback(hspi);
3446 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3447 }
3448 
3449 /**
3450   * @brief  DMA SPI half receive process complete callback
3451   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3452   *               the configuration information for the specified DMA module.
3453   * @retval None
3454   */
3455 static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma) /* Derogation MISRAC2012-Rule-8.13 */
3456 {
3457   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)
3458                             ((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-8.13 */
3459 
3460 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3461   hspi->RxHalfCpltCallback(hspi);
3462 #else
3463   HAL_SPI_RxHalfCpltCallback(hspi);
3464 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3465 }
3466 
3467 /**
3468   * @brief  DMA SPI half transmit receive process complete callback.
3469   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3470   *               the configuration information for the specified DMA module.
3471   * @retval None
3472   */
3473 static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma) /* Derogation MISRAC2012-Rule-8.13 */
3474 {
3475   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)
3476                             ((DMA_HandleTypeDef *)hdma)->Parent; /* Derogation MISRAC2012-Rule-8.13 */
3477 
3478 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3479   hspi->TxRxHalfCpltCallback(hspi);
3480 #else
3481   HAL_SPI_TxRxHalfCpltCallback(hspi);
3482 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3483 }
3484 
3485 /**
3486   * @brief  DMA SPI communication error callback.
3487   * @param  hdma: pointer to a DMA_HandleTypeDef structure that contains
3488   *               the configuration information for the specified DMA module.
3489   * @retval None
3490   */
3491 static void SPI_DMAError(DMA_HandleTypeDef *hdma)
3492 {
3493   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3494 
3495   /* if DMA error is FIFO error ignore it */
3496   if (HAL_DMA_GetError(hdma) != HAL_DMA_ERROR_FE)
3497   {
3498     /* Call SPI standard close procedure */
3499     SPI_CloseTransfer(hspi);
3500 
3501     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
3502     hspi->State = HAL_SPI_STATE_READY;
3503 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3504     hspi->ErrorCallback(hspi);
3505 #else
3506     HAL_SPI_ErrorCallback(hspi);
3507 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3508   }
3509 }
3510 
3511 /**
3512   * @brief  DMA SPI communication abort callback, when initiated by HAL services on Error
3513   *         (To be called at end of DMA Abort procedure following error occurrence).
3514   * @param  hdma DMA handle.
3515   * @retval None
3516   */
3517 static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3518 {
3519   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3520   hspi->RxXferCount = (uint16_t) 0UL;
3521   hspi->TxXferCount = (uint16_t) 0UL;
3522 
3523   /* Restore hspi->State to Ready */
3524   hspi->State = HAL_SPI_STATE_READY;
3525 
3526 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3527   hspi->ErrorCallback(hspi);
3528 #else
3529   HAL_SPI_ErrorCallback(hspi);
3530 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3531 }
3532 
3533 /**
3534   * @brief  DMA SPI Tx communication abort callback, when initiated by user
3535   *         (To be called at end of DMA Tx Abort procedure following user abort request).
3536   * @note   When this callback is executed, User Abort complete call back is called only if no
3537   *         Abort still ongoing for Rx DMA Handle.
3538   * @param  hdma DMA handle.
3539   * @retval None
3540   */
3541 static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3542 {
3543   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3544 
3545   hspi->hdmatx->XferAbortCallback = NULL;
3546 
3547   /* Check if an Abort process is still ongoing */
3548   if (hspi->hdmarx != NULL)
3549   {
3550     if (hspi->hdmarx->XferAbortCallback != NULL)
3551     {
3552       return;
3553     }
3554   }
3555 
3556   /* Call the Abort procedure */
3557   SPI_AbortTransfer(hspi);
3558 
3559   /* Restore hspi->State to Ready */
3560   hspi->State = HAL_SPI_STATE_READY;
3561 
3562   /* Call user Abort complete callback */
3563 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3564   hspi->AbortCpltCallback(hspi);
3565 #else
3566   HAL_SPI_AbortCpltCallback(hspi);
3567 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3568 }
3569 
3570 /**
3571   * @brief  DMA SPI Rx communication abort callback, when initiated by user
3572   *         (To be called at end of DMA Rx Abort procedure following user abort request).
3573   * @note   When this callback is executed, User Abort complete call back is called only if no
3574   *         Abort still ongoing for Tx DMA Handle.
3575   * @param  hdma DMA handle.
3576   * @retval None
3577   */
3578 static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3579 {
3580   SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
3581 
3582   hspi->hdmarx->XferAbortCallback = NULL;
3583 
3584   /* Check if an Abort process is still ongoing */
3585   if (hspi->hdmatx != NULL)
3586   {
3587     if (hspi->hdmatx->XferAbortCallback != NULL)
3588     {
3589       return;
3590     }
3591   }
3592 
3593   /* Call the Abort procedure */
3594   SPI_AbortTransfer(hspi);
3595 
3596   /* Restore hspi->State to Ready */
3597   hspi->State = HAL_SPI_STATE_READY;
3598 
3599   /* Call user Abort complete callback */
3600 #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1UL)
3601   hspi->AbortCpltCallback(hspi);
3602 #else
3603   HAL_SPI_AbortCpltCallback(hspi);
3604 #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
3605 }
3606 
3607 /**
3608   * @brief  Manage the receive 8-bit in Interrupt context.
3609   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3610   *               the configuration information for SPI module.
3611   * @retval None
3612   */
3613 static void SPI_RxISR_8BIT(SPI_HandleTypeDef *hspi)
3614 {
3615   /* Receive data in 8 Bit mode */
3616   *((uint8_t *)hspi->pRxBuffPtr) = (*(__IO uint8_t *)&hspi->Instance->RXDR);
3617   hspi->pRxBuffPtr += sizeof(uint8_t);
3618   hspi->RxXferCount--;
3619 
3620   /* Disable IT if no more data excepted */
3621   if (hspi->RxXferCount == 0UL)
3622   {
3623 #if defined(USE_SPI_RELOAD_TRANSFER)
3624     /* Check if there is any request to reload */
3625     if (hspi->Reload.Requested == 1UL)
3626     {
3627       hspi->RxXferSize  = hspi->Reload.RxXferSize;
3628       hspi->RxXferCount = hspi->Reload.RxXferSize;
3629       hspi->pRxBuffPtr  = hspi->Reload.pRxBuffPtr;
3630       hspi->Reload.Requested = 0UL;
3631     }
3632     else
3633     {
3634       /* Disable RXP interrupts */
3635       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3636     }
3637 #else
3638     /* Disable RXP interrupts */
3639     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3640 #endif /* USE_SPI_RELOAD_TRANSFER */
3641   }
3642 }
3643 
3644 
3645 /**
3646   * @brief  Manage the 16-bit receive in Interrupt context.
3647   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3648   *               the configuration information for SPI module.
3649   * @retval None
3650   */
3651 static void SPI_RxISR_16BIT(SPI_HandleTypeDef *hspi)
3652 {
3653   /* Receive data in 16 Bit mode */
3654 #if defined (__GNUC__)
3655   __IO uint16_t *prxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->RXDR));
3656 
3657   *((uint16_t *)hspi->pRxBuffPtr) = *prxdr_16bits;
3658 #else
3659   *((uint16_t *)hspi->pRxBuffPtr) = (*(__IO uint16_t *)&hspi->Instance->RXDR);
3660 #endif /* __GNUC__ */
3661   hspi->pRxBuffPtr += sizeof(uint16_t);
3662   hspi->RxXferCount--;
3663 
3664   /* Disable IT if no more data excepted */
3665   if (hspi->RxXferCount == 0UL)
3666   {
3667 #if defined(USE_SPI_RELOAD_TRANSFER)
3668     /* Check if there is any request to reload */
3669     if (hspi->Reload.Requested == 1UL)
3670     {
3671       hspi->RxXferSize  = hspi->Reload.RxXferSize;
3672       hspi->RxXferCount = hspi->Reload.RxXferSize;
3673       hspi->pRxBuffPtr  = hspi->Reload.pRxBuffPtr;
3674       hspi->Reload.Requested = 0UL;
3675     }
3676     else
3677     {
3678       /* Disable RXP interrupts */
3679       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3680     }
3681 #else
3682     /* Disable RXP interrupts */
3683     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3684 #endif /* USE_SPI_RELOAD_TRANSFER */
3685   }
3686 }
3687 
3688 
3689 /**
3690   * @brief  Manage the 32-bit receive in Interrupt context.
3691   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3692   *               the configuration information for SPI module.
3693   * @retval None
3694   */
3695 static void SPI_RxISR_32BIT(SPI_HandleTypeDef *hspi)
3696 {
3697   /* Receive data in 32 Bit mode */
3698   *((uint32_t *)hspi->pRxBuffPtr) = (*(__IO uint32_t *)&hspi->Instance->RXDR);
3699   hspi->pRxBuffPtr += sizeof(uint32_t);
3700   hspi->RxXferCount--;
3701 
3702   /* Disable IT if no more data excepted */
3703   if (hspi->RxXferCount == 0UL)
3704   {
3705 #if defined(USE_SPI_RELOAD_TRANSFER)
3706     /* Check if there is any request to reload */
3707     if (hspi->Reload.Requested == 1UL)
3708     {
3709       hspi->RxXferSize  = hspi->Reload.RxXferSize;
3710       hspi->RxXferCount = hspi->Reload.RxXferSize;
3711       hspi->pRxBuffPtr  = hspi->Reload.pRxBuffPtr;
3712       hspi->Reload.Requested = 0UL;
3713     }
3714     else
3715     {
3716       /* Disable RXP interrupts */
3717       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3718     }
3719 #else
3720     /* Disable RXP interrupts */
3721     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXP);
3722 #endif /* USE_SPI_RELOAD_TRANSFER */
3723   }
3724 }
3725 
3726 
3727 /**
3728   * @brief  Handle the data 8-bit transmit in Interrupt mode.
3729   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3730   *               the configuration information for SPI module.
3731   * @retval None
3732   */
3733 static void SPI_TxISR_8BIT(SPI_HandleTypeDef *hspi)
3734 {
3735   /* Transmit data in 8 Bit mode */
3736   *(__IO uint8_t *)&hspi->Instance->TXDR = *((const uint8_t *)hspi->pTxBuffPtr);
3737   hspi->pTxBuffPtr += sizeof(uint8_t);
3738   hspi->TxXferCount--;
3739 
3740   /* Disable IT if no more data excepted */
3741   if (hspi->TxXferCount == 0UL)
3742   {
3743 #if defined(USE_SPI_RELOAD_TRANSFER)
3744     /* Check if there is any request to reload */
3745     if (hspi->Reload.Requested == 1UL)
3746     {
3747       hspi->TxXferSize  = hspi->Reload.TxXferSize;
3748       hspi->TxXferCount = hspi->Reload.TxXferSize;
3749       hspi->pTxBuffPtr  = hspi->Reload.pTxBuffPtr;
3750 
3751       /* In full duplex mode the reload request is reset in RX side */
3752       if (hspi->State == HAL_SPI_STATE_BUSY_TX)
3753       {
3754         hspi->Reload.Requested = 0UL;
3755       }
3756     }
3757     else
3758     {
3759       /* Disable TXP interrupts */
3760       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3761     }
3762 #else
3763     /* Disable TXP interrupts */
3764     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3765 #endif /* USE_SPI_RELOAD_TRANSFER */
3766   }
3767 }
3768 
3769 /**
3770   * @brief  Handle the data 16-bit transmit in Interrupt mode.
3771   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3772   *               the configuration information for SPI module.
3773   * @retval None
3774   */
3775 static void SPI_TxISR_16BIT(SPI_HandleTypeDef *hspi)
3776 {
3777   /* Transmit data in 16 Bit mode */
3778 #if defined (__GNUC__)
3779   __IO uint16_t *ptxdr_16bits = (__IO uint16_t *)(&(hspi->Instance->TXDR));
3780 
3781   *ptxdr_16bits = *((const uint16_t *)hspi->pTxBuffPtr);
3782 #else
3783   *((__IO uint16_t *)&hspi->Instance->TXDR) = *((const uint16_t *)hspi->pTxBuffPtr);
3784 #endif /* __GNUC__ */
3785   hspi->pTxBuffPtr += sizeof(uint16_t);
3786   hspi->TxXferCount--;
3787 
3788   /* Disable IT if no more data excepted */
3789   if (hspi->TxXferCount == 0UL)
3790   {
3791 #if defined(USE_SPI_RELOAD_TRANSFER)
3792     /* Check if there is any request to reload */
3793     if (hspi->Reload.Requested == 1UL)
3794     {
3795       hspi->TxXferSize  = hspi->Reload.TxXferSize;
3796       hspi->TxXferCount = hspi->Reload.TxXferSize;
3797       hspi->pTxBuffPtr  = hspi->Reload.pTxBuffPtr;
3798 
3799       /* In full duplex mode the reload request is reset in RX side */
3800       if (hspi->State == HAL_SPI_STATE_BUSY_TX)
3801       {
3802         hspi->Reload.Requested = 0UL;
3803       }
3804     }
3805     else
3806     {
3807       /* Disable TXP interrupts */
3808       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3809     }
3810 #else
3811     /* Disable TXP interrupts */
3812     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3813 #endif /* USE_SPI_RELOAD_TRANSFER */
3814   }
3815 }
3816 
3817 /**
3818   * @brief  Handle the data 32-bit transmit in Interrupt mode.
3819   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3820   *               the configuration information for SPI module.
3821   * @retval None
3822   */
3823 static void SPI_TxISR_32BIT(SPI_HandleTypeDef *hspi)
3824 {
3825   /* Transmit data in 32 Bit mode */
3826   *((__IO uint32_t *)&hspi->Instance->TXDR) = *((const uint32_t *)hspi->pTxBuffPtr);
3827   hspi->pTxBuffPtr += sizeof(uint32_t);
3828   hspi->TxXferCount--;
3829 
3830   /* Disable IT if no more data excepted */
3831   if (hspi->TxXferCount == 0UL)
3832   {
3833 #if defined(USE_SPI_RELOAD_TRANSFER)
3834     /* Check if there is any request to reload */
3835     if (hspi->Reload.Requested == 1UL)
3836     {
3837       hspi->TxXferSize  = hspi->Reload.TxXferSize;
3838       hspi->TxXferCount = hspi->Reload.TxXferSize;
3839       hspi->pTxBuffPtr  = hspi->Reload.pTxBuffPtr;
3840 
3841       /* In full duplex mode the reload request is reset in RX side */
3842       if (hspi->State == HAL_SPI_STATE_BUSY_TX)
3843       {
3844         hspi->Reload.Requested = 0UL;
3845       }
3846     }
3847     else
3848     {
3849       /* Disable TXP interrupts */
3850       __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3851     }
3852 #else
3853     /* Disable TXP interrupts */
3854     __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXP);
3855 #endif /* USE_SPI_RELOAD_TRANSFER */
3856   }
3857 }
3858 
3859 /**
3860   * @brief  Abort Transfer and clear flags.
3861   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3862   *               the configuration information for SPI module.
3863   * @retval None
3864   */
3865 static void SPI_AbortTransfer(SPI_HandleTypeDef *hspi)
3866 {
3867   /* Disable SPI peripheral */
3868   __HAL_SPI_DISABLE(hspi);
3869 
3870   /* Disable ITs */
3871   __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_TXP | SPI_IT_RXP | SPI_IT_DXP | SPI_IT_UDR | SPI_IT_OVR | \
3872                               SPI_IT_FRE | SPI_IT_MODF));
3873 
3874   /* Clear the Status flags in the SR register */
3875   __HAL_SPI_CLEAR_EOTFLAG(hspi);
3876   __HAL_SPI_CLEAR_TXTFFLAG(hspi);
3877 
3878   /* Disable Tx DMA Request */
3879   CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN);
3880 
3881   /* Clear the Error flags in the SR register */
3882   __HAL_SPI_CLEAR_OVRFLAG(hspi);
3883   __HAL_SPI_CLEAR_UDRFLAG(hspi);
3884   __HAL_SPI_CLEAR_FREFLAG(hspi);
3885   __HAL_SPI_CLEAR_MODFFLAG(hspi);
3886   __HAL_SPI_CLEAR_SUSPFLAG(hspi);
3887 
3888 #if (USE_SPI_CRC != 0U)
3889   __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
3890 #endif /* USE_SPI_CRC */
3891 
3892   hspi->TxXferCount = (uint16_t)0UL;
3893   hspi->RxXferCount = (uint16_t)0UL;
3894 }
3895 
3896 
3897 /**
3898   * @brief  Close Transfer and clear flags.
3899   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3900   *               the configuration information for SPI module.
3901   * @retval HAL_ERROR: if any error detected
3902   *         HAL_OK: if nothing detected
3903   */
3904 static void SPI_CloseTransfer(SPI_HandleTypeDef *hspi)
3905 {
3906   uint32_t itflag = hspi->Instance->SR;
3907 
3908   __HAL_SPI_CLEAR_EOTFLAG(hspi);
3909   __HAL_SPI_CLEAR_TXTFFLAG(hspi);
3910 
3911   /* Disable SPI peripheral */
3912   __HAL_SPI_DISABLE(hspi);
3913 
3914   /* Disable ITs */
3915   __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_EOT | SPI_IT_TXP | SPI_IT_RXP | SPI_IT_DXP | SPI_IT_UDR | SPI_IT_OVR | \
3916                               SPI_IT_FRE | SPI_IT_MODF));
3917 
3918   /* Disable Tx DMA Request */
3919   CLEAR_BIT(hspi->Instance->CFG1, SPI_CFG1_TXDMAEN | SPI_CFG1_RXDMAEN);
3920 
3921   /* Report UnderRun error for non RX Only communication */
3922   if (hspi->State != HAL_SPI_STATE_BUSY_RX)
3923   {
3924     if ((itflag & SPI_FLAG_UDR) != 0UL)
3925     {
3926       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_UDR);
3927       __HAL_SPI_CLEAR_UDRFLAG(hspi);
3928     }
3929   }
3930 
3931   /* Report OverRun error for non TX Only communication */
3932   if (hspi->State != HAL_SPI_STATE_BUSY_TX)
3933   {
3934     if ((itflag & SPI_FLAG_OVR) != 0UL)
3935     {
3936       SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
3937       __HAL_SPI_CLEAR_OVRFLAG(hspi);
3938     }
3939 
3940 #if (USE_SPI_CRC != 0UL)
3941     /* Check if CRC error occurred */
3942     if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
3943     {
3944       if ((itflag & SPI_FLAG_CRCERR) != 0UL)
3945       {
3946         SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
3947         __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
3948       }
3949     }
3950 #endif /* USE_SPI_CRC */
3951   }
3952 
3953   /* SPI Mode Fault error interrupt occurred -------------------------------*/
3954   if ((itflag & SPI_FLAG_MODF) != 0UL)
3955   {
3956     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
3957     __HAL_SPI_CLEAR_MODFFLAG(hspi);
3958   }
3959 
3960   /* SPI Frame error interrupt occurred ------------------------------------*/
3961   if ((itflag & SPI_FLAG_FRE) != 0UL)
3962   {
3963     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FRE);
3964     __HAL_SPI_CLEAR_FREFLAG(hspi);
3965   }
3966 
3967   hspi->TxXferCount = (uint16_t)0UL;
3968   hspi->RxXferCount = (uint16_t)0UL;
3969 }
3970 
3971 /**
3972   * @brief Handle SPI Communication Timeout.
3973   * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
3974   *              the configuration information for SPI module.
3975   * @param Flag: SPI flag to check
3976   * @param Status: flag state to check
3977   * @param Timeout: Timeout duration
3978   * @param Tickstart: Tick start value
3979   * @retval HAL status
3980   */
3981 static HAL_StatusTypeDef SPI_WaitOnFlagUntilTimeout(const SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus Status,
3982                                                     uint32_t Timeout, uint32_t Tickstart)
3983 {
3984   /* Wait until flag is set */
3985   while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) == Status)
3986   {
3987     /* Check for the Timeout */
3988     if ((((HAL_GetTick() - Tickstart) >=  Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
3989     {
3990       return HAL_TIMEOUT;
3991     }
3992   }
3993   return HAL_OK;
3994 }
3995 
3996 /**
3997   * @brief  Compute configured packet size from fifo perspective.
3998   * @param  hspi: pointer to a SPI_HandleTypeDef structure that contains
3999   *               the configuration information for SPI module.
4000   * @retval Packet size occupied in the fifo
4001   */
4002 static uint32_t SPI_GetPacketSize(const SPI_HandleTypeDef *hspi)
4003 {
4004   uint32_t fifo_threashold = (hspi->Init.FifoThreshold >> SPI_CFG1_FTHLV_Pos) + 1UL;
4005   uint32_t data_size       = (hspi->Init.DataSize      >> SPI_CFG1_DSIZE_Pos) + 1UL;
4006 
4007   /* Convert data size to Byte */
4008   data_size = (data_size + 7UL) / 8UL;
4009 
4010   return data_size * fifo_threashold;
4011 }
4012 
4013 /**
4014   * @}
4015   */
4016 
4017 #endif /* HAL_SPI_MODULE_ENABLED */
4018 
4019 /**
4020   * @}
4021   */
4022 
4023 /**
4024   * @}
4025   */