Back to home page

LXR

 
 

    


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

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_usart.c
0004   * @author  MCD Application Team
0005   * @brief   USART HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
0008   *          Peripheral (USART).
0009   *           + Initialization and de-initialization functions
0010   *           + IO operation functions
0011   *           + Peripheral Control functions
0012   *           + Peripheral State and Error functions
0013   *
0014   ******************************************************************************
0015   * @attention
0016   *
0017   * Copyright (c) 2017 STMicroelectronics.
0018   * All rights reserved.
0019   *
0020   * This software is licensed under terms that can be found in the LICENSE file
0021   * in the root directory of this software component.
0022   * If no LICENSE file comes with this software, it is provided AS-IS.
0023   *
0024   ******************************************************************************
0025   @verbatim
0026  ===============================================================================
0027                         ##### How to use this driver #####
0028  ===============================================================================
0029     [..]
0030       The USART HAL driver can be used as follows:
0031 
0032       (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
0033       (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
0034           (++) Enable the USARTx interface clock.
0035           (++) USART pins configuration:
0036             (+++) Enable the clock for the USART GPIOs.
0037             (+++) Configure these USART pins as alternate function pull-up.
0038           (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
0039                 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
0040             (+++) Configure the USARTx interrupt priority.
0041             (+++) Enable the NVIC USART IRQ handle.
0042             (++) USART interrupts handling:
0043               -@@-   The specific USART interrupts (Transmission complete interrupt,
0044                   RXNE interrupt and Error Interrupts) will be managed using the macros
0045                   __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
0046           (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
0047                HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
0048             (+++) Declare a DMA handle structure for the Tx/Rx channel.
0049             (+++) Enable the DMAx interface clock.
0050             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
0051             (+++) Configure the DMA Tx/Rx channel.
0052             (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
0053             (+++) Configure the priority and enable the NVIC for the transfer
0054                   complete interrupt on the DMA Tx/Rx channel.
0055 
0056       (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode
0057           (Receiver/Transmitter) in the husart handle Init structure.
0058 
0059       (#) Initialize the USART registers by calling the HAL_USART_Init() API:
0060           (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
0061                by calling the customized HAL_USART_MspInit(&husart) API.
0062 
0063     [..]
0064      (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
0065         HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
0066         HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
0067 
0068     ##### Callback registration #####
0069     ==================================
0070 
0071     [..]
0072     The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
0073     allows the user to configure dynamically the driver callbacks.
0074 
0075     [..]
0076     Use Function HAL_USART_RegisterCallback() to register a user callback.
0077     Function HAL_USART_RegisterCallback() allows to register following callbacks:
0078     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
0079     (+) TxCpltCallback            : Tx Complete Callback.
0080     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
0081     (+) RxCpltCallback            : Rx Complete Callback.
0082     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
0083     (+) ErrorCallback             : Error Callback.
0084     (+) AbortCpltCallback         : Abort Complete Callback.
0085     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0086     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0087     (+) MspInitCallback           : USART MspInit.
0088     (+) MspDeInitCallback         : USART MspDeInit.
0089     This function takes as parameters the HAL peripheral handle, the Callback ID
0090     and a pointer to the user callback function.
0091 
0092     [..]
0093     Use function HAL_USART_UnRegisterCallback() to reset a callback to the default
0094     weak function.
0095     HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0096     and the Callback ID.
0097     This function allows to reset following callbacks:
0098     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
0099     (+) TxCpltCallback            : Tx Complete Callback.
0100     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
0101     (+) RxCpltCallback            : Rx Complete Callback.
0102     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
0103     (+) ErrorCallback             : Error Callback.
0104     (+) AbortCpltCallback         : Abort Complete Callback.
0105     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0106     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0107     (+) MspInitCallback           : USART MspInit.
0108     (+) MspDeInitCallback         : USART MspDeInit.
0109 
0110     [..]
0111     By default, after the HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
0112     all callbacks are set to the corresponding weak functions:
0113     examples HAL_USART_TxCpltCallback(), HAL_USART_RxHalfCpltCallback().
0114     Exception done for MspInit and MspDeInit functions that are respectively
0115     reset to the legacy weak functions in the HAL_USART_Init()
0116     and HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
0117     If not, MspInit or MspDeInit are not null, the HAL_USART_Init() and HAL_USART_DeInit()
0118     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
0119 
0120     [..]
0121     Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
0122     Exception done MspInit/MspDeInit that can be registered/unregistered
0123     in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
0124     MspInit/DeInit callbacks can be used during the Init/DeInit.
0125     In that case first register the MspInit/MspDeInit user callbacks
0126     using HAL_USART_RegisterCallback() before calling HAL_USART_DeInit()
0127     or HAL_USART_Init() function.
0128 
0129     [..]
0130     When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
0131     not defined, the callback registration feature is not available
0132     and weak callbacks are used.
0133 
0134 
0135   @endverbatim
0136   ******************************************************************************
0137   */
0138 
0139 /* Includes ------------------------------------------------------------------*/
0140 #include "stm32h7xx_hal.h"
0141 
0142 /** @addtogroup STM32H7xx_HAL_Driver
0143   * @{
0144   */
0145 
0146 /** @defgroup USART USART
0147   * @ingroup RTEMSBSPsARMSTM32H7
0148   * @brief HAL USART Synchronous module driver
0149   * @{
0150   */
0151 
0152 #ifdef HAL_USART_MODULE_ENABLED
0153 
0154 /* Private typedef -----------------------------------------------------------*/
0155 /* Private define ------------------------------------------------------------*/
0156 /** @defgroup USART_Private_Constants USART Private Constants
0157   * @ingroup RTEMSBSPsARMSTM32H7
0158   * @{
0159   */
0160 #define USART_DUMMY_DATA          ((uint16_t) 0xFFFF)           /*!< USART transmitted dummy data                     */
0161 #define USART_TEACK_REACK_TIMEOUT             1000U             /*!< USART TX or RX enable acknowledge time-out value */
0162 #define USART_CR1_FIELDS          ((uint32_t)(USART_CR1_M |  USART_CR1_PCE | USART_CR1_PS    | \
0163                                               USART_CR1_TE | USART_CR1_RE  | USART_CR1_OVER8 | \
0164                                               USART_CR1_FIFOEN ))                                  /*!< USART CR1 fields of parameters set by USART_SetConfig API */
0165 
0166 #define USART_CR2_FIELDS          ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \
0167                                               USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \
0168                                               USART_CR2_DIS_NSS))                                  /*!< USART CR2 fields of parameters set by USART_SetConfig API */
0169 
0170 #define USART_CR3_FIELDS          ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG ))             /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */
0171 
0172 #define USART_BRR_MIN    0x10U        /* USART BRR minimum authorized value */
0173 #define USART_BRR_MAX    0xFFFFU      /* USART BRR maximum authorized value */
0174 /**
0175   * @}
0176   */
0177 
0178 /* Private macros ------------------------------------------------------------*/
0179 /* Private variables ---------------------------------------------------------*/
0180 /* Private function prototypes -----------------------------------------------*/
0181 /** @addtogroup USART_Private_Functions
0182   * @{
0183   */
0184 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
0185 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
0186 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
0187 static void USART_EndTransfer(USART_HandleTypeDef *husart);
0188 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
0189 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
0190 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
0191 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
0192 static void USART_DMAError(DMA_HandleTypeDef *hdma);
0193 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
0194 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
0195 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
0196 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
0197                                                       uint32_t Tickstart, uint32_t Timeout);
0198 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
0199 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
0200 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart);
0201 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart);
0202 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
0203 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
0204 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart);
0205 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart);
0206 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart);
0207 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
0208 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
0209 
0210 
0211 /**
0212   * @}
0213   */
0214 
0215 /* Exported functions --------------------------------------------------------*/
0216 
0217 /** @defgroup USART_Exported_Functions USART Exported Functions
0218   * @ingroup RTEMSBSPsARMSTM32H7
0219   * @{
0220   */
0221 
0222 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
0223   * @ingroup RTEMSBSPsARMSTM32H7
0224   * @brief    Initialization and Configuration functions
0225   *
0226 @verbatim
0227  ===============================================================================
0228             ##### Initialization and Configuration functions #####
0229  ===============================================================================
0230     [..]
0231     This subsection provides a set of functions allowing to initialize the USART
0232     in asynchronous and in synchronous modes.
0233       (+) For the asynchronous mode only these parameters can be configured:
0234         (++) Baud Rate
0235         (++) Word Length
0236         (++) Stop Bit
0237         (++) Parity: If the parity is enabled, then the MSB bit of the data written
0238              in the data register is transmitted but is changed by the parity bit.
0239         (++) USART polarity
0240         (++) USART phase
0241         (++) USART LastBit
0242         (++) Receiver/transmitter modes
0243 
0244     [..]
0245     The HAL_USART_Init() function follows the USART  synchronous configuration
0246     procedure (details for the procedure are available in reference manual).
0247 
0248 @endverbatim
0249 
0250   Depending on the frame length defined by the M1 and M0 bits (7-bit,
0251   8-bit or 9-bit), the possible USART formats are listed in the
0252   following table.
0253 
0254     Table 1. USART frame format.
0255     +-----------------------------------------------------------------------+
0256     |  M1 bit |  M0 bit |  PCE bit  |            USART frame                |
0257     |---------|---------|-----------|---------------------------------------|
0258     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
0259     |---------|---------|-----------|---------------------------------------|
0260     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
0261     |---------|---------|-----------|---------------------------------------|
0262     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
0263     |---------|---------|-----------|---------------------------------------|
0264     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
0265     |---------|---------|-----------|---------------------------------------|
0266     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
0267     |---------|---------|-----------|---------------------------------------|
0268     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
0269     +-----------------------------------------------------------------------+
0270 
0271   * @{
0272   */
0273 
0274 /**
0275   * @brief  Initialize the USART mode according to the specified
0276   *         parameters in the USART_InitTypeDef and initialize the associated handle.
0277   * @param  husart USART handle.
0278   * @retval HAL status
0279   */
0280 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
0281 {
0282   /* Check the USART handle allocation */
0283   if (husart == NULL)
0284   {
0285     return HAL_ERROR;
0286   }
0287 
0288   /* Check the parameters */
0289   assert_param(IS_USART_INSTANCE(husart->Instance));
0290 
0291   if (husart->State == HAL_USART_STATE_RESET)
0292   {
0293     /* Allocate lock resource and initialize it */
0294     husart->Lock = HAL_UNLOCKED;
0295 
0296 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
0297     USART_InitCallbacksToDefault(husart);
0298 
0299     if (husart->MspInitCallback == NULL)
0300     {
0301       husart->MspInitCallback = HAL_USART_MspInit;
0302     }
0303 
0304     /* Init the low level hardware */
0305     husart->MspInitCallback(husart);
0306 #else
0307     /* Init the low level hardware : GPIO, CLOCK */
0308     HAL_USART_MspInit(husart);
0309 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
0310   }
0311 
0312   husart->State = HAL_USART_STATE_BUSY;
0313 
0314   /* Disable the Peripheral */
0315   __HAL_USART_DISABLE(husart);
0316 
0317   /* Set the Usart Communication parameters */
0318   if (USART_SetConfig(husart) == HAL_ERROR)
0319   {
0320     return HAL_ERROR;
0321   }
0322 
0323   /* In Synchronous mode, the following bits must be kept cleared:
0324   - LINEN bit in the USART_CR2 register
0325   - HDSEL, SCEN and IREN bits in the USART_CR3 register.
0326   */
0327   husart->Instance->CR2 &= ~USART_CR2_LINEN;
0328   husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
0329 
0330   /* Enable the Peripheral */
0331   __HAL_USART_ENABLE(husart);
0332 
0333   /* TEACK and/or REACK to check before moving husart->State to Ready */
0334   return (USART_CheckIdleState(husart));
0335 }
0336 
0337 /**
0338   * @brief DeInitialize the USART peripheral.
0339   * @param  husart USART handle.
0340   * @retval HAL status
0341   */
0342 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
0343 {
0344   /* Check the USART handle allocation */
0345   if (husart == NULL)
0346   {
0347     return HAL_ERROR;
0348   }
0349 
0350   /* Check the parameters */
0351   assert_param(IS_USART_INSTANCE(husart->Instance));
0352 
0353   husart->State = HAL_USART_STATE_BUSY;
0354 
0355   husart->Instance->CR1 = 0x0U;
0356   husart->Instance->CR2 = 0x0U;
0357   husart->Instance->CR3 = 0x0U;
0358 
0359 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
0360   if (husart->MspDeInitCallback == NULL)
0361   {
0362     husart->MspDeInitCallback = HAL_USART_MspDeInit;
0363   }
0364   /* DeInit the low level hardware */
0365   husart->MspDeInitCallback(husart);
0366 #else
0367   /* DeInit the low level hardware */
0368   HAL_USART_MspDeInit(husart);
0369 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
0370 
0371   husart->ErrorCode = HAL_USART_ERROR_NONE;
0372   husart->State = HAL_USART_STATE_RESET;
0373 
0374   /* Process Unlock */
0375   __HAL_UNLOCK(husart);
0376 
0377   return HAL_OK;
0378 }
0379 
0380 #ifndef __rtems__
0381 /**
0382   * @brief Initialize the USART MSP.
0383   * @param husart USART handle.
0384   * @retval None
0385   */
0386 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
0387 {
0388   /* Prevent unused argument(s) compilation warning */
0389   UNUSED(husart);
0390 
0391   /* NOTE : This function should not be modified, when the callback is needed,
0392             the HAL_USART_MspInit can be implemented in the user file
0393    */
0394 }
0395 #endif /* __rtems__ */
0396 
0397 /**
0398   * @brief DeInitialize the USART MSP.
0399   * @param husart USART handle.
0400   * @retval None
0401   */
0402 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
0403 {
0404   /* Prevent unused argument(s) compilation warning */
0405   UNUSED(husart);
0406 
0407   /* NOTE : This function should not be modified, when the callback is needed,
0408             the HAL_USART_MspDeInit can be implemented in the user file
0409    */
0410 }
0411 
0412 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
0413 /**
0414   * @brief  Register a User USART Callback
0415   *         To be used to override the weak predefined callback
0416   * @note   The HAL_USART_RegisterCallback() may be called before HAL_USART_Init() in HAL_USART_STATE_RESET
0417   *         to register callbacks for HAL_USART_MSPINIT_CB_ID and HAL_USART_MSPDEINIT_CB_ID
0418   * @param  husart usart handle
0419   * @param  CallbackID ID of the callback to be registered
0420   *         This parameter can be one of the following values:
0421   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
0422   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
0423   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
0424   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
0425   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
0426   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
0427   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0428   *           @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0429   *           @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0430   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
0431   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
0432   * @param  pCallback pointer to the Callback function
0433   * @retval HAL status
0434 +  */
0435 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
0436                                              pUSART_CallbackTypeDef pCallback)
0437 {
0438   HAL_StatusTypeDef status = HAL_OK;
0439 
0440   if (pCallback == NULL)
0441   {
0442     /* Update the error code */
0443     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0444 
0445     return HAL_ERROR;
0446   }
0447 
0448   if (husart->State == HAL_USART_STATE_READY)
0449   {
0450     switch (CallbackID)
0451     {
0452       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
0453         husart->TxHalfCpltCallback = pCallback;
0454         break;
0455 
0456       case HAL_USART_TX_COMPLETE_CB_ID :
0457         husart->TxCpltCallback = pCallback;
0458         break;
0459 
0460       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
0461         husart->RxHalfCpltCallback = pCallback;
0462         break;
0463 
0464       case HAL_USART_RX_COMPLETE_CB_ID :
0465         husart->RxCpltCallback = pCallback;
0466         break;
0467 
0468       case HAL_USART_TX_RX_COMPLETE_CB_ID :
0469         husart->TxRxCpltCallback = pCallback;
0470         break;
0471 
0472       case HAL_USART_ERROR_CB_ID :
0473         husart->ErrorCallback = pCallback;
0474         break;
0475 
0476       case HAL_USART_ABORT_COMPLETE_CB_ID :
0477         husart->AbortCpltCallback = pCallback;
0478         break;
0479 
0480       case HAL_USART_RX_FIFO_FULL_CB_ID :
0481         husart->RxFifoFullCallback = pCallback;
0482         break;
0483 
0484       case HAL_USART_TX_FIFO_EMPTY_CB_ID :
0485         husart->TxFifoEmptyCallback = pCallback;
0486         break;
0487 
0488       case HAL_USART_MSPINIT_CB_ID :
0489         husart->MspInitCallback = pCallback;
0490         break;
0491 
0492       case HAL_USART_MSPDEINIT_CB_ID :
0493         husart->MspDeInitCallback = pCallback;
0494         break;
0495 
0496       default :
0497         /* Update the error code */
0498         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0499 
0500         /* Return error status */
0501         status =  HAL_ERROR;
0502         break;
0503     }
0504   }
0505   else if (husart->State == HAL_USART_STATE_RESET)
0506   {
0507     switch (CallbackID)
0508     {
0509       case HAL_USART_MSPINIT_CB_ID :
0510         husart->MspInitCallback = pCallback;
0511         break;
0512 
0513       case HAL_USART_MSPDEINIT_CB_ID :
0514         husart->MspDeInitCallback = pCallback;
0515         break;
0516 
0517       default :
0518         /* Update the error code */
0519         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0520 
0521         /* Return error status */
0522         status =  HAL_ERROR;
0523         break;
0524     }
0525   }
0526   else
0527   {
0528     /* Update the error code */
0529     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0530 
0531     /* Return error status */
0532     status =  HAL_ERROR;
0533   }
0534 
0535   return status;
0536 }
0537 
0538 /**
0539   * @brief  Unregister an USART Callback
0540   *         USART callaback is redirected to the weak predefined callback
0541   * @note   The HAL_USART_UnRegisterCallback() may be called before HAL_USART_Init() in HAL_USART_STATE_RESET
0542   *         to un-register callbacks for HAL_USART_MSPINIT_CB_ID and HAL_USART_MSPDEINIT_CB_ID
0543   * @param  husart usart handle
0544   * @param  CallbackID ID of the callback to be unregistered
0545   *         This parameter can be one of the following values:
0546   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
0547   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
0548   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
0549   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
0550   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
0551   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
0552   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0553   *           @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0554   *           @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0555   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
0556   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
0557   * @retval HAL status
0558   */
0559 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
0560 {
0561   HAL_StatusTypeDef status = HAL_OK;
0562 
0563   if (HAL_USART_STATE_READY == husart->State)
0564   {
0565     switch (CallbackID)
0566     {
0567       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
0568         husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback  */
0569         break;
0570 
0571       case HAL_USART_TX_COMPLETE_CB_ID :
0572         husart->TxCpltCallback = HAL_USART_TxCpltCallback;                       /* Legacy weak TxCpltCallback       */
0573         break;
0574 
0575       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
0576         husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback   */
0577         break;
0578 
0579       case HAL_USART_RX_COMPLETE_CB_ID :
0580         husart->RxCpltCallback = HAL_USART_RxCpltCallback;                       /* Legacy weak RxCpltCallback       */
0581         break;
0582 
0583       case HAL_USART_TX_RX_COMPLETE_CB_ID :
0584         husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback;                   /* Legacy weak TxRxCpltCallback     */
0585         break;
0586 
0587       case HAL_USART_ERROR_CB_ID :
0588         husart->ErrorCallback = HAL_USART_ErrorCallback;                         /* Legacy weak ErrorCallback        */
0589         break;
0590 
0591       case HAL_USART_ABORT_COMPLETE_CB_ID :
0592         husart->AbortCpltCallback = HAL_USART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback    */
0593         break;
0594 
0595       case HAL_USART_RX_FIFO_FULL_CB_ID :
0596         husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback;             /* Legacy weak RxFifoFullCallback   */
0597         break;
0598 
0599       case HAL_USART_TX_FIFO_EMPTY_CB_ID :
0600         husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback;           /* Legacy weak TxFifoEmptyCallback  */
0601         break;
0602 
0603       case HAL_USART_MSPINIT_CB_ID :
0604         husart->MspInitCallback = HAL_USART_MspInit;                             /* Legacy weak MspInitCallback      */
0605         break;
0606 
0607       case HAL_USART_MSPDEINIT_CB_ID :
0608         husart->MspDeInitCallback = HAL_USART_MspDeInit;                         /* Legacy weak MspDeInitCallback    */
0609         break;
0610 
0611       default :
0612         /* Update the error code */
0613         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0614 
0615         /* Return error status */
0616         status =  HAL_ERROR;
0617         break;
0618     }
0619   }
0620   else if (HAL_USART_STATE_RESET == husart->State)
0621   {
0622     switch (CallbackID)
0623     {
0624       case HAL_USART_MSPINIT_CB_ID :
0625         husart->MspInitCallback = HAL_USART_MspInit;
0626         break;
0627 
0628       case HAL_USART_MSPDEINIT_CB_ID :
0629         husart->MspDeInitCallback = HAL_USART_MspDeInit;
0630         break;
0631 
0632       default :
0633         /* Update the error code */
0634         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0635 
0636         /* Return error status */
0637         status =  HAL_ERROR;
0638         break;
0639     }
0640   }
0641   else
0642   {
0643     /* Update the error code */
0644     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
0645 
0646     /* Return error status */
0647     status =  HAL_ERROR;
0648   }
0649 
0650   return status;
0651 }
0652 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
0653 
0654 
0655 /**
0656   * @}
0657   */
0658 
0659 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
0660   * @ingroup RTEMSBSPsARMSTM32H7
0661   * @brief   USART Transmit and Receive functions
0662   *
0663 @verbatim
0664  ===============================================================================
0665                       ##### IO operation functions #####
0666  ===============================================================================
0667     [..] This subsection provides a set of functions allowing to manage the USART synchronous
0668     data transfers.
0669 
0670     [..] The USART supports master mode only: it cannot receive or send data related to an input
0671          clock (SCLK is always an output).
0672 
0673     [..]
0674 
0675     (#) There are two modes of transfer:
0676         (++) Blocking mode: The communication is performed in polling mode.
0677              The HAL status of all data processing is returned by the same function
0678              after finishing transfer.
0679         (++) No-Blocking mode: The communication is performed using Interrupts
0680              or DMA, These API's return the HAL status.
0681              The end of the data processing will be indicated through the
0682              dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
0683              using DMA mode.
0684              The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
0685              will be executed respectively at the end of the transmit or Receive process
0686              The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
0687 
0688     (#) Blocking mode API's are :
0689         (++) HAL_USART_Transmit() in simplex mode
0690         (++) HAL_USART_Receive() in full duplex receive only
0691         (++) HAL_USART_TransmitReceive() in full duplex mode
0692 
0693     (#) Non-Blocking mode API's with Interrupt are :
0694         (++) HAL_USART_Transmit_IT() in simplex mode
0695         (++) HAL_USART_Receive_IT() in full duplex receive only
0696         (++) HAL_USART_TransmitReceive_IT() in full duplex mode
0697         (++) HAL_USART_IRQHandler()
0698 
0699     (#) No-Blocking mode API's  with DMA are :
0700         (++) HAL_USART_Transmit_DMA() in simplex mode
0701         (++) HAL_USART_Receive_DMA() in full duplex receive only
0702         (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
0703         (++) HAL_USART_DMAPause()
0704         (++) HAL_USART_DMAResume()
0705         (++) HAL_USART_DMAStop()
0706 
0707     (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
0708         (++) HAL_USART_TxCpltCallback()
0709         (++) HAL_USART_RxCpltCallback()
0710         (++) HAL_USART_TxHalfCpltCallback()
0711         (++) HAL_USART_RxHalfCpltCallback()
0712         (++) HAL_USART_ErrorCallback()
0713         (++) HAL_USART_TxRxCpltCallback()
0714 
0715     (#) Non-Blocking mode transfers could be aborted using Abort API's :
0716         (++) HAL_USART_Abort()
0717         (++) HAL_USART_Abort_IT()
0718 
0719     (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
0720         (++) HAL_USART_AbortCpltCallback()
0721 
0722     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
0723         Errors are handled as follows :
0724         (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
0725              to be evaluated by user : this concerns Frame Error,
0726              Parity Error or Noise Error in Interrupt mode reception .
0727              Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify
0728              error type, and HAL_USART_ErrorCallback() user callback is executed.
0729              Transfer is kept ongoing on USART side.
0730              If user wants to abort it, Abort services should be called by user.
0731         (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
0732              This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
0733              Error code is set to allow user to identify error type,
0734              and HAL_USART_ErrorCallback() user callback is executed.
0735 
0736 @endverbatim
0737   * @{
0738   */
0739 
0740 /**
0741   * @brief  Simplex send an amount of data in blocking mode.
0742   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
0743   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
0744   *         of u16 provided through pTxData.
0745   * @param  husart USART handle.
0746   * @param  pTxData Pointer to data buffer (u8 or u16 data elements).
0747   * @param  Size Amount of data elements (u8 or u16) to be sent.
0748   * @param  Timeout Timeout duration.
0749   * @retval HAL status
0750   */
0751 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size,
0752                                      uint32_t Timeout)
0753 {
0754   const uint8_t  *ptxdata8bits;
0755   const uint16_t *ptxdata16bits;
0756   uint32_t tickstart;
0757 
0758   if (husart->State == HAL_USART_STATE_READY)
0759   {
0760     if ((pTxData == NULL) || (Size == 0U))
0761     {
0762       return  HAL_ERROR;
0763     }
0764 
0765     /* Process Locked */
0766     __HAL_LOCK(husart);
0767 
0768     husart->ErrorCode = HAL_USART_ERROR_NONE;
0769     husart->State = HAL_USART_STATE_BUSY_TX;
0770 
0771     /* Init tickstart for timeout management */
0772     tickstart = HAL_GetTick();
0773 
0774     husart->TxXferSize = Size;
0775     husart->TxXferCount = Size;
0776 
0777     /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
0778     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
0779     {
0780       ptxdata8bits  = NULL;
0781       ptxdata16bits = (const uint16_t *) pTxData;
0782     }
0783     else
0784     {
0785       ptxdata8bits  = pTxData;
0786       ptxdata16bits = NULL;
0787     }
0788 
0789     /* Check the remaining data to be sent */
0790     while (husart->TxXferCount > 0U)
0791     {
0792       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
0793       {
0794         return HAL_TIMEOUT;
0795       }
0796       if (ptxdata8bits == NULL)
0797       {
0798         husart->Instance->TDR = (uint16_t)(*ptxdata16bits & 0x01FFU);
0799         ptxdata16bits++;
0800       }
0801       else
0802       {
0803         husart->Instance->TDR = (uint8_t)(*ptxdata8bits & 0xFFU);
0804         ptxdata8bits++;
0805       }
0806 
0807       husart->TxXferCount--;
0808     }
0809 
0810     if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
0811     {
0812       return HAL_TIMEOUT;
0813     }
0814 
0815     /* Clear Transmission Complete Flag */
0816     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
0817 
0818     /* Clear overrun flag and discard the received data */
0819     __HAL_USART_CLEAR_OREFLAG(husart);
0820     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
0821     __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
0822 
0823     /* At end of Tx process, restore husart->State to Ready */
0824     husart->State = HAL_USART_STATE_READY;
0825 
0826     /* Process Unlocked */
0827     __HAL_UNLOCK(husart);
0828 
0829     return HAL_OK;
0830   }
0831   else
0832   {
0833     return HAL_BUSY;
0834   }
0835 }
0836 
0837 /**
0838   * @brief Receive an amount of data in blocking mode.
0839   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
0840   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
0841   *         the received data is handled as a set of u16. In this case, Size must indicate the number
0842   *         of u16 available through pRxData.
0843   * @param husart USART handle.
0844   * @param pRxData Pointer to data buffer (u8 or u16 data elements).
0845   * @param Size Amount of data elements (u8 or u16) to be received.
0846   * @param Timeout Timeout duration.
0847   * @retval HAL status
0848   */
0849 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
0850 {
0851   uint8_t  *prxdata8bits;
0852   uint16_t *prxdata16bits;
0853   uint16_t uhMask;
0854   uint32_t tickstart;
0855 
0856   if (husart->State == HAL_USART_STATE_READY)
0857   {
0858     if ((pRxData == NULL) || (Size == 0U))
0859     {
0860       return  HAL_ERROR;
0861     }
0862 
0863     /* Process Locked */
0864     __HAL_LOCK(husart);
0865 
0866     husart->ErrorCode = HAL_USART_ERROR_NONE;
0867     husart->State = HAL_USART_STATE_BUSY_RX;
0868 
0869     /* Init tickstart for timeout management */
0870     tickstart = HAL_GetTick();
0871 
0872     husart->RxXferSize = Size;
0873     husart->RxXferCount = Size;
0874 
0875     /* Computation of USART mask to apply to RDR register */
0876     USART_MASK_COMPUTATION(husart);
0877     uhMask = husart->Mask;
0878 
0879     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
0880     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
0881     {
0882       prxdata8bits  = NULL;
0883       prxdata16bits = (uint16_t *) pRxData;
0884     }
0885     else
0886     {
0887       prxdata8bits  = pRxData;
0888       prxdata16bits = NULL;
0889     }
0890 
0891     /* as long as data have to be received */
0892     while (husart->RxXferCount > 0U)
0893     {
0894       if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
0895       {
0896         /* Wait until TXE flag is set to send dummy byte in order to generate the
0897         * clock for the slave to send data.
0898         * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
0899         * can be written for all the cases. */
0900         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
0901         {
0902           return HAL_TIMEOUT;
0903         }
0904         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
0905       }
0906 
0907       /* Wait for RXNE Flag */
0908       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
0909       {
0910         return HAL_TIMEOUT;
0911       }
0912 
0913       if (prxdata8bits == NULL)
0914       {
0915         *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
0916         prxdata16bits++;
0917       }
0918       else
0919       {
0920         *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
0921         prxdata8bits++;
0922       }
0923 
0924       husart->RxXferCount--;
0925 
0926     }
0927 
0928     /* Clear SPI slave underrun flag and discard transmit data */
0929     if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
0930     {
0931       __HAL_USART_CLEAR_UDRFLAG(husart);
0932       __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
0933     }
0934 
0935     /* At end of Rx process, restore husart->State to Ready */
0936     husart->State = HAL_USART_STATE_READY;
0937 
0938     /* Process Unlocked */
0939     __HAL_UNLOCK(husart);
0940 
0941     return HAL_OK;
0942   }
0943   else
0944   {
0945     return HAL_BUSY;
0946   }
0947 }
0948 
0949 /**
0950   * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
0951   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
0952   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
0953   *         of u16 available through pTxData and through pRxData.
0954   * @param  husart USART handle.
0955   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
0956   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
0957   * @param  Size amount of data elements (u8 or u16) to be sent (same amount to be received).
0958   * @param  Timeout Timeout duration.
0959   * @retval HAL status
0960   */
0961 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
0962                                             uint16_t Size, uint32_t Timeout)
0963 {
0964   uint8_t  *prxdata8bits;
0965   uint16_t *prxdata16bits;
0966   const uint8_t  *ptxdata8bits;
0967   const uint16_t *ptxdata16bits;
0968   uint16_t uhMask;
0969   uint16_t rxdatacount;
0970   uint32_t tickstart;
0971 
0972   if (husart->State == HAL_USART_STATE_READY)
0973   {
0974     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
0975     {
0976       return  HAL_ERROR;
0977     }
0978 
0979     /* Process Locked */
0980     __HAL_LOCK(husart);
0981 
0982     husart->ErrorCode = HAL_USART_ERROR_NONE;
0983     husart->State = HAL_USART_STATE_BUSY_RX;
0984 
0985     /* Init tickstart for timeout management */
0986     tickstart = HAL_GetTick();
0987 
0988     husart->RxXferSize = Size;
0989     husart->TxXferSize = Size;
0990     husart->TxXferCount = Size;
0991     husart->RxXferCount = Size;
0992 
0993     /* Computation of USART mask to apply to RDR register */
0994     USART_MASK_COMPUTATION(husart);
0995     uhMask = husart->Mask;
0996 
0997     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
0998     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
0999     {
1000       prxdata8bits  = NULL;
1001       ptxdata8bits  = NULL;
1002       ptxdata16bits = (const uint16_t *) pTxData;
1003       prxdata16bits = (uint16_t *) pRxData;
1004     }
1005     else
1006     {
1007       prxdata8bits  = pRxData;
1008       ptxdata8bits  = pTxData;
1009       ptxdata16bits = NULL;
1010       prxdata16bits = NULL;
1011     }
1012 
1013     if ((husart->TxXferCount == 0x01U) || (husart->SlaveMode == USART_SLAVEMODE_ENABLE))
1014     {
1015       /* Wait until TXE flag is set to send data */
1016       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1017       {
1018         return HAL_TIMEOUT;
1019       }
1020       if (ptxdata8bits == NULL)
1021       {
1022         husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
1023         ptxdata16bits++;
1024       }
1025       else
1026       {
1027         husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
1028         ptxdata8bits++;
1029       }
1030 
1031       husart->TxXferCount--;
1032     }
1033 
1034     /* Check the remain data to be sent */
1035     /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
1036     rxdatacount = husart->RxXferCount;
1037     while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
1038     {
1039       if (husart->TxXferCount > 0U)
1040       {
1041         /* Wait until TXE flag is set to send data */
1042         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1043         {
1044           return HAL_TIMEOUT;
1045         }
1046         if (ptxdata8bits == NULL)
1047         {
1048           husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
1049           ptxdata16bits++;
1050         }
1051         else
1052         {
1053           husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
1054           ptxdata8bits++;
1055         }
1056 
1057         husart->TxXferCount--;
1058       }
1059 
1060       if (husart->RxXferCount > 0U)
1061       {
1062         /* Wait for RXNE Flag */
1063         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1064         {
1065           return HAL_TIMEOUT;
1066         }
1067 
1068         if (prxdata8bits == NULL)
1069         {
1070           *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
1071           prxdata16bits++;
1072         }
1073         else
1074         {
1075           *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
1076           prxdata8bits++;
1077         }
1078 
1079         husart->RxXferCount--;
1080       }
1081       rxdatacount = husart->RxXferCount;
1082     }
1083 
1084     /* At end of TxRx process, restore husart->State to Ready */
1085     husart->State = HAL_USART_STATE_READY;
1086 
1087     /* Process Unlocked */
1088     __HAL_UNLOCK(husart);
1089 
1090     return HAL_OK;
1091   }
1092   else
1093   {
1094     return HAL_BUSY;
1095   }
1096 }
1097 
1098 /**
1099   * @brief  Send an amount of data in interrupt mode.
1100   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1101   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1102   *         of u16 provided through pTxData.
1103   * @param  husart USART handle.
1104   * @param  pTxData pointer to data buffer (u8 or u16 data elements).
1105   * @param  Size amount of data elements (u8 or u16) to be sent.
1106   * @retval HAL status
1107   */
1108 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
1109 {
1110   if (husart->State == HAL_USART_STATE_READY)
1111   {
1112     if ((pTxData == NULL) || (Size == 0U))
1113     {
1114       return HAL_ERROR;
1115     }
1116 
1117     /* Process Locked */
1118     __HAL_LOCK(husart);
1119 
1120     husart->pTxBuffPtr  = pTxData;
1121     husart->TxXferSize  = Size;
1122     husart->TxXferCount = Size;
1123     husart->TxISR       = NULL;
1124 
1125     husart->ErrorCode = HAL_USART_ERROR_NONE;
1126     husart->State     = HAL_USART_STATE_BUSY_TX;
1127 
1128     /* The USART Error Interrupts: (Frame error, noise error, overrun error)
1129     are not managed by the USART Transmit Process to avoid the overrun interrupt
1130     when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
1131     to benefit for the frame error and noise interrupts the usart mode should be
1132     configured only for transmit "USART_MODE_TX" */
1133 
1134     /* Configure Tx interrupt processing */
1135     if (husart->FifoMode == USART_FIFOMODE_ENABLE)
1136     {
1137       /* Set the Tx ISR function pointer according to the data word length */
1138       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1139       {
1140         husart->TxISR = USART_TxISR_16BIT_FIFOEN;
1141       }
1142       else
1143       {
1144         husart->TxISR = USART_TxISR_8BIT_FIFOEN;
1145       }
1146 
1147       /* Process Unlocked */
1148       __HAL_UNLOCK(husart);
1149 
1150       /* Enable the TX FIFO threshold interrupt */
1151       __HAL_USART_ENABLE_IT(husart, USART_IT_TXFT);
1152     }
1153     else
1154     {
1155       /* Set the Tx ISR function pointer according to the data word length */
1156       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1157       {
1158         husart->TxISR = USART_TxISR_16BIT;
1159       }
1160       else
1161       {
1162         husart->TxISR = USART_TxISR_8BIT;
1163       }
1164 
1165       /* Process Unlocked */
1166       __HAL_UNLOCK(husart);
1167 
1168       /* Enable the USART Transmit Data Register Empty Interrupt */
1169       __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
1170     }
1171 
1172     return HAL_OK;
1173   }
1174   else
1175   {
1176     return HAL_BUSY;
1177   }
1178 }
1179 
1180 /**
1181   * @brief Receive an amount of data in interrupt mode.
1182   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
1183   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1184   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1185   *         of u16 available through pRxData.
1186   * @param  husart USART handle.
1187   * @param  pRxData pointer to data buffer (u8 or u16 data elements).
1188   * @param  Size amount of data elements (u8 or u16) to be received.
1189   * @retval HAL status
1190   */
1191 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1192 {
1193   uint16_t nb_dummy_data;
1194 
1195   if (husart->State == HAL_USART_STATE_READY)
1196   {
1197     if ((pRxData == NULL) || (Size == 0U))
1198     {
1199       return HAL_ERROR;
1200     }
1201 
1202     /* Process Locked */
1203     __HAL_LOCK(husart);
1204 
1205     husart->pRxBuffPtr  = pRxData;
1206     husart->RxXferSize  = Size;
1207     husart->RxXferCount = Size;
1208     husart->RxISR       = NULL;
1209 
1210     USART_MASK_COMPUTATION(husart);
1211 
1212     husart->ErrorCode = HAL_USART_ERROR_NONE;
1213     husart->State = HAL_USART_STATE_BUSY_RX;
1214 
1215     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1216     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1217 
1218     /* Configure Rx interrupt processing */
1219     if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1220     {
1221       /* Set the Rx ISR function pointer according to the data word length */
1222       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1223       {
1224         husart->RxISR = USART_RxISR_16BIT_FIFOEN;
1225       }
1226       else
1227       {
1228         husart->RxISR = USART_RxISR_8BIT_FIFOEN;
1229       }
1230 
1231       /* Process Unlocked */
1232       __HAL_UNLOCK(husart);
1233 
1234       /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */
1235       if (husart->Init.Parity != USART_PARITY_NONE)
1236       {
1237         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1238       }
1239       SET_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
1240     }
1241     else
1242     {
1243       /* Set the Rx ISR function pointer according to the data word length */
1244       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1245       {
1246         husart->RxISR = USART_RxISR_16BIT;
1247       }
1248       else
1249       {
1250         husart->RxISR = USART_RxISR_8BIT;
1251       }
1252 
1253       /* Process Unlocked */
1254       __HAL_UNLOCK(husart);
1255 
1256       /* Enable the USART Parity Error and Data Register not empty Interrupts */
1257       if (husart->Init.Parity != USART_PARITY_NONE)
1258       {
1259         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1260       }
1261       else
1262       {
1263         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
1264       }
1265     }
1266 
1267     if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
1268     {
1269       /* Send dummy data in order to generate the clock for the Slave to send the next data.
1270          When FIFO mode is disabled only one data must be transferred.
1271          When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold.
1272       */
1273       if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1274       {
1275         for (nb_dummy_data = husart->NbRxDataToProcess ; nb_dummy_data > 0U ; nb_dummy_data--)
1276         {
1277           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
1278         }
1279       }
1280       else
1281       {
1282         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
1283       }
1284     }
1285 
1286     return HAL_OK;
1287   }
1288   else
1289   {
1290     return HAL_BUSY;
1291   }
1292 }
1293 
1294 /**
1295   * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
1296   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1297   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1298   *         of u16 available through pTxData and through pRxData.
1299   * @param  husart USART handle.
1300   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
1301   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
1302   * @param  Size amount of data elements (u8 or u16) to be sent (same amount to be received).
1303   * @retval HAL status
1304   */
1305 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
1306                                                uint16_t Size)
1307 {
1308 
1309   if (husart->State == HAL_USART_STATE_READY)
1310   {
1311     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1312     {
1313       return HAL_ERROR;
1314     }
1315 
1316     /* Process Locked */
1317     __HAL_LOCK(husart);
1318 
1319     husart->pRxBuffPtr = pRxData;
1320     husart->RxXferSize = Size;
1321     husart->RxXferCount = Size;
1322     husart->pTxBuffPtr = pTxData;
1323     husart->TxXferSize = Size;
1324     husart->TxXferCount = Size;
1325 
1326     /* Computation of USART mask to apply to RDR register */
1327     USART_MASK_COMPUTATION(husart);
1328 
1329     husart->ErrorCode = HAL_USART_ERROR_NONE;
1330     husart->State = HAL_USART_STATE_BUSY_TX_RX;
1331 
1332     /* Configure TxRx interrupt processing */
1333     if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
1334     {
1335       /* Set the Rx ISR function pointer according to the data word length */
1336       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1337       {
1338         husart->TxISR = USART_TxISR_16BIT_FIFOEN;
1339         husart->RxISR = USART_RxISR_16BIT_FIFOEN;
1340       }
1341       else
1342       {
1343         husart->TxISR = USART_TxISR_8BIT_FIFOEN;
1344         husart->RxISR = USART_RxISR_8BIT_FIFOEN;
1345       }
1346 
1347       /* Process Locked */
1348       __HAL_UNLOCK(husart);
1349 
1350       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1351       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1352 
1353       if (husart->Init.Parity != USART_PARITY_NONE)
1354       {
1355         /* Enable the USART Parity Error interrupt  */
1356         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1357       }
1358 
1359       /* Enable the TX and  RX FIFO Threshold interrupts */
1360       SET_BIT(husart->Instance->CR3, (USART_CR3_TXFTIE | USART_CR3_RXFTIE));
1361     }
1362     else
1363     {
1364       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
1365       {
1366         husart->TxISR = USART_TxISR_16BIT;
1367         husart->RxISR = USART_RxISR_16BIT;
1368       }
1369       else
1370       {
1371         husart->TxISR = USART_TxISR_8BIT;
1372         husart->RxISR = USART_RxISR_8BIT;
1373       }
1374 
1375       /* Process Locked */
1376       __HAL_UNLOCK(husart);
1377 
1378       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1379       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1380 
1381       /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
1382       if (husart->Init.Parity != USART_PARITY_NONE)
1383       {
1384         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
1385       }
1386       else
1387       {
1388         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
1389       }
1390 
1391       /* Enable the USART Transmit Data Register Empty Interrupt */
1392       SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1393     }
1394 
1395     return HAL_OK;
1396   }
1397   else
1398   {
1399     return HAL_BUSY;
1400   }
1401 }
1402 
1403 /**
1404   * @brief Send an amount of data in DMA mode.
1405   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1406   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1407   *         of u16 provided through pTxData.
1408   * @param  husart USART handle.
1409   * @param  pTxData pointer to data buffer (u8 or u16 data elements).
1410   * @param  Size amount of data elements (u8 or u16) to be sent.
1411   * @retval HAL status
1412   */
1413 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
1414 {
1415   HAL_StatusTypeDef status = HAL_OK;
1416   const uint32_t *tmp;
1417 
1418   if (husart->State == HAL_USART_STATE_READY)
1419   {
1420     if ((pTxData == NULL) || (Size == 0U))
1421     {
1422       return HAL_ERROR;
1423     }
1424 
1425     /* Process Locked */
1426     __HAL_LOCK(husart);
1427 
1428     husart->pTxBuffPtr = pTxData;
1429     husart->TxXferSize = Size;
1430     husart->TxXferCount = Size;
1431 
1432     husart->ErrorCode = HAL_USART_ERROR_NONE;
1433     husart->State = HAL_USART_STATE_BUSY_TX;
1434 
1435     if (husart->hdmatx != NULL)
1436     {
1437       /* Set the USART DMA transfer complete callback */
1438       husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1439 
1440       /* Set the USART DMA Half transfer complete callback */
1441       husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1442 
1443       /* Set the DMA error callback */
1444       husart->hdmatx->XferErrorCallback = USART_DMAError;
1445 
1446       /* Enable the USART transmit DMA channel */
1447       tmp = (const uint32_t *)&pTxData;
1448       status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1449     }
1450 
1451     if (status == HAL_OK)
1452     {
1453       /* Clear the TC flag in the ICR register */
1454       __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
1455 
1456       /* Process Unlocked */
1457       __HAL_UNLOCK(husart);
1458 
1459       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1460          in the USART CR3 register */
1461       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1462 
1463       return HAL_OK;
1464     }
1465     else
1466     {
1467       /* Set error code to DMA */
1468       husart->ErrorCode = HAL_USART_ERROR_DMA;
1469 
1470       /* Process Unlocked */
1471       __HAL_UNLOCK(husart);
1472 
1473       /* Restore husart->State to ready */
1474       husart->State = HAL_USART_STATE_READY;
1475 
1476       return HAL_ERROR;
1477     }
1478   }
1479   else
1480   {
1481     return HAL_BUSY;
1482   }
1483 }
1484 
1485 /**
1486   * @brief Receive an amount of data in DMA mode.
1487   * @note   When the USART parity is enabled (PCE = 1), the received data contain
1488   *         the parity bit (MSB position).
1489   * @note   The USART DMA transmit channel must be configured in order to generate the clock for the slave.
1490   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1491   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1492   *         of u16 available through pRxData.
1493   * @param  husart USART handle.
1494   * @param  pRxData pointer to data buffer (u8 or u16 data elements).
1495   * @param  Size amount of data elements (u8 or u16) to be received.
1496   * @retval HAL status
1497   */
1498 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
1499 {
1500   HAL_StatusTypeDef status = HAL_OK;
1501   uint32_t *tmp = (uint32_t *)&pRxData;
1502 
1503   /* Check that a Rx process is not already ongoing */
1504   if (husart->State == HAL_USART_STATE_READY)
1505   {
1506     if ((pRxData == NULL) || (Size == 0U))
1507     {
1508       return HAL_ERROR;
1509     }
1510 
1511     /* Process Locked */
1512     __HAL_LOCK(husart);
1513 
1514     husart->pRxBuffPtr = pRxData;
1515     husart->RxXferSize = Size;
1516     husart->pTxBuffPtr = pRxData;
1517     husart->TxXferSize = Size;
1518 
1519     husart->ErrorCode = HAL_USART_ERROR_NONE;
1520     husart->State = HAL_USART_STATE_BUSY_RX;
1521 
1522     if (husart->hdmarx != NULL)
1523     {
1524       /* Set the USART DMA Rx transfer complete callback */
1525       husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1526 
1527       /* Set the USART DMA Half transfer complete callback */
1528       husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1529 
1530       /* Set the USART DMA Rx transfer error callback */
1531       husart->hdmarx->XferErrorCallback = USART_DMAError;
1532 
1533       /* Enable the USART receive DMA channel */
1534       status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size);
1535     }
1536 
1537     if ((status == HAL_OK) &&
1538         (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
1539     {
1540       /* Enable the USART transmit DMA channel: the transmit channel is used in order
1541          to generate in the non-blocking mode the clock to the slave device,
1542          this mode isn't a simplex receive mode but a full-duplex receive mode */
1543 
1544       /* Set the USART DMA Tx Complete and Error callback to Null */
1545       if (husart->hdmatx != NULL)
1546       {
1547         husart->hdmatx->XferErrorCallback = NULL;
1548         husart->hdmatx->XferHalfCpltCallback = NULL;
1549         husart->hdmatx->XferCpltCallback = NULL;
1550         status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1551       }
1552     }
1553 
1554     if (status == HAL_OK)
1555     {
1556       /* Process Unlocked */
1557       __HAL_UNLOCK(husart);
1558 
1559       if (husart->Init.Parity != USART_PARITY_NONE)
1560       {
1561         /* Enable the USART Parity Error Interrupt */
1562         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1563       }
1564 
1565       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1566       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1567 
1568       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1569          in the USART CR3 register */
1570       SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1571 
1572       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1573          in the USART CR3 register */
1574       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1575 
1576       return HAL_OK;
1577     }
1578     else
1579     {
1580       if (husart->hdmarx != NULL)
1581       {
1582         status = HAL_DMA_Abort(husart->hdmarx);
1583       }
1584 
1585       /* No need to check on error code */
1586       UNUSED(status);
1587 
1588       /* Set error code to DMA */
1589       husart->ErrorCode = HAL_USART_ERROR_DMA;
1590 
1591       /* Process Unlocked */
1592       __HAL_UNLOCK(husart);
1593 
1594       /* Restore husart->State to ready */
1595       husart->State = HAL_USART_STATE_READY;
1596 
1597       return HAL_ERROR;
1598     }
1599   }
1600   else
1601   {
1602     return HAL_BUSY;
1603   }
1604 }
1605 
1606 /**
1607   * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
1608   * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
1609   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1610   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
1611   *         of u16 available through pTxData and through pRxData.
1612   * @param  husart USART handle.
1613   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
1614   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
1615   * @param  Size amount of data elements (u8 or u16) to be received/sent.
1616   * @retval HAL status
1617   */
1618 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
1619                                                 uint16_t Size)
1620 {
1621   HAL_StatusTypeDef status;
1622   const uint32_t *tmp;
1623 
1624   if (husart->State == HAL_USART_STATE_READY)
1625   {
1626     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
1627     {
1628       return HAL_ERROR;
1629     }
1630 
1631     /* Process Locked */
1632     __HAL_LOCK(husart);
1633 
1634     husart->pRxBuffPtr = pRxData;
1635     husart->RxXferSize = Size;
1636     husart->pTxBuffPtr = pTxData;
1637     husart->TxXferSize = Size;
1638 
1639     husart->ErrorCode = HAL_USART_ERROR_NONE;
1640     husart->State = HAL_USART_STATE_BUSY_TX_RX;
1641 
1642     if ((husart->hdmarx != NULL) && (husart->hdmatx != NULL))
1643     {
1644       /* Set the USART DMA Rx transfer complete callback */
1645       husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
1646 
1647       /* Set the USART DMA Half transfer complete callback */
1648       husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
1649 
1650       /* Set the USART DMA Tx transfer complete callback */
1651       husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
1652 
1653       /* Set the USART DMA Half transfer complete callback */
1654       husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
1655 
1656       /* Set the USART DMA Tx transfer error callback */
1657       husart->hdmatx->XferErrorCallback = USART_DMAError;
1658 
1659       /* Set the USART DMA Rx transfer error callback */
1660       husart->hdmarx->XferErrorCallback = USART_DMAError;
1661 
1662       /* Enable the USART receive DMA channel */
1663       tmp = (uint32_t *)&pRxData;
1664       status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(const uint32_t *)tmp, Size);
1665 
1666       /* Enable the USART transmit DMA channel */
1667       if (status == HAL_OK)
1668       {
1669         tmp = (const uint32_t *)&pTxData;
1670         status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
1671       }
1672     }
1673     else
1674     {
1675       status = HAL_ERROR;
1676     }
1677 
1678     if (status == HAL_OK)
1679     {
1680       /* Process Unlocked */
1681       __HAL_UNLOCK(husart);
1682 
1683       if (husart->Init.Parity != USART_PARITY_NONE)
1684       {
1685         /* Enable the USART Parity Error Interrupt */
1686         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1687       }
1688 
1689       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
1690       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1691 
1692       /* Clear the TC flag in the ICR register */
1693       __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
1694 
1695       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
1696          in the USART CR3 register */
1697       SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1698 
1699       /* Enable the DMA transfer for transmit request by setting the DMAT bit
1700          in the USART CR3 register */
1701       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1702 
1703       return HAL_OK;
1704     }
1705     else
1706     {
1707       if (husart->hdmarx != NULL)
1708       {
1709         status = HAL_DMA_Abort(husart->hdmarx);
1710       }
1711 
1712       /* No need to check on error code */
1713       UNUSED(status);
1714 
1715       /* Set error code to DMA */
1716       husart->ErrorCode = HAL_USART_ERROR_DMA;
1717 
1718       /* Process Unlocked */
1719       __HAL_UNLOCK(husart);
1720 
1721       /* Restore husart->State to ready */
1722       husart->State = HAL_USART_STATE_READY;
1723 
1724       return HAL_ERROR;
1725     }
1726   }
1727   else
1728   {
1729     return HAL_BUSY;
1730   }
1731 }
1732 
1733 /**
1734   * @brief Pause the DMA Transfer.
1735   * @param  husart USART handle.
1736   * @retval HAL status
1737   */
1738 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
1739 {
1740   const HAL_USART_StateTypeDef state = husart->State;
1741 
1742   /* Process Locked */
1743   __HAL_LOCK(husart);
1744 
1745   if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) &&
1746       (state == HAL_USART_STATE_BUSY_TX))
1747   {
1748     /* Disable the USART DMA Tx request */
1749     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1750   }
1751   else if ((state == HAL_USART_STATE_BUSY_RX) ||
1752            (state == HAL_USART_STATE_BUSY_TX_RX))
1753   {
1754     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1755     {
1756       /* Disable the USART DMA Tx request */
1757       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1758     }
1759     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1760     {
1761       /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1762       CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1763       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
1764 
1765       /* Disable the USART DMA Rx request */
1766       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1767     }
1768   }
1769   else
1770   {
1771     /* Nothing to do */
1772   }
1773 
1774   /* Process Unlocked */
1775   __HAL_UNLOCK(husart);
1776 
1777   return HAL_OK;
1778 }
1779 
1780 /**
1781   * @brief Resume the DMA Transfer.
1782   * @param  husart USART handle.
1783   * @retval HAL status
1784   */
1785 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
1786 {
1787   const HAL_USART_StateTypeDef state = husart->State;
1788 
1789   /* Process Locked */
1790   __HAL_LOCK(husart);
1791 
1792   if (state == HAL_USART_STATE_BUSY_TX)
1793   {
1794     /* Enable the USART DMA Tx request */
1795     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1796   }
1797   else if ((state == HAL_USART_STATE_BUSY_RX) ||
1798            (state == HAL_USART_STATE_BUSY_TX_RX))
1799   {
1800     /* Clear the Overrun flag before resuming the Rx transfer*/
1801     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
1802 
1803     /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1804     if (husart->Init.Parity != USART_PARITY_NONE)
1805     {
1806       SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
1807     }
1808     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
1809 
1810     /* Enable the USART DMA Rx request  before the DMA Tx request */
1811     SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1812 
1813     /* Enable the USART DMA Tx request */
1814     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1815   }
1816   else
1817   {
1818     /* Nothing to do */
1819   }
1820 
1821   /* Process Unlocked */
1822   __HAL_UNLOCK(husart);
1823 
1824   return HAL_OK;
1825 }
1826 
1827 /**
1828   * @brief Stop the DMA Transfer.
1829   * @param  husart USART handle.
1830   * @retval HAL status
1831   */
1832 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
1833 {
1834   /* The Lock is not implemented on this API to allow the user application
1835      to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
1836      HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
1837      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1838      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1839      the stream and the corresponding call back is executed. */
1840 
1841   /* Disable the USART Tx/Rx DMA requests */
1842   CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1843   CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1844 
1845   /* Abort the USART DMA tx channel */
1846   if (husart->hdmatx != NULL)
1847   {
1848     if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
1849     {
1850       if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1851       {
1852         /* Set error code to DMA */
1853         husart->ErrorCode = HAL_USART_ERROR_DMA;
1854 
1855         return HAL_TIMEOUT;
1856       }
1857     }
1858   }
1859   /* Abort the USART DMA rx channel */
1860   if (husart->hdmarx != NULL)
1861   {
1862     if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
1863     {
1864       if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1865       {
1866         /* Set error code to DMA */
1867         husart->ErrorCode = HAL_USART_ERROR_DMA;
1868 
1869         return HAL_TIMEOUT;
1870       }
1871     }
1872   }
1873 
1874   USART_EndTransfer(husart);
1875   husart->State = HAL_USART_STATE_READY;
1876 
1877   return HAL_OK;
1878 }
1879 
1880 /**
1881   * @brief  Abort ongoing transfers (blocking mode).
1882   * @param  husart USART handle.
1883   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1884   *         This procedure performs following operations :
1885   *           - Disable USART Interrupts (Tx and Rx)
1886   *           - Disable the DMA transfer in the peripheral register (if enabled)
1887   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1888   *           - Set handle State to READY
1889   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1890   * @retval HAL status
1891   */
1892 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
1893 {
1894   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1895   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
1896                                     USART_CR1_TCIE));
1897   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1898 
1899   /* Abort the USART DMA Tx channel if enabled */
1900   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
1901   {
1902     /* Disable the USART DMA Tx request if enabled */
1903     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
1904 
1905     /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
1906     if (husart->hdmatx != NULL)
1907     {
1908       /* Set the USART DMA Abort callback to Null.
1909          No call back execution at end of DMA abort procedure */
1910       husart->hdmatx->XferAbortCallback = NULL;
1911 
1912       if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
1913       {
1914         if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1915         {
1916           /* Set error code to DMA */
1917           husart->ErrorCode = HAL_USART_ERROR_DMA;
1918 
1919           return HAL_TIMEOUT;
1920         }
1921       }
1922     }
1923   }
1924 
1925   /* Abort the USART DMA Rx channel if enabled */
1926   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
1927   {
1928     /* Disable the USART DMA Rx request if enabled */
1929     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
1930 
1931     /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
1932     if (husart->hdmarx != NULL)
1933     {
1934       /* Set the USART DMA Abort callback to Null.
1935          No call back execution at end of DMA abort procedure */
1936       husart->hdmarx->XferAbortCallback = NULL;
1937 
1938       if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
1939       {
1940         if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1941         {
1942           /* Set error code to DMA */
1943           husart->ErrorCode = HAL_USART_ERROR_DMA;
1944 
1945           return HAL_TIMEOUT;
1946         }
1947       }
1948     }
1949   }
1950 
1951   /* Reset Tx and Rx transfer counters */
1952   husart->TxXferCount = 0U;
1953   husart->RxXferCount = 0U;
1954 
1955   /* Clear the Error flags in the ICR register */
1956   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
1957 
1958   /* Flush the whole TX FIFO (if needed) */
1959   if (husart->FifoMode == USART_FIFOMODE_ENABLE)
1960   {
1961     __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
1962   }
1963 
1964   /* Discard the received data */
1965   __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
1966 
1967   /* Restore husart->State to Ready */
1968   husart->State  = HAL_USART_STATE_READY;
1969 
1970   /* Reset Handle ErrorCode to No Error */
1971   husart->ErrorCode = HAL_USART_ERROR_NONE;
1972 
1973   return HAL_OK;
1974 }
1975 
1976 /**
1977   * @brief  Abort ongoing transfers (Interrupt mode).
1978   * @param  husart USART handle.
1979   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1980   *         This procedure performs following operations :
1981   *           - Disable USART Interrupts (Tx and Rx)
1982   *           - Disable the DMA transfer in the peripheral register (if enabled)
1983   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1984   *           - Set handle State to READY
1985   *           - At abort completion, call user abort complete callback
1986   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1987   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1988   * @retval HAL status
1989   */
1990 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
1991 {
1992   uint32_t abortcplt = 1U;
1993 
1994   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
1995   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
1996                                     USART_CR1_TCIE));
1997   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1998 
1999   /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
2000      before any call to DMA Abort functions */
2001   /* DMA Tx Handle is valid */
2002   if (husart->hdmatx != NULL)
2003   {
2004     /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
2005        Otherwise, set it to NULL */
2006     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
2007     {
2008       husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
2009     }
2010     else
2011     {
2012       husart->hdmatx->XferAbortCallback = NULL;
2013     }
2014   }
2015   /* DMA Rx Handle is valid */
2016   if (husart->hdmarx != NULL)
2017   {
2018     /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
2019        Otherwise, set it to NULL */
2020     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
2021     {
2022       husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
2023     }
2024     else
2025     {
2026       husart->hdmarx->XferAbortCallback = NULL;
2027     }
2028   }
2029 
2030   /* Abort the USART DMA Tx channel if enabled */
2031   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
2032   {
2033     /* Disable DMA Tx at USART level */
2034     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2035 
2036     /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
2037     if (husart->hdmatx != NULL)
2038     {
2039       /* USART Tx DMA Abort callback has already been initialised :
2040          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
2041 
2042       /* Abort DMA TX */
2043       if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
2044       {
2045         husart->hdmatx->XferAbortCallback = NULL;
2046       }
2047       else
2048       {
2049         abortcplt = 0U;
2050       }
2051     }
2052   }
2053 
2054   /* Abort the USART DMA Rx channel if enabled */
2055   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
2056   {
2057     /* Disable the USART DMA Rx request if enabled */
2058     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
2059 
2060     /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
2061     if (husart->hdmarx != NULL)
2062     {
2063       /* USART Rx DMA Abort callback has already been initialised :
2064          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
2065 
2066       /* Abort DMA RX */
2067       if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
2068       {
2069         husart->hdmarx->XferAbortCallback = NULL;
2070         abortcplt = 1U;
2071       }
2072       else
2073       {
2074         abortcplt = 0U;
2075       }
2076     }
2077   }
2078 
2079   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
2080   if (abortcplt == 1U)
2081   {
2082     /* Reset Tx and Rx transfer counters */
2083     husart->TxXferCount = 0U;
2084     husart->RxXferCount = 0U;
2085 
2086     /* Reset errorCode */
2087     husart->ErrorCode = HAL_USART_ERROR_NONE;
2088 
2089     /* Clear the Error flags in the ICR register */
2090     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2091 
2092     /* Flush the whole TX FIFO (if needed) */
2093     if (husart->FifoMode == USART_FIFOMODE_ENABLE)
2094     {
2095       __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
2096     }
2097 
2098     /* Discard the received data */
2099     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
2100 
2101     /* Restore husart->State to Ready */
2102     husart->State  = HAL_USART_STATE_READY;
2103 
2104     /* As no DMA to be aborted, call directly user Abort complete callback */
2105 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2106     /* Call registered Abort Complete Callback */
2107     husart->AbortCpltCallback(husart);
2108 #else
2109     /* Call legacy weak Abort Complete Callback */
2110     HAL_USART_AbortCpltCallback(husart);
2111 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2112   }
2113 
2114   return HAL_OK;
2115 }
2116 
2117 /**
2118   * @brief  Handle USART interrupt request.
2119   * @param  husart USART handle.
2120   * @retval None
2121   */
2122 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
2123 {
2124   uint32_t isrflags   = READ_REG(husart->Instance->ISR);
2125   uint32_t cr1its     = READ_REG(husart->Instance->CR1);
2126   uint32_t cr3its     = READ_REG(husart->Instance->CR3);
2127 
2128   uint32_t errorflags;
2129   uint32_t errorcode;
2130 
2131   /* If no error occurs */
2132   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF |
2133                                       USART_ISR_UDR));
2134   if (errorflags == 0U)
2135   {
2136     /* USART in mode Receiver ---------------------------------------------------*/
2137     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2138         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2139             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2140     {
2141       if (husart->RxISR != NULL)
2142       {
2143         husart->RxISR(husart);
2144       }
2145       return;
2146     }
2147   }
2148 
2149   /* If some errors occur */
2150   if ((errorflags != 0U)
2151       && (((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2152           || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))
2153   {
2154     /* USART parity error interrupt occurred -------------------------------------*/
2155     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2156     {
2157       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
2158 
2159       husart->ErrorCode |= HAL_USART_ERROR_PE;
2160     }
2161 
2162     /* USART frame error interrupt occurred --------------------------------------*/
2163     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2164     {
2165       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
2166 
2167       husart->ErrorCode |= HAL_USART_ERROR_FE;
2168     }
2169 
2170     /* USART noise error interrupt occurred --------------------------------------*/
2171     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2172     {
2173       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
2174 
2175       husart->ErrorCode |= HAL_USART_ERROR_NE;
2176     }
2177 
2178     /* USART Over-Run interrupt occurred -----------------------------------------*/
2179     if (((isrflags & USART_ISR_ORE) != 0U)
2180         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2181             ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2182     {
2183       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
2184 
2185       husart->ErrorCode |= HAL_USART_ERROR_ORE;
2186     }
2187 
2188     /* USART Receiver Timeout interrupt occurred ---------------------------------*/
2189     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
2190     {
2191       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_RTOF);
2192 
2193       husart->ErrorCode |= HAL_USART_ERROR_RTO;
2194     }
2195 
2196     /* USART SPI slave underrun error interrupt occurred -------------------------*/
2197     if (((isrflags & USART_ISR_UDR) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2198     {
2199       /* Ignore SPI slave underrun errors when reception is going on */
2200       if (husart->State == HAL_USART_STATE_BUSY_RX)
2201       {
2202         __HAL_USART_CLEAR_UDRFLAG(husart);
2203         return;
2204       }
2205       else
2206       {
2207         __HAL_USART_CLEAR_UDRFLAG(husart);
2208         husart->ErrorCode |= HAL_USART_ERROR_UDR;
2209       }
2210     }
2211 
2212     /* Call USART Error Call back function if need be --------------------------*/
2213     if (husart->ErrorCode != HAL_USART_ERROR_NONE)
2214     {
2215       /* USART in mode Receiver ---------------------------------------------------*/
2216       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2217           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2218               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2219       {
2220         if (husart->RxISR != NULL)
2221         {
2222           husart->RxISR(husart);
2223         }
2224       }
2225 
2226       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
2227          consider error as blocking */
2228       errorcode = husart->ErrorCode & HAL_USART_ERROR_ORE;
2229       if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) ||
2230           (errorcode != 0U))
2231       {
2232         /* Blocking error : transfer is aborted
2233            Set the USART state ready to be able to start again the process,
2234            Disable Interrupts, and disable DMA requests, if ongoing */
2235         USART_EndTransfer(husart);
2236 
2237         /* Abort the USART DMA Rx channel if enabled */
2238         if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
2239         {
2240           /* Disable the USART DMA Rx request if enabled */
2241           CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR);
2242 
2243           /* Abort the USART DMA Tx channel */
2244           if (husart->hdmatx != NULL)
2245           {
2246             /* Set the USART Tx DMA Abort callback to NULL : no callback
2247                executed at end of DMA abort procedure */
2248             husart->hdmatx->XferAbortCallback = NULL;
2249 
2250             /* Abort DMA TX */
2251             (void)HAL_DMA_Abort_IT(husart->hdmatx);
2252           }
2253 
2254           /* Abort the USART DMA Rx channel */
2255           if (husart->hdmarx != NULL)
2256           {
2257             /* Set the USART Rx DMA Abort callback :
2258                will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
2259             husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
2260 
2261             /* Abort DMA RX */
2262             if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
2263             {
2264               /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
2265               husart->hdmarx->XferAbortCallback(husart->hdmarx);
2266             }
2267           }
2268           else
2269           {
2270             /* Call user error callback */
2271 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2272             /* Call registered Error Callback */
2273             husart->ErrorCallback(husart);
2274 #else
2275             /* Call legacy weak Error Callback */
2276             HAL_USART_ErrorCallback(husart);
2277 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2278           }
2279         }
2280         else
2281         {
2282           /* Call user error callback */
2283 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2284           /* Call registered Error Callback */
2285           husart->ErrorCallback(husart);
2286 #else
2287           /* Call legacy weak Error Callback */
2288           HAL_USART_ErrorCallback(husart);
2289 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2290         }
2291       }
2292       else
2293       {
2294         /* Non Blocking error : transfer could go on.
2295            Error is notified to user through user error callback */
2296 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2297         /* Call registered Error Callback */
2298         husart->ErrorCallback(husart);
2299 #else
2300         /* Call legacy weak Error Callback */
2301         HAL_USART_ErrorCallback(husart);
2302 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2303         husart->ErrorCode = HAL_USART_ERROR_NONE;
2304       }
2305     }
2306     return;
2307 
2308   } /* End if some error occurs */
2309 
2310 
2311   /* USART in mode Transmitter ------------------------------------------------*/
2312   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2313       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2314           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2315   {
2316     if (husart->TxISR != NULL)
2317     {
2318       husart->TxISR(husart);
2319     }
2320     return;
2321   }
2322 
2323   /* USART in mode Transmitter (transmission end) -----------------------------*/
2324   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2325   {
2326     USART_EndTransmit_IT(husart);
2327     return;
2328   }
2329 
2330   /* USART TX Fifo Empty occurred ----------------------------------------------*/
2331   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2332   {
2333 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2334     /* Call registered Tx Fifo Empty Callback */
2335     husart->TxFifoEmptyCallback(husart);
2336 #else
2337     /* Call legacy weak Tx Fifo Empty Callback */
2338     HAL_USARTEx_TxFifoEmptyCallback(husart);
2339 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2340     return;
2341   }
2342 
2343   /* USART RX Fifo Full occurred ----------------------------------------------*/
2344   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2345   {
2346 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2347     /* Call registered Rx Fifo Full Callback */
2348     husart->RxFifoFullCallback(husart);
2349 #else
2350     /* Call legacy weak Rx Fifo Full Callback */
2351     HAL_USARTEx_RxFifoFullCallback(husart);
2352 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2353     return;
2354   }
2355 }
2356 
2357 /**
2358   * @brief Tx Transfer completed callback.
2359   * @param husart USART handle.
2360   * @retval None
2361   */
2362 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
2363 {
2364   /* Prevent unused argument(s) compilation warning */
2365   UNUSED(husart);
2366 
2367   /* NOTE : This function should not be modified, when the callback is needed,
2368             the HAL_USART_TxCpltCallback can be implemented in the user file.
2369    */
2370 }
2371 
2372 /**
2373   * @brief  Tx Half Transfer completed callback.
2374   * @param husart USART handle.
2375   * @retval None
2376   */
2377 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
2378 {
2379   /* Prevent unused argument(s) compilation warning */
2380   UNUSED(husart);
2381 
2382   /* NOTE: This function should not be modified, when the callback is needed,
2383            the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
2384    */
2385 }
2386 
2387 /**
2388   * @brief  Rx Transfer completed callback.
2389   * @param husart USART handle.
2390   * @retval None
2391   */
2392 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
2393 {
2394   /* Prevent unused argument(s) compilation warning */
2395   UNUSED(husart);
2396 
2397   /* NOTE: This function should not be modified, when the callback is needed,
2398            the HAL_USART_RxCpltCallback can be implemented in the user file.
2399    */
2400 }
2401 
2402 /**
2403   * @brief Rx Half Transfer completed callback.
2404   * @param husart USART handle.
2405   * @retval None
2406   */
2407 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
2408 {
2409   /* Prevent unused argument(s) compilation warning */
2410   UNUSED(husart);
2411 
2412   /* NOTE : This function should not be modified, when the callback is needed,
2413             the HAL_USART_RxHalfCpltCallback can be implemented in the user file
2414    */
2415 }
2416 
2417 /**
2418   * @brief Tx/Rx Transfers completed callback for the non-blocking process.
2419   * @param husart USART handle.
2420   * @retval None
2421   */
2422 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
2423 {
2424   /* Prevent unused argument(s) compilation warning */
2425   UNUSED(husart);
2426 
2427   /* NOTE : This function should not be modified, when the callback is needed,
2428             the HAL_USART_TxRxCpltCallback can be implemented in the user file
2429    */
2430 }
2431 
2432 /**
2433   * @brief USART error callback.
2434   * @param husart USART handle.
2435   * @retval None
2436   */
2437 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
2438 {
2439   /* Prevent unused argument(s) compilation warning */
2440   UNUSED(husart);
2441 
2442   /* NOTE : This function should not be modified, when the callback is needed,
2443             the HAL_USART_ErrorCallback can be implemented in the user file.
2444    */
2445 }
2446 
2447 /**
2448   * @brief  USART Abort Complete callback.
2449   * @param  husart USART handle.
2450   * @retval None
2451   */
2452 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
2453 {
2454   /* Prevent unused argument(s) compilation warning */
2455   UNUSED(husart);
2456 
2457   /* NOTE : This function should not be modified, when the callback is needed,
2458             the HAL_USART_AbortCpltCallback can be implemented in the user file.
2459    */
2460 }
2461 
2462 /**
2463   * @}
2464   */
2465 
2466 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
2467   * @ingroup RTEMSBSPsARMSTM32H7
2468   *  @brief   USART Peripheral State and Error functions
2469   *
2470 @verbatim
2471   ==============================================================================
2472             ##### Peripheral State and Error functions #####
2473   ==============================================================================
2474     [..]
2475     This subsection provides functions allowing to :
2476       (+) Return the USART handle state
2477       (+) Return the USART handle error code
2478 
2479 @endverbatim
2480   * @{
2481   */
2482 
2483 
2484 /**
2485   * @brief Return the USART handle state.
2486   * @param husart pointer to a USART_HandleTypeDef structure that contains
2487   *              the configuration information for the specified USART.
2488   * @retval USART handle state
2489   */
2490 HAL_USART_StateTypeDef HAL_USART_GetState(const USART_HandleTypeDef *husart)
2491 {
2492   return husart->State;
2493 }
2494 
2495 /**
2496   * @brief Return the USART error code.
2497   * @param husart pointer to a USART_HandleTypeDef structure that contains
2498   *              the configuration information for the specified USART.
2499   * @retval USART handle Error Code
2500   */
2501 uint32_t HAL_USART_GetError(const USART_HandleTypeDef *husart)
2502 {
2503   return husart->ErrorCode;
2504 }
2505 
2506 /**
2507   * @}
2508   */
2509 
2510 /**
2511   * @}
2512   */
2513 
2514 /** @defgroup USART_Private_Functions USART Private Functions
2515   * @ingroup RTEMSBSPsARMSTM32H7
2516   * @{
2517   */
2518 
2519 /**
2520   * @brief  Initialize the callbacks to their default values.
2521   * @param  husart USART handle.
2522   * @retval none
2523   */
2524 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2525 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
2526 {
2527   /* Init the USART Callback settings */
2528   husart->TxHalfCpltCallback        = HAL_USART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
2529   husart->TxCpltCallback            = HAL_USART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
2530   husart->RxHalfCpltCallback        = HAL_USART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
2531   husart->RxCpltCallback            = HAL_USART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
2532   husart->TxRxCpltCallback          = HAL_USART_TxRxCpltCallback;          /* Legacy weak TxRxCpltCallback          */
2533   husart->ErrorCallback             = HAL_USART_ErrorCallback;             /* Legacy weak ErrorCallback             */
2534   husart->AbortCpltCallback         = HAL_USART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
2535   husart->RxFifoFullCallback        = HAL_USARTEx_RxFifoFullCallback;      /* Legacy weak RxFifoFullCallback        */
2536   husart->TxFifoEmptyCallback       = HAL_USARTEx_TxFifoEmptyCallback;     /* Legacy weak TxFifoEmptyCallback       */
2537 }
2538 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2539 
2540 /**
2541   * @brief  End ongoing transfer on USART peripheral (following error detection or Transfer completion).
2542   * @param  husart USART handle.
2543   * @retval None
2544   */
2545 static void USART_EndTransfer(USART_HandleTypeDef *husart)
2546 {
2547   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
2548   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
2549                                     USART_CR1_TCIE));
2550   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
2551 
2552   /* At end of process, restore husart->State to Ready */
2553   husart->State = HAL_USART_STATE_READY;
2554 }
2555 
2556 /**
2557   * @brief DMA USART transmit process complete callback.
2558   * @param  hdma DMA handle.
2559   * @retval None
2560   */
2561 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
2562 {
2563   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2564 
2565   /* DMA Normal mode */
2566   if (hdma->Init.Mode != DMA_CIRCULAR)
2567   {
2568     husart->TxXferCount = 0U;
2569 
2570     if (husart->State == HAL_USART_STATE_BUSY_TX)
2571     {
2572       /* Disable the DMA transfer for transmit request by resetting the DMAT bit
2573          in the USART CR3 register */
2574       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2575 
2576       /* Enable the USART Transmit Complete Interrupt */
2577       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
2578     }
2579   }
2580   /* DMA Circular mode */
2581   else
2582   {
2583     if (husart->State == HAL_USART_STATE_BUSY_TX)
2584     {
2585 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2586       /* Call registered Tx Complete Callback */
2587       husart->TxCpltCallback(husart);
2588 #else
2589       /* Call legacy weak Tx Complete Callback */
2590       HAL_USART_TxCpltCallback(husart);
2591 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2592     }
2593   }
2594 }
2595 
2596 /**
2597   * @brief DMA USART transmit process half complete callback.
2598   * @param  hdma DMA handle.
2599   * @retval None
2600   */
2601 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
2602 {
2603   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2604 
2605 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2606   /* Call registered Tx Half Complete Callback */
2607   husart->TxHalfCpltCallback(husart);
2608 #else
2609   /* Call legacy weak Tx Half Complete Callback */
2610   HAL_USART_TxHalfCpltCallback(husart);
2611 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2612 }
2613 
2614 /**
2615   * @brief DMA USART receive process complete callback.
2616   * @param  hdma DMA handle.
2617   * @retval None
2618   */
2619 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
2620 {
2621   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2622 
2623   /* DMA Normal mode */
2624   if (hdma->Init.Mode != DMA_CIRCULAR)
2625   {
2626     husart->RxXferCount = 0U;
2627 
2628     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2629     CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
2630     CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
2631 
2632     /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
2633        in USART CR3 register */
2634     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
2635     /* similarly, disable the DMA TX transfer that was started to provide the
2636        clock to the slave device */
2637     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
2638 
2639     if (husart->State == HAL_USART_STATE_BUSY_RX)
2640     {
2641 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2642       /* Call registered Rx Complete Callback */
2643       husart->RxCpltCallback(husart);
2644 #else
2645       /* Call legacy weak Rx Complete Callback */
2646       HAL_USART_RxCpltCallback(husart);
2647 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2648     }
2649     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2650     else
2651     {
2652 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2653       /* Call registered Tx Rx Complete Callback */
2654       husart->TxRxCpltCallback(husart);
2655 #else
2656       /* Call legacy weak Tx Rx Complete Callback */
2657       HAL_USART_TxRxCpltCallback(husart);
2658 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2659     }
2660     husart->State = HAL_USART_STATE_READY;
2661   }
2662   /* DMA circular mode */
2663   else
2664   {
2665     if (husart->State == HAL_USART_STATE_BUSY_RX)
2666     {
2667 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2668       /* Call registered Rx Complete Callback */
2669       husart->RxCpltCallback(husart);
2670 #else
2671       /* Call legacy weak Rx Complete Callback */
2672       HAL_USART_RxCpltCallback(husart);
2673 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2674     }
2675     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
2676     else
2677     {
2678 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2679       /* Call registered Tx Rx Complete Callback */
2680       husart->TxRxCpltCallback(husart);
2681 #else
2682       /* Call legacy weak Tx Rx Complete Callback */
2683       HAL_USART_TxRxCpltCallback(husart);
2684 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2685     }
2686   }
2687 }
2688 
2689 /**
2690   * @brief DMA USART receive process half complete callback.
2691   * @param  hdma DMA handle.
2692   * @retval None
2693   */
2694 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
2695 {
2696   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2697 
2698 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2699   /* Call registered Rx Half Complete Callback */
2700   husart->RxHalfCpltCallback(husart);
2701 #else
2702   /* Call legacy weak Rx Half Complete Callback */
2703   HAL_USART_RxHalfCpltCallback(husart);
2704 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2705 }
2706 
2707 /**
2708   * @brief DMA USART communication error callback.
2709   * @param  hdma DMA handle.
2710   * @retval None
2711   */
2712 static void USART_DMAError(DMA_HandleTypeDef *hdma)
2713 {
2714   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2715 
2716   husart->RxXferCount = 0U;
2717   husart->TxXferCount = 0U;
2718   USART_EndTransfer(husart);
2719 
2720   husart->ErrorCode |= HAL_USART_ERROR_DMA;
2721   husart->State = HAL_USART_STATE_READY;
2722 
2723 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2724   /* Call registered Error Callback */
2725   husart->ErrorCallback(husart);
2726 #else
2727   /* Call legacy weak Error Callback */
2728   HAL_USART_ErrorCallback(husart);
2729 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2730 }
2731 
2732 /**
2733   * @brief  DMA USART communication abort callback, when initiated by HAL services on Error
2734   *         (To be called at end of DMA Abort procedure following error occurrence).
2735   * @param  hdma DMA handle.
2736   * @retval None
2737   */
2738 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
2739 {
2740   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2741   husart->RxXferCount = 0U;
2742   husart->TxXferCount = 0U;
2743 
2744 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2745   /* Call registered Error Callback */
2746   husart->ErrorCallback(husart);
2747 #else
2748   /* Call legacy weak Error Callback */
2749   HAL_USART_ErrorCallback(husart);
2750 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2751 }
2752 
2753 /**
2754   * @brief  DMA USART Tx communication abort callback, when initiated by user
2755   *         (To be called at end of DMA Tx Abort procedure following user abort request).
2756   * @note   When this callback is executed, User Abort complete call back is called only if no
2757   *         Abort still ongoing for Rx DMA Handle.
2758   * @param  hdma DMA handle.
2759   * @retval None
2760   */
2761 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
2762 {
2763   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2764 
2765   husart->hdmatx->XferAbortCallback = NULL;
2766 
2767   /* Check if an Abort process is still ongoing */
2768   if (husart->hdmarx != NULL)
2769   {
2770     if (husart->hdmarx->XferAbortCallback != NULL)
2771     {
2772       return;
2773     }
2774   }
2775 
2776   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2777   husart->TxXferCount = 0U;
2778   husart->RxXferCount = 0U;
2779 
2780   /* Reset errorCode */
2781   husart->ErrorCode = HAL_USART_ERROR_NONE;
2782 
2783   /* Clear the Error flags in the ICR register */
2784   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2785 
2786   /* Restore husart->State to Ready */
2787   husart->State = HAL_USART_STATE_READY;
2788 
2789   /* Call user Abort complete callback */
2790 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2791   /* Call registered Abort Complete Callback */
2792   husart->AbortCpltCallback(husart);
2793 #else
2794   /* Call legacy weak Abort Complete Callback */
2795   HAL_USART_AbortCpltCallback(husart);
2796 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2797 
2798 }
2799 
2800 
2801 /**
2802   * @brief  DMA USART Rx communication abort callback, when initiated by user
2803   *         (To be called at end of DMA Rx Abort procedure following user abort request).
2804   * @note   When this callback is executed, User Abort complete call back is called only if no
2805   *         Abort still ongoing for Tx DMA Handle.
2806   * @param  hdma DMA handle.
2807   * @retval None
2808   */
2809 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
2810 {
2811   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
2812 
2813   husart->hdmarx->XferAbortCallback = NULL;
2814 
2815   /* Check if an Abort process is still ongoing */
2816   if (husart->hdmatx != NULL)
2817   {
2818     if (husart->hdmatx->XferAbortCallback != NULL)
2819     {
2820       return;
2821     }
2822   }
2823 
2824   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
2825   husart->TxXferCount = 0U;
2826   husart->RxXferCount = 0U;
2827 
2828   /* Reset errorCode */
2829   husart->ErrorCode = HAL_USART_ERROR_NONE;
2830 
2831   /* Clear the Error flags in the ICR register */
2832   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
2833 
2834   /* Restore husart->State to Ready */
2835   husart->State  = HAL_USART_STATE_READY;
2836 
2837   /* Call user Abort complete callback */
2838 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
2839   /* Call registered Abort Complete Callback */
2840   husart->AbortCpltCallback(husart);
2841 #else
2842   /* Call legacy weak Abort Complete Callback */
2843   HAL_USART_AbortCpltCallback(husart);
2844 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
2845 }
2846 
2847 
2848 /**
2849   * @brief  Handle USART Communication Timeout. It waits
2850   *         until a flag is no longer in the specified status.
2851   * @param  husart USART handle.
2852   * @param  Flag Specifies the USART flag to check.
2853   * @param  Status the actual Flag status (SET or RESET).
2854   * @param  Tickstart Tick start value
2855   * @param  Timeout timeout duration.
2856   * @retval HAL status
2857   */
2858 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
2859                                                       uint32_t Tickstart, uint32_t Timeout)
2860 {
2861   /* Wait until flag is set */
2862   while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
2863   {
2864     /* Check for the Timeout */
2865     if (Timeout != HAL_MAX_DELAY)
2866     {
2867       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
2868       {
2869         husart->State = HAL_USART_STATE_READY;
2870 
2871         /* Process Unlocked */
2872         __HAL_UNLOCK(husart);
2873 
2874         return HAL_TIMEOUT;
2875       }
2876     }
2877   }
2878   return HAL_OK;
2879 }
2880 
2881 /**
2882   * @brief Configure the USART peripheral.
2883   * @param husart USART handle.
2884   * @retval HAL status
2885   */
2886 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
2887 {
2888   uint32_t tmpreg;
2889   USART_ClockSourceTypeDef clocksource;
2890   HAL_StatusTypeDef ret                = HAL_OK;
2891   uint16_t brrtemp;
2892   uint32_t usartdiv                    = 0x00000000;
2893   PLL2_ClocksTypeDef pll2_clocks;
2894   PLL3_ClocksTypeDef pll3_clocks;
2895   uint32_t pclk;
2896 
2897   /* Check the parameters */
2898   assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
2899   assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
2900   assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
2901   assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
2902   assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
2903   assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
2904   assert_param(IS_USART_PARITY(husart->Init.Parity));
2905   assert_param(IS_USART_MODE(husart->Init.Mode));
2906   assert_param(IS_USART_PRESCALER(husart->Init.ClockPrescaler));
2907 
2908   /*-------------------------- USART CR1 Configuration -----------------------*/
2909   /* Clear M, PCE, PS, TE and RE bits and configure
2910   *  the USART Word Length, Parity and Mode:
2911   *  set the M bits according to husart->Init.WordLength value
2912   *  set PCE and PS bits according to husart->Init.Parity value
2913   *  set TE and RE bits according to husart->Init.Mode value
2914   *  force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
2915   tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
2916   MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
2917 
2918   /*---------------------------- USART CR2 Configuration ---------------------*/
2919   /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits:
2920    * set CPOL bit according to husart->Init.CLKPolarity value
2921    * set CPHA bit according to husart->Init.CLKPhase value
2922    * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
2923    * set STOP[13:12] bits according to husart->Init.StopBits value */
2924   tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
2925   tmpreg |= (uint32_t)husart->Init.CLKLastBit;
2926   tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
2927   tmpreg |= (uint32_t)husart->Init.StopBits;
2928   MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
2929 
2930   /*-------------------------- USART PRESC Configuration -----------------------*/
2931   /* Configure
2932    * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */
2933   MODIFY_REG(husart->Instance->PRESC, USART_PRESC_PRESCALER, husart->Init.ClockPrescaler);
2934 
2935   /*-------------------------- USART BRR Configuration -----------------------*/
2936   /* BRR is filled-up according to OVER8 bit setting which is forced to 1     */
2937   USART_GETCLOCKSOURCE(husart, clocksource);
2938 
2939   switch (clocksource)
2940   {
2941     case USART_CLOCKSOURCE_D2PCLK1:
2942       pclk = HAL_RCC_GetPCLK1Freq();
2943       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2944       break;
2945     case USART_CLOCKSOURCE_D2PCLK2:
2946       pclk = HAL_RCC_GetPCLK2Freq();
2947       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2948       break;
2949     case USART_CLOCKSOURCE_PLL2:
2950       HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
2951       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll2_clocks.PLL2_Q_Frequency, husart->Init.BaudRate,
2952                                                 husart->Init.ClockPrescaler));
2953       break;
2954     case USART_CLOCKSOURCE_PLL3:
2955       HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
2956       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pll3_clocks.PLL3_Q_Frequency, husart->Init.BaudRate,
2957                                                 husart->Init.ClockPrescaler));
2958       break;
2959     case USART_CLOCKSOURCE_HSI:
2960       if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
2961       {
2962         usartdiv = (uint32_t)(USART_DIV_SAMPLING8((HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U)),
2963                                                   husart->Init.BaudRate, husart->Init.ClockPrescaler));
2964       }
2965       else
2966       {
2967         usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2968       }
2969       break;
2970     case USART_CLOCKSOURCE_CSI:
2971       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(CSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2972       break;
2973     case USART_CLOCKSOURCE_LSE:
2974       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
2975       break;
2976     default:
2977       ret = HAL_ERROR;
2978       break;
2979   }
2980 
2981   /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */
2982   if ((usartdiv >= USART_BRR_MIN) && (usartdiv <= USART_BRR_MAX))
2983   {
2984     brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
2985     brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
2986     husart->Instance->BRR = brrtemp;
2987   }
2988   else
2989   {
2990     ret = HAL_ERROR;
2991   }
2992 
2993   /* Initialize the number of data to process during RX/TX ISR execution */
2994   husart->NbTxDataToProcess = 1U;
2995   husart->NbRxDataToProcess = 1U;
2996 
2997   /* Clear ISR function pointers */
2998   husart->RxISR   = NULL;
2999   husart->TxISR   = NULL;
3000 
3001   return ret;
3002 }
3003 
3004 /**
3005   * @brief Check the USART Idle State.
3006   * @param husart USART handle.
3007   * @retval HAL status
3008   */
3009 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
3010 {
3011   uint32_t tickstart;
3012 
3013   /* Initialize the USART ErrorCode */
3014   husart->ErrorCode = HAL_USART_ERROR_NONE;
3015 
3016   /* Init tickstart for timeout management */
3017   tickstart = HAL_GetTick();
3018 
3019   /* Check if the Transmitter is enabled */
3020   if ((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
3021   {
3022     /* Wait until TEACK flag is set */
3023     if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
3024     {
3025       /* Timeout occurred */
3026       return HAL_TIMEOUT;
3027     }
3028   }
3029   /* Check if the Receiver is enabled */
3030   if ((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
3031   {
3032     /* Wait until REACK flag is set */
3033     if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
3034     {
3035       /* Timeout occurred */
3036       return HAL_TIMEOUT;
3037     }
3038   }
3039 
3040   /* Initialize the USART state*/
3041   husart->State = HAL_USART_STATE_READY;
3042 
3043   /* Process Unlocked */
3044   __HAL_UNLOCK(husart);
3045 
3046   return HAL_OK;
3047 }
3048 
3049 /**
3050   * @brief  Simplex send an amount of data in non-blocking mode.
3051   * @note   Function called under interruption only, once
3052   *         interruptions have been enabled by HAL_USART_Transmit_IT().
3053   * @note   The USART errors are not managed to avoid the overrun error.
3054   * @note   ISR function executed when FIFO mode is disabled and when the
3055   *         data word length is less than 9 bits long.
3056   * @param  husart USART handle.
3057   * @retval None
3058   */
3059 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart)
3060 {
3061   const HAL_USART_StateTypeDef state = husart->State;
3062 
3063   /* Check that a Tx process is ongoing */
3064   if ((state == HAL_USART_STATE_BUSY_TX) ||
3065       (state == HAL_USART_STATE_BUSY_TX_RX))
3066   {
3067     if (husart->TxXferCount == 0U)
3068     {
3069       /* Disable the USART Transmit data register empty interrupt */
3070       __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
3071 
3072       /* Enable the USART Transmit Complete Interrupt */
3073       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3074     }
3075     else
3076     {
3077       husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
3078       husart->pTxBuffPtr++;
3079       husart->TxXferCount--;
3080     }
3081   }
3082 }
3083 
3084 /**
3085   * @brief  Simplex send an amount of data in non-blocking mode.
3086   * @note   Function called under interruption only, once
3087   *         interruptions have been enabled by HAL_USART_Transmit_IT().
3088   * @note   The USART errors are not managed to avoid the overrun error.
3089   * @note   ISR function executed when FIFO mode is disabled and when the
3090   *         data word length is 9 bits long.
3091   * @param  husart USART handle.
3092   * @retval None
3093   */
3094 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart)
3095 {
3096   const HAL_USART_StateTypeDef state = husart->State;
3097   const uint16_t *tmp;
3098 
3099   if ((state == HAL_USART_STATE_BUSY_TX) ||
3100       (state == HAL_USART_STATE_BUSY_TX_RX))
3101   {
3102     if (husart->TxXferCount == 0U)
3103     {
3104       /* Disable the USART Transmit data register empty interrupt */
3105       __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
3106 
3107       /* Enable the USART Transmit Complete Interrupt */
3108       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3109     }
3110     else
3111     {
3112       tmp = (const uint16_t *) husart->pTxBuffPtr;
3113       husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
3114       husart->pTxBuffPtr += 2U;
3115       husart->TxXferCount--;
3116     }
3117   }
3118 }
3119 
3120 /**
3121   * @brief  Simplex send an amount of data in non-blocking mode.
3122   * @note   Function called under interruption only, once
3123   *         interruptions have been enabled by HAL_USART_Transmit_IT().
3124   * @note   The USART errors are not managed to avoid the overrun error.
3125   * @note   ISR function executed when FIFO mode is enabled and when the
3126   *         data word length is less than 9 bits long.
3127   * @param  husart USART handle.
3128   * @retval None
3129   */
3130 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
3131 {
3132   const HAL_USART_StateTypeDef state = husart->State;
3133   uint16_t  nb_tx_data;
3134 
3135   /* Check that a Tx process is ongoing */
3136   if ((state == HAL_USART_STATE_BUSY_TX) ||
3137       (state == HAL_USART_STATE_BUSY_TX_RX))
3138   {
3139     for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3140     {
3141       if (husart->TxXferCount == 0U)
3142       {
3143         /* Disable the TX FIFO threshold interrupt */
3144         __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
3145 
3146         /* Enable the USART Transmit Complete Interrupt */
3147         __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3148 
3149         break; /* force exit loop */
3150       }
3151       else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
3152       {
3153         husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
3154         husart->pTxBuffPtr++;
3155         husart->TxXferCount--;
3156       }
3157       else
3158       {
3159         /* Nothing to do */
3160       }
3161     }
3162   }
3163 }
3164 
3165 /**
3166   * @brief  Simplex send an amount of data in non-blocking mode.
3167   * @note   Function called under interruption only, once
3168   *         interruptions have been enabled by HAL_USART_Transmit_IT().
3169   * @note   The USART errors are not managed to avoid the overrun error.
3170   * @note   ISR function executed when FIFO mode is enabled and when the
3171   *         data word length is 9 bits long.
3172   * @param  husart USART handle.
3173   * @retval None
3174   */
3175 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
3176 {
3177   const HAL_USART_StateTypeDef state = husart->State;
3178   const uint16_t *tmp;
3179   uint16_t  nb_tx_data;
3180 
3181   /* Check that a Tx process is ongoing */
3182   if ((state == HAL_USART_STATE_BUSY_TX) ||
3183       (state == HAL_USART_STATE_BUSY_TX_RX))
3184   {
3185     for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
3186     {
3187       if (husart->TxXferCount == 0U)
3188       {
3189         /* Disable the TX FIFO threshold interrupt */
3190         __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
3191 
3192         /* Enable the USART Transmit Complete Interrupt */
3193         __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
3194 
3195         break; /* force exit loop */
3196       }
3197       else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
3198       {
3199         tmp = (const uint16_t *) husart->pTxBuffPtr;
3200         husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
3201         husart->pTxBuffPtr += 2U;
3202         husart->TxXferCount--;
3203       }
3204       else
3205       {
3206         /* Nothing to do */
3207       }
3208     }
3209   }
3210 }
3211 
3212 /**
3213   * @brief  Wraps up transmission in non-blocking mode.
3214   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
3215   *                the configuration information for the specified USART module.
3216   * @retval None
3217   */
3218 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart)
3219 {
3220   /* Disable the USART Transmit Complete Interrupt */
3221   __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
3222 
3223   /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3224   __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
3225 
3226   /* Clear TxISR function pointer */
3227   husart->TxISR = NULL;
3228 
3229   if (husart->State == HAL_USART_STATE_BUSY_TX)
3230   {
3231     /* Clear overrun flag and discard the received data */
3232     __HAL_USART_CLEAR_OREFLAG(husart);
3233     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3234 
3235     /* Tx process is completed, restore husart->State to Ready */
3236     husart->State = HAL_USART_STATE_READY;
3237 
3238 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3239     /* Call registered Tx Complete Callback */
3240     husart->TxCpltCallback(husart);
3241 #else
3242     /* Call legacy weak Tx Complete Callback */
3243     HAL_USART_TxCpltCallback(husart);
3244 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3245   }
3246   else if (husart->RxXferCount == 0U)
3247   {
3248     /* TxRx process is completed, restore husart->State to Ready */
3249     husart->State = HAL_USART_STATE_READY;
3250 
3251 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3252     /* Call registered Tx Rx Complete Callback */
3253     husart->TxRxCpltCallback(husart);
3254 #else
3255     /* Call legacy weak Tx Rx Complete Callback */
3256     HAL_USART_TxRxCpltCallback(husart);
3257 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3258   }
3259   else
3260   {
3261     /* Nothing to do */
3262   }
3263 }
3264 
3265 
3266 /**
3267   * @brief  Simplex receive an amount of data in non-blocking mode.
3268   * @note   Function called under interruption only, once
3269   *         interruptions have been enabled by HAL_USART_Receive_IT().
3270   * @note   ISR function executed when FIFO mode is disabled and when the
3271   *         data word length is less than 9 bits long.
3272   * @param  husart USART handle
3273   * @retval None
3274   */
3275 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart)
3276 {
3277   const HAL_USART_StateTypeDef state = husart->State;
3278   uint16_t txdatacount;
3279   uint16_t uhMask = husart->Mask;
3280   uint32_t txftie;
3281 
3282   if ((state == HAL_USART_STATE_BUSY_RX) ||
3283       (state == HAL_USART_STATE_BUSY_TX_RX))
3284   {
3285     *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
3286     husart->pRxBuffPtr++;
3287     husart->RxXferCount--;
3288 
3289     if (husart->RxXferCount == 0U)
3290     {
3291       /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3292       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3293 
3294       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3295       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
3296 
3297       /* Clear RxISR function pointer */
3298       husart->RxISR = NULL;
3299 
3300       /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3301       txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3302       txdatacount = husart->TxXferCount;
3303 
3304       if (state == HAL_USART_STATE_BUSY_RX)
3305       {
3306         /* Clear SPI slave underrun flag and discard transmit data */
3307         if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3308         {
3309           __HAL_USART_CLEAR_UDRFLAG(husart);
3310           __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3311         }
3312 
3313         /* Rx process is completed, restore husart->State to Ready */
3314         husart->State = HAL_USART_STATE_READY;
3315 
3316 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3317         /* Call registered Rx Complete Callback */
3318         husart->RxCpltCallback(husart);
3319 #else
3320         /* Call legacy weak Rx Complete Callback */
3321         HAL_USART_RxCpltCallback(husart);
3322 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3323       }
3324       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3325                (txftie != USART_CR3_TXFTIE) &&
3326                (txdatacount == 0U))
3327       {
3328         /* TxRx process is completed, restore husart->State to Ready */
3329         husart->State = HAL_USART_STATE_READY;
3330 
3331 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3332         /* Call registered Tx Rx Complete Callback */
3333         husart->TxRxCpltCallback(husart);
3334 #else
3335         /* Call legacy weak Tx Rx Complete Callback */
3336         HAL_USART_TxRxCpltCallback(husart);
3337 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3338       }
3339       else
3340       {
3341         /* Nothing to do */
3342       }
3343     }
3344     else if ((state == HAL_USART_STATE_BUSY_RX) &&
3345              (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3346     {
3347       /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3348       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3349     }
3350     else
3351     {
3352       /* Nothing to do */
3353     }
3354   }
3355 }
3356 
3357 /**
3358   * @brief  Simplex receive an amount of data in non-blocking mode.
3359   * @note   Function called under interruption only, once
3360   *         interruptions have been enabled by HAL_USART_Receive_IT().
3361   * @note   ISR function executed when FIFO mode is disabled and when the
3362   *         data word length is 9 bits long.
3363   * @param  husart USART handle
3364   * @retval None
3365   */
3366 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart)
3367 {
3368   const HAL_USART_StateTypeDef state = husart->State;
3369   uint16_t txdatacount;
3370   uint16_t *tmp;
3371   uint16_t uhMask = husart->Mask;
3372   uint32_t txftie;
3373 
3374   if ((state == HAL_USART_STATE_BUSY_RX) ||
3375       (state == HAL_USART_STATE_BUSY_TX_RX))
3376   {
3377     tmp = (uint16_t *) husart->pRxBuffPtr;
3378     *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
3379     husart->pRxBuffPtr += 2U;
3380     husart->RxXferCount--;
3381 
3382     if (husart->RxXferCount == 0U)
3383     {
3384       /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
3385       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3386 
3387       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
3388       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
3389 
3390       /* Clear RxISR function pointer */
3391       husart->RxISR = NULL;
3392 
3393       /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3394       txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3395       txdatacount = husart->TxXferCount;
3396 
3397       if (state == HAL_USART_STATE_BUSY_RX)
3398       {
3399         /* Clear SPI slave underrun flag and discard transmit data */
3400         if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3401         {
3402           __HAL_USART_CLEAR_UDRFLAG(husart);
3403           __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3404         }
3405 
3406         /* Rx process is completed, restore husart->State to Ready */
3407         husart->State = HAL_USART_STATE_READY;
3408 
3409 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3410         /* Call registered Rx Complete Callback */
3411         husart->RxCpltCallback(husart);
3412 #else
3413         /* Call legacy weak Rx Complete Callback */
3414         HAL_USART_RxCpltCallback(husart);
3415 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3416       }
3417       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3418                (txftie != USART_CR3_TXFTIE) &&
3419                (txdatacount == 0U))
3420       {
3421         /* TxRx process is completed, restore husart->State to Ready */
3422         husart->State = HAL_USART_STATE_READY;
3423 
3424 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3425         /* Call registered Tx Rx Complete Callback */
3426         husart->TxRxCpltCallback(husart);
3427 #else
3428         /* Call legacy weak Tx Rx Complete Callback */
3429         HAL_USART_TxRxCpltCallback(husart);
3430 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3431       }
3432       else
3433       {
3434         /* Nothing to do */
3435       }
3436     }
3437     else if ((state == HAL_USART_STATE_BUSY_RX) &&
3438              (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3439     {
3440       /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3441       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3442     }
3443     else
3444     {
3445       /* Nothing to do */
3446     }
3447   }
3448 }
3449 
3450 /**
3451   * @brief  Simplex receive an amount of data in non-blocking mode.
3452   * @note   Function called under interruption only, once
3453   *         interruptions have been enabled by HAL_USART_Receive_IT().
3454   * @note   ISR function executed when FIFO mode is enabled and when the
3455   *         data word length is less than 9 bits long.
3456   * @param  husart USART handle
3457   * @retval None
3458   */
3459 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
3460 {
3461   HAL_USART_StateTypeDef state = husart->State;
3462   uint16_t txdatacount;
3463   uint16_t rxdatacount;
3464   uint16_t uhMask = husart->Mask;
3465   uint16_t nb_rx_data;
3466   uint32_t txftie;
3467 
3468   /* Check that a Rx process is ongoing */
3469   if ((state == HAL_USART_STATE_BUSY_RX) ||
3470       (state == HAL_USART_STATE_BUSY_TX_RX))
3471   {
3472     for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3473     {
3474       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
3475       {
3476         *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
3477         husart->pRxBuffPtr++;
3478         husart->RxXferCount--;
3479 
3480         if (husart->RxXferCount == 0U)
3481         {
3482           /* Disable the USART Parity Error Interrupt */
3483           CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
3484 
3485           /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error)
3486              and RX FIFO Threshold interrupt */
3487           CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3488 
3489           /* Clear RxISR function pointer */
3490           husart->RxISR = NULL;
3491 
3492           /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3493           txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3494           txdatacount = husart->TxXferCount;
3495 
3496           if (state == HAL_USART_STATE_BUSY_RX)
3497           {
3498             /* Clear SPI slave underrun flag and discard transmit data */
3499             if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3500             {
3501               __HAL_USART_CLEAR_UDRFLAG(husart);
3502               __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3503             }
3504 
3505             /* Rx process is completed, restore husart->State to Ready */
3506             husart->State = HAL_USART_STATE_READY;
3507             state = HAL_USART_STATE_READY;
3508 
3509 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3510             /* Call registered Rx Complete Callback */
3511             husart->RxCpltCallback(husart);
3512 #else
3513             /* Call legacy weak Rx Complete Callback */
3514             HAL_USART_RxCpltCallback(husart);
3515 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3516           }
3517           else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3518                    (txftie != USART_CR3_TXFTIE) &&
3519                    (txdatacount == 0U))
3520           {
3521             /* TxRx process is completed, restore husart->State to Ready */
3522             husart->State = HAL_USART_STATE_READY;
3523             state = HAL_USART_STATE_READY;
3524 
3525 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3526             /* Call registered Tx Rx Complete Callback */
3527             husart->TxRxCpltCallback(husart);
3528 #else
3529             /* Call legacy weak Tx Rx Complete Callback */
3530             HAL_USART_TxRxCpltCallback(husart);
3531 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3532           }
3533           else
3534           {
3535             /* Nothing to do */
3536           }
3537         }
3538         else if ((state == HAL_USART_STATE_BUSY_RX) &&
3539                  (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3540         {
3541           /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3542           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3543         }
3544         else
3545         {
3546           /* Nothing to do */
3547         }
3548       }
3549     }
3550 
3551     /* When remaining number of bytes to receive is less than the RX FIFO
3552     threshold, next incoming frames are processed as if FIFO mode was
3553     disabled (i.e. one interrupt per received frame).
3554     */
3555     rxdatacount = husart->RxXferCount;
3556     if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
3557     {
3558       /* Disable the USART RXFT interrupt*/
3559       CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
3560 
3561       /* Update the RxISR function pointer */
3562       husart->RxISR = USART_RxISR_8BIT;
3563 
3564       /* Enable the USART Data Register Not Empty interrupt */
3565       SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3566 
3567       if ((husart->TxXferCount == 0U) &&
3568           (state == HAL_USART_STATE_BUSY_TX_RX) &&
3569           (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3570       {
3571         /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3572         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3573       }
3574     }
3575   }
3576   else
3577   {
3578     /* Clear RXNE interrupt flag */
3579     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3580   }
3581 }
3582 
3583 /**
3584   * @brief  Simplex receive an amount of data in non-blocking mode.
3585   * @note   Function called under interruption only, once
3586   *         interruptions have been enabled by HAL_USART_Receive_IT().
3587   * @note   ISR function executed when FIFO mode is enabled and when the
3588   *         data word length is 9 bits long.
3589   * @param  husart USART handle
3590   * @retval None
3591   */
3592 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
3593 {
3594   HAL_USART_StateTypeDef state = husart->State;
3595   uint16_t txdatacount;
3596   uint16_t rxdatacount;
3597   uint16_t *tmp;
3598   uint16_t uhMask = husart->Mask;
3599   uint16_t nb_rx_data;
3600   uint32_t txftie;
3601 
3602   /* Check that a Tx process is ongoing */
3603   if ((state == HAL_USART_STATE_BUSY_RX) ||
3604       (state == HAL_USART_STATE_BUSY_TX_RX))
3605   {
3606     for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
3607     {
3608       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
3609       {
3610         tmp = (uint16_t *) husart->pRxBuffPtr;
3611         *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
3612         husart->pRxBuffPtr += 2U;
3613         husart->RxXferCount--;
3614 
3615         if (husart->RxXferCount == 0U)
3616         {
3617           /* Disable the USART Parity Error Interrupt */
3618           CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
3619 
3620           /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error)
3621              and RX FIFO Threshold interrupt */
3622           CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3623 
3624           /* Clear RxISR function pointer */
3625           husart->RxISR = NULL;
3626 
3627           /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
3628           txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
3629           txdatacount = husart->TxXferCount;
3630 
3631           if (state == HAL_USART_STATE_BUSY_RX)
3632           {
3633             /* Clear SPI slave underrun flag and discard transmit data */
3634             if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
3635             {
3636               __HAL_USART_CLEAR_UDRFLAG(husart);
3637               __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
3638             }
3639 
3640             /* Rx process is completed, restore husart->State to Ready */
3641             husart->State = HAL_USART_STATE_READY;
3642             state = HAL_USART_STATE_READY;
3643 
3644 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3645             /* Call registered Rx Complete Callback */
3646             husart->RxCpltCallback(husart);
3647 #else
3648             /* Call legacy weak Rx Complete Callback */
3649             HAL_USART_RxCpltCallback(husart);
3650 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3651           }
3652           else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
3653                    (txftie != USART_CR3_TXFTIE) &&
3654                    (txdatacount == 0U))
3655           {
3656             /* TxRx process is completed, restore husart->State to Ready */
3657             husart->State = HAL_USART_STATE_READY;
3658             state = HAL_USART_STATE_READY;
3659 
3660 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
3661             /* Call registered Tx Rx Complete Callback */
3662             husart->TxRxCpltCallback(husart);
3663 #else
3664             /* Call legacy weak Tx Rx Complete Callback */
3665             HAL_USART_TxRxCpltCallback(husart);
3666 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
3667           }
3668           else
3669           {
3670             /* Nothing to do */
3671           }
3672         }
3673         else if ((state == HAL_USART_STATE_BUSY_RX) &&
3674                  (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3675         {
3676           /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3677           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3678         }
3679         else
3680         {
3681           /* Nothing to do */
3682         }
3683       }
3684     }
3685 
3686     /* When remaining number of bytes to receive is less than the RX FIFO
3687     threshold, next incoming frames are processed as if FIFO mode was
3688     disabled (i.e. one interrupt per received frame).
3689     */
3690     rxdatacount = husart->RxXferCount;
3691     if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
3692     {
3693       /* Disable the USART RXFT interrupt*/
3694       CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
3695 
3696       /* Update the RxISR function pointer */
3697       husart->RxISR = USART_RxISR_16BIT;
3698 
3699       /* Enable the USART Data Register Not Empty interrupt */
3700       SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3701 
3702       if ((husart->TxXferCount == 0U) &&
3703           (state == HAL_USART_STATE_BUSY_TX_RX) &&
3704           (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
3705       {
3706         /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
3707         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
3708       }
3709     }
3710   }
3711   else
3712   {
3713     /* Clear RXNE interrupt flag */
3714     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
3715   }
3716 }
3717 
3718 /**
3719   * @}
3720   */
3721 
3722 #endif /* HAL_USART_MODULE_ENABLED */
3723 /**
3724   * @}
3725   */
3726 
3727 /**
3728   * @}
3729   */
3730