Back to home page

LXR

 
 

    


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

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_uart.c
0004   * @author  MCD Application Team
0005   * @brief   UART HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
0008   *           + Initialization and de-initialization functions
0009   *           + IO operation functions
0010   *           + Peripheral Control functions
0011   *
0012   *
0013   ******************************************************************************
0014   * @attention
0015   *
0016   * Copyright (c) 2017 STMicroelectronics.
0017   * All rights reserved.
0018   *
0019   * This software is licensed under terms that can be found in the LICENSE file
0020   * in the root directory of this software component.
0021   * If no LICENSE file comes with this software, it is provided AS-IS.
0022   *
0023   ******************************************************************************
0024   @verbatim
0025  ===============================================================================
0026                         ##### How to use this driver #####
0027  ===============================================================================
0028   [..]
0029     The UART HAL driver can be used as follows:
0030 
0031     (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
0032     (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
0033         (++) Enable the USARTx interface clock.
0034         (++) UART pins configuration:
0035             (+++) Enable the clock for the UART GPIOs.
0036             (+++) Configure these UART pins as alternate function pull-up.
0037         (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
0038              and HAL_UART_Receive_IT() APIs):
0039             (+++) Configure the USARTx interrupt priority.
0040             (+++) Enable the NVIC USART IRQ handle.
0041         (++) UART interrupts handling:
0042               -@@-  The specific UART interrupts (Transmission complete interrupt,
0043                 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
0044                 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
0045                 inside the transmit and receive processes.
0046         (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
0047              and HAL_UART_Receive_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 UART DMA Tx/Rx handle.
0053             (+++) Configure the priority and enable the NVIC for the transfer complete
0054                   interrupt on the DMA Tx/Rx channel.
0055 
0056     (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Prescaler value , Hardware
0057         flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
0058 
0059     (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
0060         in the huart handle AdvancedInit structure.
0061 
0062     (#) For the UART asynchronous mode, initialize the UART registers by calling
0063         the HAL_UART_Init() API.
0064 
0065     (#) For the UART Half duplex mode, initialize the UART registers by calling
0066         the HAL_HalfDuplex_Init() API.
0067 
0068     (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
0069         by calling the HAL_LIN_Init() API.
0070 
0071     (#) For the UART Multiprocessor mode, initialize the UART registers
0072         by calling the HAL_MultiProcessor_Init() API.
0073 
0074     (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
0075         by calling the HAL_RS485Ex_Init() API.
0076 
0077     [..]
0078     (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
0079         also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
0080         calling the customized HAL_UART_MspInit() API.
0081 
0082     ##### Callback registration #####
0083     ==================================
0084 
0085     [..]
0086     The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
0087     allows the user to configure dynamically the driver callbacks.
0088 
0089     [..]
0090     Use Function HAL_UART_RegisterCallback() to register a user callback.
0091     Function HAL_UART_RegisterCallback() allows to register following callbacks:
0092     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
0093     (+) TxCpltCallback            : Tx Complete Callback.
0094     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
0095     (+) RxCpltCallback            : Rx Complete Callback.
0096     (+) ErrorCallback             : Error Callback.
0097     (+) AbortCpltCallback         : Abort Complete Callback.
0098     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
0099     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
0100     (+) WakeupCallback            : Wakeup Callback.
0101     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0102     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0103     (+) MspInitCallback           : UART MspInit.
0104     (+) MspDeInitCallback         : UART MspDeInit.
0105     This function takes as parameters the HAL peripheral handle, the Callback ID
0106     and a pointer to the user callback function.
0107 
0108     [..]
0109     Use function HAL_UART_UnRegisterCallback() to reset a callback to the default
0110     weak function.
0111     HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
0112     and the Callback ID.
0113     This function allows to reset following callbacks:
0114     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
0115     (+) TxCpltCallback            : Tx Complete Callback.
0116     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
0117     (+) RxCpltCallback            : Rx Complete Callback.
0118     (+) ErrorCallback             : Error Callback.
0119     (+) AbortCpltCallback         : Abort Complete Callback.
0120     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
0121     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
0122     (+) WakeupCallback            : Wakeup Callback.
0123     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
0124     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
0125     (+) MspInitCallback           : UART MspInit.
0126     (+) MspDeInitCallback         : UART MspDeInit.
0127 
0128     [..]
0129     For specific callback RxEventCallback, use dedicated registration/reset functions:
0130     respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback().
0131 
0132     [..]
0133     By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
0134     all callbacks are set to the corresponding weak functions:
0135     examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback().
0136     Exception done for MspInit and MspDeInit functions that are respectively
0137     reset to the legacy weak functions in the HAL_UART_Init()
0138     and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
0139     If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit()
0140     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
0141 
0142     [..]
0143     Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
0144     Exception done MspInit/MspDeInit that can be registered/unregistered
0145     in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
0146     MspInit/DeInit callbacks can be used during the Init/DeInit.
0147     In that case first register the MspInit/MspDeInit user callbacks
0148     using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit()
0149     or HAL_UART_Init() function.
0150 
0151     [..]
0152     When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
0153     not defined, the callback registration feature is not available
0154     and weak callbacks are used.
0155 
0156 
0157   @endverbatim
0158   ******************************************************************************
0159   */
0160 
0161 /* Includes ------------------------------------------------------------------*/
0162 #include "stm32h7xx_hal.h"
0163 
0164 /** @addtogroup STM32H7xx_HAL_Driver
0165   * @{
0166   */
0167 
0168 /** @defgroup UART UART
0169   * @ingroup RTEMSBSPsARMSTM32H7
0170   * @brief HAL UART module driver
0171   * @{
0172   */
0173 
0174 #ifdef HAL_UART_MODULE_ENABLED
0175 
0176 /* Private typedef -----------------------------------------------------------*/
0177 /* Private define ------------------------------------------------------------*/
0178 /** @defgroup UART_Private_Constants UART Private Constants
0179   * @ingroup RTEMSBSPsARMSTM32H7
0180   * @{
0181   */
0182 #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE | \
0183                                       USART_CR1_OVER8 | USART_CR1_FIFOEN)) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
0184 
0185 #define USART_CR3_FIELDS  ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT | USART_CR3_TXFTCFG | \
0186                                       USART_CR3_RXFTCFG)) /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
0187 
0188 #define LPUART_BRR_MIN  0x00000300U  /* LPUART BRR minimum authorized value */
0189 #define LPUART_BRR_MAX  0x000FFFFFU  /* LPUART BRR maximum authorized value */
0190 
0191 #define UART_BRR_MIN    0x10U        /* UART BRR minimum authorized value */
0192 #define UART_BRR_MAX    0x0000FFFFU  /* UART BRR maximum authorized value */
0193 /**
0194   * @}
0195   */
0196 
0197 /* Private macros ------------------------------------------------------------*/
0198 /* Private function prototypes -----------------------------------------------*/
0199 /** @addtogroup UART_Private_Functions
0200   * @{
0201   */
0202 static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
0203 static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
0204 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
0205 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
0206 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
0207 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
0208 static void UART_DMAError(DMA_HandleTypeDef *hdma);
0209 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
0210 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
0211 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
0212 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
0213 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
0214 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
0215 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
0216 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
0217 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
0218 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
0219 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
0220 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
0221 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart);
0222 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart);
0223 /**
0224   * @}
0225   */
0226 
0227 /* Private variables ---------------------------------------------------------*/
0228 /** @addtogroup UART_Private_variables
0229   * @{
0230   */
0231 const uint16_t UARTPrescTable[12] = {1U, 2U, 4U, 6U, 8U, 10U, 12U, 16U, 32U, 64U, 128U, 256U};
0232 /**
0233   * @}
0234   */
0235 
0236 /* Exported Constants --------------------------------------------------------*/
0237 /* Exported functions --------------------------------------------------------*/
0238 
0239 /** @defgroup UART_Exported_Functions UART Exported Functions
0240   * @ingroup RTEMSBSPsARMSTM32H7
0241   * @{
0242   */
0243 
0244 /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
0245   * @ingroup RTEMSBSPsARMSTM32H7
0246   *  @brief    Initialization and Configuration functions
0247   *
0248 @verbatim
0249 ===============================================================================
0250             ##### Initialization and Configuration functions #####
0251  ===============================================================================
0252     [..]
0253     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
0254     in asynchronous mode.
0255       (+) For the asynchronous mode the parameters below can be configured:
0256         (++) Baud Rate
0257         (++) Word Length
0258         (++) Stop Bit
0259         (++) Parity: If the parity is enabled, then the MSB bit of the data written
0260              in the data register is transmitted but is changed by the parity bit.
0261         (++) Hardware flow control
0262         (++) Receiver/transmitter modes
0263         (++) Over Sampling Method
0264         (++) One-Bit Sampling Method
0265       (+) For the asynchronous mode, the following advanced features can be configured as well:
0266         (++) TX and/or RX pin level inversion
0267         (++) data logical level inversion
0268         (++) RX and TX pins swap
0269         (++) RX overrun detection disabling
0270         (++) DMA disabling on RX error
0271         (++) MSB first on communication line
0272         (++) auto Baud rate detection
0273     [..]
0274     The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
0275     follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
0276     and UART multiprocessor mode configuration procedures (details for the procedures
0277     are available in reference manual).
0278 
0279 @endverbatim
0280 
0281   Depending on the frame length defined by the M1 and M0 bits (7-bit,
0282   8-bit or 9-bit), the possible UART formats are listed in the
0283   following table.
0284 
0285   Table 1. UART frame format.
0286     +-----------------------------------------------------------------------+
0287     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
0288     |---------|---------|-----------|---------------------------------------|
0289     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
0290     |---------|---------|-----------|---------------------------------------|
0291     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
0292     |---------|---------|-----------|---------------------------------------|
0293     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
0294     |---------|---------|-----------|---------------------------------------|
0295     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
0296     |---------|---------|-----------|---------------------------------------|
0297     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
0298     |---------|---------|-----------|---------------------------------------|
0299     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
0300     +-----------------------------------------------------------------------+
0301 
0302   * @{
0303   */
0304 
0305 /**
0306   * @brief Initialize the UART mode according to the specified
0307   *        parameters in the UART_InitTypeDef and initialize the associated handle.
0308   * @param huart UART handle.
0309   * @retval HAL status
0310   */
0311 HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
0312 {
0313   /* Check the UART handle allocation */
0314   if (huart == NULL)
0315   {
0316     return HAL_ERROR;
0317   }
0318 
0319   if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
0320   {
0321     /* Check the parameters */
0322     assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
0323   }
0324   else
0325   {
0326     /* Check the parameters */
0327     assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
0328   }
0329 
0330   if (huart->gState == HAL_UART_STATE_RESET)
0331   {
0332     /* Allocate lock resource and initialize it */
0333     huart->Lock = HAL_UNLOCKED;
0334 
0335 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0336     UART_InitCallbacksToDefault(huart);
0337 
0338     if (huart->MspInitCallback == NULL)
0339     {
0340       huart->MspInitCallback = HAL_UART_MspInit;
0341     }
0342 
0343     /* Init the low level hardware */
0344     huart->MspInitCallback(huart);
0345 #else
0346     /* Init the low level hardware : GPIO, CLOCK */
0347     HAL_UART_MspInit(huart);
0348 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
0349   }
0350 
0351   huart->gState = HAL_UART_STATE_BUSY;
0352 
0353   __HAL_UART_DISABLE(huart);
0354 
0355   /* Perform advanced settings configuration */
0356   /* For some items, configuration requires to be done prior TE and RE bits are set */
0357   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
0358   {
0359     UART_AdvFeatureConfig(huart);
0360   }
0361 
0362   /* Set the UART Communication parameters */
0363   if (UART_SetConfig(huart) == HAL_ERROR)
0364   {
0365     return HAL_ERROR;
0366   }
0367 
0368   /* In asynchronous mode, the following bits must be kept cleared:
0369   - LINEN and CLKEN bits in the USART_CR2 register,
0370   - SCEN, HDSEL and IREN  bits in the USART_CR3 register.*/
0371   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
0372   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
0373 
0374   __HAL_UART_ENABLE(huart);
0375 
0376   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
0377   return (UART_CheckIdleState(huart));
0378 }
0379 
0380 /**
0381   * @brief Initialize the half-duplex mode according to the specified
0382   *        parameters in the UART_InitTypeDef and creates the associated handle.
0383   * @param huart UART handle.
0384   * @retval HAL status
0385   */
0386 HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
0387 {
0388   /* Check the UART handle allocation */
0389   if (huart == NULL)
0390   {
0391     return HAL_ERROR;
0392   }
0393 
0394   /* Check UART instance */
0395   assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
0396 
0397   if (huart->gState == HAL_UART_STATE_RESET)
0398   {
0399     /* Allocate lock resource and initialize it */
0400     huart->Lock = HAL_UNLOCKED;
0401 
0402 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0403     UART_InitCallbacksToDefault(huart);
0404 
0405     if (huart->MspInitCallback == NULL)
0406     {
0407       huart->MspInitCallback = HAL_UART_MspInit;
0408     }
0409 
0410     /* Init the low level hardware */
0411     huart->MspInitCallback(huart);
0412 #else
0413     /* Init the low level hardware : GPIO, CLOCK */
0414     HAL_UART_MspInit(huart);
0415 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
0416   }
0417 
0418   huart->gState = HAL_UART_STATE_BUSY;
0419 
0420   __HAL_UART_DISABLE(huart);
0421 
0422   /* Perform advanced settings configuration */
0423   /* For some items, configuration requires to be done prior TE and RE bits are set */
0424   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
0425   {
0426     UART_AdvFeatureConfig(huart);
0427   }
0428 
0429   /* Set the UART Communication parameters */
0430   if (UART_SetConfig(huart) == HAL_ERROR)
0431   {
0432     return HAL_ERROR;
0433   }
0434 
0435   /* In half-duplex mode, the following bits must be kept cleared:
0436   - LINEN and CLKEN bits in the USART_CR2 register,
0437   - SCEN and IREN bits in the USART_CR3 register.*/
0438   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
0439   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
0440 
0441   /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
0442   SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
0443 
0444   __HAL_UART_ENABLE(huart);
0445 
0446   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
0447   return (UART_CheckIdleState(huart));
0448 }
0449 
0450 
0451 /**
0452   * @brief Initialize the LIN mode according to the specified
0453   *        parameters in the UART_InitTypeDef and creates the associated handle.
0454   * @param huart             UART handle.
0455   * @param BreakDetectLength Specifies the LIN break detection length.
0456   *        This parameter can be one of the following values:
0457   *          @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
0458   *          @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
0459   * @retval HAL status
0460   */
0461 HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
0462 {
0463   /* Check the UART handle allocation */
0464   if (huart == NULL)
0465   {
0466     return HAL_ERROR;
0467   }
0468 
0469   /* Check the LIN UART instance */
0470   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
0471   /* Check the Break detection length parameter */
0472   assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
0473 
0474   /* LIN mode limited to 16-bit oversampling only */
0475   if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
0476   {
0477     return HAL_ERROR;
0478   }
0479   /* LIN mode limited to 8-bit data length */
0480   if (huart->Init.WordLength != UART_WORDLENGTH_8B)
0481   {
0482     return HAL_ERROR;
0483   }
0484 
0485   if (huart->gState == HAL_UART_STATE_RESET)
0486   {
0487     /* Allocate lock resource and initialize it */
0488     huart->Lock = HAL_UNLOCKED;
0489 
0490 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0491     UART_InitCallbacksToDefault(huart);
0492 
0493     if (huart->MspInitCallback == NULL)
0494     {
0495       huart->MspInitCallback = HAL_UART_MspInit;
0496     }
0497 
0498     /* Init the low level hardware */
0499     huart->MspInitCallback(huart);
0500 #else
0501     /* Init the low level hardware : GPIO, CLOCK */
0502     HAL_UART_MspInit(huart);
0503 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
0504   }
0505 
0506   huart->gState = HAL_UART_STATE_BUSY;
0507 
0508   __HAL_UART_DISABLE(huart);
0509 
0510   /* Perform advanced settings configuration */
0511   /* For some items, configuration requires to be done prior TE and RE bits are set */
0512   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
0513   {
0514     UART_AdvFeatureConfig(huart);
0515   }
0516 
0517   /* Set the UART Communication parameters */
0518   if (UART_SetConfig(huart) == HAL_ERROR)
0519   {
0520     return HAL_ERROR;
0521   }
0522 
0523   /* In LIN mode, the following bits must be kept cleared:
0524   - LINEN and CLKEN bits in the USART_CR2 register,
0525   - SCEN and IREN bits in the USART_CR3 register.*/
0526   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
0527   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
0528 
0529   /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
0530   SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
0531 
0532   /* Set the USART LIN Break detection length. */
0533   MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
0534 
0535   __HAL_UART_ENABLE(huart);
0536 
0537   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
0538   return (UART_CheckIdleState(huart));
0539 }
0540 
0541 
0542 /**
0543   * @brief Initialize the multiprocessor mode according to the specified
0544   *        parameters in the UART_InitTypeDef and initialize the associated handle.
0545   * @param huart        UART handle.
0546   * @param Address      UART node address (4-, 6-, 7- or 8-bit long).
0547   * @param WakeUpMethod Specifies the UART wakeup method.
0548   *        This parameter can be one of the following values:
0549   *          @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
0550   *          @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
0551   * @note  If the user resorts to idle line detection wake up, the Address parameter
0552   *        is useless and ignored by the initialization function.
0553   * @note  If the user resorts to address mark wake up, the address length detection
0554   *        is configured by default to 4 bits only. For the UART to be able to
0555   *        manage 6-, 7- or 8-bit long addresses detection, the API
0556   *        HAL_MultiProcessorEx_AddressLength_Set() must be called after
0557   *        HAL_MultiProcessor_Init().
0558   * @retval HAL status
0559   */
0560 HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
0561 {
0562   /* Check the UART handle allocation */
0563   if (huart == NULL)
0564   {
0565     return HAL_ERROR;
0566   }
0567 
0568   /* Check the wake up method parameter */
0569   assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
0570 
0571   if (huart->gState == HAL_UART_STATE_RESET)
0572   {
0573     /* Allocate lock resource and initialize it */
0574     huart->Lock = HAL_UNLOCKED;
0575 
0576 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0577     UART_InitCallbacksToDefault(huart);
0578 
0579     if (huart->MspInitCallback == NULL)
0580     {
0581       huart->MspInitCallback = HAL_UART_MspInit;
0582     }
0583 
0584     /* Init the low level hardware */
0585     huart->MspInitCallback(huart);
0586 #else
0587     /* Init the low level hardware : GPIO, CLOCK */
0588     HAL_UART_MspInit(huart);
0589 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
0590   }
0591 
0592   huart->gState = HAL_UART_STATE_BUSY;
0593 
0594   __HAL_UART_DISABLE(huart);
0595 
0596   /* Perform advanced settings configuration */
0597   /* For some items, configuration requires to be done prior TE and RE bits are set */
0598   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
0599   {
0600     UART_AdvFeatureConfig(huart);
0601   }
0602 
0603   /* Set the UART Communication parameters */
0604   if (UART_SetConfig(huart) == HAL_ERROR)
0605   {
0606     return HAL_ERROR;
0607   }
0608 
0609   /* In multiprocessor mode, the following bits must be kept cleared:
0610   - LINEN and CLKEN bits in the USART_CR2 register,
0611   - SCEN, HDSEL and IREN  bits in the USART_CR3 register. */
0612   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
0613   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
0614 
0615   if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
0616   {
0617     /* If address mark wake up method is chosen, set the USART address node */
0618     MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
0619   }
0620 
0621   /* Set the wake up method by setting the WAKE bit in the CR1 register */
0622   MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
0623 
0624   __HAL_UART_ENABLE(huart);
0625 
0626   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
0627   return (UART_CheckIdleState(huart));
0628 }
0629 
0630 
0631 /**
0632   * @brief DeInitialize the UART peripheral.
0633   * @param huart UART handle.
0634   * @retval HAL status
0635   */
0636 HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
0637 {
0638   /* Check the UART handle allocation */
0639   if (huart == NULL)
0640   {
0641     return HAL_ERROR;
0642   }
0643 
0644   /* Check the parameters */
0645   assert_param((IS_UART_INSTANCE(huart->Instance)) || (IS_LPUART_INSTANCE(huart->Instance)));
0646 
0647   huart->gState = HAL_UART_STATE_BUSY;
0648 
0649   __HAL_UART_DISABLE(huart);
0650 
0651   huart->Instance->CR1 = 0x0U;
0652   huart->Instance->CR2 = 0x0U;
0653   huart->Instance->CR3 = 0x0U;
0654 
0655 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0656   if (huart->MspDeInitCallback == NULL)
0657   {
0658     huart->MspDeInitCallback = HAL_UART_MspDeInit;
0659   }
0660   /* DeInit the low level hardware */
0661   huart->MspDeInitCallback(huart);
0662 #else
0663   /* DeInit the low level hardware */
0664   HAL_UART_MspDeInit(huart);
0665 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
0666 
0667   huart->ErrorCode = HAL_UART_ERROR_NONE;
0668   huart->gState = HAL_UART_STATE_RESET;
0669   huart->RxState = HAL_UART_STATE_RESET;
0670   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
0671   huart->RxEventType = HAL_UART_RXEVENT_TC;
0672 
0673   __HAL_UNLOCK(huart);
0674 
0675   return HAL_OK;
0676 }
0677 
0678 #ifndef __rtems__
0679 /**
0680   * @brief Initialize the UART MSP.
0681   * @param huart UART handle.
0682   * @retval None
0683   */
0684 __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
0685 {
0686   /* Prevent unused argument(s) compilation warning */
0687   UNUSED(huart);
0688 
0689   /* NOTE : This function should not be modified, when the callback is needed,
0690             the HAL_UART_MspInit can be implemented in the user file
0691    */
0692 }
0693 #endif /* __rtems__ */
0694 
0695 /**
0696   * @brief DeInitialize the UART MSP.
0697   * @param huart UART handle.
0698   * @retval None
0699   */
0700 __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
0701 {
0702   /* Prevent unused argument(s) compilation warning */
0703   UNUSED(huart);
0704 
0705   /* NOTE : This function should not be modified, when the callback is needed,
0706             the HAL_UART_MspDeInit can be implemented in the user file
0707    */
0708 }
0709 
0710 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
0711 /**
0712   * @brief  Register a User UART Callback
0713   *         To be used to override the weak predefined callback
0714   * @note   The HAL_UART_RegisterCallback() may be called before HAL_UART_Init(), HAL_HalfDuplex_Init(),
0715   *         HAL_LIN_Init(), HAL_MultiProcessor_Init() or HAL_RS485Ex_Init() in HAL_UART_STATE_RESET to register
0716   *         callbacks for HAL_UART_MSPINIT_CB_ID and HAL_UART_MSPDEINIT_CB_ID
0717   * @param  huart uart handle
0718   * @param  CallbackID ID of the callback to be registered
0719   *         This parameter can be one of the following values:
0720   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
0721   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
0722   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
0723   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
0724   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
0725   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0726   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
0727   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
0728   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
0729   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0730   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0731   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
0732   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
0733   * @param  pCallback pointer to the Callback function
0734   * @retval HAL status
0735   */
0736 HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
0737                                             pUART_CallbackTypeDef pCallback)
0738 {
0739   HAL_StatusTypeDef status = HAL_OK;
0740 
0741   if (pCallback == NULL)
0742   {
0743     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0744 
0745     return HAL_ERROR;
0746   }
0747 
0748   if (huart->gState == HAL_UART_STATE_READY)
0749   {
0750     switch (CallbackID)
0751     {
0752       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
0753         huart->TxHalfCpltCallback = pCallback;
0754         break;
0755 
0756       case HAL_UART_TX_COMPLETE_CB_ID :
0757         huart->TxCpltCallback = pCallback;
0758         break;
0759 
0760       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
0761         huart->RxHalfCpltCallback = pCallback;
0762         break;
0763 
0764       case HAL_UART_RX_COMPLETE_CB_ID :
0765         huart->RxCpltCallback = pCallback;
0766         break;
0767 
0768       case HAL_UART_ERROR_CB_ID :
0769         huart->ErrorCallback = pCallback;
0770         break;
0771 
0772       case HAL_UART_ABORT_COMPLETE_CB_ID :
0773         huart->AbortCpltCallback = pCallback;
0774         break;
0775 
0776       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
0777         huart->AbortTransmitCpltCallback = pCallback;
0778         break;
0779 
0780       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
0781         huart->AbortReceiveCpltCallback = pCallback;
0782         break;
0783 
0784       case HAL_UART_WAKEUP_CB_ID :
0785         huart->WakeupCallback = pCallback;
0786         break;
0787 
0788       case HAL_UART_RX_FIFO_FULL_CB_ID :
0789         huart->RxFifoFullCallback = pCallback;
0790         break;
0791 
0792       case HAL_UART_TX_FIFO_EMPTY_CB_ID :
0793         huart->TxFifoEmptyCallback = pCallback;
0794         break;
0795 
0796       case HAL_UART_MSPINIT_CB_ID :
0797         huart->MspInitCallback = pCallback;
0798         break;
0799 
0800       case HAL_UART_MSPDEINIT_CB_ID :
0801         huart->MspDeInitCallback = pCallback;
0802         break;
0803 
0804       default :
0805         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0806 
0807         status =  HAL_ERROR;
0808         break;
0809     }
0810   }
0811   else if (huart->gState == HAL_UART_STATE_RESET)
0812   {
0813     switch (CallbackID)
0814     {
0815       case HAL_UART_MSPINIT_CB_ID :
0816         huart->MspInitCallback = pCallback;
0817         break;
0818 
0819       case HAL_UART_MSPDEINIT_CB_ID :
0820         huart->MspDeInitCallback = pCallback;
0821         break;
0822 
0823       default :
0824         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0825 
0826         status =  HAL_ERROR;
0827         break;
0828     }
0829   }
0830   else
0831   {
0832     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0833 
0834     status =  HAL_ERROR;
0835   }
0836 
0837   return status;
0838 }
0839 
0840 /**
0841   * @brief  Unregister an UART Callback
0842   *         UART callaback is redirected to the weak predefined callback
0843   * @note   The HAL_UART_UnRegisterCallback() may be called before HAL_UART_Init(), HAL_HalfDuplex_Init(),
0844   *         HAL_LIN_Init(), HAL_MultiProcessor_Init() or HAL_RS485Ex_Init() in HAL_UART_STATE_RESET to un-register
0845   *         callbacks for HAL_UART_MSPINIT_CB_ID and HAL_UART_MSPDEINIT_CB_ID
0846   * @param  huart uart handle
0847   * @param  CallbackID ID of the callback to be unregistered
0848   *         This parameter can be one of the following values:
0849   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
0850   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
0851   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
0852   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
0853   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
0854   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
0855   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
0856   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
0857   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
0858   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
0859   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
0860   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
0861   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
0862   * @retval HAL status
0863   */
0864 HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
0865 {
0866   HAL_StatusTypeDef status = HAL_OK;
0867 
0868   if (HAL_UART_STATE_READY == huart->gState)
0869   {
0870     switch (CallbackID)
0871     {
0872       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
0873         huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback    */
0874         break;
0875 
0876       case HAL_UART_TX_COMPLETE_CB_ID :
0877         huart->TxCpltCallback = HAL_UART_TxCpltCallback;                       /* Legacy weak TxCpltCallback         */
0878         break;
0879 
0880       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
0881         huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback     */
0882         break;
0883 
0884       case HAL_UART_RX_COMPLETE_CB_ID :
0885         huart->RxCpltCallback = HAL_UART_RxCpltCallback;                       /* Legacy weak RxCpltCallback         */
0886         break;
0887 
0888       case HAL_UART_ERROR_CB_ID :
0889         huart->ErrorCallback = HAL_UART_ErrorCallback;                         /* Legacy weak ErrorCallback          */
0890         break;
0891 
0892       case HAL_UART_ABORT_COMPLETE_CB_ID :
0893         huart->AbortCpltCallback = HAL_UART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback      */
0894         break;
0895 
0896       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
0897         huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak
0898                                                                                   AbortTransmitCpltCallback          */
0899         break;
0900 
0901       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
0902         huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback;   /* Legacy weak
0903                                                                                   AbortReceiveCpltCallback           */
0904         break;
0905 
0906       case HAL_UART_WAKEUP_CB_ID :
0907         huart->WakeupCallback = HAL_UARTEx_WakeupCallback;                     /* Legacy weak WakeupCallback         */
0908         break;
0909 
0910       case HAL_UART_RX_FIFO_FULL_CB_ID :
0911         huart->RxFifoFullCallback = HAL_UARTEx_RxFifoFullCallback;             /* Legacy weak RxFifoFullCallback     */
0912         break;
0913 
0914       case HAL_UART_TX_FIFO_EMPTY_CB_ID :
0915         huart->TxFifoEmptyCallback = HAL_UARTEx_TxFifoEmptyCallback;           /* Legacy weak TxFifoEmptyCallback    */
0916         break;
0917 
0918       case HAL_UART_MSPINIT_CB_ID :
0919         huart->MspInitCallback = HAL_UART_MspInit;                             /* Legacy weak MspInitCallback        */
0920         break;
0921 
0922       case HAL_UART_MSPDEINIT_CB_ID :
0923         huart->MspDeInitCallback = HAL_UART_MspDeInit;                         /* Legacy weak MspDeInitCallback      */
0924         break;
0925 
0926       default :
0927         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0928 
0929         status =  HAL_ERROR;
0930         break;
0931     }
0932   }
0933   else if (HAL_UART_STATE_RESET == huart->gState)
0934   {
0935     switch (CallbackID)
0936     {
0937       case HAL_UART_MSPINIT_CB_ID :
0938         huart->MspInitCallback = HAL_UART_MspInit;
0939         break;
0940 
0941       case HAL_UART_MSPDEINIT_CB_ID :
0942         huart->MspDeInitCallback = HAL_UART_MspDeInit;
0943         break;
0944 
0945       default :
0946         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0947 
0948         status =  HAL_ERROR;
0949         break;
0950     }
0951   }
0952   else
0953   {
0954     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0955 
0956     status =  HAL_ERROR;
0957   }
0958 
0959   return status;
0960 }
0961 
0962 /**
0963   * @brief  Register a User UART Rx Event Callback
0964   *         To be used instead of the weak predefined callback
0965   * @param  huart     Uart handle
0966   * @param  pCallback Pointer to the Rx Event Callback function
0967   * @retval HAL status
0968   */
0969 HAL_StatusTypeDef HAL_UART_RegisterRxEventCallback(UART_HandleTypeDef *huart, pUART_RxEventCallbackTypeDef pCallback)
0970 {
0971   HAL_StatusTypeDef status = HAL_OK;
0972 
0973   if (pCallback == NULL)
0974   {
0975     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0976 
0977     return HAL_ERROR;
0978   }
0979 
0980   if (huart->RxState == HAL_UART_STATE_READY)
0981   {
0982     huart->RxEventCallback = pCallback;
0983   }
0984   else
0985   {
0986     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
0987 
0988     status =  HAL_ERROR;
0989   }
0990 
0991   return status;
0992 }
0993 
0994 /**
0995   * @brief  UnRegister the UART Rx Event Callback
0996   *         UART Rx Event Callback is redirected to the weak HAL_UARTEx_RxEventCallback() predefined callback
0997   * @param  huart     Uart handle
0998   * @retval HAL status
0999   */
1000 HAL_StatusTypeDef HAL_UART_UnRegisterRxEventCallback(UART_HandleTypeDef *huart)
1001 {
1002   HAL_StatusTypeDef status = HAL_OK;
1003 
1004   if (huart->RxState == HAL_UART_STATE_READY)
1005   {
1006     huart->RxEventCallback = HAL_UARTEx_RxEventCallback; /* Legacy weak UART Rx Event Callback  */
1007   }
1008   else
1009   {
1010     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
1011 
1012     status =  HAL_ERROR;
1013   }
1014 
1015   return status;
1016 }
1017 
1018 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
1019 
1020 /**
1021   * @}
1022   */
1023 
1024 /** @defgroup UART_Exported_Functions_Group2 IO operation functions
1025   * @ingroup RTEMSBSPsARMSTM32H7
1026   * @brief UART Transmit/Receive functions
1027   *
1028 @verbatim
1029  ===============================================================================
1030                       ##### IO operation functions #####
1031  ===============================================================================
1032     This subsection provides a set of functions allowing to manage the UART asynchronous
1033     and Half duplex data transfers.
1034 
1035     (#) There are two mode of transfer:
1036        (+) Blocking mode: The communication is performed in polling mode.
1037            The HAL status of all data processing is returned by the same function
1038            after finishing transfer.
1039        (+) Non-Blocking mode: The communication is performed using Interrupts
1040            or DMA, These API's return the HAL status.
1041            The end of the data processing will be indicated through the
1042            dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
1043            using DMA mode.
1044            The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
1045            will be executed respectively at the end of the transmit or Receive process
1046            The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
1047 
1048     (#) Blocking mode API's are :
1049         (+) HAL_UART_Transmit()
1050         (+) HAL_UART_Receive()
1051 
1052     (#) Non-Blocking mode API's with Interrupt are :
1053         (+) HAL_UART_Transmit_IT()
1054         (+) HAL_UART_Receive_IT()
1055         (+) HAL_UART_IRQHandler()
1056 
1057     (#) Non-Blocking mode API's with DMA are :
1058         (+) HAL_UART_Transmit_DMA()
1059         (+) HAL_UART_Receive_DMA()
1060         (+) HAL_UART_DMAPause()
1061         (+) HAL_UART_DMAResume()
1062         (+) HAL_UART_DMAStop()
1063 
1064     (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
1065         (+) HAL_UART_TxHalfCpltCallback()
1066         (+) HAL_UART_TxCpltCallback()
1067         (+) HAL_UART_RxHalfCpltCallback()
1068         (+) HAL_UART_RxCpltCallback()
1069         (+) HAL_UART_ErrorCallback()
1070 
1071     (#) Non-Blocking mode transfers could be aborted using Abort API's :
1072         (+) HAL_UART_Abort()
1073         (+) HAL_UART_AbortTransmit()
1074         (+) HAL_UART_AbortReceive()
1075         (+) HAL_UART_Abort_IT()
1076         (+) HAL_UART_AbortTransmit_IT()
1077         (+) HAL_UART_AbortReceive_IT()
1078 
1079     (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
1080         (+) HAL_UART_AbortCpltCallback()
1081         (+) HAL_UART_AbortTransmitCpltCallback()
1082         (+) HAL_UART_AbortReceiveCpltCallback()
1083 
1084     (#) A Rx Event Reception Callback (Rx event notification) is available for Non_Blocking modes of enhanced
1085         reception services:
1086         (+) HAL_UARTEx_RxEventCallback()
1087 
1088     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
1089         Errors are handled as follows :
1090        (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
1091            to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error
1092            in Interrupt mode reception .
1093            Received character is then retrieved and stored in Rx buffer, Error code is set to allow user
1094            to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
1095            Transfer is kept ongoing on UART side.
1096            If user wants to abort it, Abort services should be called by user.
1097        (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
1098            This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
1099            Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback()
1100            user callback is executed.
1101 
1102     -@- In the Half duplex communication, it is forbidden to run the transmit
1103         and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
1104 
1105 @endverbatim
1106   * @{
1107   */
1108 
1109 /**
1110   * @brief Send an amount of data in blocking mode.
1111   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1112   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1113   *         of u16 provided through pData.
1114   * @note When FIFO mode is enabled, writing a data in the TDR register adds one
1115   *       data to the TXFIFO. Write operations to the TDR register are performed
1116   *       when TXFNF flag is set. From hardware perspective, TXFNF flag and
1117   *       TXE are mapped on the same bit-field.
1118   * @param huart   UART handle.
1119   * @param pData   Pointer to data buffer (u8 or u16 data elements).
1120   * @param Size    Amount of data elements (u8 or u16) to be sent.
1121   * @param Timeout Timeout duration.
1122   * @retval HAL status
1123   */
1124 HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
1125 {
1126   const uint8_t  *pdata8bits;
1127   const uint16_t *pdata16bits;
1128   uint32_t tickstart;
1129 
1130   /* Check that a Tx process is not already ongoing */
1131   if (huart->gState == HAL_UART_STATE_READY)
1132   {
1133     if ((pData == NULL) || (Size == 0U))
1134     {
1135       return  HAL_ERROR;
1136     }
1137 
1138     huart->ErrorCode = HAL_UART_ERROR_NONE;
1139     huart->gState = HAL_UART_STATE_BUSY_TX;
1140 
1141     /* Init tickstart for timeout management */
1142     tickstart = HAL_GetTick();
1143 
1144     huart->TxXferSize  = Size;
1145     huart->TxXferCount = Size;
1146 
1147     /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
1148     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1149     {
1150       pdata8bits  = NULL;
1151       pdata16bits = (const uint16_t *) pData;
1152     }
1153     else
1154     {
1155       pdata8bits  = pData;
1156       pdata16bits = NULL;
1157     }
1158 
1159     while (huart->TxXferCount > 0U)
1160     {
1161       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
1162       {
1163 
1164         huart->gState = HAL_UART_STATE_READY;
1165 
1166         return HAL_TIMEOUT;
1167       }
1168       if (pdata8bits == NULL)
1169       {
1170         huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
1171         pdata16bits++;
1172       }
1173       else
1174       {
1175         huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
1176         pdata8bits++;
1177       }
1178       huart->TxXferCount--;
1179     }
1180 
1181     if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
1182     {
1183       huart->gState = HAL_UART_STATE_READY;
1184 
1185       return HAL_TIMEOUT;
1186     }
1187 
1188     /* At end of Tx process, restore huart->gState to Ready */
1189     huart->gState = HAL_UART_STATE_READY;
1190 
1191     return HAL_OK;
1192   }
1193   else
1194   {
1195     return HAL_BUSY;
1196   }
1197 }
1198 
1199 /**
1200   * @brief Receive an amount of data in blocking mode.
1201   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1202   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1203   *         of u16 available through pData.
1204   * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
1205   *       is not empty. Read operations from the RDR register are performed when
1206   *       RXFNE flag is set. From hardware perspective, RXFNE flag and
1207   *       RXNE are mapped on the same bit-field.
1208   * @param huart   UART handle.
1209   * @param pData   Pointer to data buffer (u8 or u16 data elements).
1210   * @param Size    Amount of data elements (u8 or u16) to be received.
1211   * @param Timeout Timeout duration.
1212   * @retval HAL status
1213   */
1214 HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
1215 {
1216   uint8_t  *pdata8bits;
1217   uint16_t *pdata16bits;
1218   uint16_t uhMask;
1219   uint32_t tickstart;
1220 
1221   /* Check that a Rx process is not already ongoing */
1222   if (huart->RxState == HAL_UART_STATE_READY)
1223   {
1224     if ((pData == NULL) || (Size == 0U))
1225     {
1226       return  HAL_ERROR;
1227     }
1228 
1229     huart->ErrorCode = HAL_UART_ERROR_NONE;
1230     huart->RxState = HAL_UART_STATE_BUSY_RX;
1231     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1232 
1233     /* Init tickstart for timeout management */
1234     tickstart = HAL_GetTick();
1235 
1236     huart->RxXferSize  = Size;
1237     huart->RxXferCount = Size;
1238 
1239     /* Computation of UART mask to apply to RDR register */
1240     UART_MASK_COMPUTATION(huart);
1241     uhMask = huart->Mask;
1242 
1243     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
1244     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1245     {
1246       pdata8bits  = NULL;
1247       pdata16bits = (uint16_t *) pData;
1248     }
1249     else
1250     {
1251       pdata8bits  = pData;
1252       pdata16bits = NULL;
1253     }
1254 
1255     /* as long as data have to be received */
1256     while (huart->RxXferCount > 0U)
1257     {
1258       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
1259       {
1260         huart->RxState = HAL_UART_STATE_READY;
1261 
1262         return HAL_TIMEOUT;
1263       }
1264       if (pdata8bits == NULL)
1265       {
1266         *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
1267         pdata16bits++;
1268       }
1269       else
1270       {
1271         *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
1272         pdata8bits++;
1273       }
1274       huart->RxXferCount--;
1275     }
1276 
1277     /* At end of Rx process, restore huart->RxState to Ready */
1278     huart->RxState = HAL_UART_STATE_READY;
1279 
1280     return HAL_OK;
1281   }
1282   else
1283   {
1284     return HAL_BUSY;
1285   }
1286 }
1287 
1288 /**
1289   * @brief Send an amount of data in interrupt mode.
1290   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1291   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1292   *         of u16 provided through pData.
1293   * @param huart UART handle.
1294   * @param pData Pointer to data buffer (u8 or u16 data elements).
1295   * @param Size  Amount of data elements (u8 or u16) to be sent.
1296   * @retval HAL status
1297   */
1298 HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1299 {
1300   /* Check that a Tx process is not already ongoing */
1301   if (huart->gState == HAL_UART_STATE_READY)
1302   {
1303     if ((pData == NULL) || (Size == 0U))
1304     {
1305       return HAL_ERROR;
1306     }
1307 
1308     huart->pTxBuffPtr  = pData;
1309     huart->TxXferSize  = Size;
1310     huart->TxXferCount = Size;
1311     huart->TxISR       = NULL;
1312 
1313     huart->ErrorCode = HAL_UART_ERROR_NONE;
1314     huart->gState = HAL_UART_STATE_BUSY_TX;
1315 
1316     /* Configure Tx interrupt processing */
1317     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1318     {
1319       /* Set the Tx ISR function pointer according to the data word length */
1320       if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1321       {
1322         huart->TxISR = UART_TxISR_16BIT_FIFOEN;
1323       }
1324       else
1325       {
1326         huart->TxISR = UART_TxISR_8BIT_FIFOEN;
1327       }
1328 
1329       /* Enable the TX FIFO threshold interrupt */
1330       ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1331     }
1332     else
1333     {
1334       /* Set the Tx ISR function pointer according to the data word length */
1335       if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
1336       {
1337         huart->TxISR = UART_TxISR_16BIT;
1338       }
1339       else
1340       {
1341         huart->TxISR = UART_TxISR_8BIT;
1342       }
1343 
1344       /* Enable the Transmit Data Register Empty interrupt */
1345       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
1346     }
1347 
1348     return HAL_OK;
1349   }
1350   else
1351   {
1352     return HAL_BUSY;
1353   }
1354 }
1355 
1356 /**
1357   * @brief Receive an amount of data in interrupt mode.
1358   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1359   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1360   *         of u16 available through pData.
1361   * @param huart UART handle.
1362   * @param pData Pointer to data buffer (u8 or u16 data elements).
1363   * @param Size  Amount of data elements (u8 or u16) to be received.
1364   * @retval HAL status
1365   */
1366 HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1367 {
1368   /* Check that a Rx process is not already ongoing */
1369   if (huart->RxState == HAL_UART_STATE_READY)
1370   {
1371     if ((pData == NULL) || (Size == 0U))
1372     {
1373       return HAL_ERROR;
1374     }
1375 
1376     /* Set Reception type to Standard reception */
1377     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1378 
1379     if (!(IS_LPUART_INSTANCE(huart->Instance)))
1380     {
1381       /* Check that USART RTOEN bit is set */
1382       if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1383       {
1384         /* Enable the UART Receiver Timeout Interrupt */
1385         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1386       }
1387     }
1388 
1389     return (UART_Start_Receive_IT(huart, pData, Size));
1390   }
1391   else
1392   {
1393     return HAL_BUSY;
1394   }
1395 }
1396 
1397 /**
1398   * @brief Send an amount of data in DMA mode.
1399   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1400   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
1401   *         of u16 provided through pData.
1402   * @param huart UART handle.
1403   * @param pData Pointer to data buffer (u8 or u16 data elements).
1404   * @param Size  Amount of data elements (u8 or u16) to be sent.
1405   * @retval HAL status
1406   */
1407 HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
1408 {
1409   /* Check that a Tx process is not already ongoing */
1410   if (huart->gState == HAL_UART_STATE_READY)
1411   {
1412     if ((pData == NULL) || (Size == 0U))
1413     {
1414       return HAL_ERROR;
1415     }
1416 
1417     huart->pTxBuffPtr  = pData;
1418     huart->TxXferSize  = Size;
1419     huart->TxXferCount = Size;
1420 
1421     huart->ErrorCode = HAL_UART_ERROR_NONE;
1422     huart->gState = HAL_UART_STATE_BUSY_TX;
1423 
1424     if (huart->hdmatx != NULL)
1425     {
1426       /* Set the UART DMA transfer complete callback */
1427       huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
1428 
1429       /* Set the UART DMA Half transfer complete callback */
1430       huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
1431 
1432       /* Set the DMA error callback */
1433       huart->hdmatx->XferErrorCallback = UART_DMAError;
1434 
1435       /* Set the DMA abort callback */
1436       huart->hdmatx->XferAbortCallback = NULL;
1437 
1438       /* Enable the UART transmit DMA channel */
1439       if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
1440       {
1441         /* Set error code to DMA */
1442         huart->ErrorCode = HAL_UART_ERROR_DMA;
1443 
1444         /* Restore huart->gState to ready */
1445         huart->gState = HAL_UART_STATE_READY;
1446 
1447         return HAL_ERROR;
1448       }
1449     }
1450     /* Clear the TC flag in the ICR register */
1451     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
1452 
1453     /* Enable the DMA transfer for transmit request by setting the DMAT bit
1454     in the UART CR3 register */
1455     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1456 
1457     return HAL_OK;
1458   }
1459   else
1460   {
1461     return HAL_BUSY;
1462   }
1463 }
1464 
1465 /**
1466   * @brief Receive an amount of data in DMA mode.
1467   * @note   When the UART parity is enabled (PCE = 1), the received data contain
1468   *         the parity bit (MSB position).
1469   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
1470   *         the received data is handled as a set of u16. In this case, Size must indicate the number
1471   *         of u16 available through pData.
1472   * @param huart UART handle.
1473   * @param pData Pointer to data buffer (u8 or u16 data elements).
1474   * @param Size  Amount of data elements (u8 or u16) to be received.
1475   * @retval HAL status
1476   */
1477 HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
1478 {
1479   /* Check that a Rx process is not already ongoing */
1480   if (huart->RxState == HAL_UART_STATE_READY)
1481   {
1482     if ((pData == NULL) || (Size == 0U))
1483     {
1484       return HAL_ERROR;
1485     }
1486 
1487     /* Set Reception type to Standard reception */
1488     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1489 
1490     if (!(IS_LPUART_INSTANCE(huart->Instance)))
1491     {
1492       /* Check that USART RTOEN bit is set */
1493       if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
1494       {
1495         /* Enable the UART Receiver Timeout Interrupt */
1496         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
1497       }
1498     }
1499 
1500     return (UART_Start_Receive_DMA(huart, pData, Size));
1501   }
1502   else
1503   {
1504     return HAL_BUSY;
1505   }
1506 }
1507 
1508 /**
1509   * @brief Pause the DMA Transfer.
1510   * @param huart UART handle.
1511   * @retval HAL status
1512   */
1513 HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
1514 {
1515   const HAL_UART_StateTypeDef gstate = huart->gState;
1516   const HAL_UART_StateTypeDef rxstate = huart->RxState;
1517 
1518   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1519       (gstate == HAL_UART_STATE_BUSY_TX))
1520   {
1521     /* Disable the UART DMA Tx request */
1522     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1523   }
1524   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1525       (rxstate == HAL_UART_STATE_BUSY_RX))
1526   {
1527     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
1528     ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1529     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
1530 
1531     /* Disable the UART DMA Rx request */
1532     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1533   }
1534 
1535   return HAL_OK;
1536 }
1537 
1538 /**
1539   * @brief Resume the DMA Transfer.
1540   * @param huart UART handle.
1541   * @retval HAL status
1542   */
1543 HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
1544 {
1545   if (huart->gState == HAL_UART_STATE_BUSY_TX)
1546   {
1547     /* Enable the UART DMA Tx request */
1548     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1549   }
1550   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
1551   {
1552     /* Clear the Overrun flag before resuming the Rx transfer */
1553     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
1554 
1555     /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
1556     if (huart->Init.Parity != UART_PARITY_NONE)
1557     {
1558       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
1559     }
1560     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
1561 
1562     /* Enable the UART DMA Rx request */
1563     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1564   }
1565 
1566   return HAL_OK;
1567 }
1568 
1569 /**
1570   * @brief Stop the DMA Transfer.
1571   * @param huart UART handle.
1572   * @retval HAL status
1573   */
1574 HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
1575 {
1576   /* The Lock is not implemented on this API to allow the user application
1577      to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
1578      HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
1579      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
1580      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
1581      the stream and the corresponding call back is executed. */
1582 
1583   const HAL_UART_StateTypeDef gstate = huart->gState;
1584   const HAL_UART_StateTypeDef rxstate = huart->RxState;
1585 
1586   /* Stop UART DMA Tx request if ongoing */
1587   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
1588       (gstate == HAL_UART_STATE_BUSY_TX))
1589   {
1590     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1591 
1592     /* Abort the UART DMA Tx channel */
1593     if (huart->hdmatx != NULL)
1594     {
1595       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1596       {
1597         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1598         {
1599           /* Set error code to DMA */
1600           huart->ErrorCode = HAL_UART_ERROR_DMA;
1601 
1602           return HAL_TIMEOUT;
1603         }
1604       }
1605     }
1606 
1607     UART_EndTxTransfer(huart);
1608   }
1609 
1610   /* Stop UART DMA Rx request if ongoing */
1611   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
1612       (rxstate == HAL_UART_STATE_BUSY_RX))
1613   {
1614     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1615 
1616     /* Abort the UART DMA Rx channel */
1617     if (huart->hdmarx != NULL)
1618     {
1619       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1620       {
1621         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1622         {
1623           /* Set error code to DMA */
1624           huart->ErrorCode = HAL_UART_ERROR_DMA;
1625 
1626           return HAL_TIMEOUT;
1627         }
1628       }
1629     }
1630 
1631     UART_EndRxTransfer(huart);
1632   }
1633 
1634   return HAL_OK;
1635 }
1636 
1637 /**
1638   * @brief  Abort ongoing transfers (blocking mode).
1639   * @param  huart UART handle.
1640   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1641   *         This procedure performs following operations :
1642   *           - Disable UART Interrupts (Tx and Rx)
1643   *           - Disable the DMA transfer in the peripheral register (if enabled)
1644   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1645   *           - Set handle State to READY
1646   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1647   * @retval HAL status
1648   */
1649 HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
1650 {
1651   /* Disable TXE, TC, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
1652   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE |
1653                                           USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
1654   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE);
1655 
1656   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1657   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1658   {
1659     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1660   }
1661 
1662   /* Abort the UART DMA Tx channel if enabled */
1663   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1664   {
1665     /* Disable the UART DMA Tx request if enabled */
1666     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1667 
1668     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1669     if (huart->hdmatx != NULL)
1670     {
1671       /* Set the UART DMA Abort callback to Null.
1672          No call back execution at end of DMA abort procedure */
1673       huart->hdmatx->XferAbortCallback = NULL;
1674 
1675       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1676       {
1677         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1678         {
1679           /* Set error code to DMA */
1680           huart->ErrorCode = HAL_UART_ERROR_DMA;
1681 
1682           return HAL_TIMEOUT;
1683         }
1684       }
1685     }
1686   }
1687 
1688   /* Abort the UART DMA Rx channel if enabled */
1689   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1690   {
1691     /* Disable the UART DMA Rx request if enabled */
1692     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1693 
1694     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1695     if (huart->hdmarx != NULL)
1696     {
1697       /* Set the UART DMA Abort callback to Null.
1698          No call back execution at end of DMA abort procedure */
1699       huart->hdmarx->XferAbortCallback = NULL;
1700 
1701       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1702       {
1703         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1704         {
1705           /* Set error code to DMA */
1706           huart->ErrorCode = HAL_UART_ERROR_DMA;
1707 
1708           return HAL_TIMEOUT;
1709         }
1710       }
1711     }
1712   }
1713 
1714   /* Reset Tx and Rx transfer counters */
1715   huart->TxXferCount = 0U;
1716   huart->RxXferCount = 0U;
1717 
1718   /* Clear the Error flags in the ICR register */
1719   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1720 
1721   /* Flush the whole TX FIFO (if needed) */
1722   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1723   {
1724     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1725   }
1726 
1727   /* Discard the received data */
1728   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1729 
1730   /* Restore huart->gState and huart->RxState to Ready */
1731   huart->gState  = HAL_UART_STATE_READY;
1732   huart->RxState = HAL_UART_STATE_READY;
1733   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1734 
1735   huart->ErrorCode = HAL_UART_ERROR_NONE;
1736 
1737   return HAL_OK;
1738 }
1739 
1740 /**
1741   * @brief  Abort ongoing Transmit transfer (blocking mode).
1742   * @param  huart UART handle.
1743   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
1744   *         This procedure performs following operations :
1745   *           - Disable UART Interrupts (Tx)
1746   *           - Disable the DMA transfer in the peripheral register (if enabled)
1747   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1748   *           - Set handle State to READY
1749   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1750   * @retval HAL status
1751   */
1752 HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
1753 {
1754   /* Disable TCIE, TXEIE and TXFTIE interrupts */
1755   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
1756   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
1757 
1758   /* Abort the UART DMA Tx channel if enabled */
1759   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1760   {
1761     /* Disable the UART DMA Tx request if enabled */
1762     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1763 
1764     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
1765     if (huart->hdmatx != NULL)
1766     {
1767       /* Set the UART DMA Abort callback to Null.
1768          No call back execution at end of DMA abort procedure */
1769       huart->hdmatx->XferAbortCallback = NULL;
1770 
1771       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
1772       {
1773         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
1774         {
1775           /* Set error code to DMA */
1776           huart->ErrorCode = HAL_UART_ERROR_DMA;
1777 
1778           return HAL_TIMEOUT;
1779         }
1780       }
1781     }
1782   }
1783 
1784   /* Reset Tx transfer counter */
1785   huart->TxXferCount = 0U;
1786 
1787   /* Flush the whole TX FIFO (if needed) */
1788   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1789   {
1790     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1791   }
1792 
1793   /* Restore huart->gState to Ready */
1794   huart->gState = HAL_UART_STATE_READY;
1795 
1796   return HAL_OK;
1797 }
1798 
1799 /**
1800   * @brief  Abort ongoing Receive transfer (blocking mode).
1801   * @param  huart UART handle.
1802   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
1803   *         This procedure performs following operations :
1804   *           - Disable UART Interrupts (Rx)
1805   *           - Disable the DMA transfer in the peripheral register (if enabled)
1806   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
1807   *           - Set handle State to READY
1808   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
1809   * @retval HAL status
1810   */
1811 HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
1812 {
1813   /* Disable PEIE, EIE, RXNEIE and RXFTIE interrupts */
1814   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
1815   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE | USART_CR3_RXFTIE);
1816 
1817   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1818   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1819   {
1820     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1821   }
1822 
1823   /* Abort the UART DMA Rx channel if enabled */
1824   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1825   {
1826     /* Disable the UART DMA Rx request if enabled */
1827     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1828 
1829     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
1830     if (huart->hdmarx != NULL)
1831     {
1832       /* Set the UART DMA Abort callback to Null.
1833          No call back execution at end of DMA abort procedure */
1834       huart->hdmarx->XferAbortCallback = NULL;
1835 
1836       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
1837       {
1838         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
1839         {
1840           /* Set error code to DMA */
1841           huart->ErrorCode = HAL_UART_ERROR_DMA;
1842 
1843           return HAL_TIMEOUT;
1844         }
1845       }
1846     }
1847   }
1848 
1849   /* Reset Rx transfer counter */
1850   huart->RxXferCount = 0U;
1851 
1852   /* Clear the Error flags in the ICR register */
1853   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1854 
1855   /* Discard the received data */
1856   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1857 
1858   /* Restore huart->RxState to Ready */
1859   huart->RxState = HAL_UART_STATE_READY;
1860   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
1861 
1862   return HAL_OK;
1863 }
1864 
1865 /**
1866   * @brief  Abort ongoing transfers (Interrupt mode).
1867   * @param  huart UART handle.
1868   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
1869   *         This procedure performs following operations :
1870   *           - Disable UART Interrupts (Tx and Rx)
1871   *           - Disable the DMA transfer in the peripheral register (if enabled)
1872   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
1873   *           - Set handle State to READY
1874   *           - At abort completion, call user abort complete callback
1875   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
1876   *         considered as completed only when user abort complete callback is executed (not when exiting function).
1877   * @retval HAL status
1878   */
1879 HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
1880 {
1881   uint32_t abortcplt = 1U;
1882 
1883   /* Disable interrupts */
1884   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_TCIE | USART_CR1_RXNEIE_RXFNEIE |
1885                                           USART_CR1_TXEIE_TXFNFIE));
1886   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
1887 
1888   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
1889   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
1890   {
1891     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
1892   }
1893 
1894   /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
1895      before any call to DMA Abort functions */
1896   /* DMA Tx Handle is valid */
1897   if (huart->hdmatx != NULL)
1898   {
1899     /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
1900        Otherwise, set it to NULL */
1901     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1902     {
1903       huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
1904     }
1905     else
1906     {
1907       huart->hdmatx->XferAbortCallback = NULL;
1908     }
1909   }
1910   /* DMA Rx Handle is valid */
1911   if (huart->hdmarx != NULL)
1912   {
1913     /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
1914        Otherwise, set it to NULL */
1915     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1916     {
1917       huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
1918     }
1919     else
1920     {
1921       huart->hdmarx->XferAbortCallback = NULL;
1922     }
1923   }
1924 
1925   /* Abort the UART DMA Tx channel if enabled */
1926   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
1927   {
1928     /* Disable DMA Tx at UART level */
1929     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
1930 
1931     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
1932     if (huart->hdmatx != NULL)
1933     {
1934       /* UART Tx DMA Abort callback has already been initialised :
1935          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1936 
1937       /* Abort DMA TX */
1938       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
1939       {
1940         huart->hdmatx->XferAbortCallback = NULL;
1941       }
1942       else
1943       {
1944         abortcplt = 0U;
1945       }
1946     }
1947   }
1948 
1949   /* Abort the UART DMA Rx channel if enabled */
1950   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
1951   {
1952     /* Disable the UART DMA Rx request if enabled */
1953     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
1954 
1955     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
1956     if (huart->hdmarx != NULL)
1957     {
1958       /* UART Rx DMA Abort callback has already been initialised :
1959          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
1960 
1961       /* Abort DMA RX */
1962       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
1963       {
1964         huart->hdmarx->XferAbortCallback = NULL;
1965         abortcplt = 1U;
1966       }
1967       else
1968       {
1969         abortcplt = 0U;
1970       }
1971     }
1972   }
1973 
1974   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
1975   if (abortcplt == 1U)
1976   {
1977     /* Reset Tx and Rx transfer counters */
1978     huart->TxXferCount = 0U;
1979     huart->RxXferCount = 0U;
1980 
1981     /* Clear ISR function pointers */
1982     huart->RxISR = NULL;
1983     huart->TxISR = NULL;
1984 
1985     /* Reset errorCode */
1986     huart->ErrorCode = HAL_UART_ERROR_NONE;
1987 
1988     /* Clear the Error flags in the ICR register */
1989     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
1990 
1991     /* Flush the whole TX FIFO (if needed) */
1992     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
1993     {
1994       __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
1995     }
1996 
1997     /* Discard the received data */
1998     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
1999 
2000     /* Restore huart->gState and huart->RxState to Ready */
2001     huart->gState  = HAL_UART_STATE_READY;
2002     huart->RxState = HAL_UART_STATE_READY;
2003     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2004 
2005     /* As no DMA to be aborted, call directly user Abort complete callback */
2006 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2007     /* Call registered Abort complete callback */
2008     huart->AbortCpltCallback(huart);
2009 #else
2010     /* Call legacy weak Abort complete callback */
2011     HAL_UART_AbortCpltCallback(huart);
2012 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2013   }
2014 
2015   return HAL_OK;
2016 }
2017 
2018 /**
2019   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
2020   * @param  huart UART handle.
2021   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
2022   *         This procedure performs following operations :
2023   *           - Disable UART Interrupts (Tx)
2024   *           - Disable the DMA transfer in the peripheral register (if enabled)
2025   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2026   *           - Set handle State to READY
2027   *           - At abort completion, call user abort complete callback
2028   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
2029   *         considered as completed only when user abort complete callback is executed (not when exiting function).
2030   * @retval HAL status
2031   */
2032 HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
2033 {
2034   /* Disable interrupts */
2035   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TCIE | USART_CR1_TXEIE_TXFNFIE));
2036   ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
2037 
2038   /* Abort the UART DMA Tx channel if enabled */
2039   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
2040   {
2041     /* Disable the UART DMA Tx request if enabled */
2042     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
2043 
2044     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
2045     if (huart->hdmatx != NULL)
2046     {
2047       /* Set the UART DMA Abort callback :
2048          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2049       huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
2050 
2051       /* Abort DMA TX */
2052       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
2053       {
2054         /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
2055         huart->hdmatx->XferAbortCallback(huart->hdmatx);
2056       }
2057     }
2058     else
2059     {
2060       /* Reset Tx transfer counter */
2061       huart->TxXferCount = 0U;
2062 
2063       /* Clear TxISR function pointers */
2064       huart->TxISR = NULL;
2065 
2066       /* Restore huart->gState to Ready */
2067       huart->gState = HAL_UART_STATE_READY;
2068 
2069       /* As no DMA to be aborted, call directly user Abort complete callback */
2070 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2071       /* Call registered Abort Transmit Complete Callback */
2072       huart->AbortTransmitCpltCallback(huart);
2073 #else
2074       /* Call legacy weak Abort Transmit Complete Callback */
2075       HAL_UART_AbortTransmitCpltCallback(huart);
2076 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2077     }
2078   }
2079   else
2080   {
2081     /* Reset Tx transfer counter */
2082     huart->TxXferCount = 0U;
2083 
2084     /* Clear TxISR function pointers */
2085     huart->TxISR = NULL;
2086 
2087     /* Flush the whole TX FIFO (if needed) */
2088     if (huart->FifoMode == UART_FIFOMODE_ENABLE)
2089     {
2090       __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
2091     }
2092 
2093     /* Restore huart->gState to Ready */
2094     huart->gState = HAL_UART_STATE_READY;
2095 
2096     /* As no DMA to be aborted, call directly user Abort complete callback */
2097 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2098     /* Call registered Abort Transmit Complete Callback */
2099     huart->AbortTransmitCpltCallback(huart);
2100 #else
2101     /* Call legacy weak Abort Transmit Complete Callback */
2102     HAL_UART_AbortTransmitCpltCallback(huart);
2103 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2104   }
2105 
2106   return HAL_OK;
2107 }
2108 
2109 /**
2110   * @brief  Abort ongoing Receive transfer (Interrupt mode).
2111   * @param  huart UART handle.
2112   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
2113   *         This procedure performs following operations :
2114   *           - Disable UART Interrupts (Rx)
2115   *           - Disable the DMA transfer in the peripheral register (if enabled)
2116   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
2117   *           - Set handle State to READY
2118   *           - At abort completion, call user abort complete callback
2119   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
2120   *         considered as completed only when user abort complete callback is executed (not when exiting function).
2121   * @retval HAL status
2122   */
2123 HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
2124 {
2125   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
2126   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE));
2127   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2128 
2129   /* If Reception till IDLE event was ongoing, disable IDLEIE interrupt */
2130   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2131   {
2132     ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_IDLEIE));
2133   }
2134 
2135   /* Abort the UART DMA Rx channel if enabled */
2136   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2137   {
2138     /* Disable the UART DMA Rx request if enabled */
2139     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2140 
2141     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
2142     if (huart->hdmarx != NULL)
2143     {
2144       /* Set the UART DMA Abort callback :
2145          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
2146       huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
2147 
2148       /* Abort DMA RX */
2149       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2150       {
2151         /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2152         huart->hdmarx->XferAbortCallback(huart->hdmarx);
2153       }
2154     }
2155     else
2156     {
2157       /* Reset Rx transfer counter */
2158       huart->RxXferCount = 0U;
2159 
2160       /* Clear RxISR function pointer */
2161       huart->pRxBuffPtr = NULL;
2162 
2163       /* Clear the Error flags in the ICR register */
2164       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2165 
2166       /* Discard the received data */
2167       __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
2168 
2169       /* Restore huart->RxState to Ready */
2170       huart->RxState = HAL_UART_STATE_READY;
2171       huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2172 
2173       /* As no DMA to be aborted, call directly user Abort complete callback */
2174 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2175       /* Call registered Abort Receive Complete Callback */
2176       huart->AbortReceiveCpltCallback(huart);
2177 #else
2178       /* Call legacy weak Abort Receive Complete Callback */
2179       HAL_UART_AbortReceiveCpltCallback(huart);
2180 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2181     }
2182   }
2183   else
2184   {
2185     /* Reset Rx transfer counter */
2186     huart->RxXferCount = 0U;
2187 
2188     /* Clear RxISR function pointer */
2189     huart->pRxBuffPtr = NULL;
2190 
2191     /* Clear the Error flags in the ICR register */
2192     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
2193 
2194     /* Restore huart->RxState to Ready */
2195     huart->RxState = HAL_UART_STATE_READY;
2196     huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2197 
2198     /* As no DMA to be aborted, call directly user Abort complete callback */
2199 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2200     /* Call registered Abort Receive Complete Callback */
2201     huart->AbortReceiveCpltCallback(huart);
2202 #else
2203     /* Call legacy weak Abort Receive Complete Callback */
2204     HAL_UART_AbortReceiveCpltCallback(huart);
2205 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2206   }
2207 
2208   return HAL_OK;
2209 }
2210 
2211 /**
2212   * @brief Handle UART interrupt request.
2213   * @param huart UART handle.
2214   * @retval None
2215   */
2216 void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
2217 {
2218   uint32_t isrflags   = READ_REG(huart->Instance->ISR);
2219   uint32_t cr1its     = READ_REG(huart->Instance->CR1);
2220   uint32_t cr3its     = READ_REG(huart->Instance->CR3);
2221 
2222   uint32_t errorflags;
2223   uint32_t errorcode;
2224 
2225   /* If no error occurs */
2226   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
2227   if (errorflags == 0U)
2228   {
2229     /* UART in mode Receiver ---------------------------------------------------*/
2230     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2231         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2232             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2233     {
2234       if (huart->RxISR != NULL)
2235       {
2236         huart->RxISR(huart);
2237       }
2238       return;
2239     }
2240   }
2241 
2242   /* If some errors occur */
2243   if ((errorflags != 0U)
2244       && ((((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
2245            || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE)) != 0U))))
2246   {
2247     /* UART parity error interrupt occurred -------------------------------------*/
2248     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
2249     {
2250       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
2251 
2252       huart->ErrorCode |= HAL_UART_ERROR_PE;
2253     }
2254 
2255     /* UART frame error interrupt occurred --------------------------------------*/
2256     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2257     {
2258       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
2259 
2260       huart->ErrorCode |= HAL_UART_ERROR_FE;
2261     }
2262 
2263     /* UART noise error interrupt occurred --------------------------------------*/
2264     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
2265     {
2266       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
2267 
2268       huart->ErrorCode |= HAL_UART_ERROR_NE;
2269     }
2270 
2271     /* UART Over-Run interrupt occurred -----------------------------------------*/
2272     if (((isrflags & USART_ISR_ORE) != 0U)
2273         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
2274             ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
2275     {
2276       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
2277 
2278       huart->ErrorCode |= HAL_UART_ERROR_ORE;
2279     }
2280 
2281     /* UART Receiver Timeout interrupt occurred ---------------------------------*/
2282     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
2283     {
2284       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
2285 
2286       huart->ErrorCode |= HAL_UART_ERROR_RTO;
2287     }
2288 
2289     /* Call UART Error Call back function if need be ----------------------------*/
2290     if (huart->ErrorCode != HAL_UART_ERROR_NONE)
2291     {
2292       /* UART in mode Receiver --------------------------------------------------*/
2293       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
2294           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
2295               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
2296       {
2297         if (huart->RxISR != NULL)
2298         {
2299           huart->RxISR(huart);
2300         }
2301       }
2302 
2303       /* If Error is to be considered as blocking :
2304           - Receiver Timeout error in Reception
2305           - Overrun error in Reception
2306           - any error occurs in DMA mode reception
2307       */
2308       errorcode = huart->ErrorCode;
2309       if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
2310           ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
2311       {
2312         /* Blocking error : transfer is aborted
2313            Set the UART state ready to be able to start again the process,
2314            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
2315         UART_EndRxTransfer(huart);
2316 
2317         /* Abort the UART DMA Rx channel if enabled */
2318         if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2319         {
2320           /* Disable the UART DMA Rx request if enabled */
2321           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2322 
2323           /* Abort the UART DMA Rx channel */
2324           if (huart->hdmarx != NULL)
2325           {
2326             /* Set the UART DMA Abort callback :
2327                will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
2328             huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
2329 
2330             /* Abort DMA RX */
2331             if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
2332             {
2333               /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
2334               huart->hdmarx->XferAbortCallback(huart->hdmarx);
2335             }
2336           }
2337           else
2338           {
2339             /* Call user error callback */
2340 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2341             /*Call registered error callback*/
2342             huart->ErrorCallback(huart);
2343 #else
2344             /*Call legacy weak error callback*/
2345             HAL_UART_ErrorCallback(huart);
2346 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2347 
2348           }
2349         }
2350         else
2351         {
2352           /* Call user error callback */
2353 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2354           /*Call registered error callback*/
2355           huart->ErrorCallback(huart);
2356 #else
2357           /*Call legacy weak error callback*/
2358           HAL_UART_ErrorCallback(huart);
2359 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2360         }
2361       }
2362       else
2363       {
2364         /* Non Blocking error : transfer could go on.
2365            Error is notified to user through user error callback */
2366 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2367         /*Call registered error callback*/
2368         huart->ErrorCallback(huart);
2369 #else
2370         /*Call legacy weak error callback*/
2371         HAL_UART_ErrorCallback(huart);
2372 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2373         huart->ErrorCode = HAL_UART_ERROR_NONE;
2374       }
2375     }
2376     return;
2377 
2378   } /* End if some error occurs */
2379 
2380   /* Check current reception Mode :
2381      If Reception till IDLE event has been selected : */
2382   if ((huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
2383       && ((isrflags & USART_ISR_IDLE) != 0U)
2384       && ((cr1its & USART_ISR_IDLE) != 0U))
2385   {
2386     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
2387 
2388     /* Check if DMA mode is enabled in UART */
2389     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
2390     {
2391       /* DMA mode enabled */
2392       /* Check received length : If all expected data are received, do nothing,
2393          (DMA cplt callback will be called).
2394          Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2395       uint16_t nb_remaining_rx_data = (uint16_t) __HAL_DMA_GET_COUNTER(huart->hdmarx);
2396       if ((nb_remaining_rx_data > 0U)
2397           && (nb_remaining_rx_data < huart->RxXferSize))
2398       {
2399         /* Reception is not complete */
2400         huart->RxXferCount = nb_remaining_rx_data;
2401 
2402         /* In Normal mode, end DMA xfer and HAL UART Rx process*/
2403         if (huart->hdmarx->Init.Mode != DMA_CIRCULAR)
2404         {
2405           /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
2406           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
2407           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
2408 
2409           /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
2410              in the UART CR3 register */
2411           ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
2412 
2413           /* At end of Rx process, restore huart->RxState to Ready */
2414           huart->RxState = HAL_UART_STATE_READY;
2415           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2416 
2417           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2418 
2419           /* Last bytes received, so no need as the abort is immediate */
2420           (void)HAL_DMA_Abort(huart->hdmarx);
2421         }
2422 
2423         /* Initialize type of RxEvent that correspond to RxEvent callback execution;
2424            In this case, Rx Event type is Idle Event */
2425         huart->RxEventType = HAL_UART_RXEVENT_IDLE;
2426 
2427 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2428         /*Call registered Rx Event callback*/
2429         huart->RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2430 #else
2431         /*Call legacy weak Rx Event callback*/
2432         HAL_UARTEx_RxEventCallback(huart, (huart->RxXferSize - huart->RxXferCount));
2433 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2434       }
2435       return;
2436     }
2437     else
2438     {
2439       /* DMA mode not enabled */
2440       /* Check received length : If all expected data are received, do nothing.
2441          Otherwise, if at least one data has already been received, IDLE event is to be notified to user */
2442       uint16_t nb_rx_data = huart->RxXferSize - huart->RxXferCount;
2443       if ((huart->RxXferCount > 0U)
2444           && (nb_rx_data > 0U))
2445       {
2446         /* Disable the UART Parity Error Interrupt and RXNE interrupts */
2447         ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
2448 
2449         /* Disable the UART Error Interrupt:(Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
2450         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
2451 
2452         /* Rx process is completed, restore huart->RxState to Ready */
2453         huart->RxState = HAL_UART_STATE_READY;
2454         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
2455 
2456         /* Clear RxISR function pointer */
2457         huart->RxISR = NULL;
2458 
2459         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
2460 
2461         /* Initialize type of RxEvent that correspond to RxEvent callback execution;
2462            In this case, Rx Event type is Idle Event */
2463         huart->RxEventType = HAL_UART_RXEVENT_IDLE;
2464 
2465 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2466         /*Call registered Rx complete callback*/
2467         huart->RxEventCallback(huart, nb_rx_data);
2468 #else
2469         /*Call legacy weak Rx Event callback*/
2470         HAL_UARTEx_RxEventCallback(huart, nb_rx_data);
2471 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
2472       }
2473       return;
2474     }
2475   }
2476 
2477   /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
2478   if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
2479   {
2480     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
2481 
2482     /* UART Rx state is not reset as a reception process might be ongoing.
2483        If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
2484 
2485 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2486     /* Call registered Wakeup Callback */
2487     huart->WakeupCallback(huart);
2488 #else
2489     /* Call legacy weak Wakeup Callback */
2490     HAL_UARTEx_WakeupCallback(huart);
2491 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2492     return;
2493   }
2494 
2495   /* UART in mode Transmitter ------------------------------------------------*/
2496   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
2497       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
2498           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
2499   {
2500     if (huart->TxISR != NULL)
2501     {
2502       huart->TxISR(huart);
2503     }
2504     return;
2505   }
2506 
2507   /* UART in mode Transmitter (transmission end) -----------------------------*/
2508   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
2509   {
2510     UART_EndTransmit_IT(huart);
2511     return;
2512   }
2513 
2514   /* UART TX Fifo Empty occurred ----------------------------------------------*/
2515   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
2516   {
2517 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2518     /* Call registered Tx Fifo Empty Callback */
2519     huart->TxFifoEmptyCallback(huart);
2520 #else
2521     /* Call legacy weak Tx Fifo Empty Callback */
2522     HAL_UARTEx_TxFifoEmptyCallback(huart);
2523 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2524     return;
2525   }
2526 
2527   /* UART RX Fifo Full occurred ----------------------------------------------*/
2528   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
2529   {
2530 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2531     /* Call registered Rx Fifo Full Callback */
2532     huart->RxFifoFullCallback(huart);
2533 #else
2534     /* Call legacy weak Rx Fifo Full Callback */
2535     HAL_UARTEx_RxFifoFullCallback(huart);
2536 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
2537     return;
2538   }
2539 }
2540 
2541 /**
2542   * @brief Tx Transfer completed callback.
2543   * @param huart UART handle.
2544   * @retval None
2545   */
2546 __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
2547 {
2548   /* Prevent unused argument(s) compilation warning */
2549   UNUSED(huart);
2550 
2551   /* NOTE : This function should not be modified, when the callback is needed,
2552             the HAL_UART_TxCpltCallback can be implemented in the user file.
2553    */
2554 }
2555 
2556 /**
2557   * @brief  Tx Half Transfer completed callback.
2558   * @param  huart UART handle.
2559   * @retval None
2560   */
2561 __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
2562 {
2563   /* Prevent unused argument(s) compilation warning */
2564   UNUSED(huart);
2565 
2566   /* NOTE: This function should not be modified, when the callback is needed,
2567            the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
2568    */
2569 }
2570 
2571 /**
2572   * @brief  Rx Transfer completed callback.
2573   * @param  huart UART handle.
2574   * @retval None
2575   */
2576 __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
2577 {
2578   /* Prevent unused argument(s) compilation warning */
2579   UNUSED(huart);
2580 
2581   /* NOTE : This function should not be modified, when the callback is needed,
2582             the HAL_UART_RxCpltCallback can be implemented in the user file.
2583    */
2584 }
2585 
2586 /**
2587   * @brief  Rx Half Transfer completed callback.
2588   * @param  huart UART handle.
2589   * @retval None
2590   */
2591 __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
2592 {
2593   /* Prevent unused argument(s) compilation warning */
2594   UNUSED(huart);
2595 
2596   /* NOTE: This function should not be modified, when the callback is needed,
2597            the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
2598    */
2599 }
2600 
2601 /**
2602   * @brief  UART error callback.
2603   * @param  huart UART handle.
2604   * @retval None
2605   */
2606 __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
2607 {
2608   /* Prevent unused argument(s) compilation warning */
2609   UNUSED(huart);
2610 
2611   /* NOTE : This function should not be modified, when the callback is needed,
2612             the HAL_UART_ErrorCallback can be implemented in the user file.
2613    */
2614 }
2615 
2616 /**
2617   * @brief  UART Abort Complete callback.
2618   * @param  huart UART handle.
2619   * @retval None
2620   */
2621 __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
2622 {
2623   /* Prevent unused argument(s) compilation warning */
2624   UNUSED(huart);
2625 
2626   /* NOTE : This function should not be modified, when the callback is needed,
2627             the HAL_UART_AbortCpltCallback can be implemented in the user file.
2628    */
2629 }
2630 
2631 /**
2632   * @brief  UART Abort Complete callback.
2633   * @param  huart UART handle.
2634   * @retval None
2635   */
2636 __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
2637 {
2638   /* Prevent unused argument(s) compilation warning */
2639   UNUSED(huart);
2640 
2641   /* NOTE : This function should not be modified, when the callback is needed,
2642             the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
2643    */
2644 }
2645 
2646 /**
2647   * @brief  UART Abort Receive Complete callback.
2648   * @param  huart UART handle.
2649   * @retval None
2650   */
2651 __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
2652 {
2653   /* Prevent unused argument(s) compilation warning */
2654   UNUSED(huart);
2655 
2656   /* NOTE : This function should not be modified, when the callback is needed,
2657             the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
2658    */
2659 }
2660 
2661 /**
2662   * @brief  Reception Event Callback (Rx event notification called after use of advanced reception service).
2663   * @param  huart UART handle
2664   * @param  Size  Number of data available in application reception buffer (indicates a position in
2665   *               reception buffer until which, data are available)
2666   * @retval None
2667   */
2668 __weak void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
2669 {
2670   /* Prevent unused argument(s) compilation warning */
2671   UNUSED(huart);
2672   UNUSED(Size);
2673 
2674   /* NOTE : This function should not be modified, when the callback is needed,
2675             the HAL_UARTEx_RxEventCallback can be implemented in the user file.
2676    */
2677 }
2678 
2679 /**
2680   * @}
2681   */
2682 
2683 /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
2684   * @ingroup RTEMSBSPsARMSTM32H7
2685   *  @brief   UART control functions
2686   *
2687 @verbatim
2688  ===============================================================================
2689                       ##### Peripheral Control functions #####
2690  ===============================================================================
2691     [..]
2692     This subsection provides a set of functions allowing to control the UART.
2693      (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
2694      (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
2695      (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
2696      (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
2697      (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
2698      (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
2699      (+) UART_SetConfig() API configures the UART peripheral
2700      (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
2701      (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
2702      (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
2703      (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
2704      (+) HAL_LIN_SendBreak() API transmits the break characters
2705 @endverbatim
2706   * @{
2707   */
2708 
2709 /**
2710   * @brief  Update on the fly the receiver timeout value in RTOR register.
2711   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2712   *                    the configuration information for the specified UART module.
2713   * @param  TimeoutValue receiver timeout value in number of baud blocks. The timeout
2714   *                     value must be less or equal to 0x0FFFFFFFF.
2715   * @retval None
2716   */
2717 void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
2718 {
2719   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2720   {
2721     assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
2722     MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
2723   }
2724 }
2725 
2726 /**
2727   * @brief  Enable the UART receiver timeout feature.
2728   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2729   *                    the configuration information for the specified UART module.
2730   * @retval HAL status
2731   */
2732 HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
2733 {
2734   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2735   {
2736     if (huart->gState == HAL_UART_STATE_READY)
2737     {
2738       /* Process Locked */
2739       __HAL_LOCK(huart);
2740 
2741       huart->gState = HAL_UART_STATE_BUSY;
2742 
2743       /* Set the USART RTOEN bit */
2744       SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2745 
2746       huart->gState = HAL_UART_STATE_READY;
2747 
2748       /* Process Unlocked */
2749       __HAL_UNLOCK(huart);
2750 
2751       return HAL_OK;
2752     }
2753     else
2754     {
2755       return HAL_BUSY;
2756     }
2757   }
2758   else
2759   {
2760     return HAL_ERROR;
2761   }
2762 }
2763 
2764 /**
2765   * @brief  Disable the UART receiver timeout feature.
2766   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2767   *                    the configuration information for the specified UART module.
2768   * @retval HAL status
2769   */
2770 HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
2771 {
2772   if (!(IS_LPUART_INSTANCE(huart->Instance)))
2773   {
2774     if (huart->gState == HAL_UART_STATE_READY)
2775     {
2776       /* Process Locked */
2777       __HAL_LOCK(huart);
2778 
2779       huart->gState = HAL_UART_STATE_BUSY;
2780 
2781       /* Clear the USART RTOEN bit */
2782       CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
2783 
2784       huart->gState = HAL_UART_STATE_READY;
2785 
2786       /* Process Unlocked */
2787       __HAL_UNLOCK(huart);
2788 
2789       return HAL_OK;
2790     }
2791     else
2792     {
2793       return HAL_BUSY;
2794     }
2795   }
2796   else
2797   {
2798     return HAL_ERROR;
2799   }
2800 }
2801 
2802 /**
2803   * @brief  Enable UART in mute mode (does not mean UART enters mute mode;
2804   *         to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
2805   * @param  huart UART handle.
2806   * @retval HAL status
2807   */
2808 HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
2809 {
2810   __HAL_LOCK(huart);
2811 
2812   huart->gState = HAL_UART_STATE_BUSY;
2813 
2814   /* Enable USART mute mode by setting the MME bit in the CR1 register */
2815   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_MME);
2816 
2817   huart->gState = HAL_UART_STATE_READY;
2818 
2819   return (UART_CheckIdleState(huart));
2820 }
2821 
2822 /**
2823   * @brief  Disable UART mute mode (does not mean the UART actually exits mute mode
2824   *         as it may not have been in mute mode at this very moment).
2825   * @param  huart UART handle.
2826   * @retval HAL status
2827   */
2828 HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
2829 {
2830   __HAL_LOCK(huart);
2831 
2832   huart->gState = HAL_UART_STATE_BUSY;
2833 
2834   /* Disable USART mute mode by clearing the MME bit in the CR1 register */
2835   ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
2836 
2837   huart->gState = HAL_UART_STATE_READY;
2838 
2839   return (UART_CheckIdleState(huart));
2840 }
2841 
2842 /**
2843   * @brief Enter UART mute mode (means UART actually enters mute mode).
2844   * @note  To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
2845   * @param huart UART handle.
2846   * @retval None
2847   */
2848 void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
2849 {
2850   __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
2851 }
2852 
2853 /**
2854   * @brief  Enable the UART transmitter and disable the UART receiver.
2855   * @param  huart UART handle.
2856   * @retval HAL status
2857   */
2858 HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
2859 {
2860   __HAL_LOCK(huart);
2861   huart->gState = HAL_UART_STATE_BUSY;
2862 
2863   /* Clear TE and RE bits */
2864   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2865 
2866   /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
2867   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TE);
2868 
2869   huart->gState = HAL_UART_STATE_READY;
2870 
2871   __HAL_UNLOCK(huart);
2872 
2873   return HAL_OK;
2874 }
2875 
2876 /**
2877   * @brief  Enable the UART receiver and disable the UART transmitter.
2878   * @param  huart UART handle.
2879   * @retval HAL status.
2880   */
2881 HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
2882 {
2883   __HAL_LOCK(huart);
2884   huart->gState = HAL_UART_STATE_BUSY;
2885 
2886   /* Clear TE and RE bits */
2887   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
2888 
2889   /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
2890   ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RE);
2891 
2892   huart->gState = HAL_UART_STATE_READY;
2893 
2894   __HAL_UNLOCK(huart);
2895 
2896   return HAL_OK;
2897 }
2898 
2899 
2900 /**
2901   * @brief  Transmit break characters.
2902   * @param  huart UART handle.
2903   * @retval HAL status
2904   */
2905 HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
2906 {
2907   /* Check the parameters */
2908   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
2909 
2910   __HAL_LOCK(huart);
2911 
2912   huart->gState = HAL_UART_STATE_BUSY;
2913 
2914   /* Send break characters */
2915   __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
2916 
2917   huart->gState = HAL_UART_STATE_READY;
2918 
2919   __HAL_UNLOCK(huart);
2920 
2921   return HAL_OK;
2922 }
2923 
2924 /**
2925   * @}
2926   */
2927 
2928 /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
2929   * @ingroup RTEMSBSPsARMSTM32H7
2930   *  @brief   UART Peripheral State functions
2931   *
2932 @verbatim
2933   ==============================================================================
2934             ##### Peripheral State and Error functions #####
2935   ==============================================================================
2936     [..]
2937     This subsection provides functions allowing to :
2938       (+) Return the UART handle state.
2939       (+) Return the UART handle error code
2940 
2941 @endverbatim
2942   * @{
2943   */
2944 
2945 /**
2946   * @brief Return the UART handle state.
2947   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2948   *               the configuration information for the specified UART.
2949   * @retval HAL state
2950   */
2951 HAL_UART_StateTypeDef HAL_UART_GetState(const UART_HandleTypeDef *huart)
2952 {
2953   uint32_t temp1;
2954   uint32_t temp2;
2955   temp1 = huart->gState;
2956   temp2 = huart->RxState;
2957 
2958   return (HAL_UART_StateTypeDef)(temp1 | temp2);
2959 }
2960 
2961 /**
2962   * @brief  Return the UART handle error code.
2963   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
2964   *               the configuration information for the specified UART.
2965   * @retval UART Error Code
2966   */
2967 uint32_t HAL_UART_GetError(const UART_HandleTypeDef *huart)
2968 {
2969   return huart->ErrorCode;
2970 }
2971 /**
2972   * @}
2973   */
2974 
2975 /**
2976   * @}
2977   */
2978 
2979 /** @defgroup UART_Private_Functions UART Private Functions
2980   * @ingroup RTEMSBSPsARMSTM32H7
2981   * @{
2982   */
2983 
2984 /**
2985   * @brief  Initialize the callbacks to their default values.
2986   * @param  huart UART handle.
2987   * @retval none
2988   */
2989 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
2990 void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
2991 {
2992   /* Init the UART Callback settings */
2993   huart->TxHalfCpltCallback        = HAL_UART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
2994   huart->TxCpltCallback            = HAL_UART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
2995   huart->RxHalfCpltCallback        = HAL_UART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
2996   huart->RxCpltCallback            = HAL_UART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
2997   huart->ErrorCallback             = HAL_UART_ErrorCallback;             /* Legacy weak ErrorCallback             */
2998   huart->AbortCpltCallback         = HAL_UART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
2999   huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
3000   huart->AbortReceiveCpltCallback  = HAL_UART_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
3001   huart->WakeupCallback            = HAL_UARTEx_WakeupCallback;          /* Legacy weak WakeupCallback            */
3002   huart->RxFifoFullCallback        = HAL_UARTEx_RxFifoFullCallback;      /* Legacy weak RxFifoFullCallback        */
3003   huart->TxFifoEmptyCallback       = HAL_UARTEx_TxFifoEmptyCallback;     /* Legacy weak TxFifoEmptyCallback       */
3004   huart->RxEventCallback           = HAL_UARTEx_RxEventCallback;         /* Legacy weak RxEventCallback           */
3005 
3006 }
3007 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3008 
3009 /**
3010   * @brief Configure the UART peripheral.
3011   * @param huart UART handle.
3012   * @retval HAL status
3013   */
3014 HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
3015 {
3016   uint32_t tmpreg;
3017   uint16_t brrtemp;
3018   UART_ClockSourceTypeDef clocksource;
3019   uint32_t usartdiv;
3020   HAL_StatusTypeDef ret               = HAL_OK;
3021   uint32_t lpuart_ker_ck_pres;
3022   PLL2_ClocksTypeDef pll2_clocks;
3023   PLL3_ClocksTypeDef pll3_clocks;
3024   uint32_t pclk;
3025 
3026   /* Check the parameters */
3027   assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
3028   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
3029   if (UART_INSTANCE_LOWPOWER(huart))
3030   {
3031     assert_param(IS_LPUART_STOPBITS(huart->Init.StopBits));
3032   }
3033   else
3034   {
3035     assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
3036     assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
3037   }
3038 
3039   assert_param(IS_UART_PARITY(huart->Init.Parity));
3040   assert_param(IS_UART_MODE(huart->Init.Mode));
3041   assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
3042   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
3043   assert_param(IS_UART_PRESCALER(huart->Init.ClockPrescaler));
3044 
3045   /*-------------------------- USART CR1 Configuration -----------------------*/
3046   /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
3047   *  the UART Word Length, Parity, Mode and oversampling:
3048   *  set the M bits according to huart->Init.WordLength value
3049   *  set PCE and PS bits according to huart->Init.Parity value
3050   *  set TE and RE bits according to huart->Init.Mode value
3051   *  set OVER8 bit according to huart->Init.OverSampling value */
3052   tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
3053   MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
3054 
3055   /*-------------------------- USART CR2 Configuration -----------------------*/
3056   /* Configure the UART Stop Bits: Set STOP[13:12] bits according
3057   * to huart->Init.StopBits value */
3058   MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
3059 
3060   /*-------------------------- USART CR3 Configuration -----------------------*/
3061   /* Configure
3062   * - UART HardWare Flow Control: set CTSE and RTSE bits according
3063   *   to huart->Init.HwFlowCtl value
3064   * - one-bit sampling method versus three samples' majority rule according
3065   *   to huart->Init.OneBitSampling (not applicable to LPUART) */
3066   tmpreg = (uint32_t)huart->Init.HwFlowCtl;
3067 
3068   if (!(UART_INSTANCE_LOWPOWER(huart)))
3069   {
3070     tmpreg |= huart->Init.OneBitSampling;
3071   }
3072   MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
3073 
3074   /*-------------------------- USART PRESC Configuration -----------------------*/
3075   /* Configure
3076   * - UART Clock Prescaler : set PRESCALER according to huart->Init.ClockPrescaler value */
3077   MODIFY_REG(huart->Instance->PRESC, USART_PRESC_PRESCALER, huart->Init.ClockPrescaler);
3078 
3079   /*-------------------------- USART BRR Configuration -----------------------*/
3080   UART_GETCLOCKSOURCE(huart, clocksource);
3081 
3082   /* Check LPUART instance */
3083   if (UART_INSTANCE_LOWPOWER(huart))
3084   {
3085     /* Retrieve frequency clock */
3086     switch (clocksource)
3087     {
3088       case UART_CLOCKSOURCE_D3PCLK1:
3089         pclk = HAL_RCCEx_GetD3PCLK1Freq();
3090         break;
3091       case UART_CLOCKSOURCE_PLL2:
3092         HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
3093         pclk = pll2_clocks.PLL2_Q_Frequency;
3094         break;
3095       case UART_CLOCKSOURCE_PLL3:
3096         HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
3097         pclk = pll3_clocks.PLL3_Q_Frequency;
3098         break;
3099       case UART_CLOCKSOURCE_HSI:
3100         if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
3101         {
3102           pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
3103         }
3104         else
3105         {
3106           pclk = (uint32_t) HSI_VALUE;
3107         }
3108         break;
3109       case UART_CLOCKSOURCE_CSI:
3110         pclk = (uint32_t) CSI_VALUE;
3111         break;
3112       case UART_CLOCKSOURCE_LSE:
3113         pclk = (uint32_t) LSE_VALUE;
3114         break;
3115       default:
3116         pclk = 0U;
3117         ret = HAL_ERROR;
3118         break;
3119     }
3120 
3121     /* If proper clock source reported */
3122     if (pclk != 0U)
3123     {
3124       /* Compute clock after Prescaler */
3125       lpuart_ker_ck_pres = (pclk / UARTPrescTable[huart->Init.ClockPrescaler]);
3126 
3127       /* Ensure that Frequency clock is in the range [3 * baudrate, 4096 * baudrate] */
3128       if ((lpuart_ker_ck_pres < (3U * huart->Init.BaudRate)) ||
3129           (lpuart_ker_ck_pres > (4096U * huart->Init.BaudRate)))
3130       {
3131         ret = HAL_ERROR;
3132       }
3133       else
3134       {
3135         /* Check computed UsartDiv value is in allocated range
3136            (it is forbidden to write values lower than 0x300 in the LPUART_BRR register) */
3137         usartdiv = (uint32_t)(UART_DIV_LPUART(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3138         if ((usartdiv >= LPUART_BRR_MIN) && (usartdiv <= LPUART_BRR_MAX))
3139         {
3140           huart->Instance->BRR = usartdiv;
3141         }
3142         else
3143         {
3144           ret = HAL_ERROR;
3145         }
3146       } /* if ( (lpuart_ker_ck_pres < (3 * huart->Init.BaudRate) ) ||
3147                 (lpuart_ker_ck_pres > (4096 * huart->Init.BaudRate) )) */
3148     } /* if (pclk != 0) */
3149   }
3150   /* Check UART Over Sampling to set Baud Rate Register */
3151   else if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
3152   {
3153     switch (clocksource)
3154     {
3155       case UART_CLOCKSOURCE_D2PCLK1:
3156         pclk = HAL_RCC_GetPCLK1Freq();
3157         break;
3158       case UART_CLOCKSOURCE_D2PCLK2:
3159         pclk = HAL_RCC_GetPCLK2Freq();
3160         break;
3161       case UART_CLOCKSOURCE_PLL2:
3162         HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
3163         pclk = pll2_clocks.PLL2_Q_Frequency;
3164         break;
3165       case UART_CLOCKSOURCE_PLL3:
3166         HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
3167         pclk = pll3_clocks.PLL3_Q_Frequency;
3168         break;
3169       case UART_CLOCKSOURCE_HSI:
3170         if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
3171         {
3172           pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
3173         }
3174         else
3175         {
3176           pclk = (uint32_t) HSI_VALUE;
3177         }
3178         break;
3179       case UART_CLOCKSOURCE_CSI:
3180         pclk = (uint32_t) CSI_VALUE;
3181         break;
3182       case UART_CLOCKSOURCE_LSE:
3183         pclk = (uint32_t) LSE_VALUE;
3184         break;
3185       default:
3186         pclk = 0U;
3187         ret = HAL_ERROR;
3188         break;
3189     }
3190 
3191     /* USARTDIV must be greater than or equal to 0d16 */
3192     if (pclk != 0U)
3193     {
3194       usartdiv = (uint32_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3195       if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3196       {
3197         brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
3198         brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
3199         huart->Instance->BRR = brrtemp;
3200       }
3201       else
3202       {
3203         ret = HAL_ERROR;
3204       }
3205     }
3206   }
3207   else
3208   {
3209     switch (clocksource)
3210     {
3211       case UART_CLOCKSOURCE_D2PCLK1:
3212         pclk = HAL_RCC_GetPCLK1Freq();
3213         break;
3214       case UART_CLOCKSOURCE_D2PCLK2:
3215         pclk = HAL_RCC_GetPCLK2Freq();
3216         break;
3217       case UART_CLOCKSOURCE_PLL2:
3218         HAL_RCCEx_GetPLL2ClockFreq(&pll2_clocks);
3219         pclk = pll2_clocks.PLL2_Q_Frequency;
3220         break;
3221       case UART_CLOCKSOURCE_PLL3:
3222         HAL_RCCEx_GetPLL3ClockFreq(&pll3_clocks);
3223         pclk = pll3_clocks.PLL3_Q_Frequency;
3224         break;
3225       case UART_CLOCKSOURCE_HSI:
3226         if (__HAL_RCC_GET_FLAG(RCC_FLAG_HSIDIV) != 0U)
3227         {
3228           pclk = (uint32_t)(HSI_VALUE >> (__HAL_RCC_GET_HSI_DIVIDER() >> 3U));
3229         }
3230         else
3231         {
3232           pclk = (uint32_t) HSI_VALUE;
3233         }
3234         break;
3235       case UART_CLOCKSOURCE_CSI:
3236         pclk = (uint32_t) CSI_VALUE;
3237         break;
3238       case UART_CLOCKSOURCE_LSE:
3239         pclk = (uint32_t) LSE_VALUE;
3240         break;
3241       default:
3242         pclk = 0U;
3243         ret = HAL_ERROR;
3244         break;
3245     }
3246 
3247     if (pclk != 0U)
3248     {
3249       /* USARTDIV must be greater than or equal to 0d16 */
3250       usartdiv = (uint32_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate, huart->Init.ClockPrescaler));
3251       if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
3252       {
3253         huart->Instance->BRR = (uint16_t)usartdiv;
3254       }
3255       else
3256       {
3257         ret = HAL_ERROR;
3258       }
3259     }
3260   }
3261 
3262   /* Initialize the number of data to process during RX/TX ISR execution */
3263   huart->NbTxDataToProcess = 1;
3264   huart->NbRxDataToProcess = 1;
3265 
3266   /* Clear ISR function pointers */
3267   huart->RxISR = NULL;
3268   huart->TxISR = NULL;
3269 
3270   return ret;
3271 }
3272 
3273 /**
3274   * @brief Configure the UART peripheral advanced features.
3275   * @param huart UART handle.
3276   * @retval None
3277   */
3278 void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
3279 {
3280   /* Check whether the set of advanced features to configure is properly set */
3281   assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
3282 
3283   /* if required, configure RX/TX pins swap */
3284   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
3285   {
3286     assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
3287     MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
3288   }
3289 
3290   /* if required, configure TX pin active level inversion */
3291   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
3292   {
3293     assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
3294     MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
3295   }
3296 
3297   /* if required, configure RX pin active level inversion */
3298   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
3299   {
3300     assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
3301     MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
3302   }
3303 
3304   /* if required, configure data inversion */
3305   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
3306   {
3307     assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
3308     MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
3309   }
3310 
3311   /* if required, configure RX overrun detection disabling */
3312   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
3313   {
3314     assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
3315     MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
3316   }
3317 
3318   /* if required, configure DMA disabling on reception error */
3319   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
3320   {
3321     assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
3322     MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
3323   }
3324 
3325   /* if required, configure auto Baud rate detection scheme */
3326   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
3327   {
3328     assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
3329     assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
3330     MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
3331     /* set auto Baudrate detection parameters if detection is enabled */
3332     if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
3333     {
3334       assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
3335       MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
3336     }
3337   }
3338 
3339   /* if required, configure MSB first on communication line */
3340   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
3341   {
3342     assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
3343     MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
3344   }
3345 }
3346 
3347 /**
3348   * @brief Check the UART Idle State.
3349   * @param huart UART handle.
3350   * @retval HAL status
3351   */
3352 HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
3353 {
3354   uint32_t tickstart;
3355 
3356   /* Initialize the UART ErrorCode */
3357   huart->ErrorCode = HAL_UART_ERROR_NONE;
3358 
3359   /* Init tickstart for timeout management */
3360   tickstart = HAL_GetTick();
3361 
3362   /* Check if the Transmitter is enabled */
3363   if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
3364   {
3365     /* Wait until TEACK flag is set */
3366     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3367     {
3368       /* Disable TXE interrupt for the interrupt process */
3369       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE));
3370 
3371       huart->gState = HAL_UART_STATE_READY;
3372 
3373       __HAL_UNLOCK(huart);
3374 
3375       /* Timeout occurred */
3376       return HAL_TIMEOUT;
3377     }
3378   }
3379 
3380   /* Check if the Receiver is enabled */
3381   if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
3382   {
3383     /* Wait until REACK flag is set */
3384     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
3385     {
3386       /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error)
3387       interrupts for the interrupt process */
3388       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3389       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3390 
3391       huart->RxState = HAL_UART_STATE_READY;
3392 
3393       __HAL_UNLOCK(huart);
3394 
3395       /* Timeout occurred */
3396       return HAL_TIMEOUT;
3397     }
3398   }
3399 
3400   /* Initialize the UART State */
3401   huart->gState = HAL_UART_STATE_READY;
3402   huart->RxState = HAL_UART_STATE_READY;
3403   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3404   huart->RxEventType = HAL_UART_RXEVENT_TC;
3405 
3406   __HAL_UNLOCK(huart);
3407 
3408   return HAL_OK;
3409 }
3410 
3411 /**
3412   * @brief  This function handles UART Communication Timeout. It waits
3413   *                  until a flag is no longer in the specified status.
3414   * @param huart     UART handle.
3415   * @param Flag      Specifies the UART flag to check
3416   * @param Status    The actual Flag status (SET or RESET)
3417   * @param Tickstart Tick start value
3418   * @param Timeout   Timeout duration
3419   * @retval HAL status
3420   */
3421 HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
3422                                               uint32_t Tickstart, uint32_t Timeout)
3423 {
3424   /* Wait until flag is set */
3425   while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
3426   {
3427     /* Check for the Timeout */
3428     if (Timeout != HAL_MAX_DELAY)
3429     {
3430       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
3431       {
3432 
3433         return HAL_TIMEOUT;
3434       }
3435 
3436       if ((READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U) && (Flag != UART_FLAG_TXE) && (Flag != UART_FLAG_TC))
3437       {
3438         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE) == SET)
3439         {
3440           /* Clear Overrun Error flag*/
3441           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
3442 
3443           /* Blocking error : transfer is aborted
3444           Set the UART state ready to be able to start again the process,
3445           Disable Rx Interrupts if ongoing */
3446           UART_EndRxTransfer(huart);
3447 
3448           huart->ErrorCode = HAL_UART_ERROR_ORE;
3449 
3450           /* Process Unlocked */
3451           __HAL_UNLOCK(huart);
3452 
3453           return HAL_ERROR;
3454         }
3455         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
3456         {
3457           /* Clear Receiver Timeout flag*/
3458           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
3459 
3460           /* Blocking error : transfer is aborted
3461           Set the UART state ready to be able to start again the process,
3462           Disable Rx Interrupts if ongoing */
3463           UART_EndRxTransfer(huart);
3464 
3465           huart->ErrorCode = HAL_UART_ERROR_RTO;
3466 
3467           /* Process Unlocked */
3468           __HAL_UNLOCK(huart);
3469 
3470           return HAL_TIMEOUT;
3471         }
3472       }
3473     }
3474   }
3475   return HAL_OK;
3476 }
3477 
3478 /**
3479   * @brief  Start Receive operation in interrupt mode.
3480   * @note   This function could be called by all HAL UART API providing reception in Interrupt mode.
3481   * @note   When calling this function, parameters validity is considered as already checked,
3482   *         i.e. Rx State, buffer address, ...
3483   *         UART Handle is assumed as Locked.
3484   * @param  huart UART handle.
3485   * @param  pData Pointer to data buffer (u8 or u16 data elements).
3486   * @param  Size  Amount of data elements (u8 or u16) to be received.
3487   * @retval HAL status
3488   */
3489 HAL_StatusTypeDef UART_Start_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3490 {
3491   huart->pRxBuffPtr  = pData;
3492   huart->RxXferSize  = Size;
3493   huart->RxXferCount = Size;
3494   huart->RxISR       = NULL;
3495 
3496   /* Computation of UART mask to apply to RDR register */
3497   UART_MASK_COMPUTATION(huart);
3498 
3499   huart->ErrorCode = HAL_UART_ERROR_NONE;
3500   huart->RxState = HAL_UART_STATE_BUSY_RX;
3501 
3502   /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3503   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3504 
3505   /* Configure Rx interrupt processing */
3506   if ((huart->FifoMode == UART_FIFOMODE_ENABLE) && (Size >= huart->NbRxDataToProcess))
3507   {
3508     /* Set the Rx ISR function pointer according to the data word length */
3509     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3510     {
3511       huart->RxISR = UART_RxISR_16BIT_FIFOEN;
3512     }
3513     else
3514     {
3515       huart->RxISR = UART_RxISR_8BIT_FIFOEN;
3516     }
3517 
3518     /* Enable the UART Parity Error interrupt and RX FIFO Threshold interrupt */
3519     if (huart->Init.Parity != UART_PARITY_NONE)
3520     {
3521       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3522     }
3523     ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
3524   }
3525   else
3526   {
3527     /* Set the Rx ISR function pointer according to the data word length */
3528     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
3529     {
3530       huart->RxISR = UART_RxISR_16BIT;
3531     }
3532     else
3533     {
3534       huart->RxISR = UART_RxISR_8BIT;
3535     }
3536 
3537     /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
3538     if (huart->Init.Parity != UART_PARITY_NONE)
3539     {
3540       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
3541     }
3542     else
3543     {
3544       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
3545     }
3546   }
3547   return HAL_OK;
3548 }
3549 
3550 /**
3551   * @brief  Start Receive operation in DMA mode.
3552   * @note   This function could be called by all HAL UART API providing reception in DMA mode.
3553   * @note   When calling this function, parameters validity is considered as already checked,
3554   *         i.e. Rx State, buffer address, ...
3555   *         UART Handle is assumed as Locked.
3556   * @param  huart UART handle.
3557   * @param  pData Pointer to data buffer (u8 or u16 data elements).
3558   * @param  Size  Amount of data elements (u8 or u16) to be received.
3559   * @retval HAL status
3560   */
3561 HAL_StatusTypeDef UART_Start_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
3562 {
3563   huart->pRxBuffPtr = pData;
3564   huart->RxXferSize = Size;
3565 
3566   huart->ErrorCode = HAL_UART_ERROR_NONE;
3567   huart->RxState = HAL_UART_STATE_BUSY_RX;
3568 
3569   if (huart->hdmarx != NULL)
3570   {
3571     /* Set the UART DMA transfer complete callback */
3572     huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
3573 
3574     /* Set the UART DMA Half transfer complete callback */
3575     huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
3576 
3577     /* Set the DMA error callback */
3578     huart->hdmarx->XferErrorCallback = UART_DMAError;
3579 
3580     /* Set the DMA abort callback */
3581     huart->hdmarx->XferAbortCallback = NULL;
3582 
3583     /* Enable the DMA channel */
3584     if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
3585     {
3586       /* Set error code to DMA */
3587       huart->ErrorCode = HAL_UART_ERROR_DMA;
3588 
3589       /* Restore huart->RxState to ready */
3590       huart->RxState = HAL_UART_STATE_READY;
3591 
3592       return HAL_ERROR;
3593     }
3594   }
3595 
3596   /* Enable the UART Parity Error Interrupt */
3597   if (huart->Init.Parity != UART_PARITY_NONE)
3598   {
3599     ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3600   }
3601 
3602   /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
3603   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
3604 
3605   /* Enable the DMA transfer for the receiver request by setting the DMAR bit
3606   in the UART CR3 register */
3607   ATOMIC_SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3608 
3609   return HAL_OK;
3610 }
3611 
3612 
3613 /**
3614   * @brief  End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
3615   * @param  huart UART handle.
3616   * @retval None
3617   */
3618 static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
3619 {
3620   /* Disable TXEIE, TCIE, TXFT interrupts */
3621   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
3622   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_TXFTIE));
3623 
3624   /* At end of Tx process, restore huart->gState to Ready */
3625   huart->gState = HAL_UART_STATE_READY;
3626 }
3627 
3628 
3629 /**
3630   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
3631   * @param  huart UART handle.
3632   * @retval None
3633   */
3634 static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
3635 {
3636   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
3637   ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
3638   ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
3639 
3640   /* In case of reception waiting for IDLE event, disable also the IDLE IE interrupt source */
3641   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3642   {
3643     ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3644   }
3645 
3646   /* At end of Rx process, restore huart->RxState to Ready */
3647   huart->RxState = HAL_UART_STATE_READY;
3648   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3649 
3650   /* Reset RxIsr function pointer */
3651   huart->RxISR = NULL;
3652 }
3653 
3654 
3655 /**
3656   * @brief DMA UART transmit process complete callback.
3657   * @param hdma DMA handle.
3658   * @retval None
3659   */
3660 static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
3661 {
3662   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3663 
3664   /* DMA Normal mode */
3665   if (hdma->Init.Mode != DMA_CIRCULAR)
3666   {
3667     huart->TxXferCount = 0U;
3668 
3669     /* Disable the DMA transfer for transmit request by resetting the DMAT bit
3670        in the UART CR3 register */
3671     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
3672 
3673     /* Enable the UART Transmit Complete Interrupt */
3674     ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
3675   }
3676   /* DMA Circular mode */
3677   else
3678   {
3679 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3680     /*Call registered Tx complete callback*/
3681     huart->TxCpltCallback(huart);
3682 #else
3683     /*Call legacy weak Tx complete callback*/
3684     HAL_UART_TxCpltCallback(huart);
3685 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3686   }
3687 }
3688 
3689 /**
3690   * @brief DMA UART transmit process half complete callback.
3691   * @param hdma DMA handle.
3692   * @retval None
3693   */
3694 static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
3695 {
3696   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3697 
3698 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3699   /*Call registered Tx Half complete callback*/
3700   huart->TxHalfCpltCallback(huart);
3701 #else
3702   /*Call legacy weak Tx Half complete callback*/
3703   HAL_UART_TxHalfCpltCallback(huart);
3704 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3705 }
3706 
3707 /**
3708   * @brief DMA UART receive process complete callback.
3709   * @param hdma DMA handle.
3710   * @retval None
3711   */
3712 static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
3713 {
3714   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3715 
3716   /* DMA Normal mode */
3717   if (hdma->Init.Mode != DMA_CIRCULAR)
3718   {
3719     huart->RxXferCount = 0U;
3720 
3721     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
3722     ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
3723     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
3724 
3725     /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
3726        in the UART CR3 register */
3727     ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
3728 
3729     /* At end of Rx process, restore huart->RxState to Ready */
3730     huart->RxState = HAL_UART_STATE_READY;
3731 
3732     /* If Reception till IDLE event has been selected, Disable IDLE Interrupt */
3733     if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3734     {
3735       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
3736     }
3737   }
3738 
3739   /* Initialize type of RxEvent that correspond to RxEvent callback execution;
3740      In this case, Rx Event type is Transfer Complete */
3741   huart->RxEventType = HAL_UART_RXEVENT_TC;
3742 
3743   /* Check current reception Mode :
3744      If Reception till IDLE event has been selected : use Rx Event callback */
3745   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3746   {
3747 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3748     /*Call registered Rx Event callback*/
3749     huart->RxEventCallback(huart, huart->RxXferSize);
3750 #else
3751     /*Call legacy weak Rx Event callback*/
3752     HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
3753 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3754   }
3755   else
3756   {
3757     /* In other cases : use Rx Complete callback */
3758 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3759     /*Call registered Rx complete callback*/
3760     huart->RxCpltCallback(huart);
3761 #else
3762     /*Call legacy weak Rx complete callback*/
3763     HAL_UART_RxCpltCallback(huart);
3764 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3765   }
3766 }
3767 
3768 /**
3769   * @brief DMA UART receive process half complete callback.
3770   * @param hdma DMA handle.
3771   * @retval None
3772   */
3773 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
3774 {
3775   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3776 
3777   /* Initialize type of RxEvent that correspond to RxEvent callback execution;
3778      In this case, Rx Event type is Half Transfer */
3779   huart->RxEventType = HAL_UART_RXEVENT_HT;
3780 
3781   /* Check current reception Mode :
3782      If Reception till IDLE event has been selected : use Rx Event callback */
3783   if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
3784   {
3785 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3786     /*Call registered Rx Event callback*/
3787     huart->RxEventCallback(huart, huart->RxXferSize / 2U);
3788 #else
3789     /*Call legacy weak Rx Event callback*/
3790     HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize / 2U);
3791 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3792   }
3793   else
3794   {
3795     /* In other cases : use Rx Half Complete callback */
3796 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3797     /*Call registered Rx Half complete callback*/
3798     huart->RxHalfCpltCallback(huart);
3799 #else
3800     /*Call legacy weak Rx Half complete callback*/
3801     HAL_UART_RxHalfCpltCallback(huart);
3802 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3803   }
3804 }
3805 
3806 /**
3807   * @brief DMA UART communication error callback.
3808   * @param hdma DMA handle.
3809   * @retval None
3810   */
3811 static void UART_DMAError(DMA_HandleTypeDef *hdma)
3812 {
3813   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3814 
3815   const HAL_UART_StateTypeDef gstate = huart->gState;
3816   const HAL_UART_StateTypeDef rxstate = huart->RxState;
3817 
3818   /* Stop UART DMA Tx request if ongoing */
3819   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
3820       (gstate == HAL_UART_STATE_BUSY_TX))
3821   {
3822     huart->TxXferCount = 0U;
3823     UART_EndTxTransfer(huart);
3824   }
3825 
3826   /* Stop UART DMA Rx request if ongoing */
3827   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
3828       (rxstate == HAL_UART_STATE_BUSY_RX))
3829   {
3830     huart->RxXferCount = 0U;
3831     UART_EndRxTransfer(huart);
3832   }
3833 
3834   huart->ErrorCode |= HAL_UART_ERROR_DMA;
3835 
3836 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3837   /*Call registered error callback*/
3838   huart->ErrorCallback(huart);
3839 #else
3840   /*Call legacy weak error callback*/
3841   HAL_UART_ErrorCallback(huart);
3842 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3843 }
3844 
3845 /**
3846   * @brief  DMA UART communication abort callback, when initiated by HAL services on Error
3847   *         (To be called at end of DMA Abort procedure following error occurrence).
3848   * @param  hdma DMA handle.
3849   * @retval None
3850   */
3851 static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
3852 {
3853   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3854   huart->RxXferCount = 0U;
3855   huart->TxXferCount = 0U;
3856 
3857 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3858   /*Call registered error callback*/
3859   huart->ErrorCallback(huart);
3860 #else
3861   /*Call legacy weak error callback*/
3862   HAL_UART_ErrorCallback(huart);
3863 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3864 }
3865 
3866 /**
3867   * @brief  DMA UART Tx communication abort callback, when initiated by user
3868   *         (To be called at end of DMA Tx Abort procedure following user abort request).
3869   * @note   When this callback is executed, User Abort complete call back is called only if no
3870   *         Abort still ongoing for Rx DMA Handle.
3871   * @param  hdma DMA handle.
3872   * @retval None
3873   */
3874 static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
3875 {
3876   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3877 
3878   huart->hdmatx->XferAbortCallback = NULL;
3879 
3880   /* Check if an Abort process is still ongoing */
3881   if (huart->hdmarx != NULL)
3882   {
3883     if (huart->hdmarx->XferAbortCallback != NULL)
3884     {
3885       return;
3886     }
3887   }
3888 
3889   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3890   huart->TxXferCount = 0U;
3891   huart->RxXferCount = 0U;
3892 
3893   /* Reset errorCode */
3894   huart->ErrorCode = HAL_UART_ERROR_NONE;
3895 
3896   /* Clear the Error flags in the ICR register */
3897   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3898 
3899   /* Flush the whole TX FIFO (if needed) */
3900   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3901   {
3902     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3903   }
3904 
3905   /* Restore huart->gState and huart->RxState to Ready */
3906   huart->gState  = HAL_UART_STATE_READY;
3907   huart->RxState = HAL_UART_STATE_READY;
3908   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3909 
3910   /* Call user Abort complete callback */
3911 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3912   /* Call registered Abort complete callback */
3913   huart->AbortCpltCallback(huart);
3914 #else
3915   /* Call legacy weak Abort complete callback */
3916   HAL_UART_AbortCpltCallback(huart);
3917 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3918 }
3919 
3920 
3921 /**
3922   * @brief  DMA UART Rx communication abort callback, when initiated by user
3923   *         (To be called at end of DMA Rx Abort procedure following user abort request).
3924   * @note   When this callback is executed, User Abort complete call back is called only if no
3925   *         Abort still ongoing for Tx DMA Handle.
3926   * @param  hdma DMA handle.
3927   * @retval None
3928   */
3929 static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
3930 {
3931   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3932 
3933   huart->hdmarx->XferAbortCallback = NULL;
3934 
3935   /* Check if an Abort process is still ongoing */
3936   if (huart->hdmatx != NULL)
3937   {
3938     if (huart->hdmatx->XferAbortCallback != NULL)
3939     {
3940       return;
3941     }
3942   }
3943 
3944   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
3945   huart->TxXferCount = 0U;
3946   huart->RxXferCount = 0U;
3947 
3948   /* Reset errorCode */
3949   huart->ErrorCode = HAL_UART_ERROR_NONE;
3950 
3951   /* Clear the Error flags in the ICR register */
3952   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
3953 
3954   /* Discard the received data */
3955   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
3956 
3957   /* Restore huart->gState and huart->RxState to Ready */
3958   huart->gState  = HAL_UART_STATE_READY;
3959   huart->RxState = HAL_UART_STATE_READY;
3960   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
3961 
3962   /* Call user Abort complete callback */
3963 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3964   /* Call registered Abort complete callback */
3965   huart->AbortCpltCallback(huart);
3966 #else
3967   /* Call legacy weak Abort complete callback */
3968   HAL_UART_AbortCpltCallback(huart);
3969 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
3970 }
3971 
3972 
3973 /**
3974   * @brief  DMA UART Tx communication abort callback, when initiated by user by a call to
3975   *         HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
3976   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
3977   *         and leads to user Tx Abort Complete callback execution).
3978   * @param  hdma DMA handle.
3979   * @retval None
3980   */
3981 static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
3982 {
3983   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
3984 
3985   huart->TxXferCount = 0U;
3986 
3987   /* Flush the whole TX FIFO (if needed) */
3988   if (huart->FifoMode == UART_FIFOMODE_ENABLE)
3989   {
3990     __HAL_UART_SEND_REQ(huart, UART_TXDATA_FLUSH_REQUEST);
3991   }
3992 
3993   /* Restore huart->gState to Ready */
3994   huart->gState = HAL_UART_STATE_READY;
3995 
3996   /* Call user Abort complete callback */
3997 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
3998   /* Call registered Abort Transmit Complete Callback */
3999   huart->AbortTransmitCpltCallback(huart);
4000 #else
4001   /* Call legacy weak Abort Transmit Complete Callback */
4002   HAL_UART_AbortTransmitCpltCallback(huart);
4003 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4004 }
4005 
4006 /**
4007   * @brief  DMA UART Rx communication abort callback, when initiated by user by a call to
4008   *         HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
4009   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
4010   *         and leads to user Rx Abort Complete callback execution).
4011   * @param  hdma DMA handle.
4012   * @retval None
4013   */
4014 static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
4015 {
4016   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
4017 
4018   huart->RxXferCount = 0U;
4019 
4020   /* Clear the Error flags in the ICR register */
4021   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
4022 
4023   /* Discard the received data */
4024   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4025 
4026   /* Restore huart->RxState to Ready */
4027   huart->RxState = HAL_UART_STATE_READY;
4028   huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4029 
4030   /* Call user Abort complete callback */
4031 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4032   /* Call registered Abort Receive Complete Callback */
4033   huart->AbortReceiveCpltCallback(huart);
4034 #else
4035   /* Call legacy weak Abort Receive Complete Callback */
4036   HAL_UART_AbortReceiveCpltCallback(huart);
4037 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4038 }
4039 
4040 /**
4041   * @brief TX interrupt handler for 7 or 8 bits data word length .
4042   * @note   Function is called under interruption only, once
4043   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4044   * @param huart UART handle.
4045   * @retval None
4046   */
4047 static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
4048 {
4049   /* Check that a Tx process is ongoing */
4050   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4051   {
4052     if (huart->TxXferCount == 0U)
4053     {
4054       /* Disable the UART Transmit Data Register Empty Interrupt */
4055       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
4056 
4057       /* Enable the UART Transmit Complete Interrupt */
4058       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4059     }
4060     else
4061     {
4062       huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4063       huart->pTxBuffPtr++;
4064       huart->TxXferCount--;
4065     }
4066   }
4067 }
4068 
4069 /**
4070   * @brief TX interrupt handler for 9 bits data word length.
4071   * @note   Function is called under interruption only, once
4072   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4073   * @param huart UART handle.
4074   * @retval None
4075   */
4076 static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
4077 {
4078   const uint16_t *tmp;
4079 
4080   /* Check that a Tx process is ongoing */
4081   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4082   {
4083     if (huart->TxXferCount == 0U)
4084     {
4085       /* Disable the UART Transmit Data Register Empty Interrupt */
4086       ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
4087 
4088       /* Enable the UART Transmit Complete Interrupt */
4089       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4090     }
4091     else
4092     {
4093       tmp = (const uint16_t *) huart->pTxBuffPtr;
4094       huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4095       huart->pTxBuffPtr += 2U;
4096       huart->TxXferCount--;
4097     }
4098   }
4099 }
4100 
4101 /**
4102   * @brief TX interrupt handler for 7 or 8 bits data word length and FIFO mode is enabled.
4103   * @note   Function is called under interruption only, once
4104   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4105   * @param huart UART handle.
4106   * @retval None
4107   */
4108 static void UART_TxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4109 {
4110   uint16_t  nb_tx_data;
4111 
4112   /* Check that a Tx process is ongoing */
4113   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4114   {
4115     for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4116     {
4117       if (huart->TxXferCount == 0U)
4118       {
4119         /* Disable the TX FIFO threshold interrupt */
4120         ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4121 
4122         /* Enable the UART Transmit Complete Interrupt */
4123         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4124 
4125         break; /* force exit loop */
4126       }
4127       else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4128       {
4129         huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
4130         huart->pTxBuffPtr++;
4131         huart->TxXferCount--;
4132       }
4133       else
4134       {
4135         /* Nothing to do */
4136       }
4137     }
4138   }
4139 }
4140 
4141 /**
4142   * @brief TX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4143   * @note   Function is called under interruption only, once
4144   *         interruptions have been enabled by HAL_UART_Transmit_IT().
4145   * @param huart UART handle.
4146   * @retval None
4147   */
4148 static void UART_TxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4149 {
4150   const uint16_t *tmp;
4151   uint16_t  nb_tx_data;
4152 
4153   /* Check that a Tx process is ongoing */
4154   if (huart->gState == HAL_UART_STATE_BUSY_TX)
4155   {
4156     for (nb_tx_data = huart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
4157     {
4158       if (huart->TxXferCount == 0U)
4159       {
4160         /* Disable the TX FIFO threshold interrupt */
4161         ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_TXFTIE);
4162 
4163         /* Enable the UART Transmit Complete Interrupt */
4164         ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4165 
4166         break; /* force exit loop */
4167       }
4168       else if (READ_BIT(huart->Instance->ISR, USART_ISR_TXE_TXFNF) != 0U)
4169       {
4170         tmp = (const uint16_t *) huart->pTxBuffPtr;
4171         huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
4172         huart->pTxBuffPtr += 2U;
4173         huart->TxXferCount--;
4174       }
4175       else
4176       {
4177         /* Nothing to do */
4178       }
4179     }
4180   }
4181 }
4182 
4183 /**
4184   * @brief  Wrap up transmission in non-blocking mode.
4185   * @param  huart pointer to a UART_HandleTypeDef structure that contains
4186   *                the configuration information for the specified UART module.
4187   * @retval None
4188   */
4189 static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
4190 {
4191   /* Disable the UART Transmit Complete Interrupt */
4192   ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
4193 
4194   /* Tx process is ended, restore huart->gState to Ready */
4195   huart->gState = HAL_UART_STATE_READY;
4196 
4197   /* Cleat TxISR function pointer */
4198   huart->TxISR = NULL;
4199 
4200 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4201   /*Call registered Tx complete callback*/
4202   huart->TxCpltCallback(huart);
4203 #else
4204   /*Call legacy weak Tx complete callback*/
4205   HAL_UART_TxCpltCallback(huart);
4206 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4207 }
4208 
4209 /**
4210   * @brief RX interrupt handler for 7 or 8 bits data word length .
4211   * @param huart UART handle.
4212   * @retval None
4213   */
4214 static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
4215 {
4216   uint16_t uhMask = huart->Mask;
4217   uint16_t  uhdata;
4218 
4219   /* Check that a Rx process is ongoing */
4220   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4221   {
4222     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4223     *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4224     huart->pRxBuffPtr++;
4225     huart->RxXferCount--;
4226 
4227     if (huart->RxXferCount == 0U)
4228     {
4229       /* Disable the UART Parity Error Interrupt and RXNE interrupts */
4230       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4231 
4232       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4233       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4234 
4235       /* Rx process is completed, restore huart->RxState to Ready */
4236       huart->RxState = HAL_UART_STATE_READY;
4237 
4238       /* Clear RxISR function pointer */
4239       huart->RxISR = NULL;
4240 
4241       /* Initialize type of RxEvent to Transfer Complete */
4242       huart->RxEventType = HAL_UART_RXEVENT_TC;
4243 
4244       if (!(IS_LPUART_INSTANCE(huart->Instance)))
4245       {
4246         /* Check that USART RTOEN bit is set */
4247         if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4248         {
4249           /* Enable the UART Receiver Timeout Interrupt */
4250           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4251         }
4252       }
4253 
4254       /* Check current reception Mode :
4255          If Reception till IDLE event has been selected : */
4256       if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4257       {
4258         /* Set reception type to Standard */
4259         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4260 
4261         /* Disable IDLE interrupt */
4262         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4263 
4264         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4265         {
4266           /* Clear IDLE Flag */
4267           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4268         }
4269 
4270 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4271         /*Call registered Rx Event callback*/
4272         huart->RxEventCallback(huart, huart->RxXferSize);
4273 #else
4274         /*Call legacy weak Rx Event callback*/
4275         HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4276 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4277       }
4278       else
4279       {
4280         /* Standard reception API called */
4281 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4282         /*Call registered Rx complete callback*/
4283         huart->RxCpltCallback(huart);
4284 #else
4285         /*Call legacy weak Rx complete callback*/
4286         HAL_UART_RxCpltCallback(huart);
4287 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4288       }
4289     }
4290   }
4291   else
4292   {
4293     /* Clear RXNE interrupt flag */
4294     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4295   }
4296 }
4297 
4298 /**
4299   * @brief RX interrupt handler for 9 bits data word length .
4300   * @note   Function is called under interruption only, once
4301   *         interruptions have been enabled by HAL_UART_Receive_IT()
4302   * @param huart UART handle.
4303   * @retval None
4304   */
4305 static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
4306 {
4307   uint16_t *tmp;
4308   uint16_t uhMask = huart->Mask;
4309   uint16_t  uhdata;
4310 
4311   /* Check that a Rx process is ongoing */
4312   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4313   {
4314     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4315     tmp = (uint16_t *) huart->pRxBuffPtr ;
4316     *tmp = (uint16_t)(uhdata & uhMask);
4317     huart->pRxBuffPtr += 2U;
4318     huart->RxXferCount--;
4319 
4320     if (huart->RxXferCount == 0U)
4321     {
4322       /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
4323       ATOMIC_CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
4324 
4325       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
4326       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
4327 
4328       /* Rx process is completed, restore huart->RxState to Ready */
4329       huart->RxState = HAL_UART_STATE_READY;
4330 
4331       /* Clear RxISR function pointer */
4332       huart->RxISR = NULL;
4333 
4334       /* Initialize type of RxEvent to Transfer Complete */
4335       huart->RxEventType = HAL_UART_RXEVENT_TC;
4336 
4337       if (!(IS_LPUART_INSTANCE(huart->Instance)))
4338       {
4339         /* Check that USART RTOEN bit is set */
4340         if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4341         {
4342           /* Enable the UART Receiver Timeout Interrupt */
4343           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4344         }
4345       }
4346 
4347       /* Check current reception Mode :
4348          If Reception till IDLE event has been selected : */
4349       if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4350       {
4351         /* Set reception type to Standard */
4352         huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4353 
4354         /* Disable IDLE interrupt */
4355         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4356 
4357         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4358         {
4359           /* Clear IDLE Flag */
4360           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4361         }
4362 
4363 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4364         /*Call registered Rx Event callback*/
4365         huart->RxEventCallback(huart, huart->RxXferSize);
4366 #else
4367         /*Call legacy weak Rx Event callback*/
4368         HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4369 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4370       }
4371       else
4372       {
4373         /* Standard reception API called */
4374 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4375         /*Call registered Rx complete callback*/
4376         huart->RxCpltCallback(huart);
4377 #else
4378         /*Call legacy weak Rx complete callback*/
4379         HAL_UART_RxCpltCallback(huart);
4380 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4381       }
4382     }
4383   }
4384   else
4385   {
4386     /* Clear RXNE interrupt flag */
4387     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4388   }
4389 }
4390 
4391 /**
4392   * @brief RX interrupt handler for 7 or 8  bits data word length and FIFO mode is enabled.
4393   * @note   Function is called under interruption only, once
4394   *         interruptions have been enabled by HAL_UART_Receive_IT()
4395   * @param huart UART handle.
4396   * @retval None
4397   */
4398 static void UART_RxISR_8BIT_FIFOEN(UART_HandleTypeDef *huart)
4399 {
4400   uint16_t  uhMask = huart->Mask;
4401   uint16_t  uhdata;
4402   uint16_t  nb_rx_data;
4403   uint16_t  rxdatacount;
4404   uint32_t  isrflags = READ_REG(huart->Instance->ISR);
4405   uint32_t  cr1its   = READ_REG(huart->Instance->CR1);
4406   uint32_t  cr3its   = READ_REG(huart->Instance->CR3);
4407 
4408   /* Check that a Rx process is ongoing */
4409   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4410   {
4411     nb_rx_data = huart->NbRxDataToProcess;
4412     while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4413     {
4414       uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4415       *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
4416       huart->pRxBuffPtr++;
4417       huart->RxXferCount--;
4418       isrflags = READ_REG(huart->Instance->ISR);
4419 
4420       /* If some non blocking errors occurred */
4421       if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4422       {
4423         /* UART parity error interrupt occurred -------------------------------------*/
4424         if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4425         {
4426           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4427 
4428           huart->ErrorCode |= HAL_UART_ERROR_PE;
4429         }
4430 
4431         /* UART frame error interrupt occurred --------------------------------------*/
4432         if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4433         {
4434           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4435 
4436           huart->ErrorCode |= HAL_UART_ERROR_FE;
4437         }
4438 
4439         /* UART noise error interrupt occurred --------------------------------------*/
4440         if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4441         {
4442           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4443 
4444           huart->ErrorCode |= HAL_UART_ERROR_NE;
4445         }
4446 
4447         /* Call UART Error Call back function if need be ----------------------------*/
4448         if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4449         {
4450           /* Non Blocking error : transfer could go on.
4451           Error is notified to user through user error callback */
4452 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4453           /*Call registered error callback*/
4454           huart->ErrorCallback(huart);
4455 #else
4456           /*Call legacy weak error callback*/
4457           HAL_UART_ErrorCallback(huart);
4458 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4459           huart->ErrorCode = HAL_UART_ERROR_NONE;
4460         }
4461       }
4462 
4463       if (huart->RxXferCount == 0U)
4464       {
4465         /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4466         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4467 
4468         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4469            and RX FIFO Threshold interrupt */
4470         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4471 
4472         /* Rx process is completed, restore huart->RxState to Ready */
4473         huart->RxState = HAL_UART_STATE_READY;
4474 
4475         /* Clear RxISR function pointer */
4476         huart->RxISR = NULL;
4477 
4478         /* Initialize type of RxEvent to Transfer Complete */
4479         huart->RxEventType = HAL_UART_RXEVENT_TC;
4480 
4481         if (!(IS_LPUART_INSTANCE(huart->Instance)))
4482         {
4483           /* Check that USART RTOEN bit is set */
4484           if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4485           {
4486             /* Enable the UART Receiver Timeout Interrupt */
4487             ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4488           }
4489         }
4490 
4491         /* Check current reception Mode :
4492            If Reception till IDLE event has been selected : */
4493         if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4494         {
4495           /* Set reception type to Standard */
4496           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4497 
4498           /* Disable IDLE interrupt */
4499           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4500 
4501           if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4502           {
4503             /* Clear IDLE Flag */
4504             __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4505           }
4506 
4507 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4508           /*Call registered Rx Event callback*/
4509           huart->RxEventCallback(huart, huart->RxXferSize);
4510 #else
4511           /*Call legacy weak Rx Event callback*/
4512           HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4513 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4514         }
4515         else
4516         {
4517           /* Standard reception API called */
4518 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4519           /*Call registered Rx complete callback*/
4520           huart->RxCpltCallback(huart);
4521 #else
4522           /*Call legacy weak Rx complete callback*/
4523           HAL_UART_RxCpltCallback(huart);
4524 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4525         }
4526       }
4527     }
4528 
4529     /* When remaining number of bytes to receive is less than the RX FIFO
4530     threshold, next incoming frames are processed as if FIFO mode was
4531     disabled (i.e. one interrupt per received frame).
4532     */
4533     rxdatacount = huart->RxXferCount;
4534     if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4535     {
4536       /* Disable the UART RXFT interrupt*/
4537       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4538 
4539       /* Update the RxISR function pointer */
4540       huart->RxISR = UART_RxISR_8BIT;
4541 
4542       /* Enable the UART Data Register Not Empty interrupt */
4543       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4544     }
4545   }
4546   else
4547   {
4548     /* Clear RXNE interrupt flag */
4549     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4550   }
4551 }
4552 
4553 /**
4554   * @brief RX interrupt handler for 9 bits data word length and FIFO mode is enabled.
4555   * @note   Function is called under interruption only, once
4556   *         interruptions have been enabled by HAL_UART_Receive_IT()
4557   * @param huart UART handle.
4558   * @retval None
4559   */
4560 static void UART_RxISR_16BIT_FIFOEN(UART_HandleTypeDef *huart)
4561 {
4562   uint16_t *tmp;
4563   uint16_t  uhMask = huart->Mask;
4564   uint16_t  uhdata;
4565   uint16_t  nb_rx_data;
4566   uint16_t  rxdatacount;
4567   uint32_t  isrflags = READ_REG(huart->Instance->ISR);
4568   uint32_t  cr1its   = READ_REG(huart->Instance->CR1);
4569   uint32_t  cr3its   = READ_REG(huart->Instance->CR3);
4570 
4571   /* Check that a Rx process is ongoing */
4572   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
4573   {
4574     nb_rx_data = huart->NbRxDataToProcess;
4575     while ((nb_rx_data > 0U) && ((isrflags & USART_ISR_RXNE_RXFNE) != 0U))
4576     {
4577       uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
4578       tmp = (uint16_t *) huart->pRxBuffPtr ;
4579       *tmp = (uint16_t)(uhdata & uhMask);
4580       huart->pRxBuffPtr += 2U;
4581       huart->RxXferCount--;
4582       isrflags = READ_REG(huart->Instance->ISR);
4583 
4584       /* If some non blocking errors occurred */
4585       if ((isrflags & (USART_ISR_PE | USART_ISR_FE | USART_ISR_NE)) != 0U)
4586       {
4587         /* UART parity error interrupt occurred -------------------------------------*/
4588         if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
4589         {
4590           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
4591 
4592           huart->ErrorCode |= HAL_UART_ERROR_PE;
4593         }
4594 
4595         /* UART frame error interrupt occurred --------------------------------------*/
4596         if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4597         {
4598           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
4599 
4600           huart->ErrorCode |= HAL_UART_ERROR_FE;
4601         }
4602 
4603         /* UART noise error interrupt occurred --------------------------------------*/
4604         if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
4605         {
4606           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
4607 
4608           huart->ErrorCode |= HAL_UART_ERROR_NE;
4609         }
4610 
4611         /* Call UART Error Call back function if need be ----------------------------*/
4612         if (huart->ErrorCode != HAL_UART_ERROR_NONE)
4613         {
4614           /* Non Blocking error : transfer could go on.
4615           Error is notified to user through user error callback */
4616 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4617           /*Call registered error callback*/
4618           huart->ErrorCallback(huart);
4619 #else
4620           /*Call legacy weak error callback*/
4621           HAL_UART_ErrorCallback(huart);
4622 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4623           huart->ErrorCode = HAL_UART_ERROR_NONE;
4624         }
4625       }
4626 
4627       if (huart->RxXferCount == 0U)
4628       {
4629         /* Disable the UART Parity Error Interrupt and RXFT interrupt*/
4630         ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
4631 
4632         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error)
4633            and RX FIFO Threshold interrupt */
4634         ATOMIC_CLEAR_BIT(huart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
4635 
4636         /* Rx process is completed, restore huart->RxState to Ready */
4637         huart->RxState = HAL_UART_STATE_READY;
4638 
4639         /* Clear RxISR function pointer */
4640         huart->RxISR = NULL;
4641 
4642         /* Initialize type of RxEvent to Transfer Complete */
4643         huart->RxEventType = HAL_UART_RXEVENT_TC;
4644 
4645         if (!(IS_LPUART_INSTANCE(huart->Instance)))
4646         {
4647           /* Check that USART RTOEN bit is set */
4648           if (READ_BIT(huart->Instance->CR2, USART_CR2_RTOEN) != 0U)
4649           {
4650             /* Enable the UART Receiver Timeout Interrupt */
4651             ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_RTOIE);
4652           }
4653         }
4654 
4655         /* Check current reception Mode :
4656            If Reception till IDLE event has been selected : */
4657         if (huart->ReceptionType == HAL_UART_RECEPTION_TOIDLE)
4658         {
4659           /* Set reception type to Standard */
4660           huart->ReceptionType = HAL_UART_RECEPTION_STANDARD;
4661 
4662           /* Disable IDLE interrupt */
4663           ATOMIC_CLEAR_BIT(huart->Instance->CR1, USART_CR1_IDLEIE);
4664 
4665           if (__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE) == SET)
4666           {
4667             /* Clear IDLE Flag */
4668             __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_IDLEF);
4669           }
4670 
4671 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4672           /*Call registered Rx Event callback*/
4673           huart->RxEventCallback(huart, huart->RxXferSize);
4674 #else
4675           /*Call legacy weak Rx Event callback*/
4676           HAL_UARTEx_RxEventCallback(huart, huart->RxXferSize);
4677 #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
4678         }
4679         else
4680         {
4681           /* Standard reception API called */
4682 #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
4683           /*Call registered Rx complete callback*/
4684           huart->RxCpltCallback(huart);
4685 #else
4686           /*Call legacy weak Rx complete callback*/
4687           HAL_UART_RxCpltCallback(huart);
4688 #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
4689         }
4690       }
4691     }
4692 
4693     /* When remaining number of bytes to receive is less than the RX FIFO
4694     threshold, next incoming frames are processed as if FIFO mode was
4695     disabled (i.e. one interrupt per received frame).
4696     */
4697     rxdatacount = huart->RxXferCount;
4698     if ((rxdatacount != 0U) && (rxdatacount < huart->NbRxDataToProcess))
4699     {
4700       /* Disable the UART RXFT interrupt*/
4701       ATOMIC_CLEAR_BIT(huart->Instance->CR3, USART_CR3_RXFTIE);
4702 
4703       /* Update the RxISR function pointer */
4704       huart->RxISR = UART_RxISR_16BIT;
4705 
4706       /* Enable the UART Data Register Not Empty interrupt */
4707       ATOMIC_SET_BIT(huart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
4708     }
4709   }
4710   else
4711   {
4712     /* Clear RXNE interrupt flag */
4713     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
4714   }
4715 }
4716 
4717 /**
4718   * @}
4719   */
4720 
4721 #endif /* HAL_UART_MODULE_ENABLED */
4722 /**
4723   * @}
4724   */
4725 
4726 /**
4727   * @}
4728   */
4729