STM32L443xx HAL User Manual
stm32l4xx_hal_usart.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_usart.c
00004   * @author  MCD Application Team
00005   * @brief   USART HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
00008   *          Peripheral (USART).
00009   *           + Initialization and de-initialization functions
00010   *           + IO operation functions
00011   *           + Peripheral Control functions
00012   *           + Peripheral State and Error functions
00013   *
00014   ******************************************************************************
00015   * @attention
00016   *
00017   * Copyright (c) 2017 STMicroelectronics.
00018   * All rights reserved.
00019   *
00020   * This software is licensed under terms that can be found in the LICENSE file
00021   * in the root directory of this software component.
00022   * If no LICENSE file comes with this software, it is provided AS-IS.
00023   *
00024   ******************************************************************************
00025   @verbatim
00026  ===============================================================================
00027                         ##### How to use this driver #####
00028  ===============================================================================
00029     [..]
00030       The USART HAL driver can be used as follows:
00031 
00032       (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
00033       (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
00034           (++) Enable the USARTx interface clock.
00035           (++) USART pins configuration:
00036             (+++) Enable the clock for the USART GPIOs.
00037             (+++) Configure these USART pins as alternate function pull-up.
00038           (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
00039                 HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
00040             (+++) Configure the USARTx interrupt priority.
00041             (+++) Enable the NVIC USART IRQ handle.
00042             (++) USART interrupts handling:
00043               -@@-   The specific USART interrupts (Transmission complete interrupt,
00044                   RXNE interrupt and Error Interrupts) will be managed using the macros
00045                   __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
00046           (++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
00047                HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
00048             (+++) Declare a DMA handle structure for the Tx/Rx channel.
00049             (+++) Enable the DMAx interface clock.
00050             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00051             (+++) Configure the DMA Tx/Rx channel.
00052             (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
00053             (+++) Configure the priority and enable the NVIC for the transfer
00054                   complete interrupt on the DMA Tx/Rx channel.
00055 
00056       (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode
00057           (Receiver/Transmitter) in the husart handle Init structure.
00058 
00059       (#) Initialize the USART registers by calling the HAL_USART_Init() API:
00060           (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00061                by calling the customized HAL_USART_MspInit(&husart) API.
00062 
00063     [..]
00064      (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
00065         HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
00066         HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
00067 
00068     ##### Callback registration #####
00069     ==================================
00070 
00071     [..]
00072     The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
00073     allows the user to configure dynamically the driver callbacks.
00074 
00075     [..]
00076     Use Function HAL_USART_RegisterCallback() to register a user callback.
00077     Function HAL_USART_RegisterCallback() allows to register following callbacks:
00078     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00079     (+) TxCpltCallback            : Tx Complete Callback.
00080     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00081     (+) RxCpltCallback            : Rx Complete Callback.
00082     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
00083     (+) ErrorCallback             : Error Callback.
00084     (+) AbortCpltCallback         : Abort Complete Callback.
00085     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
00086     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
00087     (+) MspInitCallback           : USART MspInit.
00088     (+) MspDeInitCallback         : USART MspDeInit.
00089     This function takes as parameters the HAL peripheral handle, the Callback ID
00090     and a pointer to the user callback function.
00091 
00092     [..]
00093     Use function HAL_USART_UnRegisterCallback() to reset a callback to the default
00094     weak (surcharged) function.
00095     HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00096     and the Callback ID.
00097     This function allows to reset following callbacks:
00098     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00099     (+) TxCpltCallback            : Tx Complete Callback.
00100     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00101     (+) RxCpltCallback            : Rx Complete Callback.
00102     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
00103     (+) ErrorCallback             : Error Callback.
00104     (+) AbortCpltCallback         : Abort Complete Callback.
00105     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
00106     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
00107     (+) MspInitCallback           : USART MspInit.
00108     (+) MspDeInitCallback         : USART MspDeInit.
00109 
00110     [..]
00111     By default, after the HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
00112     all callbacks are set to the corresponding weak (surcharged) functions:
00113     examples HAL_USART_TxCpltCallback(), HAL_USART_RxHalfCpltCallback().
00114     Exception done for MspInit and MspDeInit functions that are respectively
00115     reset to the legacy weak (surcharged) functions in the HAL_USART_Init()
00116     and HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
00117     If not, MspInit or MspDeInit are not null, the HAL_USART_Init() and HAL_USART_DeInit()
00118     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00119 
00120     [..]
00121     Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
00122     Exception done MspInit/MspDeInit that can be registered/unregistered
00123     in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
00124     MspInit/DeInit callbacks can be used during the Init/DeInit.
00125     In that case first register the MspInit/MspDeInit user callbacks
00126     using HAL_USART_RegisterCallback() before calling HAL_USART_DeInit()
00127     or HAL_USART_Init() function.
00128 
00129     [..]
00130     When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
00131     not defined, the callback registration feature is not available
00132     and weak (surcharged) callbacks are used.
00133 
00134 
00135   @endverbatim
00136   ******************************************************************************
00137   */
00138 
00139 /* Includes ------------------------------------------------------------------*/
00140 #include "stm32l4xx_hal.h"
00141 
00142 /** @addtogroup STM32L4xx_HAL_Driver
00143   * @{
00144   */
00145 
00146 /** @defgroup USART USART
00147   * @brief HAL USART Synchronous module driver
00148   * @{
00149   */
00150 
00151 #ifdef HAL_USART_MODULE_ENABLED
00152 
00153 /* Private typedef -----------------------------------------------------------*/
00154 /* Private define ------------------------------------------------------------*/
00155 /** @defgroup USART_Private_Constants USART Private Constants
00156   * @{
00157   */
00158 #define USART_DUMMY_DATA          ((uint16_t) 0xFFFF)           /*!< USART transmitted dummy data                     */
00159 #define USART_TEACK_REACK_TIMEOUT             1000U             /*!< USART TX or RX enable acknowledge time-out value */
00160 #if defined(USART_CR1_FIFOEN)
00161 #define USART_CR1_FIELDS          ((uint32_t)(USART_CR1_M |  USART_CR1_PCE | USART_CR1_PS    | \
00162                                               USART_CR1_TE | USART_CR1_RE  | USART_CR1_OVER8 | \
00163                                               USART_CR1_FIFOEN ))                                  /*!< USART CR1 fields of parameters set by USART_SetConfig API */
00164 
00165 #define USART_CR2_FIELDS          ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \
00166                                               USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \
00167                                               USART_CR2_DIS_NSS))                                  /*!< USART CR2 fields of parameters set by USART_SetConfig API */
00168 
00169 #define USART_CR3_FIELDS          ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG ))             /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */
00170 #else
00171 #define USART_CR1_FIELDS          ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
00172                                               USART_CR1_TE | USART_CR1_RE  | USART_CR1_OVER8))    /*!< USART CR1 fields of parameters set by USART_SetConfig API */
00173 #define USART_CR2_FIELDS          ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
00174                                               USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
00175 #endif /* USART_CR1_FIFOEN */
00176 
00177 #define USART_BRR_MIN    0x10U        /* USART BRR minimum authorized value */
00178 #define USART_BRR_MAX    0xFFFFU      /* USART BRR maximum authorized value */
00179 /**
00180   * @}
00181   */
00182 
00183 /* Private macros ------------------------------------------------------------*/
00184 /* Private variables ---------------------------------------------------------*/
00185 /* Private function prototypes -----------------------------------------------*/
00186 /** @addtogroup USART_Private_Functions
00187   * @{
00188   */
00189 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00190 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
00191 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00192 static void USART_EndTransfer(USART_HandleTypeDef *husart);
00193 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
00194 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
00195 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
00196 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
00197 static void USART_DMAError(DMA_HandleTypeDef *hdma);
00198 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
00199 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
00200 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
00201 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
00202                                                       uint32_t Tickstart, uint32_t Timeout);
00203 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
00204 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
00205 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart);
00206 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart);
00207 #if defined(USART_CR1_FIFOEN)
00208 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
00209 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
00210 #endif /* USART_CR1_FIFOEN */
00211 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart);
00212 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart);
00213 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart);
00214 #if defined(USART_CR1_FIFOEN)
00215 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
00216 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
00217 #endif /* USART_CR1_FIFOEN */
00218 
00219 
00220 /**
00221   * @}
00222   */
00223 
00224 /* Exported functions --------------------------------------------------------*/
00225 
00226 /** @defgroup USART_Exported_Functions USART Exported Functions
00227   * @{
00228   */
00229 
00230 /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
00231   * @brief    Initialization and Configuration functions
00232   *
00233 @verbatim
00234  ===============================================================================
00235             ##### Initialization and Configuration functions #####
00236  ===============================================================================
00237     [..]
00238     This subsection provides a set of functions allowing to initialize the USART
00239     in asynchronous and in synchronous modes.
00240       (+) For the asynchronous mode only these parameters can be configured:
00241         (++) Baud Rate
00242         (++) Word Length
00243         (++) Stop Bit
00244         (++) Parity: If the parity is enabled, then the MSB bit of the data written
00245              in the data register is transmitted but is changed by the parity bit.
00246         (++) USART polarity
00247         (++) USART phase
00248         (++) USART LastBit
00249         (++) Receiver/transmitter modes
00250 
00251     [..]
00252     The HAL_USART_Init() function follows the USART  synchronous configuration
00253     procedure (details for the procedure are available in reference manual).
00254 
00255 @endverbatim
00256 
00257   Depending on the frame length defined by the M1 and M0 bits (7-bit,
00258   8-bit or 9-bit), the possible USART formats are listed in the
00259   following table.
00260 
00261     Table 1. USART frame format.
00262     +-----------------------------------------------------------------------+
00263     |  M1 bit |  M0 bit |  PCE bit  |            USART frame                |
00264     |---------|---------|-----------|---------------------------------------|
00265     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
00266     |---------|---------|-----------|---------------------------------------|
00267     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
00268     |---------|---------|-----------|---------------------------------------|
00269     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
00270     |---------|---------|-----------|---------------------------------------|
00271     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
00272     |---------|---------|-----------|---------------------------------------|
00273     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
00274     |---------|---------|-----------|---------------------------------------|
00275     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
00276     +-----------------------------------------------------------------------+
00277 
00278   * @{
00279   */
00280 
00281 /**
00282   * @brief  Initialize the USART mode according to the specified
00283   *         parameters in the USART_InitTypeDef and initialize the associated handle.
00284   * @param  husart USART handle.
00285   * @retval HAL status
00286   */
00287 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
00288 {
00289   /* Check the USART handle allocation */
00290   if (husart == NULL)
00291   {
00292     return HAL_ERROR;
00293   }
00294 
00295   /* Check the parameters */
00296   assert_param(IS_USART_INSTANCE(husart->Instance));
00297 
00298   if (husart->State == HAL_USART_STATE_RESET)
00299   {
00300     /* Allocate lock resource and initialize it */
00301     husart->Lock = HAL_UNLOCKED;
00302 
00303 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00304     USART_InitCallbacksToDefault(husart);
00305 
00306     if (husart->MspInitCallback == NULL)
00307     {
00308       husart->MspInitCallback = HAL_USART_MspInit;
00309     }
00310 
00311     /* Init the low level hardware */
00312     husart->MspInitCallback(husart);
00313 #else
00314     /* Init the low level hardware : GPIO, CLOCK */
00315     HAL_USART_MspInit(husart);
00316 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00317   }
00318 
00319   husart->State = HAL_USART_STATE_BUSY;
00320 
00321   /* Disable the Peripheral */
00322   __HAL_USART_DISABLE(husart);
00323 
00324   /* Set the Usart Communication parameters */
00325   if (USART_SetConfig(husart) == HAL_ERROR)
00326   {
00327     return HAL_ERROR;
00328   }
00329 
00330   /* In Synchronous mode, the following bits must be kept cleared:
00331   - LINEN bit in the USART_CR2 register
00332   - HDSEL, SCEN and IREN bits in the USART_CR3 register.
00333   */
00334   husart->Instance->CR2 &= ~USART_CR2_LINEN;
00335   husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
00336 
00337   /* Enable the Peripheral */
00338   __HAL_USART_ENABLE(husart);
00339 
00340   /* TEACK and/or REACK to check before moving husart->State to Ready */
00341   return (USART_CheckIdleState(husart));
00342 }
00343 
00344 /**
00345   * @brief DeInitialize the USART peripheral.
00346   * @param  husart USART handle.
00347   * @retval HAL status
00348   */
00349 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
00350 {
00351   /* Check the USART handle allocation */
00352   if (husart == NULL)
00353   {
00354     return HAL_ERROR;
00355   }
00356 
00357   /* Check the parameters */
00358   assert_param(IS_USART_INSTANCE(husart->Instance));
00359 
00360   husart->State = HAL_USART_STATE_BUSY;
00361 
00362   husart->Instance->CR1 = 0x0U;
00363   husart->Instance->CR2 = 0x0U;
00364   husart->Instance->CR3 = 0x0U;
00365 
00366 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00367   if (husart->MspDeInitCallback == NULL)
00368   {
00369     husart->MspDeInitCallback = HAL_USART_MspDeInit;
00370   }
00371   /* DeInit the low level hardware */
00372   husart->MspDeInitCallback(husart);
00373 #else
00374   /* DeInit the low level hardware */
00375   HAL_USART_MspDeInit(husart);
00376 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00377 
00378   husart->ErrorCode = HAL_USART_ERROR_NONE;
00379   husart->State = HAL_USART_STATE_RESET;
00380 
00381   /* Process Unlock */
00382   __HAL_UNLOCK(husart);
00383 
00384   return HAL_OK;
00385 }
00386 
00387 /**
00388   * @brief Initialize the USART MSP.
00389   * @param husart USART handle.
00390   * @retval None
00391   */
00392 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
00393 {
00394   /* Prevent unused argument(s) compilation warning */
00395   UNUSED(husart);
00396 
00397   /* NOTE : This function should not be modified, when the callback is needed,
00398             the HAL_USART_MspInit can be implemented in the user file
00399    */
00400 }
00401 
00402 /**
00403   * @brief DeInitialize the USART MSP.
00404   * @param husart USART handle.
00405   * @retval None
00406   */
00407 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
00408 {
00409   /* Prevent unused argument(s) compilation warning */
00410   UNUSED(husart);
00411 
00412   /* NOTE : This function should not be modified, when the callback is needed,
00413             the HAL_USART_MspDeInit can be implemented in the user file
00414    */
00415 }
00416 
00417 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00418 /**
00419   * @brief  Register a User USART Callback
00420   *         To be used instead of the weak predefined callback
00421   * @param  husart usart handle
00422   * @param  CallbackID ID of the callback to be registered
00423   *         This parameter can be one of the following values:
00424   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
00425   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
00426   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
00427   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
00428   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
00429   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
00430   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00431   *           @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
00432   *           @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
00433   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
00434   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
00435   * @param  pCallback pointer to the Callback function
00436   * @retval HAL status
00437 +  */
00438 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID,
00439                                              pUSART_CallbackTypeDef pCallback)
00440 {
00441   HAL_StatusTypeDef status = HAL_OK;
00442 
00443   if (pCallback == NULL)
00444   {
00445     /* Update the error code */
00446     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00447 
00448     return HAL_ERROR;
00449   }
00450   /* Process locked */
00451   __HAL_LOCK(husart);
00452 
00453   if (husart->State == HAL_USART_STATE_READY)
00454   {
00455     switch (CallbackID)
00456     {
00457       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
00458         husart->TxHalfCpltCallback = pCallback;
00459         break;
00460 
00461       case HAL_USART_TX_COMPLETE_CB_ID :
00462         husart->TxCpltCallback = pCallback;
00463         break;
00464 
00465       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
00466         husart->RxHalfCpltCallback = pCallback;
00467         break;
00468 
00469       case HAL_USART_RX_COMPLETE_CB_ID :
00470         husart->RxCpltCallback = pCallback;
00471         break;
00472 
00473       case HAL_USART_TX_RX_COMPLETE_CB_ID :
00474         husart->TxRxCpltCallback = pCallback;
00475         break;
00476 
00477       case HAL_USART_ERROR_CB_ID :
00478         husart->ErrorCallback = pCallback;
00479         break;
00480 
00481       case HAL_USART_ABORT_COMPLETE_CB_ID :
00482         husart->AbortCpltCallback = pCallback;
00483         break;
00484 
00485 #if defined(USART_CR1_FIFOEN)
00486       case HAL_USART_RX_FIFO_FULL_CB_ID :
00487         husart->RxFifoFullCallback = pCallback;
00488         break;
00489 
00490       case HAL_USART_TX_FIFO_EMPTY_CB_ID :
00491         husart->TxFifoEmptyCallback = pCallback;
00492         break;
00493 #endif /* USART_CR1_FIFOEN */
00494 
00495       case HAL_USART_MSPINIT_CB_ID :
00496         husart->MspInitCallback = pCallback;
00497         break;
00498 
00499       case HAL_USART_MSPDEINIT_CB_ID :
00500         husart->MspDeInitCallback = pCallback;
00501         break;
00502 
00503       default :
00504         /* Update the error code */
00505         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00506 
00507         /* Return error status */
00508         status =  HAL_ERROR;
00509         break;
00510     }
00511   }
00512   else if (husart->State == HAL_USART_STATE_RESET)
00513   {
00514     switch (CallbackID)
00515     {
00516       case HAL_USART_MSPINIT_CB_ID :
00517         husart->MspInitCallback = pCallback;
00518         break;
00519 
00520       case HAL_USART_MSPDEINIT_CB_ID :
00521         husart->MspDeInitCallback = pCallback;
00522         break;
00523 
00524       default :
00525         /* Update the error code */
00526         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00527 
00528         /* Return error status */
00529         status =  HAL_ERROR;
00530         break;
00531     }
00532   }
00533   else
00534   {
00535     /* Update the error code */
00536     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00537 
00538     /* Return error status */
00539     status =  HAL_ERROR;
00540   }
00541 
00542   /* Release Lock */
00543   __HAL_UNLOCK(husart);
00544 
00545   return status;
00546 }
00547 
00548 /**
00549   * @brief  Unregister an USART Callback
00550   *         USART callaback is redirected to the weak predefined callback
00551   * @param  husart usart handle
00552   * @param  CallbackID ID of the callback to be unregistered
00553   *         This parameter can be one of the following values:
00554   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
00555   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
00556   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
00557   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
00558   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
00559   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
00560   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00561   *           @arg @ref HAL_USART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
00562   *           @arg @ref HAL_USART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
00563   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
00564   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
00565   * @retval HAL status
00566   */
00567 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
00568 {
00569   HAL_StatusTypeDef status = HAL_OK;
00570 
00571   /* Process locked */
00572   __HAL_LOCK(husart);
00573 
00574   if (HAL_USART_STATE_READY == husart->State)
00575   {
00576     switch (CallbackID)
00577     {
00578       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
00579         husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback  */
00580         break;
00581 
00582       case HAL_USART_TX_COMPLETE_CB_ID :
00583         husart->TxCpltCallback = HAL_USART_TxCpltCallback;                       /* Legacy weak TxCpltCallback       */
00584         break;
00585 
00586       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
00587         husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback   */
00588         break;
00589 
00590       case HAL_USART_RX_COMPLETE_CB_ID :
00591         husart->RxCpltCallback = HAL_USART_RxCpltCallback;                       /* Legacy weak RxCpltCallback       */
00592         break;
00593 
00594       case HAL_USART_TX_RX_COMPLETE_CB_ID :
00595         husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback;                   /* Legacy weak TxRxCpltCallback     */
00596         break;
00597 
00598       case HAL_USART_ERROR_CB_ID :
00599         husart->ErrorCallback = HAL_USART_ErrorCallback;                         /* Legacy weak ErrorCallback        */
00600         break;
00601 
00602       case HAL_USART_ABORT_COMPLETE_CB_ID :
00603         husart->AbortCpltCallback = HAL_USART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback    */
00604         break;
00605 
00606 #if defined(USART_CR1_FIFOEN)
00607       case HAL_USART_RX_FIFO_FULL_CB_ID :
00608         husart->RxFifoFullCallback = HAL_USARTEx_RxFifoFullCallback;             /* Legacy weak RxFifoFullCallback   */
00609         break;
00610 
00611       case HAL_USART_TX_FIFO_EMPTY_CB_ID :
00612         husart->TxFifoEmptyCallback = HAL_USARTEx_TxFifoEmptyCallback;           /* Legacy weak TxFifoEmptyCallback  */
00613         break;
00614 #endif /* USART_CR1_FIFOEN */
00615 
00616       case HAL_USART_MSPINIT_CB_ID :
00617         husart->MspInitCallback = HAL_USART_MspInit;                             /* Legacy weak MspInitCallback      */
00618         break;
00619 
00620       case HAL_USART_MSPDEINIT_CB_ID :
00621         husart->MspDeInitCallback = HAL_USART_MspDeInit;                         /* Legacy weak MspDeInitCallback    */
00622         break;
00623 
00624       default :
00625         /* Update the error code */
00626         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00627 
00628         /* Return error status */
00629         status =  HAL_ERROR;
00630         break;
00631     }
00632   }
00633   else if (HAL_USART_STATE_RESET == husart->State)
00634   {
00635     switch (CallbackID)
00636     {
00637       case HAL_USART_MSPINIT_CB_ID :
00638         husart->MspInitCallback = HAL_USART_MspInit;
00639         break;
00640 
00641       case HAL_USART_MSPDEINIT_CB_ID :
00642         husart->MspDeInitCallback = HAL_USART_MspDeInit;
00643         break;
00644 
00645       default :
00646         /* Update the error code */
00647         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00648 
00649         /* Return error status */
00650         status =  HAL_ERROR;
00651         break;
00652     }
00653   }
00654   else
00655   {
00656     /* Update the error code */
00657     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00658 
00659     /* Return error status */
00660     status =  HAL_ERROR;
00661   }
00662 
00663   /* Release Lock */
00664   __HAL_UNLOCK(husart);
00665 
00666   return status;
00667 }
00668 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00669 
00670 
00671 /**
00672   * @}
00673   */
00674 
00675 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
00676   * @brief   USART Transmit and Receive functions
00677   *
00678 @verbatim
00679  ===============================================================================
00680                       ##### IO operation functions #####
00681  ===============================================================================
00682     [..] This subsection provides a set of functions allowing to manage the USART synchronous
00683     data transfers.
00684 
00685     [..] The USART supports master mode only: it cannot receive or send data related to an input
00686          clock (SCLK is always an output).
00687 
00688     [..]
00689 
00690     (#) There are two modes of transfer:
00691         (++) Blocking mode: The communication is performed in polling mode.
00692              The HAL status of all data processing is returned by the same function
00693              after finishing transfer.
00694         (++) No-Blocking mode: The communication is performed using Interrupts
00695              or DMA, These API's return the HAL status.
00696              The end of the data processing will be indicated through the
00697              dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
00698              using DMA mode.
00699              The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
00700              will be executed respectively at the end of the transmit or Receive process
00701              The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
00702 
00703     (#) Blocking mode API's are :
00704         (++) HAL_USART_Transmit() in simplex mode
00705         (++) HAL_USART_Receive() in full duplex receive only
00706         (++) HAL_USART_TransmitReceive() in full duplex mode
00707 
00708     (#) Non-Blocking mode API's with Interrupt are :
00709         (++) HAL_USART_Transmit_IT() in simplex mode
00710         (++) HAL_USART_Receive_IT() in full duplex receive only
00711         (++) HAL_USART_TransmitReceive_IT() in full duplex mode
00712         (++) HAL_USART_IRQHandler()
00713 
00714     (#) No-Blocking mode API's  with DMA are :
00715         (++) HAL_USART_Transmit_DMA() in simplex mode
00716         (++) HAL_USART_Receive_DMA() in full duplex receive only
00717         (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
00718         (++) HAL_USART_DMAPause()
00719         (++) HAL_USART_DMAResume()
00720         (++) HAL_USART_DMAStop()
00721 
00722     (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
00723         (++) HAL_USART_TxCpltCallback()
00724         (++) HAL_USART_RxCpltCallback()
00725         (++) HAL_USART_TxHalfCpltCallback()
00726         (++) HAL_USART_RxHalfCpltCallback()
00727         (++) HAL_USART_ErrorCallback()
00728         (++) HAL_USART_TxRxCpltCallback()
00729 
00730     (#) Non-Blocking mode transfers could be aborted using Abort API's :
00731         (++) HAL_USART_Abort()
00732         (++) HAL_USART_Abort_IT()
00733 
00734     (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
00735         (++) HAL_USART_AbortCpltCallback()
00736 
00737     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
00738         Errors are handled as follows :
00739         (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
00740              to be evaluated by user : this concerns Frame Error,
00741              Parity Error or Noise Error in Interrupt mode reception .
00742              Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify
00743              error type, and HAL_USART_ErrorCallback() user callback is executed.
00744              Transfer is kept ongoing on USART side.
00745              If user wants to abort it, Abort services should be called by user.
00746         (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
00747              This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
00748              Error code is set to allow user to identify error type,
00749              and HAL_USART_ErrorCallback() user callback is executed.
00750 
00751 @endverbatim
00752   * @{
00753   */
00754 
00755 /**
00756   * @brief  Simplex send an amount of data in blocking mode.
00757   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00758   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
00759   *         of u16 provided through pTxData.
00760   * @param  husart USART handle.
00761   * @param  pTxData Pointer to data buffer (u8 or u16 data elements).
00762   * @param  Size Amount of data elements (u8 or u16) to be sent.
00763   * @param  Timeout Timeout duration.
00764   * @retval HAL status
00765   */
00766 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
00767 {
00768   const uint8_t  *ptxdata8bits;
00769   const uint16_t *ptxdata16bits;
00770   uint32_t tickstart;
00771 
00772   if (husart->State == HAL_USART_STATE_READY)
00773   {
00774     if ((pTxData == NULL) || (Size == 0U))
00775     {
00776       return  HAL_ERROR;
00777     }
00778 
00779     /* Process Locked */
00780     __HAL_LOCK(husart);
00781 
00782     husart->ErrorCode = HAL_USART_ERROR_NONE;
00783     husart->State = HAL_USART_STATE_BUSY_TX;
00784 
00785     /* Init tickstart for timeout management */
00786     tickstart = HAL_GetTick();
00787 
00788     husart->TxXferSize = Size;
00789     husart->TxXferCount = Size;
00790 
00791     /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
00792     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00793     {
00794       ptxdata8bits  = NULL;
00795       ptxdata16bits = (const uint16_t *) pTxData;
00796     }
00797     else
00798     {
00799       ptxdata8bits  = pTxData;
00800       ptxdata16bits = NULL;
00801     }
00802 
00803     /* Check the remaining data to be sent */
00804     while (husart->TxXferCount > 0U)
00805     {
00806       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00807       {
00808         return HAL_TIMEOUT;
00809       }
00810       if (ptxdata8bits == NULL)
00811       {
00812         husart->Instance->TDR = (uint16_t)(*ptxdata16bits & 0x01FFU);
00813         ptxdata16bits++;
00814       }
00815       else
00816       {
00817         husart->Instance->TDR = (uint8_t)(*ptxdata8bits & 0xFFU);
00818         ptxdata8bits++;
00819       }
00820 
00821       husart->TxXferCount--;
00822     }
00823 
00824     if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
00825     {
00826       return HAL_TIMEOUT;
00827     }
00828 
00829     /* Clear Transmission Complete Flag */
00830     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
00831 
00832     /* Clear overrun flag and discard the received data */
00833     __HAL_USART_CLEAR_OREFLAG(husart);
00834     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
00835     __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
00836 
00837     /* At end of Tx process, restore husart->State to Ready */
00838     husart->State = HAL_USART_STATE_READY;
00839 
00840     /* Process Unlocked */
00841     __HAL_UNLOCK(husart);
00842 
00843     return HAL_OK;
00844   }
00845   else
00846   {
00847     return HAL_BUSY;
00848   }
00849 }
00850 
00851 /**
00852   * @brief Receive an amount of data in blocking mode.
00853   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
00854   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00855   *         the received data is handled as a set of u16. In this case, Size must indicate the number
00856   *         of u16 available through pRxData.
00857   * @param husart USART handle.
00858   * @param pRxData Pointer to data buffer (u8 or u16 data elements).
00859   * @param Size Amount of data elements (u8 or u16) to be received.
00860   * @param Timeout Timeout duration.
00861   * @retval HAL status
00862   */
00863 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
00864 {
00865   uint8_t  *prxdata8bits;
00866   uint16_t *prxdata16bits;
00867   uint16_t uhMask;
00868   uint32_t tickstart;
00869 
00870   if (husart->State == HAL_USART_STATE_READY)
00871   {
00872     if ((pRxData == NULL) || (Size == 0U))
00873     {
00874       return  HAL_ERROR;
00875     }
00876 
00877     /* Process Locked */
00878     __HAL_LOCK(husart);
00879 
00880     husart->ErrorCode = HAL_USART_ERROR_NONE;
00881     husart->State = HAL_USART_STATE_BUSY_RX;
00882 
00883     /* Init tickstart for timeout management */
00884     tickstart = HAL_GetTick();
00885 
00886     husart->RxXferSize = Size;
00887     husart->RxXferCount = Size;
00888 
00889     /* Computation of USART mask to apply to RDR register */
00890     USART_MASK_COMPUTATION(husart);
00891     uhMask = husart->Mask;
00892 
00893     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
00894     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00895     {
00896       prxdata8bits  = NULL;
00897       prxdata16bits = (uint16_t *) pRxData;
00898     }
00899     else
00900     {
00901       prxdata8bits  = pRxData;
00902       prxdata16bits = NULL;
00903     }
00904 
00905     /* as long as data have to be received */
00906     while (husart->RxXferCount > 0U)
00907     {
00908 #if defined(USART_CR2_SLVEN)
00909       if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
00910 #endif /* USART_CR2_SLVEN */
00911       {
00912         /* Wait until TXE flag is set to send dummy byte in order to generate the
00913         * clock for the slave to send data.
00914         * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
00915         * can be written for all the cases. */
00916         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00917         {
00918           return HAL_TIMEOUT;
00919         }
00920         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
00921       }
00922 
00923       /* Wait for RXNE Flag */
00924       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
00925       {
00926         return HAL_TIMEOUT;
00927       }
00928 
00929       if (prxdata8bits == NULL)
00930       {
00931         *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
00932         prxdata16bits++;
00933       }
00934       else
00935       {
00936         *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
00937         prxdata8bits++;
00938       }
00939 
00940       husart->RxXferCount--;
00941 
00942     }
00943 
00944 #if defined(USART_CR2_SLVEN)
00945     /* Clear SPI slave underrun flag and discard transmit data */
00946     if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
00947     {
00948       __HAL_USART_CLEAR_UDRFLAG(husart);
00949       __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
00950     }
00951 #endif /* USART_CR2_SLVEN */
00952 
00953     /* At end of Rx process, restore husart->State to Ready */
00954     husart->State = HAL_USART_STATE_READY;
00955 
00956     /* Process Unlocked */
00957     __HAL_UNLOCK(husart);
00958 
00959     return HAL_OK;
00960   }
00961   else
00962   {
00963     return HAL_BUSY;
00964   }
00965 }
00966 
00967 /**
00968   * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
00969   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00970   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
00971   *         of u16 available through pTxData and through pRxData.
00972   * @param  husart USART handle.
00973   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
00974   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
00975   * @param  Size amount of data elements (u8 or u16) to be sent (same amount to be received).
00976   * @param  Timeout Timeout duration.
00977   * @retval HAL status
00978   */
00979 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
00980                                             uint16_t Size, uint32_t Timeout)
00981 {
00982   uint8_t  *prxdata8bits;
00983   uint16_t *prxdata16bits;
00984   const uint8_t  *ptxdata8bits;
00985   const uint16_t *ptxdata16bits;
00986   uint16_t uhMask;
00987   uint16_t rxdatacount;
00988   uint32_t tickstart;
00989 
00990   if (husart->State == HAL_USART_STATE_READY)
00991   {
00992     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
00993     {
00994       return  HAL_ERROR;
00995     }
00996 
00997     /* Process Locked */
00998     __HAL_LOCK(husart);
00999 
01000     husart->ErrorCode = HAL_USART_ERROR_NONE;
01001     husart->State = HAL_USART_STATE_BUSY_RX;
01002 
01003     /* Init tickstart for timeout management */
01004     tickstart = HAL_GetTick();
01005 
01006     husart->RxXferSize = Size;
01007     husart->TxXferSize = Size;
01008     husart->TxXferCount = Size;
01009     husart->RxXferCount = Size;
01010 
01011     /* Computation of USART mask to apply to RDR register */
01012     USART_MASK_COMPUTATION(husart);
01013     uhMask = husart->Mask;
01014 
01015     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
01016     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01017     {
01018       prxdata8bits  = NULL;
01019       ptxdata8bits  = NULL;
01020       ptxdata16bits = (const uint16_t *) pTxData;
01021       prxdata16bits = (uint16_t *) pRxData;
01022     }
01023     else
01024     {
01025       prxdata8bits  = pRxData;
01026       ptxdata8bits  = pTxData;
01027       ptxdata16bits = NULL;
01028       prxdata16bits = NULL;
01029     }
01030 
01031 #if defined(USART_CR2_SLVEN)
01032     if ((husart->TxXferCount == 0x01U) || (husart->SlaveMode == USART_SLAVEMODE_ENABLE))
01033 #else
01034     if (husart->TxXferCount == 0x01U)
01035 #endif /* USART_CR2_SLVEN */
01036     {
01037       /* Wait until TXE flag is set to send data */
01038       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
01039       {
01040         return HAL_TIMEOUT;
01041       }
01042       if (ptxdata8bits == NULL)
01043       {
01044         husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
01045         ptxdata16bits++;
01046       }
01047       else
01048       {
01049         husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
01050         ptxdata8bits++;
01051       }
01052 
01053       husart->TxXferCount--;
01054     }
01055 
01056     /* Check the remain data to be sent */
01057     /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
01058     rxdatacount = husart->RxXferCount;
01059     while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
01060     {
01061       if (husart->TxXferCount > 0U)
01062       {
01063         /* Wait until TXE flag is set to send data */
01064         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
01065         {
01066           return HAL_TIMEOUT;
01067         }
01068         if (ptxdata8bits == NULL)
01069         {
01070           husart->Instance->TDR = (uint16_t)(*ptxdata16bits & uhMask);
01071           ptxdata16bits++;
01072         }
01073         else
01074         {
01075           husart->Instance->TDR = (uint8_t)(*ptxdata8bits & (uint8_t)(uhMask & 0xFFU));
01076           ptxdata8bits++;
01077         }
01078 
01079         husart->TxXferCount--;
01080       }
01081 
01082       if (husart->RxXferCount > 0U)
01083       {
01084         /* Wait for RXNE Flag */
01085         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
01086         {
01087           return HAL_TIMEOUT;
01088         }
01089 
01090         if (prxdata8bits == NULL)
01091         {
01092           *prxdata16bits = (uint16_t)(husart->Instance->RDR & uhMask);
01093           prxdata16bits++;
01094         }
01095         else
01096         {
01097           *prxdata8bits = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
01098           prxdata8bits++;
01099         }
01100 
01101         husart->RxXferCount--;
01102       }
01103       rxdatacount = husart->RxXferCount;
01104     }
01105 
01106     /* At end of TxRx process, restore husart->State to Ready */
01107     husart->State = HAL_USART_STATE_READY;
01108 
01109     /* Process Unlocked */
01110     __HAL_UNLOCK(husart);
01111 
01112     return HAL_OK;
01113   }
01114   else
01115   {
01116     return HAL_BUSY;
01117   }
01118 }
01119 
01120 /**
01121   * @brief  Send an amount of data in interrupt mode.
01122   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01123   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
01124   *         of u16 provided through pTxData.
01125   * @param  husart USART handle.
01126   * @param  pTxData pointer to data buffer (u8 or u16 data elements).
01127   * @param  Size amount of data elements (u8 or u16) to be sent.
01128   * @retval HAL status
01129   */
01130 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
01131 {
01132   if (husart->State == HAL_USART_STATE_READY)
01133   {
01134     if ((pTxData == NULL) || (Size == 0U))
01135     {
01136       return HAL_ERROR;
01137     }
01138 
01139     /* Process Locked */
01140     __HAL_LOCK(husart);
01141 
01142     husart->pTxBuffPtr  = pTxData;
01143     husart->TxXferSize  = Size;
01144     husart->TxXferCount = Size;
01145     husart->TxISR       = NULL;
01146 
01147     husart->ErrorCode = HAL_USART_ERROR_NONE;
01148     husart->State     = HAL_USART_STATE_BUSY_TX;
01149 
01150     /* The USART Error Interrupts: (Frame error, noise error, overrun error)
01151     are not managed by the USART Transmit Process to avoid the overrun interrupt
01152     when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
01153     to benefit for the frame error and noise interrupts the usart mode should be
01154     configured only for transmit "USART_MODE_TX" */
01155 
01156 #if defined(USART_CR1_FIFOEN)
01157     /* Configure Tx interrupt processing */
01158     if (husart->FifoMode == USART_FIFOMODE_ENABLE)
01159     {
01160       /* Set the Tx ISR function pointer according to the data word length */
01161       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01162       {
01163         husart->TxISR = USART_TxISR_16BIT_FIFOEN;
01164       }
01165       else
01166       {
01167         husart->TxISR = USART_TxISR_8BIT_FIFOEN;
01168       }
01169 
01170       /* Process Unlocked */
01171       __HAL_UNLOCK(husart);
01172 
01173       /* Enable the TX FIFO threshold interrupt */
01174       __HAL_USART_ENABLE_IT(husart, USART_IT_TXFT);
01175     }
01176     else
01177 #endif /* USART_CR1_FIFOEN */
01178     {
01179       /* Set the Tx ISR function pointer according to the data word length */
01180       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01181       {
01182         husart->TxISR = USART_TxISR_16BIT;
01183       }
01184       else
01185       {
01186         husart->TxISR = USART_TxISR_8BIT;
01187       }
01188 
01189       /* Process Unlocked */
01190       __HAL_UNLOCK(husart);
01191 
01192       /* Enable the USART Transmit Data Register Empty Interrupt */
01193       __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
01194     }
01195 
01196     return HAL_OK;
01197   }
01198   else
01199   {
01200     return HAL_BUSY;
01201   }
01202 }
01203 
01204 /**
01205   * @brief Receive an amount of data in interrupt mode.
01206   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
01207   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01208   *         the received data is handled as a set of u16. In this case, Size must indicate the number
01209   *         of u16 available through pRxData.
01210   * @param  husart USART handle.
01211   * @param  pRxData pointer to data buffer (u8 or u16 data elements).
01212   * @param  Size amount of data elements (u8 or u16) to be received.
01213   * @retval HAL status
01214   */
01215 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
01216 {
01217 #if defined(USART_CR1_FIFOEN)
01218   uint16_t nb_dummy_data;
01219 #endif /* USART_CR1_FIFOEN */
01220 
01221   if (husart->State == HAL_USART_STATE_READY)
01222   {
01223     if ((pRxData == NULL) || (Size == 0U))
01224     {
01225       return HAL_ERROR;
01226     }
01227 
01228     /* Process Locked */
01229     __HAL_LOCK(husart);
01230 
01231     husart->pRxBuffPtr  = pRxData;
01232     husart->RxXferSize  = Size;
01233     husart->RxXferCount = Size;
01234     husart->RxISR       = NULL;
01235 
01236     USART_MASK_COMPUTATION(husart);
01237 
01238     husart->ErrorCode = HAL_USART_ERROR_NONE;
01239     husart->State = HAL_USART_STATE_BUSY_RX;
01240 
01241     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01242     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01243 
01244 #if defined(USART_CR1_FIFOEN)
01245     /* Configure Rx interrupt processing */
01246     if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
01247     {
01248       /* Set the Rx ISR function pointer according to the data word length */
01249       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01250       {
01251         husart->RxISR = USART_RxISR_16BIT_FIFOEN;
01252       }
01253       else
01254       {
01255         husart->RxISR = USART_RxISR_8BIT_FIFOEN;
01256       }
01257 
01258       /* Process Unlocked */
01259       __HAL_UNLOCK(husart);
01260 
01261       /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */
01262       if (husart->Init.Parity != USART_PARITY_NONE)
01263       {
01264        SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01265       }
01266       SET_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
01267     }
01268     else
01269 #endif /* USART_CR1_FIFOEN */
01270     {
01271       /* Set the Rx ISR function pointer according to the data word length */
01272       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01273       {
01274         husart->RxISR = USART_RxISR_16BIT;
01275       }
01276       else
01277       {
01278         husart->RxISR = USART_RxISR_8BIT;
01279       }
01280 
01281       /* Process Unlocked */
01282       __HAL_UNLOCK(husart);
01283 
01284       /* Enable the USART Parity Error and Data Register not empty Interrupts */
01285 #if defined(USART_CR1_FIFOEN)
01286       if (husart->Init.Parity != USART_PARITY_NONE)
01287       {
01288         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
01289       }
01290       else
01291       {
01292         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
01293       }
01294 #else
01295       if (husart->Init.Parity != USART_PARITY_NONE)
01296       {  
01297         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
01298       }
01299       else
01300       {
01301         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
01302       }
01303 #endif /* USART_CR1_FIFOEN */
01304     }
01305 
01306 #if defined(USART_CR2_SLVEN)
01307     if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
01308 #endif /* USART_CR2_SLVEN */
01309     {
01310       /* Send dummy data in order to generate the clock for the Slave to send the next data.
01311          When FIFO mode is disabled only one data must be transferred.
01312          When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold.
01313       */
01314 #if defined(USART_CR1_FIFOEN)
01315       if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
01316       {
01317         for (nb_dummy_data = husart->NbRxDataToProcess ; nb_dummy_data > 0U ; nb_dummy_data--)
01318         {
01319           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
01320         }
01321       }
01322       else
01323 #endif /* USART_CR1_FIFOEN */
01324       {
01325         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
01326       }
01327     }
01328 
01329     return HAL_OK;
01330   }
01331   else
01332   {
01333     return HAL_BUSY;
01334   }
01335 }
01336 
01337 /**
01338   * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
01339   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01340   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
01341   *         of u16 available through pTxData and through pRxData.
01342   * @param  husart USART handle.
01343   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
01344   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
01345   * @param  Size amount of data elements (u8 or u16) to be sent (same amount to be received).
01346   * @retval HAL status
01347   */
01348 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
01349                                                uint16_t Size)
01350 {
01351 
01352   if (husart->State == HAL_USART_STATE_READY)
01353   {
01354     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
01355     {
01356       return HAL_ERROR;
01357     }
01358 
01359     /* Process Locked */
01360     __HAL_LOCK(husart);
01361 
01362     husart->pRxBuffPtr = pRxData;
01363     husart->RxXferSize = Size;
01364     husart->RxXferCount = Size;
01365     husart->pTxBuffPtr = pTxData;
01366     husart->TxXferSize = Size;
01367     husart->TxXferCount = Size;
01368 
01369     /* Computation of USART mask to apply to RDR register */
01370     USART_MASK_COMPUTATION(husart);
01371 
01372     husart->ErrorCode = HAL_USART_ERROR_NONE;
01373     husart->State = HAL_USART_STATE_BUSY_TX_RX;
01374 
01375 #if defined(USART_CR1_FIFOEN)
01376     /* Configure TxRx interrupt processing */
01377     if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
01378     {
01379       /* Set the Rx ISR function pointer according to the data word length */
01380       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01381       {
01382         husart->TxISR = USART_TxISR_16BIT_FIFOEN;
01383         husart->RxISR = USART_RxISR_16BIT_FIFOEN;
01384       }
01385       else
01386       {
01387         husart->TxISR = USART_TxISR_8BIT_FIFOEN;
01388         husart->RxISR = USART_RxISR_8BIT_FIFOEN;
01389       }
01390 
01391       /* Process Locked */
01392       __HAL_UNLOCK(husart);
01393 
01394       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01395       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01396 
01397       if (husart->Init.Parity != USART_PARITY_NONE)
01398       {
01399         /* Enable the USART Parity Error interrupt  */
01400         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01401       }
01402 
01403       /* Enable the TX and  RX FIFO Threshold interrupts */
01404       SET_BIT(husart->Instance->CR3, (USART_CR3_TXFTIE | USART_CR3_RXFTIE));
01405     }
01406     else
01407 #endif /* USART_CR1_FIFOEN */
01408     {
01409       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
01410       {
01411         husart->TxISR = USART_TxISR_16BIT;
01412         husart->RxISR = USART_RxISR_16BIT;
01413       }
01414       else
01415       {
01416         husart->TxISR = USART_TxISR_8BIT;
01417         husart->RxISR = USART_RxISR_8BIT;
01418       }
01419 
01420       /* Process Locked */
01421       __HAL_UNLOCK(husart);
01422 
01423       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01424       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01425 
01426       /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
01427 #if defined(USART_CR1_FIFOEN)
01428       if (husart->Init.Parity != USART_PARITY_NONE)
01429       {
01430         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
01431       }
01432       else
01433       {
01434         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
01435       }
01436 #else
01437       if (husart->Init.Parity != USART_PARITY_NONE)
01438       {  
01439         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
01440       }
01441       else
01442       {
01443         SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
01444       }
01445 #endif /* USART_CR1_FIFOEN */
01446 
01447       /* Enable the USART Transmit Data Register Empty Interrupt */
01448 #if defined(USART_CR1_FIFOEN)
01449       SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
01450 #else
01451       SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
01452 #endif /* USART_CR1_FIFOEN */
01453     }
01454 
01455     return HAL_OK;
01456   }
01457   else
01458   {
01459     return HAL_BUSY;
01460   }
01461 }
01462 
01463 /**
01464   * @brief Send an amount of data in DMA mode.
01465   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01466   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
01467   *         of u16 provided through pTxData.
01468   * @param  husart USART handle.
01469   * @param  pTxData pointer to data buffer (u8 or u16 data elements).
01470   * @param  Size amount of data elements (u8 or u16) to be sent.
01471   * @retval HAL status
01472   */
01473 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint16_t Size)
01474 {
01475   HAL_StatusTypeDef status = HAL_OK;
01476   const uint32_t *tmp;
01477 
01478   if (husart->State == HAL_USART_STATE_READY)
01479   {
01480     if ((pTxData == NULL) || (Size == 0U))
01481     {
01482       return HAL_ERROR;
01483     }
01484 
01485     /* Process Locked */
01486     __HAL_LOCK(husart);
01487 
01488     husart->pTxBuffPtr = pTxData;
01489     husart->TxXferSize = Size;
01490     husart->TxXferCount = Size;
01491 
01492     husart->ErrorCode = HAL_USART_ERROR_NONE;
01493     husart->State = HAL_USART_STATE_BUSY_TX;
01494 
01495     if (husart->hdmatx != NULL)
01496     {
01497       /* Set the USART DMA transfer complete callback */
01498       husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
01499 
01500       /* Set the USART DMA Half transfer complete callback */
01501       husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
01502 
01503       /* Set the DMA error callback */
01504       husart->hdmatx->XferErrorCallback = USART_DMAError;
01505 
01506       /* Enable the USART transmit DMA channel */
01507       tmp = (const uint32_t *)&pTxData;
01508       status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
01509     }
01510 
01511     if (status == HAL_OK)
01512     {
01513       /* Clear the TC flag in the ICR register */
01514       __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
01515 
01516       /* Process Unlocked */
01517       __HAL_UNLOCK(husart);
01518 
01519       /* Enable the DMA transfer for transmit request by setting the DMAT bit
01520          in the USART CR3 register */
01521       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01522 
01523       return HAL_OK;
01524     }
01525     else
01526     {
01527       /* Set error code to DMA */
01528       husart->ErrorCode = HAL_USART_ERROR_DMA;
01529 
01530       /* Process Unlocked */
01531       __HAL_UNLOCK(husart);
01532 
01533       /* Restore husart->State to ready */
01534       husart->State = HAL_USART_STATE_READY;
01535 
01536       return HAL_ERROR;
01537     }
01538   }
01539   else
01540   {
01541     return HAL_BUSY;
01542   }
01543 }
01544 
01545 /**
01546   * @brief Receive an amount of data in DMA mode.
01547   * @note   When the USART parity is enabled (PCE = 1), the received data contain
01548   *         the parity bit (MSB position).
01549   * @note   The USART DMA transmit channel must be configured in order to generate the clock for the slave.
01550   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01551   *         the received data is handled as a set of u16. In this case, Size must indicate the number
01552   *         of u16 available through pRxData.
01553   * @param  husart USART handle.
01554   * @param  pRxData pointer to data buffer (u8 or u16 data elements).
01555   * @param  Size amount of data elements (u8 or u16) to be received.
01556   * @retval HAL status
01557   */
01558 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
01559 {
01560   HAL_StatusTypeDef status = HAL_OK;
01561   uint32_t *tmp = (uint32_t *)&pRxData;
01562 
01563   /* Check that a Rx process is not already ongoing */
01564   if (husart->State == HAL_USART_STATE_READY)
01565   {
01566     if ((pRxData == NULL) || (Size == 0U))
01567     {
01568       return HAL_ERROR;
01569     }
01570 
01571     /* Process Locked */
01572     __HAL_LOCK(husart);
01573 
01574     husart->pRxBuffPtr = pRxData;
01575     husart->RxXferSize = Size;
01576     husart->pTxBuffPtr = pRxData;
01577     husart->TxXferSize = Size;
01578 
01579     husart->ErrorCode = HAL_USART_ERROR_NONE;
01580     husart->State = HAL_USART_STATE_BUSY_RX;
01581 
01582     if (husart->hdmarx != NULL)
01583     {
01584       /* Set the USART DMA Rx transfer complete callback */
01585       husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
01586 
01587       /* Set the USART DMA Half transfer complete callback */
01588       husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
01589 
01590       /* Set the USART DMA Rx transfer error callback */
01591       husart->hdmarx->XferErrorCallback = USART_DMAError;
01592 
01593       /* Enable the USART receive DMA channel */
01594       status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t *)tmp, Size);
01595     }
01596 
01597 #if defined(USART_CR2_SLVEN)
01598     if ((status == HAL_OK) &&
01599         (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
01600 #endif /* USART_CR2_SLVEN */
01601     {
01602       /* Enable the USART transmit DMA channel: the transmit channel is used in order
01603          to generate in the non-blocking mode the clock to the slave device,
01604          this mode isn't a simplex receive mode but a full-duplex receive mode */
01605 
01606       /* Set the USART DMA Tx Complete and Error callback to Null */
01607       if (husart->hdmatx != NULL)
01608       {
01609         husart->hdmatx->XferErrorCallback = NULL;
01610         husart->hdmatx->XferHalfCpltCallback = NULL;
01611         husart->hdmatx->XferCpltCallback = NULL;
01612         status = HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
01613       }
01614     }
01615 
01616     if (status == HAL_OK)
01617     {
01618       /* Process Unlocked */
01619       __HAL_UNLOCK(husart);
01620 
01621       if (husart->Init.Parity != USART_PARITY_NONE)
01622       {
01623         /* Enable the USART Parity Error Interrupt */
01624         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01625       }
01626 
01627       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01628       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01629 
01630       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
01631          in the USART CR3 register */
01632       SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01633 
01634       /* Enable the DMA transfer for transmit request by setting the DMAT bit
01635          in the USART CR3 register */
01636       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01637 
01638       return HAL_OK;
01639     }
01640     else
01641     {
01642       if (husart->hdmarx != NULL)
01643       {
01644         status = HAL_DMA_Abort(husart->hdmarx);
01645       }
01646 
01647       /* No need to check on error code */
01648       UNUSED(status);
01649 
01650       /* Set error code to DMA */
01651       husart->ErrorCode = HAL_USART_ERROR_DMA;
01652 
01653       /* Process Unlocked */
01654       __HAL_UNLOCK(husart);
01655 
01656       /* Restore husart->State to ready */
01657       husart->State = HAL_USART_STATE_READY;
01658 
01659       return HAL_ERROR;
01660     }
01661   }
01662   else
01663   {
01664     return HAL_BUSY;
01665   }
01666 }
01667 
01668 /**
01669   * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
01670   * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
01671   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01672   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
01673   *         of u16 available through pTxData and through pRxData.
01674   * @param  husart USART handle.
01675   * @param  pTxData pointer to TX data buffer (u8 or u16 data elements).
01676   * @param  pRxData pointer to RX data buffer (u8 or u16 data elements).
01677   * @param  Size amount of data elements (u8 or u16) to be received/sent.
01678   * @retval HAL status
01679   */
01680 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, const uint8_t *pTxData, uint8_t *pRxData,
01681                                                 uint16_t Size)
01682 {
01683   HAL_StatusTypeDef status;
01684   const uint32_t *tmp;
01685 
01686   if (husart->State == HAL_USART_STATE_READY)
01687   {
01688     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
01689     {
01690       return HAL_ERROR;
01691     }
01692 
01693     /* Process Locked */
01694     __HAL_LOCK(husart);
01695 
01696     husart->pRxBuffPtr = pRxData;
01697     husart->RxXferSize = Size;
01698     husart->pTxBuffPtr = pTxData;
01699     husart->TxXferSize = Size;
01700 
01701     husart->ErrorCode = HAL_USART_ERROR_NONE;
01702     husart->State = HAL_USART_STATE_BUSY_TX_RX;
01703 
01704     if ((husart->hdmarx != NULL) && (husart->hdmatx != NULL))
01705     {
01706       /* Set the USART DMA Rx transfer complete callback */
01707       husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
01708 
01709       /* Set the USART DMA Half transfer complete callback */
01710       husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
01711 
01712       /* Set the USART DMA Tx transfer complete callback */
01713       husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
01714 
01715       /* Set the USART DMA Half transfer complete callback */
01716       husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
01717 
01718       /* Set the USART DMA Tx transfer error callback */
01719       husart->hdmatx->XferErrorCallback = USART_DMAError;
01720 
01721       /* Set the USART DMA Rx transfer error callback */
01722       husart->hdmarx->XferErrorCallback = USART_DMAError;
01723 
01724       /* Enable the USART receive DMA channel */
01725       tmp = (uint32_t *)&pRxData;
01726       status = HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(const uint32_t *)tmp, Size);
01727 
01728       /* Enable the USART transmit DMA channel */
01729       if (status == HAL_OK)
01730       {
01731         tmp = (const uint32_t *)&pTxData;
01732         status = HAL_DMA_Start_IT(husart->hdmatx, *(const uint32_t *)tmp, (uint32_t)&husart->Instance->TDR, Size);
01733       }
01734     }
01735     else
01736     {
01737       status = HAL_ERROR;
01738     }
01739 
01740     if (status == HAL_OK)
01741     {
01742       /* Process Unlocked */
01743       __HAL_UNLOCK(husart);
01744 
01745       if (husart->Init.Parity != USART_PARITY_NONE)
01746       {
01747         /* Enable the USART Parity Error Interrupt */
01748         SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01749       }
01750 
01751       /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01752       SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01753 
01754       /* Clear the TC flag in the ICR register */
01755       __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
01756 
01757       /* Enable the DMA transfer for the receiver request by setting the DMAR bit
01758          in the USART CR3 register */
01759       SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01760 
01761       /* Enable the DMA transfer for transmit request by setting the DMAT bit
01762          in the USART CR3 register */
01763       SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01764 
01765       return HAL_OK;
01766     }
01767     else
01768     {
01769       if (husart->hdmarx != NULL)
01770       {
01771         status = HAL_DMA_Abort(husart->hdmarx);
01772       }
01773 
01774       /* No need to check on error code */
01775       UNUSED(status);
01776 
01777       /* Set error code to DMA */
01778       husart->ErrorCode = HAL_USART_ERROR_DMA;
01779 
01780       /* Process Unlocked */
01781       __HAL_UNLOCK(husart);
01782 
01783       /* Restore husart->State to ready */
01784       husart->State = HAL_USART_STATE_READY;
01785 
01786       return HAL_ERROR;
01787     }
01788   }
01789   else
01790   {
01791     return HAL_BUSY;
01792   }
01793 }
01794 
01795 /**
01796   * @brief Pause the DMA Transfer.
01797   * @param  husart USART handle.
01798   * @retval HAL status
01799   */
01800 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
01801 {
01802   const HAL_USART_StateTypeDef state = husart->State;
01803 
01804   /* Process Locked */
01805   __HAL_LOCK(husart);
01806 
01807   if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)) &&
01808       (state == HAL_USART_STATE_BUSY_TX))
01809   {
01810     /* Disable the USART DMA Tx request */
01811     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01812   }
01813   else if ((state == HAL_USART_STATE_BUSY_RX) ||
01814            (state == HAL_USART_STATE_BUSY_TX_RX))
01815   {
01816     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
01817     {
01818       /* Disable the USART DMA Tx request */
01819       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01820     }
01821     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01822     {
01823       /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
01824       CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01825       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
01826 
01827       /* Disable the USART DMA Rx request */
01828       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01829     }
01830   }
01831   else
01832   {
01833     /* Nothing to do */
01834   }
01835 
01836   /* Process Unlocked */
01837   __HAL_UNLOCK(husart);
01838 
01839   return HAL_OK;
01840 }
01841 
01842 /**
01843   * @brief Resume the DMA Transfer.
01844   * @param  husart USART handle.
01845   * @retval HAL status
01846   */
01847 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
01848 {
01849   const HAL_USART_StateTypeDef state = husart->State;
01850 
01851   /* Process Locked */
01852   __HAL_LOCK(husart);
01853 
01854   if (state == HAL_USART_STATE_BUSY_TX)
01855   {
01856     /* Enable the USART DMA Tx request */
01857     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01858   }
01859   else if ((state == HAL_USART_STATE_BUSY_RX) ||
01860            (state == HAL_USART_STATE_BUSY_TX_RX))
01861   {
01862     /* Clear the Overrun flag before resuming the Rx transfer*/
01863     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
01864 
01865     /* Re-enable PE and ERR (Frame error, noise error, overrun error) interrupts */
01866     if (husart->Init.Parity != USART_PARITY_NONE)
01867     {    
01868       SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01869     }
01870     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01871 
01872     /* Enable the USART DMA Rx request  before the DMA Tx request */
01873     SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01874 
01875     /* Enable the USART DMA Tx request */
01876     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01877   }
01878   else
01879   {
01880     /* Nothing to do */
01881   }
01882 
01883   /* Process Unlocked */
01884   __HAL_UNLOCK(husart);
01885 
01886   return HAL_OK;
01887 }
01888 
01889 /**
01890   * @brief Stop the DMA Transfer.
01891   * @param  husart USART handle.
01892   * @retval HAL status
01893   */
01894 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
01895 {
01896   /* The Lock is not implemented on this API to allow the user application
01897      to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
01898      HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
01899      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
01900      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
01901      the stream and the corresponding call back is executed. */
01902 
01903   /* Disable the USART Tx/Rx DMA requests */
01904   CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01905   CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01906 
01907   /* Abort the USART DMA tx channel */
01908   if (husart->hdmatx != NULL)
01909   {
01910     if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
01911     {
01912       if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
01913       {
01914         /* Set error code to DMA */
01915         husart->ErrorCode = HAL_USART_ERROR_DMA;
01916 
01917         return HAL_TIMEOUT;
01918       }
01919     }
01920   }
01921   /* Abort the USART DMA rx channel */
01922   if (husart->hdmarx != NULL)
01923   {
01924     if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
01925     {
01926       if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
01927       {
01928         /* Set error code to DMA */
01929         husart->ErrorCode = HAL_USART_ERROR_DMA;
01930 
01931         return HAL_TIMEOUT;
01932       }
01933     }
01934   }
01935 
01936   USART_EndTransfer(husart);
01937   husart->State = HAL_USART_STATE_READY;
01938 
01939   return HAL_OK;
01940 }
01941 
01942 /**
01943   * @brief  Abort ongoing transfers (blocking mode).
01944   * @param  husart USART handle.
01945   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
01946   *         This procedure performs following operations :
01947   *           - Disable USART Interrupts (Tx and Rx)
01948   *           - Disable the DMA transfer in the peripheral register (if enabled)
01949   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
01950   *           - Set handle State to READY
01951   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
01952   * @retval HAL status
01953   */
01954 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
01955 {
01956 #if defined(USART_CR1_FIFOEN)
01957   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
01958   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
01959                                     USART_CR1_TCIE));
01960   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
01961 #else
01962   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
01963   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
01964 #endif /* USART_CR1_FIFOEN */
01965 
01966   /* Abort the USART DMA Tx channel if enabled */
01967   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
01968   {
01969     /* Disable the USART DMA Tx request if enabled */
01970     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01971 
01972     /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
01973     if (husart->hdmatx != NULL)
01974     {
01975       /* Set the USART DMA Abort callback to Null.
01976          No call back execution at end of DMA abort procedure */
01977       husart->hdmatx->XferAbortCallback = NULL;
01978 
01979       if (HAL_DMA_Abort(husart->hdmatx) != HAL_OK)
01980       {
01981         if (HAL_DMA_GetError(husart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
01982         {
01983           /* Set error code to DMA */
01984           husart->ErrorCode = HAL_USART_ERROR_DMA;
01985 
01986           return HAL_TIMEOUT;
01987         }
01988       }
01989     }
01990   }
01991 
01992   /* Abort the USART DMA Rx channel if enabled */
01993   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01994   {
01995     /* Disable the USART DMA Rx request if enabled */
01996     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01997 
01998     /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
01999     if (husart->hdmarx != NULL)
02000     {
02001       /* Set the USART DMA Abort callback to Null.
02002          No call back execution at end of DMA abort procedure */
02003       husart->hdmarx->XferAbortCallback = NULL;
02004 
02005       if (HAL_DMA_Abort(husart->hdmarx) != HAL_OK)
02006       {
02007         if (HAL_DMA_GetError(husart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
02008         {
02009           /* Set error code to DMA */
02010           husart->ErrorCode = HAL_USART_ERROR_DMA;
02011 
02012           return HAL_TIMEOUT;
02013         }
02014       }
02015     }
02016   }
02017 
02018   /* Reset Tx and Rx transfer counters */
02019   husart->TxXferCount = 0U;
02020   husart->RxXferCount = 0U;
02021 
02022   /* Clear the Error flags in the ICR register */
02023   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
02024 
02025 #if defined(USART_CR1_FIFOEN)
02026   /* Flush the whole TX FIFO (if needed) */
02027   if (husart->FifoMode == USART_FIFOMODE_ENABLE)
02028   {
02029     __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
02030   }
02031 #endif /* USART_CR1_FIFOEN */
02032 
02033   /* Discard the received data */
02034   __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
02035 
02036   /* Restore husart->State to Ready */
02037   husart->State  = HAL_USART_STATE_READY;
02038 
02039   /* Reset Handle ErrorCode to No Error */
02040   husart->ErrorCode = HAL_USART_ERROR_NONE;
02041 
02042   return HAL_OK;
02043 }
02044 
02045 /**
02046   * @brief  Abort ongoing transfers (Interrupt mode).
02047   * @param  husart USART handle.
02048   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
02049   *         This procedure performs following operations :
02050   *           - Disable USART Interrupts (Tx and Rx)
02051   *           - Disable the DMA transfer in the peripheral register (if enabled)
02052   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
02053   *           - Set handle State to READY
02054   *           - At abort completion, call user abort complete callback
02055   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
02056   *         considered as completed only when user abort complete callback is executed (not when exiting function).
02057   * @retval HAL status
02058   */
02059 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
02060 {
02061   uint32_t abortcplt = 1U;
02062 
02063 #if defined(USART_CR1_FIFOEN)
02064   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
02065   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
02066                                     USART_CR1_TCIE));
02067   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
02068 #else
02069   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
02070   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02071 #endif /* USART_CR1_FIFOEN */
02072 
02073   /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
02074      before any call to DMA Abort functions */
02075   /* DMA Tx Handle is valid */
02076   if (husart->hdmatx != NULL)
02077   {
02078     /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
02079        Otherwise, set it to NULL */
02080     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
02081     {
02082       husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
02083     }
02084     else
02085     {
02086       husart->hdmatx->XferAbortCallback = NULL;
02087     }
02088   }
02089   /* DMA Rx Handle is valid */
02090   if (husart->hdmarx != NULL)
02091   {
02092     /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
02093        Otherwise, set it to NULL */
02094     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
02095     {
02096       husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
02097     }
02098     else
02099     {
02100       husart->hdmarx->XferAbortCallback = NULL;
02101     }
02102   }
02103 
02104   /* Abort the USART DMA Tx channel if enabled */
02105   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
02106   {
02107     /* Disable DMA Tx at USART level */
02108     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
02109 
02110     /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
02111     if (husart->hdmatx != NULL)
02112     {
02113       /* USART Tx DMA Abort callback has already been initialised :
02114          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
02115 
02116       /* Abort DMA TX */
02117       if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
02118       {
02119         husart->hdmatx->XferAbortCallback = NULL;
02120       }
02121       else
02122       {
02123         abortcplt = 0U;
02124       }
02125     }
02126   }
02127 
02128   /* Abort the USART DMA Rx channel if enabled */
02129   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
02130   {
02131     /* Disable the USART DMA Rx request if enabled */
02132     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
02133 
02134     /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
02135     if (husart->hdmarx != NULL)
02136     {
02137       /* USART Rx DMA Abort callback has already been initialised :
02138          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
02139 
02140       /* Abort DMA RX */
02141       if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
02142       {
02143         husart->hdmarx->XferAbortCallback = NULL;
02144         abortcplt = 1U;
02145       }
02146       else
02147       {
02148         abortcplt = 0U;
02149       }
02150     }
02151   }
02152 
02153   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
02154   if (abortcplt == 1U)
02155   {
02156     /* Reset Tx and Rx transfer counters */
02157     husart->TxXferCount = 0U;
02158     husart->RxXferCount = 0U;
02159 
02160     /* Reset errorCode */
02161     husart->ErrorCode = HAL_USART_ERROR_NONE;
02162 
02163     /* Clear the Error flags in the ICR register */
02164     __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
02165 
02166 #if defined(USART_CR1_FIFOEN)
02167     /* Flush the whole TX FIFO (if needed) */
02168     if (husart->FifoMode == USART_FIFOMODE_ENABLE)
02169     {
02170       __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
02171     }
02172 #endif /* USART_CR1_FIFOEN */
02173 
02174     /* Discard the received data */
02175     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
02176 
02177     /* Restore husart->State to Ready */
02178     husart->State  = HAL_USART_STATE_READY;
02179 
02180     /* As no DMA to be aborted, call directly user Abort complete callback */
02181 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02182     /* Call registered Abort Complete Callback */
02183     husart->AbortCpltCallback(husart);
02184 #else
02185     /* Call legacy weak Abort Complete Callback */
02186     HAL_USART_AbortCpltCallback(husart);
02187 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02188   }
02189 
02190   return HAL_OK;
02191 }
02192 
02193 /**
02194   * @brief  Handle USART interrupt request.
02195   * @param  husart USART handle.
02196   * @retval None
02197   */
02198 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
02199 {
02200   uint32_t isrflags   = READ_REG(husart->Instance->ISR);
02201   uint32_t cr1its     = READ_REG(husart->Instance->CR1);
02202   uint32_t cr3its     = READ_REG(husart->Instance->CR3);
02203 
02204   uint32_t errorflags;
02205   uint32_t errorcode;
02206 
02207   /* If no error occurs */
02208 #if defined(USART_CR2_SLVEN)
02209   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF |
02210                                       USART_ISR_UDR));
02211 #else
02212   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
02213 #endif /* USART_CR2_SLVEN */
02214   if (errorflags == 0U)
02215   {
02216     /* USART in mode Receiver ---------------------------------------------------*/
02217 #if defined(USART_CR1_FIFOEN)
02218     if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
02219         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
02220             || ((cr3its & USART_CR3_RXFTIE) != 0U)))
02221 #else
02222     if (((isrflags & USART_ISR_RXNE) != 0U)
02223         && ((cr1its & USART_CR1_RXNEIE) != 0U))
02224 #endif /* USART_CR1_FIFOEN */
02225     {
02226       if (husart->RxISR != NULL)
02227       {
02228         husart->RxISR(husart);
02229       }
02230       return;
02231     }
02232   }
02233 
02234   /* If some errors occur */
02235 #if defined(USART_CR1_FIFOEN)
02236   if ((errorflags != 0U)
02237       && (((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)
02238           || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != 0U)))
02239 #else
02240   if ((errorflags != 0U)
02241       && (((cr3its & USART_CR3_EIE) != 0U)
02242           || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
02243 #endif /* USART_CR1_FIFOEN */
02244   {
02245     /* USART parity error interrupt occurred -------------------------------------*/
02246     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
02247     {
02248       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
02249 
02250       husart->ErrorCode |= HAL_USART_ERROR_PE;
02251     }
02252 
02253     /* USART frame error interrupt occurred --------------------------------------*/
02254     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
02255     {
02256       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
02257 
02258       husart->ErrorCode |= HAL_USART_ERROR_FE;
02259     }
02260 
02261     /* USART noise error interrupt occurred --------------------------------------*/
02262     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
02263     {
02264       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
02265 
02266       husart->ErrorCode |= HAL_USART_ERROR_NE;
02267     }
02268 
02269     /* USART Over-Run interrupt occurred -----------------------------------------*/
02270 #if defined(USART_CR1_FIFOEN)
02271     if (((isrflags & USART_ISR_ORE) != 0U)
02272         && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U) ||
02273             ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != 0U)))
02274 #else
02275     if (((isrflags & USART_ISR_ORE) != 0U)
02276         && (((cr1its & USART_CR1_RXNEIE) != 0U) ||
02277             ((cr3its & USART_CR3_EIE) != 0U)))
02278 #endif /* USART_CR1_FIFOEN */
02279     {
02280       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
02281 
02282       husart->ErrorCode |= HAL_USART_ERROR_ORE;
02283     }
02284 
02285     /* USART Receiver Timeout interrupt occurred ---------------------------------*/
02286     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
02287     {
02288       __HAL_USART_CLEAR_IT(husart, USART_CLEAR_RTOF);
02289 
02290       husart->ErrorCode |= HAL_USART_ERROR_RTO;
02291     }
02292 
02293 #if defined(USART_CR2_SLVEN)
02294     /* USART SPI slave underrun error interrupt occurred -------------------------*/
02295     if (((isrflags & USART_ISR_UDR) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
02296     {
02297       /* Ignore SPI slave underrun errors when reception is going on */
02298       if (husart->State == HAL_USART_STATE_BUSY_RX)
02299       {
02300         __HAL_USART_CLEAR_UDRFLAG(husart);
02301         return;
02302       }
02303       else
02304       {
02305         __HAL_USART_CLEAR_UDRFLAG(husart);
02306         husart->ErrorCode |= HAL_USART_ERROR_UDR;
02307       }
02308     }
02309 #endif /* USART_CR2_SLVEN */
02310 
02311     /* Call USART Error Call back function if need be --------------------------*/
02312     if (husart->ErrorCode != HAL_USART_ERROR_NONE)
02313     {
02314       /* USART in mode Receiver ---------------------------------------------------*/
02315 #if defined(USART_CR1_FIFOEN)
02316       if (((isrflags & USART_ISR_RXNE_RXFNE) != 0U)
02317           && (((cr1its & USART_CR1_RXNEIE_RXFNEIE) != 0U)
02318               || ((cr3its & USART_CR3_RXFTIE) != 0U)))
02319 #else
02320       if (((isrflags & USART_ISR_RXNE) != 0U)
02321           && ((cr1its & USART_CR1_RXNEIE) != 0U))
02322 #endif /* USART_CR1_FIFOEN */
02323       {
02324         if (husart->RxISR != NULL)
02325         {
02326           husart->RxISR(husart);
02327         }
02328       }
02329 
02330       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
02331          consider error as blocking */
02332       errorcode = husart->ErrorCode & HAL_USART_ERROR_ORE;
02333       if ((HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)) ||
02334           (errorcode != 0U))
02335       {
02336         /* Blocking error : transfer is aborted
02337            Set the USART state ready to be able to start again the process,
02338            Disable Interrupts, and disable DMA requests, if ongoing */
02339         USART_EndTransfer(husart);
02340 
02341         /* Abort the USART DMA Rx channel if enabled */
02342         if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
02343         {
02344           /* Disable the USART DMA Rx request if enabled */
02345           CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR);
02346 
02347           /* Abort the USART DMA Tx channel */
02348           if (husart->hdmatx != NULL)
02349           {
02350             /* Set the USART Tx DMA Abort callback to NULL : no callback
02351                executed at end of DMA abort procedure */
02352             husart->hdmatx->XferAbortCallback = NULL;
02353 
02354             /* Abort DMA TX */
02355             (void)HAL_DMA_Abort_IT(husart->hdmatx);
02356           }
02357 
02358           /* Abort the USART DMA Rx channel */
02359           if (husart->hdmarx != NULL)
02360           {
02361             /* Set the USART Rx DMA Abort callback :
02362                will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
02363             husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
02364 
02365             /* Abort DMA RX */
02366             if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
02367             {
02368               /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
02369               husart->hdmarx->XferAbortCallback(husart->hdmarx);
02370             }
02371           }
02372           else
02373           {
02374             /* Call user error callback */
02375 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02376             /* Call registered Error Callback */
02377             husart->ErrorCallback(husart);
02378 #else
02379             /* Call legacy weak Error Callback */
02380             HAL_USART_ErrorCallback(husart);
02381 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02382           }
02383         }
02384         else
02385         {
02386           /* Call user error callback */
02387 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02388           /* Call registered Error Callback */
02389           husart->ErrorCallback(husart);
02390 #else
02391           /* Call legacy weak Error Callback */
02392           HAL_USART_ErrorCallback(husart);
02393 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02394         }
02395       }
02396       else
02397       {
02398         /* Non Blocking error : transfer could go on.
02399            Error is notified to user through user error callback */
02400 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02401         /* Call registered Error Callback */
02402         husart->ErrorCallback(husart);
02403 #else
02404         /* Call legacy weak Error Callback */
02405         HAL_USART_ErrorCallback(husart);
02406 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02407         husart->ErrorCode = HAL_USART_ERROR_NONE;
02408       }
02409     }
02410     return;
02411 
02412   } /* End if some error occurs */
02413 
02414 
02415   /* USART in mode Transmitter ------------------------------------------------*/
02416 #if defined(USART_CR1_FIFOEN)
02417   if (((isrflags & USART_ISR_TXE_TXFNF) != 0U)
02418       && (((cr1its & USART_CR1_TXEIE_TXFNFIE) != 0U)
02419           || ((cr3its & USART_CR3_TXFTIE) != 0U)))
02420 #else
02421   if (((isrflags & USART_ISR_TXE) != 0U)
02422       && ((cr1its & USART_CR1_TXEIE) != 0U))
02423 #endif /* USART_CR1_FIFOEN */
02424   {
02425     if (husart->TxISR != NULL)
02426     {
02427       husart->TxISR(husart);
02428     }
02429     return;
02430   }
02431 
02432   /* USART in mode Transmitter (transmission end) -----------------------------*/
02433   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
02434   {
02435     USART_EndTransmit_IT(husart);
02436     return;
02437   }
02438 
02439 #if defined(USART_CR1_FIFOEN)
02440   /* USART TX Fifo Empty occurred ----------------------------------------------*/
02441   if (((isrflags & USART_ISR_TXFE) != 0U) && ((cr1its & USART_CR1_TXFEIE) != 0U))
02442   {
02443 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02444     /* Call registered Tx Fifo Empty Callback */
02445     husart->TxFifoEmptyCallback(husart);
02446 #else
02447     /* Call legacy weak Tx Fifo Empty Callback */
02448     HAL_USARTEx_TxFifoEmptyCallback(husart);
02449 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02450     return;
02451   }
02452 
02453   /* USART RX Fifo Full occurred ----------------------------------------------*/
02454   if (((isrflags & USART_ISR_RXFF) != 0U) && ((cr1its & USART_CR1_RXFFIE) != 0U))
02455   {
02456 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02457     /* Call registered Rx Fifo Full Callback */
02458     husart->RxFifoFullCallback(husart);
02459 #else
02460     /* Call legacy weak Rx Fifo Full Callback */
02461     HAL_USARTEx_RxFifoFullCallback(husart);
02462 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02463     return;
02464   }
02465 #endif /* USART_CR1_FIFOEN */
02466 }
02467 
02468 /**
02469   * @brief Tx Transfer completed callback.
02470   * @param husart USART handle.
02471   * @retval None
02472   */
02473 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
02474 {
02475   /* Prevent unused argument(s) compilation warning */
02476   UNUSED(husart);
02477 
02478   /* NOTE : This function should not be modified, when the callback is needed,
02479             the HAL_USART_TxCpltCallback can be implemented in the user file.
02480    */
02481 }
02482 
02483 /**
02484   * @brief  Tx Half Transfer completed callback.
02485   * @param husart USART handle.
02486   * @retval None
02487   */
02488 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
02489 {
02490   /* Prevent unused argument(s) compilation warning */
02491   UNUSED(husart);
02492 
02493   /* NOTE: This function should not be modified, when the callback is needed,
02494            the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
02495    */
02496 }
02497 
02498 /**
02499   * @brief  Rx Transfer completed callback.
02500   * @param husart USART handle.
02501   * @retval None
02502   */
02503 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
02504 {
02505   /* Prevent unused argument(s) compilation warning */
02506   UNUSED(husart);
02507 
02508   /* NOTE: This function should not be modified, when the callback is needed,
02509            the HAL_USART_RxCpltCallback can be implemented in the user file.
02510    */
02511 }
02512 
02513 /**
02514   * @brief Rx Half Transfer completed callback.
02515   * @param husart USART handle.
02516   * @retval None
02517   */
02518 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
02519 {
02520   /* Prevent unused argument(s) compilation warning */
02521   UNUSED(husart);
02522 
02523   /* NOTE : This function should not be modified, when the callback is needed,
02524             the HAL_USART_RxHalfCpltCallback can be implemented in the user file
02525    */
02526 }
02527 
02528 /**
02529   * @brief Tx/Rx Transfers completed callback for the non-blocking process.
02530   * @param husart USART handle.
02531   * @retval None
02532   */
02533 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
02534 {
02535   /* Prevent unused argument(s) compilation warning */
02536   UNUSED(husart);
02537 
02538   /* NOTE : This function should not be modified, when the callback is needed,
02539             the HAL_USART_TxRxCpltCallback can be implemented in the user file
02540    */
02541 }
02542 
02543 /**
02544   * @brief USART error callback.
02545   * @param husart USART handle.
02546   * @retval None
02547   */
02548 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
02549 {
02550   /* Prevent unused argument(s) compilation warning */
02551   UNUSED(husart);
02552 
02553   /* NOTE : This function should not be modified, when the callback is needed,
02554             the HAL_USART_ErrorCallback can be implemented in the user file.
02555    */
02556 }
02557 
02558 /**
02559   * @brief  USART Abort Complete callback.
02560   * @param  husart USART handle.
02561   * @retval None
02562   */
02563 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
02564 {
02565   /* Prevent unused argument(s) compilation warning */
02566   UNUSED(husart);
02567 
02568   /* NOTE : This function should not be modified, when the callback is needed,
02569             the HAL_USART_AbortCpltCallback can be implemented in the user file.
02570    */
02571 }
02572 
02573 /**
02574   * @}
02575   */
02576 
02577 /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
02578   *  @brief   USART Peripheral State and Error functions
02579   *
02580 @verbatim
02581   ==============================================================================
02582             ##### Peripheral State and Error functions #####
02583   ==============================================================================
02584     [..]
02585     This subsection provides functions allowing to :
02586       (+) Return the USART handle state
02587       (+) Return the USART handle error code
02588 
02589 @endverbatim
02590   * @{
02591   */
02592 
02593 
02594 /**
02595   * @brief Return the USART handle state.
02596   * @param husart pointer to a USART_HandleTypeDef structure that contains
02597   *              the configuration information for the specified USART.
02598   * @retval USART handle state
02599   */
02600 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
02601 {
02602   return husart->State;
02603 }
02604 
02605 /**
02606   * @brief Return the USART error code.
02607   * @param husart pointer to a USART_HandleTypeDef structure that contains
02608   *              the configuration information for the specified USART.
02609   * @retval USART handle Error Code
02610   */
02611 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
02612 {
02613   return husart->ErrorCode;
02614 }
02615 
02616 /**
02617   * @}
02618   */
02619 
02620 /**
02621   * @}
02622   */
02623 
02624 /** @defgroup USART_Private_Functions USART Private Functions
02625   * @{
02626   */
02627 
02628 /**
02629   * @brief  Initialize the callbacks to their default values.
02630   * @param  husart USART handle.
02631   * @retval none
02632   */
02633 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02634 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
02635 {
02636   /* Init the USART Callback settings */
02637   husart->TxHalfCpltCallback        = HAL_USART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
02638   husart->TxCpltCallback            = HAL_USART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
02639   husart->RxHalfCpltCallback        = HAL_USART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
02640   husart->RxCpltCallback            = HAL_USART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
02641   husart->TxRxCpltCallback          = HAL_USART_TxRxCpltCallback;          /* Legacy weak TxRxCpltCallback          */
02642   husart->ErrorCallback             = HAL_USART_ErrorCallback;             /* Legacy weak ErrorCallback             */
02643   husart->AbortCpltCallback         = HAL_USART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
02644 #if defined(USART_CR1_FIFOEN)
02645   husart->RxFifoFullCallback        = HAL_USARTEx_RxFifoFullCallback;      /* Legacy weak RxFifoFullCallback        */
02646   husart->TxFifoEmptyCallback       = HAL_USARTEx_TxFifoEmptyCallback;     /* Legacy weak TxFifoEmptyCallback       */
02647 #endif /* USART_CR1_FIFOEN */
02648 }
02649 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02650 
02651 /**
02652   * @brief  End ongoing transfer on USART peripheral (following error detection or Transfer completion).
02653   * @param  husart USART handle.
02654   * @retval None
02655   */
02656 static void USART_EndTransfer(USART_HandleTypeDef *husart)
02657 {
02658 #if defined(USART_CR1_FIFOEN)
02659   /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
02660   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE |
02661                                     USART_CR1_TCIE));
02662   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
02663 #else
02664   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
02665   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
02666   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02667 #endif /* USART_CR1_FIFOEN */
02668 
02669   /* At end of process, restore husart->State to Ready */
02670   husart->State = HAL_USART_STATE_READY;
02671 }
02672 
02673 /**
02674   * @brief DMA USART transmit process complete callback.
02675   * @param  hdma DMA handle.
02676   * @retval None
02677   */
02678 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
02679 {
02680   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02681 
02682   /* DMA Normal mode */
02683   if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
02684   {
02685     husart->TxXferCount = 0U;
02686 
02687     if (husart->State == HAL_USART_STATE_BUSY_TX)
02688     {
02689       /* Disable the DMA transfer for transmit request by resetting the DMAT bit
02690          in the USART CR3 register */
02691       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
02692 
02693       /* Enable the USART Transmit Complete Interrupt */
02694       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
02695     }
02696   }
02697   /* DMA Circular mode */
02698   else
02699   {
02700     if (husart->State == HAL_USART_STATE_BUSY_TX)
02701     {
02702 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02703       /* Call registered Tx Complete Callback */
02704       husart->TxCpltCallback(husart);
02705 #else
02706       /* Call legacy weak Tx Complete Callback */
02707       HAL_USART_TxCpltCallback(husart);
02708 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02709     }
02710   }
02711 }
02712 
02713 /**
02714   * @brief DMA USART transmit process half complete callback.
02715   * @param  hdma DMA handle.
02716   * @retval None
02717   */
02718 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
02719 {
02720   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02721 
02722 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02723   /* Call registered Tx Half Complete Callback */
02724   husart->TxHalfCpltCallback(husart);
02725 #else
02726   /* Call legacy weak Tx Half Complete Callback */
02727   HAL_USART_TxHalfCpltCallback(husart);
02728 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02729 }
02730 
02731 /**
02732   * @brief DMA USART receive process complete callback.
02733   * @param  hdma DMA handle.
02734   * @retval None
02735   */
02736 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
02737 {
02738   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02739 
02740   /* DMA Normal mode */
02741   if (HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC))
02742   {
02743     husart->RxXferCount = 0U;
02744 
02745     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
02746     CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
02747     CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02748 
02749     /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
02750        in USART CR3 register */
02751     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
02752     /* similarly, disable the DMA TX transfer that was started to provide the
02753        clock to the slave device */
02754     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
02755 
02756     if (husart->State == HAL_USART_STATE_BUSY_RX)
02757     {
02758 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02759       /* Call registered Rx Complete Callback */
02760       husart->RxCpltCallback(husart);
02761 #else
02762       /* Call legacy weak Rx Complete Callback */
02763       HAL_USART_RxCpltCallback(husart);
02764 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02765     }
02766     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
02767     else
02768     {
02769 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02770       /* Call registered Tx Rx Complete Callback */
02771       husart->TxRxCpltCallback(husart);
02772 #else
02773       /* Call legacy weak Tx Rx Complete Callback */
02774       HAL_USART_TxRxCpltCallback(husart);
02775 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02776     }
02777     husart->State = HAL_USART_STATE_READY;
02778   }
02779   /* DMA circular mode */
02780   else
02781   {
02782     if (husart->State == HAL_USART_STATE_BUSY_RX)
02783     {
02784 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02785       /* Call registered Rx Complete Callback */
02786       husart->RxCpltCallback(husart);
02787 #else
02788       /* Call legacy weak Rx Complete Callback */
02789       HAL_USART_RxCpltCallback(husart);
02790 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02791     }
02792     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
02793     else
02794     {
02795 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02796       /* Call registered Tx Rx Complete Callback */
02797       husart->TxRxCpltCallback(husart);
02798 #else
02799       /* Call legacy weak Tx Rx Complete Callback */
02800       HAL_USART_TxRxCpltCallback(husart);
02801 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02802     }
02803   }
02804 }
02805 
02806 /**
02807   * @brief DMA USART receive process half complete callback.
02808   * @param  hdma DMA handle.
02809   * @retval None
02810   */
02811 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
02812 {
02813   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02814 
02815 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02816   /* Call registered Rx Half Complete Callback */
02817   husart->RxHalfCpltCallback(husart);
02818 #else
02819   /* Call legacy weak Rx Half Complete Callback */
02820   HAL_USART_RxHalfCpltCallback(husart);
02821 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02822 }
02823 
02824 /**
02825   * @brief DMA USART communication error callback.
02826   * @param  hdma DMA handle.
02827   * @retval None
02828   */
02829 static void USART_DMAError(DMA_HandleTypeDef *hdma)
02830 {
02831   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02832 
02833   husart->RxXferCount = 0U;
02834   husart->TxXferCount = 0U;
02835   USART_EndTransfer(husart);
02836 
02837   husart->ErrorCode |= HAL_USART_ERROR_DMA;
02838   husart->State = HAL_USART_STATE_READY;
02839 
02840 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02841   /* Call registered Error Callback */
02842   husart->ErrorCallback(husart);
02843 #else
02844   /* Call legacy weak Error Callback */
02845   HAL_USART_ErrorCallback(husart);
02846 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02847 }
02848 
02849 /**
02850   * @brief  DMA USART communication abort callback, when initiated by HAL services on Error
02851   *         (To be called at end of DMA Abort procedure following error occurrence).
02852   * @param  hdma DMA handle.
02853   * @retval None
02854   */
02855 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
02856 {
02857   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02858   husart->RxXferCount = 0U;
02859   husart->TxXferCount = 0U;
02860 
02861 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02862   /* Call registered Error Callback */
02863   husart->ErrorCallback(husart);
02864 #else
02865   /* Call legacy weak Error Callback */
02866   HAL_USART_ErrorCallback(husart);
02867 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02868 }
02869 
02870 /**
02871   * @brief  DMA USART Tx communication abort callback, when initiated by user
02872   *         (To be called at end of DMA Tx Abort procedure following user abort request).
02873   * @note   When this callback is executed, User Abort complete call back is called only if no
02874   *         Abort still ongoing for Rx DMA Handle.
02875   * @param  hdma DMA handle.
02876   * @retval None
02877   */
02878 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
02879 {
02880   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02881 
02882   husart->hdmatx->XferAbortCallback = NULL;
02883 
02884   /* Check if an Abort process is still ongoing */
02885   if (husart->hdmarx != NULL)
02886   {
02887     if (husart->hdmarx->XferAbortCallback != NULL)
02888     {
02889       return;
02890     }
02891   }
02892 
02893   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02894   husart->TxXferCount = 0U;
02895   husart->RxXferCount = 0U;
02896 
02897   /* Reset errorCode */
02898   husart->ErrorCode = HAL_USART_ERROR_NONE;
02899 
02900   /* Clear the Error flags in the ICR register */
02901   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
02902 
02903   /* Restore husart->State to Ready */
02904   husart->State = HAL_USART_STATE_READY;
02905 
02906   /* Call user Abort complete callback */
02907 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02908   /* Call registered Abort Complete Callback */
02909   husart->AbortCpltCallback(husart);
02910 #else
02911   /* Call legacy weak Abort Complete Callback */
02912   HAL_USART_AbortCpltCallback(husart);
02913 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02914 
02915 }
02916 
02917 
02918 /**
02919   * @brief  DMA USART Rx communication abort callback, when initiated by user
02920   *         (To be called at end of DMA Rx Abort procedure following user abort request).
02921   * @note   When this callback is executed, User Abort complete call back is called only if no
02922   *         Abort still ongoing for Tx DMA Handle.
02923   * @param  hdma DMA handle.
02924   * @retval None
02925   */
02926 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
02927 {
02928   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)(hdma->Parent);
02929 
02930   husart->hdmarx->XferAbortCallback = NULL;
02931 
02932   /* Check if an Abort process is still ongoing */
02933   if (husart->hdmatx != NULL)
02934   {
02935     if (husart->hdmatx->XferAbortCallback != NULL)
02936     {
02937       return;
02938     }
02939   }
02940 
02941   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02942   husart->TxXferCount = 0U;
02943   husart->RxXferCount = 0U;
02944 
02945   /* Reset errorCode */
02946   husart->ErrorCode = HAL_USART_ERROR_NONE;
02947 
02948   /* Clear the Error flags in the ICR register */
02949   __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
02950 
02951   /* Restore husart->State to Ready */
02952   husart->State  = HAL_USART_STATE_READY;
02953 
02954   /* Call user Abort complete callback */
02955 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02956   /* Call registered Abort Complete Callback */
02957   husart->AbortCpltCallback(husart);
02958 #else
02959   /* Call legacy weak Abort Complete Callback */
02960   HAL_USART_AbortCpltCallback(husart);
02961 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02962 }
02963 
02964 
02965 /**
02966   * @brief  Handle USART Communication Timeout. It waits
02967   *         until a flag is no longer in the specified status.
02968   * @param  husart USART handle.
02969   * @param  Flag Specifies the USART flag to check.
02970   * @param  Status the actual Flag status (SET or RESET).
02971   * @param  Tickstart Tick start value
02972   * @param  Timeout timeout duration.
02973   * @retval HAL status
02974   */
02975 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status,
02976                                                       uint32_t Tickstart, uint32_t Timeout)
02977 {
02978   /* Wait until flag is set */
02979   while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
02980   {
02981     /* Check for the Timeout */
02982     if (Timeout != HAL_MAX_DELAY)
02983     {
02984       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
02985       {
02986         husart->State = HAL_USART_STATE_READY;
02987 
02988         /* Process Unlocked */
02989         __HAL_UNLOCK(husart);
02990 
02991         return HAL_TIMEOUT;
02992       }
02993     }
02994   }
02995   return HAL_OK;
02996 }
02997 
02998 /**
02999   * @brief Configure the USART peripheral.
03000   * @param husart USART handle.
03001   * @retval HAL status
03002   */
03003 static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
03004 {
03005   uint32_t tmpreg;
03006   USART_ClockSourceTypeDef clocksource;
03007   HAL_StatusTypeDef ret                = HAL_OK;
03008   uint16_t brrtemp;
03009   uint32_t usartdiv                    = 0x00000000;
03010   uint32_t pclk;
03011 
03012   /* Check the parameters */
03013   assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
03014   assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
03015   assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
03016   assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
03017   assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
03018   assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
03019   assert_param(IS_USART_PARITY(husart->Init.Parity));
03020   assert_param(IS_USART_MODE(husart->Init.Mode));
03021 #if defined(USART_PRESC_PRESCALER)
03022   assert_param(IS_USART_PRESCALER(husart->Init.ClockPrescaler));
03023 #endif /* USART_PRESC_PRESCALER */
03024 
03025   /*-------------------------- USART CR1 Configuration -----------------------*/
03026   /* Clear M, PCE, PS, TE and RE bits and configure
03027   *  the USART Word Length, Parity and Mode:
03028   *  set the M bits according to husart->Init.WordLength value
03029   *  set PCE and PS bits according to husart->Init.Parity value
03030   *  set TE and RE bits according to husart->Init.Mode value
03031   *  force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
03032   tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
03033   MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
03034 
03035   /*---------------------------- USART CR2 Configuration ---------------------*/
03036   /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits:
03037    * set CPOL bit according to husart->Init.CLKPolarity value
03038    * set CPHA bit according to husart->Init.CLKPhase value
03039    * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
03040    * set STOP[13:12] bits according to husart->Init.StopBits value */
03041   tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
03042   tmpreg |= (uint32_t)husart->Init.CLKLastBit;
03043   tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
03044   tmpreg |= (uint32_t)husart->Init.StopBits;
03045   MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
03046 
03047 #if defined(USART_PRESC_PRESCALER)
03048   /*-------------------------- USART PRESC Configuration -----------------------*/
03049   /* Configure
03050    * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */
03051   MODIFY_REG(husart->Instance->PRESC, USART_PRESC_PRESCALER, husart->Init.ClockPrescaler);
03052 #endif /* USART_PRESC_PRESCALER */
03053 
03054   /*-------------------------- USART BRR Configuration -----------------------*/
03055   /* BRR is filled-up according to OVER8 bit setting which is forced to 1     */
03056   USART_GETCLOCKSOURCE(husart, clocksource);
03057 
03058   switch (clocksource)
03059   {
03060     case USART_CLOCKSOURCE_PCLK1:
03061       pclk = HAL_RCC_GetPCLK1Freq();
03062 #if defined(USART_PRESC_PRESCALER)
03063       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
03064 #else
03065       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate));
03066 #endif /* USART_PRESC_PRESCALER */
03067       break;
03068     case USART_CLOCKSOURCE_PCLK2:
03069       pclk = HAL_RCC_GetPCLK2Freq();
03070 #if defined(USART_PRESC_PRESCALER)
03071       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
03072 #else
03073       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate));
03074 #endif /* USART_PRESC_PRESCALER */
03075       break;
03076     case USART_CLOCKSOURCE_HSI:
03077 #if defined(USART_PRESC_PRESCALER)
03078       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
03079 #else
03080       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate));
03081 #endif /* USART_PRESC_PRESCALER */
03082       break;
03083     case USART_CLOCKSOURCE_SYSCLK:
03084       pclk = HAL_RCC_GetSysClockFreq();
03085 #if defined(USART_PRESC_PRESCALER)
03086       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate, husart->Init.ClockPrescaler));
03087 #else
03088       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(pclk, husart->Init.BaudRate));
03089 #endif /* USART_PRESC_PRESCALER */
03090       break;
03091     case USART_CLOCKSOURCE_LSE:
03092 #if defined(USART_PRESC_PRESCALER)
03093       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
03094 #else
03095       usartdiv = (uint32_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate));
03096 #endif /* USART_PRESC_PRESCALER */
03097       break;
03098     default:
03099       ret = HAL_ERROR;
03100       break;
03101   }
03102 
03103   /* USARTDIV must be greater than or equal to 0d16 and smaller than or equal to ffff */
03104   if ((usartdiv >= USART_BRR_MIN) && (usartdiv <= USART_BRR_MAX))
03105   {
03106     brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
03107     brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
03108     husart->Instance->BRR = brrtemp;
03109   }
03110   else
03111   {
03112     ret = HAL_ERROR;
03113   }
03114 
03115 #if defined(USART_CR1_FIFOEN)
03116   /* Initialize the number of data to process during RX/TX ISR execution */
03117   husart->NbTxDataToProcess = 1U;
03118   husart->NbRxDataToProcess = 1U;
03119 #endif /* USART_CR1_FIFOEN */
03120 
03121   /* Clear ISR function pointers */
03122   husart->RxISR   = NULL;
03123   husart->TxISR   = NULL;
03124 
03125   return ret;
03126 }
03127 
03128 /**
03129   * @brief Check the USART Idle State.
03130   * @param husart USART handle.
03131   * @retval HAL status
03132   */
03133 static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
03134 {
03135   uint32_t tickstart;
03136 
03137   /* Initialize the USART ErrorCode */
03138   husart->ErrorCode = HAL_USART_ERROR_NONE;
03139 
03140   /* Init tickstart for timeout management */
03141   tickstart = HAL_GetTick();
03142 
03143   /* Check if the Transmitter is enabled */
03144   if ((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
03145   {
03146     /* Wait until TEACK flag is set */
03147     if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
03148     {
03149       /* Timeout occurred */
03150       return HAL_TIMEOUT;
03151     }
03152   }
03153   /* Check if the Receiver is enabled */
03154   if ((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
03155   {
03156     /* Wait until REACK flag is set */
03157     if (USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
03158     {
03159       /* Timeout occurred */
03160       return HAL_TIMEOUT;
03161     }
03162   }
03163 
03164   /* Initialize the USART state*/
03165   husart->State = HAL_USART_STATE_READY;
03166 
03167   /* Process Unlocked */
03168   __HAL_UNLOCK(husart);
03169 
03170   return HAL_OK;
03171 }
03172 
03173 /**
03174   * @brief  Simplex send an amount of data in non-blocking mode.
03175   * @note   Function called under interruption only, once
03176   *         interruptions have been enabled by HAL_USART_Transmit_IT().
03177   * @note   The USART errors are not managed to avoid the overrun error.
03178   * @note   ISR function executed when FIFO mode is disabled and when the
03179   *         data word length is less than 9 bits long.
03180   * @param  husart USART handle.
03181   * @retval None
03182   */
03183 static void USART_TxISR_8BIT(USART_HandleTypeDef *husart)
03184 {
03185   const HAL_USART_StateTypeDef state = husart->State;
03186 
03187   /* Check that a Tx process is ongoing */
03188   if ((state == HAL_USART_STATE_BUSY_TX) ||
03189       (state == HAL_USART_STATE_BUSY_TX_RX))
03190   {
03191     if (husart->TxXferCount == 0U)
03192     {
03193       /* Disable the USART Transmit data register empty interrupt */
03194       __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
03195 
03196       /* Enable the USART Transmit Complete Interrupt */
03197       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
03198     }
03199     else
03200     {
03201       husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
03202       husart->pTxBuffPtr++;
03203       husart->TxXferCount--;
03204     }
03205   }
03206 }
03207 
03208 /**
03209   * @brief  Simplex send an amount of data in non-blocking mode.
03210   * @note   Function called under interruption only, once
03211   *         interruptions have been enabled by HAL_USART_Transmit_IT().
03212   * @note   The USART errors are not managed to avoid the overrun error.
03213   * @note   ISR function executed when FIFO mode is disabled and when the
03214   *         data word length is 9 bits long.
03215   * @param  husart USART handle.
03216   * @retval None
03217   */
03218 static void USART_TxISR_16BIT(USART_HandleTypeDef *husart)
03219 {
03220   const HAL_USART_StateTypeDef state = husart->State;
03221   const uint16_t *tmp;
03222 
03223   if ((state == HAL_USART_STATE_BUSY_TX) ||
03224       (state == HAL_USART_STATE_BUSY_TX_RX))
03225   {
03226     if (husart->TxXferCount == 0U)
03227     {
03228       /* Disable the USART Transmit data register empty interrupt */
03229       __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
03230 
03231       /* Enable the USART Transmit Complete Interrupt */
03232       __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
03233     }
03234     else
03235     {
03236       tmp = (const uint16_t *) husart->pTxBuffPtr;
03237       husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
03238       husart->pTxBuffPtr += 2U;
03239       husart->TxXferCount--;
03240     }
03241   }
03242 }
03243 
03244 #if defined(USART_CR1_FIFOEN)
03245 /**
03246   * @brief  Simplex send an amount of data in non-blocking mode.
03247   * @note   Function called under interruption only, once
03248   *         interruptions have been enabled by HAL_USART_Transmit_IT().
03249   * @note   The USART errors are not managed to avoid the overrun error.
03250   * @note   ISR function executed when FIFO mode is enabled and when the
03251   *         data word length is less than 9 bits long.
03252   * @param  husart USART handle.
03253   * @retval None
03254   */
03255 static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
03256 {
03257   const HAL_USART_StateTypeDef state = husart->State;
03258   uint16_t  nb_tx_data;
03259 
03260   /* Check that a Tx process is ongoing */
03261   if ((state == HAL_USART_STATE_BUSY_TX) ||
03262       (state == HAL_USART_STATE_BUSY_TX_RX))
03263   {
03264     for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
03265     {
03266       if (husart->TxXferCount == 0U)
03267       {
03268         /* Disable the TX FIFO threshold interrupt */
03269         __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
03270 
03271         /* Enable the USART Transmit Complete Interrupt */
03272         __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
03273 
03274         break; /* force exit loop */
03275       }
03276       else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
03277       {
03278         husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr & (uint8_t)0xFF);
03279         husart->pTxBuffPtr++;
03280         husart->TxXferCount--;
03281       }
03282       else
03283       {
03284         /* Nothing to do */
03285       }
03286     }
03287   }
03288 }
03289 
03290 /**
03291   * @brief  Simplex send an amount of data in non-blocking mode.
03292   * @note   Function called under interruption only, once
03293   *         interruptions have been enabled by HAL_USART_Transmit_IT().
03294   * @note   The USART errors are not managed to avoid the overrun error.
03295   * @note   ISR function executed when FIFO mode is enabled and when the
03296   *         data word length is 9 bits long.
03297   * @param  husart USART handle.
03298   * @retval None
03299   */
03300 static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
03301 {
03302   const HAL_USART_StateTypeDef state = husart->State;
03303   const uint16_t *tmp;
03304   uint16_t  nb_tx_data;
03305 
03306   /* Check that a Tx process is ongoing */
03307   if ((state == HAL_USART_STATE_BUSY_TX) ||
03308       (state == HAL_USART_STATE_BUSY_TX_RX))
03309   {
03310     for (nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0U ; nb_tx_data--)
03311     {
03312       if (husart->TxXferCount == 0U)
03313       {
03314         /* Disable the TX FIFO threshold interrupt */
03315         __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
03316 
03317         /* Enable the USART Transmit Complete Interrupt */
03318         __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
03319 
03320         break; /* force exit loop */
03321       }
03322       else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
03323       {
03324         tmp = (const uint16_t *) husart->pTxBuffPtr;
03325         husart->Instance->TDR = (uint16_t)(*tmp & 0x01FFU);
03326         husart->pTxBuffPtr += 2U;
03327         husart->TxXferCount--;
03328       }
03329       else
03330       {
03331         /* Nothing to do */
03332       }
03333     }
03334   }
03335 }
03336 #endif /* USART_CR1_FIFOEN */
03337 
03338 /**
03339   * @brief  Wraps up transmission in non-blocking mode.
03340   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
03341   *                the configuration information for the specified USART module.
03342   * @retval None
03343   */
03344 static void USART_EndTransmit_IT(USART_HandleTypeDef *husart)
03345 {
03346   /* Disable the USART Transmit Complete Interrupt */
03347   __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
03348 
03349   /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
03350   __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
03351 
03352   /* Clear TxISR function pointer */
03353   husart->TxISR = NULL;
03354 
03355   if (husart->State == HAL_USART_STATE_BUSY_TX)
03356   {
03357     /* Clear overrun flag and discard the received data */
03358     __HAL_USART_CLEAR_OREFLAG(husart);
03359     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
03360 
03361     /* Tx process is completed, restore husart->State to Ready */
03362     husart->State = HAL_USART_STATE_READY;
03363 
03364 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03365     /* Call registered Tx Complete Callback */
03366     husart->TxCpltCallback(husart);
03367 #else
03368     /* Call legacy weak Tx Complete Callback */
03369     HAL_USART_TxCpltCallback(husart);
03370 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03371   }
03372   else if (husart->RxXferCount == 0U)
03373   {
03374     /* TxRx process is completed, restore husart->State to Ready */
03375     husart->State = HAL_USART_STATE_READY;
03376 
03377 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03378     /* Call registered Tx Rx Complete Callback */
03379     husart->TxRxCpltCallback(husart);
03380 #else
03381     /* Call legacy weak Tx Rx Complete Callback */
03382     HAL_USART_TxRxCpltCallback(husart);
03383 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03384   }
03385   else
03386   {
03387     /* Nothing to do */
03388   }
03389 }
03390 
03391 
03392 /**
03393   * @brief  Simplex receive an amount of data in non-blocking mode.
03394   * @note   Function called under interruption only, once
03395   *         interruptions have been enabled by HAL_USART_Receive_IT().
03396   * @note   ISR function executed when FIFO mode is disabled and when the
03397   *         data word length is less than 9 bits long.
03398   * @param  husart USART handle
03399   * @retval None
03400   */
03401 static void USART_RxISR_8BIT(USART_HandleTypeDef *husart)
03402 {
03403   const HAL_USART_StateTypeDef state = husart->State;
03404   uint16_t txdatacount;
03405   uint16_t uhMask = husart->Mask;
03406 #if defined(USART_CR1_FIFOEN)
03407   uint32_t txftie;
03408 #endif /* USART_CR1_FIFOEN */
03409 
03410   if ((state == HAL_USART_STATE_BUSY_RX) ||
03411       (state == HAL_USART_STATE_BUSY_TX_RX))
03412   {
03413     *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
03414     husart->pRxBuffPtr++;
03415     husart->RxXferCount--;
03416 
03417     if (husart->RxXferCount == 0U)
03418     {
03419       /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
03420 #if defined(USART_CR1_FIFOEN)
03421       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
03422 #else
03423       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
03424 #endif /* USART_CR1_FIFOEN */
03425 
03426       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
03427       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
03428 
03429       /* Clear RxISR function pointer */
03430       husart->RxISR = NULL;
03431 
03432 #if defined(USART_CR1_FIFOEN)
03433       /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
03434       txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
03435 #else
03436       /* txdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
03437 #endif /* USART_CR1_FIFOEN */
03438       txdatacount = husart->TxXferCount;
03439 
03440       if (state == HAL_USART_STATE_BUSY_RX)
03441       {
03442 #if defined(USART_CR2_SLVEN)
03443         /* Clear SPI slave underrun flag and discard transmit data */
03444         if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
03445         {
03446           __HAL_USART_CLEAR_UDRFLAG(husart);
03447           __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
03448         }
03449 #endif /* USART_CR2_SLVEN */
03450 
03451         /* Rx process is completed, restore husart->State to Ready */
03452         husart->State = HAL_USART_STATE_READY;
03453 
03454 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03455         /* Call registered Rx Complete Callback */
03456         husart->RxCpltCallback(husart);
03457 #else
03458         /* Call legacy weak Rx Complete Callback */
03459         HAL_USART_RxCpltCallback(husart);
03460 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03461       }
03462 #if defined(USART_CR1_FIFOEN)
03463       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03464                (txftie != USART_CR3_TXFTIE) &&
03465                (txdatacount == 0U))
03466 #else
03467       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03468                (txdatacount == 0U))
03469 #endif /* USART_CR1_FIFOEN */
03470       {
03471         /* TxRx process is completed, restore husart->State to Ready */
03472         husart->State = HAL_USART_STATE_READY;
03473 
03474 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03475         /* Call registered Tx Rx Complete Callback */
03476         husart->TxRxCpltCallback(husart);
03477 #else
03478         /* Call legacy weak Tx Rx Complete Callback */
03479         HAL_USART_TxRxCpltCallback(husart);
03480 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03481       }
03482       else
03483       {
03484         /* Nothing to do */
03485       }
03486     }
03487 #if defined(USART_CR2_SLVEN)
03488     else if ((state == HAL_USART_STATE_BUSY_RX) &&
03489              (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03490 #else
03491     else if (state == HAL_USART_STATE_BUSY_RX)
03492 #endif /* USART_CR2_SLVEN */
03493     {
03494       /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03495       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03496     }
03497     else
03498     {
03499       /* Nothing to do */
03500     }
03501   }
03502 }
03503 
03504 /**
03505   * @brief  Simplex receive an amount of data in non-blocking mode.
03506   * @note   Function called under interruption only, once
03507   *         interruptions have been enabled by HAL_USART_Receive_IT().
03508   * @note   ISR function executed when FIFO mode is disabled and when the
03509   *         data word length is 9 bits long.
03510   * @param  husart USART handle
03511   * @retval None
03512   */
03513 static void USART_RxISR_16BIT(USART_HandleTypeDef *husart)
03514 {
03515   const HAL_USART_StateTypeDef state = husart->State;
03516   uint16_t txdatacount;
03517   uint16_t *tmp;
03518   uint16_t uhMask = husart->Mask;
03519 #if defined(USART_CR1_FIFOEN)
03520   uint32_t txftie;
03521 #endif /* USART_CR1_FIFOEN */
03522 
03523   if ((state == HAL_USART_STATE_BUSY_RX) ||
03524       (state == HAL_USART_STATE_BUSY_TX_RX))
03525   {
03526     tmp = (uint16_t *) husart->pRxBuffPtr;
03527     *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
03528     husart->pRxBuffPtr += 2U;
03529     husart->RxXferCount--;
03530 
03531     if (husart->RxXferCount == 0U)
03532     {
03533       /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
03534 #if defined(USART_CR1_FIFOEN)
03535       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
03536 #else
03537       CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
03538 #endif /* USART_CR1_FIFOEN */
03539 
03540       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
03541       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
03542 
03543       /* Clear RxISR function pointer */
03544       husart->RxISR = NULL;
03545 
03546 #if defined(USART_CR1_FIFOEN)
03547       /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
03548       txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
03549 #else
03550       /* txdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
03551 #endif /* USART_CR1_FIFOEN */
03552       txdatacount = husart->TxXferCount;
03553 
03554       if (state == HAL_USART_STATE_BUSY_RX)
03555       {
03556 #if defined(USART_CR2_SLVEN)
03557         /* Clear SPI slave underrun flag and discard transmit data */
03558         if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
03559         {
03560           __HAL_USART_CLEAR_UDRFLAG(husart);
03561           __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
03562         }
03563 #endif /* USART_CR2_SLVEN */
03564 
03565         /* Rx process is completed, restore husart->State to Ready */
03566         husart->State = HAL_USART_STATE_READY;
03567 
03568 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03569         /* Call registered Rx Complete Callback */
03570         husart->RxCpltCallback(husart);
03571 #else
03572         /* Call legacy weak Rx Complete Callback */
03573         HAL_USART_RxCpltCallback(husart);
03574 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03575       }
03576 #if defined(USART_CR1_FIFOEN)
03577       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03578                (txftie != USART_CR3_TXFTIE) &&
03579                (txdatacount == 0U))
03580 #else
03581       else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03582                (txdatacount == 0U))
03583 #endif /* USART_CR1_FIFOEN */
03584       {
03585         /* TxRx process is completed, restore husart->State to Ready */
03586         husart->State = HAL_USART_STATE_READY;
03587 
03588 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03589         /* Call registered Tx Rx Complete Callback */
03590         husart->TxRxCpltCallback(husart);
03591 #else
03592         /* Call legacy weak Tx Rx Complete Callback */
03593         HAL_USART_TxRxCpltCallback(husart);
03594 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03595       }
03596       else
03597       {
03598         /* Nothing to do */
03599       }
03600     }
03601 #if defined(USART_CR2_SLVEN)
03602     else if ((state == HAL_USART_STATE_BUSY_RX) &&
03603              (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03604 #else
03605     else if (state == HAL_USART_STATE_BUSY_RX)
03606 #endif /* USART_CR2_SLVEN */
03607     {
03608       /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03609       husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03610     }
03611     else
03612     {
03613       /* Nothing to do */
03614     }
03615   }
03616 }
03617 
03618 #if defined(USART_CR1_FIFOEN)
03619 /**
03620   * @brief  Simplex receive an amount of data in non-blocking mode.
03621   * @note   Function called under interruption only, once
03622   *         interruptions have been enabled by HAL_USART_Receive_IT().
03623   * @note   ISR function executed when FIFO mode is enabled and when the
03624   *         data word length is less than 9 bits long.
03625   * @param  husart USART handle
03626   * @retval None
03627   */
03628 static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
03629 {
03630   HAL_USART_StateTypeDef state = husart->State;
03631   uint16_t txdatacount;
03632   uint16_t rxdatacount;
03633   uint16_t uhMask = husart->Mask;
03634   uint16_t nb_rx_data;
03635   uint32_t txftie;
03636 
03637   /* Check that a Rx process is ongoing */
03638   if ((state == HAL_USART_STATE_BUSY_RX) ||
03639       (state == HAL_USART_STATE_BUSY_TX_RX))
03640   {
03641     for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
03642     {
03643       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
03644       {
03645         *husart->pRxBuffPtr = (uint8_t)(husart->Instance->RDR & (uint8_t)(uhMask & 0xFFU));
03646         husart->pRxBuffPtr++;
03647         husart->RxXferCount--;
03648 
03649         if (husart->RxXferCount == 0U)
03650         {
03651           /* Disable the USART Parity Error Interrupt */
03652           CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
03653 
03654           /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error)
03655              and RX FIFO Threshold interrupt */
03656           CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
03657 
03658           /* Clear RxISR function pointer */
03659           husart->RxISR = NULL;
03660 
03661           /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
03662           txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
03663           txdatacount = husart->TxXferCount;
03664 
03665           if (state == HAL_USART_STATE_BUSY_RX)
03666           {
03667 #if defined(USART_CR2_SLVEN)
03668             /* Clear SPI slave underrun flag and discard transmit data */
03669             if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
03670             {
03671               __HAL_USART_CLEAR_UDRFLAG(husart);
03672               __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
03673             }
03674 #endif /* USART_CR2_SLVEN */
03675 
03676             /* Rx process is completed, restore husart->State to Ready */
03677             husart->State = HAL_USART_STATE_READY;
03678             state = HAL_USART_STATE_READY;
03679 
03680 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03681             /* Call registered Rx Complete Callback */
03682             husart->RxCpltCallback(husart);
03683 #else
03684             /* Call legacy weak Rx Complete Callback */
03685             HAL_USART_RxCpltCallback(husart);
03686 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03687           }
03688           else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03689                    (txftie != USART_CR3_TXFTIE) &&
03690                    (txdatacount == 0U))
03691           {
03692             /* TxRx process is completed, restore husart->State to Ready */
03693             husart->State = HAL_USART_STATE_READY;
03694             state = HAL_USART_STATE_READY;
03695 
03696 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03697             /* Call registered Tx Rx Complete Callback */
03698             husart->TxRxCpltCallback(husart);
03699 #else
03700             /* Call legacy weak Tx Rx Complete Callback */
03701             HAL_USART_TxRxCpltCallback(husart);
03702 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03703           }
03704           else
03705           {
03706             /* Nothing to do */
03707           }
03708         }
03709 #if defined(USART_CR2_SLVEN)
03710         else if ((state == HAL_USART_STATE_BUSY_RX) &&
03711                  (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03712 #else
03713         else if (state == HAL_USART_STATE_BUSY_RX)
03714 #endif /* USART_CR2_SLVEN */
03715         {
03716           /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03717           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03718         }
03719         else
03720         {
03721           /* Nothing to do */
03722         }
03723       }
03724     }
03725 
03726     /* When remaining number of bytes to receive is less than the RX FIFO
03727     threshold, next incoming frames are processed as if FIFO mode was
03728     disabled (i.e. one interrupt per received frame).
03729     */
03730     rxdatacount = husart->RxXferCount;
03731     if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
03732     {
03733       /* Disable the USART RXFT interrupt*/
03734       CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
03735 
03736       /* Update the RxISR function pointer */
03737       husart->RxISR = USART_RxISR_8BIT;
03738 
03739       /* Enable the USART Data Register Not Empty interrupt */
03740       SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
03741 
03742 #if defined(USART_CR2_SLVEN)
03743       if ((husart->TxXferCount == 0U) &&
03744           (state == HAL_USART_STATE_BUSY_TX_RX) &&
03745           (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03746 #else
03747       if ((husart->TxXferCount == 0U) &&
03748           (state == HAL_USART_STATE_BUSY_TX_RX))
03749 #endif /* USART_CR2_SLVEN */
03750       {
03751         /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03752         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03753       }
03754     }
03755   }
03756   else
03757   {
03758     /* Clear RXNE interrupt flag */
03759     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
03760   }
03761 }
03762 
03763 /**
03764   * @brief  Simplex receive an amount of data in non-blocking mode.
03765   * @note   Function called under interruption only, once
03766   *         interruptions have been enabled by HAL_USART_Receive_IT().
03767   * @note   ISR function executed when FIFO mode is enabled and when the
03768   *         data word length is 9 bits long.
03769   * @param  husart USART handle
03770   * @retval None
03771   */
03772 static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
03773 {
03774   HAL_USART_StateTypeDef state = husart->State;
03775   uint16_t txdatacount;
03776   uint16_t rxdatacount;
03777   uint16_t *tmp;
03778   uint16_t uhMask = husart->Mask;
03779   uint16_t nb_rx_data;
03780   uint32_t txftie;
03781 
03782   /* Check that a Tx process is ongoing */
03783   if ((state == HAL_USART_STATE_BUSY_RX) ||
03784       (state == HAL_USART_STATE_BUSY_TX_RX))
03785   {
03786     for (nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0U ; nb_rx_data--)
03787     {
03788       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
03789       {
03790         tmp = (uint16_t *) husart->pRxBuffPtr;
03791         *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
03792         husart->pRxBuffPtr += 2U;
03793         husart->RxXferCount--;
03794 
03795         if (husart->RxXferCount == 0U)
03796         {
03797           /* Disable the USART Parity Error Interrupt */
03798           CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
03799 
03800           /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error)
03801              and RX FIFO Threshold interrupt */
03802           CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
03803 
03804           /* Clear RxISR function pointer */
03805           husart->RxISR = NULL;
03806 
03807           /* txftie and txdatacount are temporary variables for MISRAC2012-Rule-13.5 */
03808           txftie = READ_BIT(husart->Instance->CR3, USART_CR3_TXFTIE);
03809           txdatacount = husart->TxXferCount;
03810 
03811           if (state == HAL_USART_STATE_BUSY_RX)
03812           {
03813 #if defined(USART_CR2_SLVEN)
03814             /* Clear SPI slave underrun flag and discard transmit data */
03815             if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
03816             {
03817               __HAL_USART_CLEAR_UDRFLAG(husart);
03818               __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
03819             }
03820 #endif /* USART_CR2_SLVEN */
03821 
03822             /* Rx process is completed, restore husart->State to Ready */
03823             husart->State = HAL_USART_STATE_READY;
03824             state = HAL_USART_STATE_READY;
03825 
03826 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03827             /* Call registered Rx Complete Callback */
03828             husart->RxCpltCallback(husart);
03829 #else
03830             /* Call legacy weak Rx Complete Callback */
03831             HAL_USART_RxCpltCallback(husart);
03832 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03833           }
03834           else if ((READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE) &&
03835                    (txftie != USART_CR3_TXFTIE) &&
03836                    (txdatacount == 0U))
03837           {
03838             /* TxRx process is completed, restore husart->State to Ready */
03839             husart->State = HAL_USART_STATE_READY;
03840             state = HAL_USART_STATE_READY;
03841 
03842 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
03843             /* Call registered Tx Rx Complete Callback */
03844             husart->TxRxCpltCallback(husart);
03845 #else
03846             /* Call legacy weak Tx Rx Complete Callback */
03847             HAL_USART_TxRxCpltCallback(husart);
03848 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
03849           }
03850           else
03851           {
03852             /* Nothing to do */
03853           }
03854         }
03855 #if defined(USART_CR2_SLVEN)
03856         else if ((state == HAL_USART_STATE_BUSY_RX) &&
03857                  (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03858 #else
03859         else if (state == HAL_USART_STATE_BUSY_RX)
03860 #endif /* USART_CR2_SLVEN */
03861         {
03862           /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03863           husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03864         }
03865         else
03866         {
03867           /* Nothing to do */
03868         }
03869       }
03870     }
03871 
03872     /* When remaining number of bytes to receive is less than the RX FIFO
03873     threshold, next incoming frames are processed as if FIFO mode was
03874     disabled (i.e. one interrupt per received frame).
03875     */
03876     rxdatacount = husart->RxXferCount;
03877     if (((rxdatacount != 0U)) && (rxdatacount < husart->NbRxDataToProcess))
03878     {
03879       /* Disable the USART RXFT interrupt*/
03880       CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
03881 
03882       /* Update the RxISR function pointer */
03883       husart->RxISR = USART_RxISR_16BIT;
03884 
03885       /* Enable the USART Data Register Not Empty interrupt */
03886       SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
03887 
03888 #if defined(USART_CR2_SLVEN)
03889       if ((husart->TxXferCount == 0U) &&
03890           (state == HAL_USART_STATE_BUSY_TX_RX) &&
03891           (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
03892 #else
03893       if ((husart->TxXferCount == 0U) &&
03894           (state == HAL_USART_STATE_BUSY_TX_RX))
03895 #endif /* USART_CR2_SLVEN */
03896       {
03897         /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
03898         husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
03899       }
03900     }
03901   }
03902   else
03903   {
03904     /* Clear RXNE interrupt flag */
03905     __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
03906   }
03907 }
03908 #endif /* USART_CR1_FIFOEN */
03909 
03910 /**
03911   * @}
03912   */
03913 
03914 #endif /* HAL_USART_MODULE_ENABLED */
03915 /**
03916   * @}
03917   */
03918 
03919 /**
03920   * @}
03921   */
03922