STM32F103xB HAL User Manual
stm32f1xx_hal_usart.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_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   @verbatim
00013   ==============================================================================
00014                         ##### How to use this driver #####
00015   ==============================================================================
00016   [..]
00017     The USART HAL driver can be used as follows:
00018 
00019     (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
00020     (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
00021         (##) Enable the USARTx interface clock.
00022         (##) USART pins configuration:
00023              (+++) Enable the clock for the USART GPIOs.
00024              (+++) Configure the USART pins as alternate function pull-up.
00025         (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
00026              HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
00027              (+++) Configure the USARTx interrupt priority.
00028              (+++) Enable the NVIC USART IRQ handle.
00029         (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
00030              HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
00031              (+++) Declare a DMA handle structure for the Tx/Rx channel.
00032              (+++) Enable the DMAx interface clock.
00033              (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
00034              (+++) Configure the DMA Tx/Rx channel.
00035              (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
00036              (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
00037              (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
00038                    (used for last byte sending completion detection in DMA non circular mode)
00039 
00040     (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
00041         flow control and Mode(Receiver/Transmitter) in the husart Init structure.
00042 
00043     (#) Initialize the USART registers by calling the HAL_USART_Init() API:
00044         (++) These APIs configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
00045              by calling the customized HAL_USART_MspInit(&husart) API.
00046 
00047         -@@- The specific USART interrupts (Transmission complete interrupt,
00048              RXNE interrupt and Error Interrupts) will be managed using the macros
00049              __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
00050 
00051     (#) Three operation modes are available within this driver :
00052 
00053      *** Polling mode IO operation ***
00054      =================================
00055      [..]
00056        (+) Send an amount of data in blocking mode using HAL_USART_Transmit()
00057        (+) Receive an amount of data in blocking mode using HAL_USART_Receive()
00058 
00059      *** Interrupt mode IO operation ***
00060      ===================================
00061      [..]
00062        (+) Send an amount of data in non blocking mode using HAL_USART_Transmit_IT()
00063        (+) At transmission end of transfer HAL_USART_TxHalfCpltCallback is executed and user can
00064             add his own code by customization of function pointer HAL_USART_TxCpltCallback
00065        (+) Receive an amount of data in non blocking mode using HAL_USART_Receive_IT()
00066        (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
00067             add his own code by customization of function pointer HAL_USART_RxCpltCallback
00068        (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
00069             add his own code by customization of function pointer HAL_USART_ErrorCallback
00070 
00071      *** DMA mode IO operation ***
00072      ==============================
00073      [..]
00074        (+) Send an amount of data in non blocking mode (DMA) using HAL_USART_Transmit_DMA()
00075        (+) At transmission end of half transfer HAL_USART_TxHalfCpltCallback is executed and user can
00076             add his own code by customization of function pointer HAL_USART_TxHalfCpltCallback
00077        (+) At transmission end of transfer HAL_USART_TxCpltCallback is executed and user can
00078             add his own code by customization of function pointer HAL_USART_TxCpltCallback
00079        (+) Receive an amount of data in non blocking mode (DMA) using HAL_USART_Receive_DMA()
00080        (+) At reception end of half transfer HAL_USART_RxHalfCpltCallback is executed and user can
00081             add his own code by customization of function pointer HAL_USART_RxHalfCpltCallback
00082        (+) At reception end of transfer HAL_USART_RxCpltCallback is executed and user can
00083             add his own code by customization of function pointer HAL_USART_RxCpltCallback
00084        (+) In case of transfer Error, HAL_USART_ErrorCallback() function is executed and user can
00085             add his own code by customization of function pointer HAL_USART_ErrorCallback
00086        (+) Pause the DMA Transfer using HAL_USART_DMAPause()
00087        (+) Resume the DMA Transfer using HAL_USART_DMAResume()
00088        (+) Stop the DMA Transfer using HAL_USART_DMAStop()
00089 
00090      *** USART HAL driver macros list ***
00091      =============================================
00092      [..]
00093        Below the list of most used macros in USART HAL driver.
00094 
00095        (+) __HAL_USART_ENABLE: Enable the USART peripheral
00096        (+) __HAL_USART_DISABLE: Disable the USART peripheral
00097        (+) __HAL_USART_GET_FLAG : Check whether the specified USART flag is set or not
00098        (+) __HAL_USART_CLEAR_FLAG : Clear the specified USART pending flag
00099        (+) __HAL_USART_ENABLE_IT: Enable the specified USART interrupt
00100        (+) __HAL_USART_DISABLE_IT: Disable the specified USART interrupt
00101 
00102      [..]
00103        (@) You can refer to the USART HAL driver header file for more useful macros
00104 
00105     ##### Callback registration #####
00106     ==================================
00107 
00108     [..]
00109     The compilation define USE_HAL_USART_REGISTER_CALLBACKS when set to 1
00110     allows the user to configure dynamically the driver callbacks.
00111 
00112     [..]
00113     Use Function @ref HAL_USART_RegisterCallback() to register a user callback.
00114     Function @ref HAL_USART_RegisterCallback() allows to register following callbacks:
00115     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00116     (+) TxCpltCallback            : Tx Complete Callback.
00117     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00118     (+) RxCpltCallback            : Rx Complete Callback.
00119     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
00120     (+) ErrorCallback             : Error Callback.
00121     (+) AbortCpltCallback         : Abort Complete Callback.
00122     (+) MspInitCallback           : USART MspInit.
00123     (+) MspDeInitCallback         : USART MspDeInit.
00124     This function takes as parameters the HAL peripheral handle, the Callback ID
00125     and a pointer to the user callback function.
00126 
00127     [..]
00128     Use function @ref HAL_USART_UnRegisterCallback() to reset a callback to the default
00129     weak (surcharged) function.
00130     @ref HAL_USART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
00131     and the Callback ID.
00132     This function allows to reset following callbacks:
00133     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
00134     (+) TxCpltCallback            : Tx Complete Callback.
00135     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
00136     (+) RxCpltCallback            : Rx Complete Callback.
00137     (+) TxRxCpltCallback          : Tx Rx Complete Callback.
00138     (+) ErrorCallback             : Error Callback.
00139     (+) AbortCpltCallback         : Abort Complete Callback.
00140     (+) MspInitCallback           : USART MspInit.
00141     (+) MspDeInitCallback         : USART MspDeInit.
00142 
00143     [..]
00144     By default, after the @ref HAL_USART_Init() and when the state is HAL_USART_STATE_RESET
00145     all callbacks are set to the corresponding weak (surcharged) functions:
00146     examples @ref HAL_USART_TxCpltCallback(), @ref HAL_USART_RxHalfCpltCallback().
00147     Exception done for MspInit and MspDeInit functions that are respectively
00148     reset to the legacy weak (surcharged) functions in the @ref HAL_USART_Init()
00149     and @ref HAL_USART_DeInit() only when these callbacks are null (not registered beforehand).
00150     If not, MspInit or MspDeInit are not null, the @ref HAL_USART_Init() and @ref HAL_USART_DeInit()
00151     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
00152 
00153     [..]
00154     Callbacks can be registered/unregistered in HAL_USART_STATE_READY state only.
00155     Exception done MspInit/MspDeInit that can be registered/unregistered
00156     in HAL_USART_STATE_READY or HAL_USART_STATE_RESET state, thus registered (user)
00157     MspInit/DeInit callbacks can be used during the Init/DeInit.
00158     In that case first register the MspInit/MspDeInit user callbacks
00159     using @ref HAL_USART_RegisterCallback() before calling @ref HAL_USART_DeInit()
00160     or @ref HAL_USART_Init() function.
00161 
00162     [..]
00163     When The compilation define USE_HAL_USART_REGISTER_CALLBACKS is set to 0 or
00164     not defined, the callback registration feature is not available
00165     and weak (surcharged) callbacks are used.
00166 
00167   @endverbatim
00168      [..]
00169        (@) Additional remark: If the parity is enabled, then the MSB bit of the data written
00170            in the data register is transmitted but is changed by the parity bit.
00171            Depending on the frame length defined by the M bit (8-bits or 9-bits),
00172            the possible USART frame formats are as listed in the following table:
00173     +-------------------------------------------------------------+
00174     |   M bit |  PCE bit  |            USART frame                 |
00175     |---------------------|---------------------------------------|
00176     |    0    |    0      |    | SB | 8 bit data | STB |          |
00177     |---------|-----------|---------------------------------------|
00178     |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
00179     |---------|-----------|---------------------------------------|
00180     |    1    |    0      |    | SB | 9 bit data | STB |          |
00181     |---------|-----------|---------------------------------------|
00182     |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
00183     +-------------------------------------------------------------+
00184   ******************************************************************************
00185   * @attention
00186   *
00187   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00188   * All rights reserved.</center></h2>
00189   *
00190   * This software component is licensed by ST under BSD 3-Clause license,
00191   * the "License"; You may not use this file except in compliance with the
00192   * License. You may obtain a copy of the License at:
00193   *                        opensource.org/licenses/BSD-3-Clause
00194   *
00195   ******************************************************************************
00196   */
00197 
00198 /* Includes ------------------------------------------------------------------*/
00199 #include "stm32f1xx_hal.h"
00200 
00201 /** @addtogroup STM32F1xx_HAL_Driver
00202   * @{
00203   */
00204 
00205 /** @defgroup USART USART
00206   * @brief HAL USART Synchronous module driver
00207   * @{
00208   */
00209 #ifdef HAL_USART_MODULE_ENABLED
00210 /* Private typedef -----------------------------------------------------------*/
00211 /* Private define ------------------------------------------------------------*/
00212 /** @addtogroup USART_Private_Constants
00213   * @{
00214   */
00215 #define DUMMY_DATA           0xFFFFU
00216 #define USART_TIMEOUT_VALUE  22000U
00217 /**
00218   * @}
00219   */
00220 /* Private macro -------------------------------------------------------------*/
00221 /* Private variables ---------------------------------------------------------*/
00222 /* Private function prototypes -----------------------------------------------*/
00223 /* Private functions ---------------------------------------------------------*/
00224 /** @addtogroup USART_Private_Functions
00225   * @{
00226   */
00227 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00228 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart);
00229 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00230 static void USART_EndTxTransfer(USART_HandleTypeDef *husart);
00231 static void USART_EndRxTransfer(USART_HandleTypeDef *husart);
00232 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
00233 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
00234 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
00235 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
00236 static void USART_SetConfig(USART_HandleTypeDef *husart);
00237 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
00238 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
00239 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
00240 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
00241 static void USART_DMAError(DMA_HandleTypeDef *hdma);
00242 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
00243 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
00244 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
00245 
00246 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
00247 /**
00248   * @}
00249   */
00250 
00251 /* Exported functions --------------------------------------------------------*/
00252 /** @defgroup USART_Exported_Functions USART Exported Functions
00253   * @{
00254   */
00255 
00256 /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
00257   *  @brief    Initialization and Configuration functions
00258   *
00259 @verbatim
00260   ==============================================================================
00261               ##### Initialization and Configuration functions #####
00262   ==============================================================================
00263   [..]
00264   This subsection provides a set of functions allowing to initialize the USART
00265   in asynchronous and in synchronous modes.
00266   (+) For the asynchronous mode only these parameters can be configured:
00267       (++) Baud Rate
00268       (++) Word Length
00269       (++) Stop Bit
00270       (++) Parity: If the parity is enabled, then the MSB bit of the data written
00271            in the data register is transmitted but is changed by the parity bit.
00272            Depending on the frame length defined by the M bit (8-bits or 9-bits),
00273            please refer to Reference manual for possible USART frame formats.
00274       (++) USART polarity
00275       (++) USART phase
00276       (++) USART LastBit
00277       (++) Receiver/transmitter modes
00278 
00279   [..]
00280     The HAL_USART_Init() function follows the USART  synchronous configuration
00281     procedures (details for the procedures are available in reference manuals
00282     (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
00283 
00284 @endverbatim
00285   * @{
00286   */
00287 
00288 /**
00289   * @brief  Initialize the USART mode according to the specified
00290   *         parameters in the USART_InitTypeDef and initialize the associated handle.
00291   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
00292   *                the configuration information for the specified USART module.
00293   * @retval HAL status
00294   */
00295 HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
00296 {
00297   /* Check the USART handle allocation */
00298   if (husart == NULL)
00299   {
00300     return HAL_ERROR;
00301   }
00302 
00303   /* Check the parameters */
00304   assert_param(IS_USART_INSTANCE(husart->Instance));
00305 
00306   if (husart->State == HAL_USART_STATE_RESET)
00307   {
00308     /* Allocate lock resource and initialize it */
00309     husart->Lock = HAL_UNLOCKED;
00310 
00311 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00312     USART_InitCallbacksToDefault(husart);
00313 
00314     if (husart->MspInitCallback == NULL)
00315     {
00316       husart->MspInitCallback = HAL_USART_MspInit;
00317     }
00318 
00319     /* Init the low level hardware */
00320     husart->MspInitCallback(husart);
00321 #else
00322     /* Init the low level hardware : GPIO, CLOCK */
00323     HAL_USART_MspInit(husart);
00324 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00325   }
00326 
00327   husart->State = HAL_USART_STATE_BUSY;
00328 
00329   /* Set the USART Communication parameters */
00330   USART_SetConfig(husart);
00331 
00332   /* In USART mode, the following bits must be kept cleared:
00333      - LINEN bit in the USART_CR2 register
00334      - HDSEL, SCEN and IREN bits in the USART_CR3 register */
00335   CLEAR_BIT(husart->Instance->CR2, USART_CR2_LINEN);
00336   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
00337 
00338   /* Enable the Peripheral */
00339   __HAL_USART_ENABLE(husart);
00340 
00341   /* Initialize the USART state */
00342   husart->ErrorCode = HAL_USART_ERROR_NONE;
00343   husart->State = HAL_USART_STATE_READY;
00344 
00345   return HAL_OK;
00346 }
00347 
00348 /**
00349   * @brief  DeInitializes the USART peripheral.
00350   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
00351   *                the configuration information for the specified USART module.
00352   * @retval HAL status
00353   */
00354 HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
00355 {
00356   /* Check the USART handle allocation */
00357   if (husart == NULL)
00358   {
00359     return HAL_ERROR;
00360   }
00361 
00362   /* Check the parameters */
00363   assert_param(IS_USART_INSTANCE(husart->Instance));
00364 
00365   husart->State = HAL_USART_STATE_BUSY;
00366 
00367   /* Disable the Peripheral */
00368   __HAL_USART_DISABLE(husart);
00369 
00370 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00371   if (husart->MspDeInitCallback == NULL)
00372   {
00373     husart->MspDeInitCallback = HAL_USART_MspDeInit;
00374   }
00375   /* DeInit the low level hardware */
00376   husart->MspDeInitCallback(husart);
00377 #else
00378   /* DeInit the low level hardware */
00379   HAL_USART_MspDeInit(husart);
00380 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00381 
00382   husart->ErrorCode = HAL_USART_ERROR_NONE;
00383   husart->State = HAL_USART_STATE_RESET;
00384 
00385   /* Release Lock */
00386   __HAL_UNLOCK(husart);
00387 
00388   return HAL_OK;
00389 }
00390 
00391 /**
00392   * @brief  USART MSP Init.
00393   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
00394   *                the configuration information for the specified USART module.
00395   * @retval None
00396   */
00397 __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
00398 {
00399   /* Prevent unused argument(s) compilation warning */
00400   UNUSED(husart);
00401   /* NOTE: This function should not be modified, when the callback is needed,
00402            the HAL_USART_MspInit could be implemented in the user file
00403    */
00404 }
00405 
00406 /**
00407   * @brief  USART MSP DeInit.
00408   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
00409   *                the configuration information for the specified USART module.
00410   * @retval None
00411   */
00412 __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
00413 {
00414   /* Prevent unused argument(s) compilation warning */
00415   UNUSED(husart);
00416   /* NOTE: This function should not be modified, when the callback is needed,
00417            the HAL_USART_MspDeInit could be implemented in the user file
00418    */
00419 }
00420 
00421 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
00422 /**
00423   * @brief  Register a User USART Callback
00424   *         To be used instead of the weak predefined callback
00425   * @param  husart usart handle
00426   * @param  CallbackID ID of the callback to be registered
00427   *         This parameter can be one of the following values:
00428   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
00429   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
00430   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
00431   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
00432   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
00433   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
00434   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00435   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
00436   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
00437   * @param  pCallback pointer to the Callback function
00438   * @retval HAL status
00439 +  */
00440 HAL_StatusTypeDef HAL_USART_RegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID, pUSART_CallbackTypeDef pCallback)
00441 {
00442   HAL_StatusTypeDef status = HAL_OK;
00443 
00444   if (pCallback == NULL)
00445   {
00446     /* Update the error code */
00447     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00448 
00449     return HAL_ERROR;
00450   }
00451   /* Process locked */
00452   __HAL_LOCK(husart);
00453 
00454   if (husart->State == HAL_USART_STATE_READY)
00455   {
00456     switch (CallbackID)
00457     {
00458       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
00459         husart->TxHalfCpltCallback = pCallback;
00460         break;
00461 
00462       case HAL_USART_TX_COMPLETE_CB_ID :
00463         husart->TxCpltCallback = pCallback;
00464         break;
00465 
00466       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
00467         husart->RxHalfCpltCallback = pCallback;
00468         break;
00469 
00470       case HAL_USART_RX_COMPLETE_CB_ID :
00471         husart->RxCpltCallback = pCallback;
00472         break;
00473 
00474       case HAL_USART_TX_RX_COMPLETE_CB_ID :
00475         husart->TxRxCpltCallback = pCallback;
00476         break;
00477 
00478       case HAL_USART_ERROR_CB_ID :
00479         husart->ErrorCallback = pCallback;
00480         break;
00481 
00482       case HAL_USART_ABORT_COMPLETE_CB_ID :
00483         husart->AbortCpltCallback = pCallback;
00484         break;
00485 
00486       case HAL_USART_MSPINIT_CB_ID :
00487         husart->MspInitCallback = pCallback;
00488         break;
00489 
00490       case HAL_USART_MSPDEINIT_CB_ID :
00491         husart->MspDeInitCallback = pCallback;
00492         break;
00493 
00494       default :
00495         /* Update the error code */
00496         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00497 
00498         /* Return error status */
00499         status =  HAL_ERROR;
00500         break;
00501     }
00502   }
00503   else if (husart->State == HAL_USART_STATE_RESET)
00504   {
00505     switch (CallbackID)
00506     {
00507       case HAL_USART_MSPINIT_CB_ID :
00508         husart->MspInitCallback = pCallback;
00509         break;
00510 
00511       case HAL_USART_MSPDEINIT_CB_ID :
00512         husart->MspDeInitCallback = pCallback;
00513         break;
00514 
00515       default :
00516         /* Update the error code */
00517         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00518 
00519         /* Return error status */
00520         status =  HAL_ERROR;
00521         break;
00522     }
00523   }
00524   else
00525   {
00526     /* Update the error code */
00527     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00528 
00529     /* Return error status */
00530     status =  HAL_ERROR;
00531   }
00532 
00533   /* Release Lock */
00534   __HAL_UNLOCK(husart);
00535 
00536   return status;
00537 }
00538 
00539 /**
00540   * @brief  Unregister an USART Callback
00541   *         USART callaback is redirected to the weak predefined callback
00542   * @param  husart usart handle
00543   * @param  CallbackID ID of the callback to be unregistered
00544   *         This parameter can be one of the following values:
00545   *           @arg @ref HAL_USART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
00546   *           @arg @ref HAL_USART_TX_COMPLETE_CB_ID Tx Complete Callback ID
00547   *           @arg @ref HAL_USART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
00548   *           @arg @ref HAL_USART_RX_COMPLETE_CB_ID Rx Complete Callback ID
00549   *           @arg @ref HAL_USART_TX_RX_COMPLETE_CB_ID Rx Complete Callback ID
00550   *           @arg @ref HAL_USART_ERROR_CB_ID Error Callback ID
00551   *           @arg @ref HAL_USART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
00552   *           @arg @ref HAL_USART_MSPINIT_CB_ID MspInit Callback ID
00553   *           @arg @ref HAL_USART_MSPDEINIT_CB_ID MspDeInit Callback ID
00554   * @retval HAL status
00555   */
00556 HAL_StatusTypeDef HAL_USART_UnRegisterCallback(USART_HandleTypeDef *husart, HAL_USART_CallbackIDTypeDef CallbackID)
00557 {
00558   HAL_StatusTypeDef status = HAL_OK;
00559 
00560   /* Process locked */
00561   __HAL_LOCK(husart);
00562 
00563   if (husart->State == HAL_USART_STATE_READY)
00564   {
00565     switch (CallbackID)
00566     {
00567       case HAL_USART_TX_HALFCOMPLETE_CB_ID :
00568         husart->TxHalfCpltCallback = HAL_USART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback       */
00569         break;
00570 
00571       case HAL_USART_TX_COMPLETE_CB_ID :
00572         husart->TxCpltCallback = HAL_USART_TxCpltCallback;                       /* Legacy weak TxCpltCallback            */
00573         break;
00574 
00575       case HAL_USART_RX_HALFCOMPLETE_CB_ID :
00576         husart->RxHalfCpltCallback = HAL_USART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback        */
00577         break;
00578 
00579       case HAL_USART_RX_COMPLETE_CB_ID :
00580         husart->RxCpltCallback = HAL_USART_RxCpltCallback;                       /* Legacy weak RxCpltCallback            */
00581         break;
00582 
00583       case HAL_USART_TX_RX_COMPLETE_CB_ID :
00584         husart->TxRxCpltCallback = HAL_USART_TxRxCpltCallback;                   /* Legacy weak TxRxCpltCallback            */
00585         break;
00586 
00587       case HAL_USART_ERROR_CB_ID :
00588         husart->ErrorCallback = HAL_USART_ErrorCallback;                         /* Legacy weak ErrorCallback             */
00589         break;
00590 
00591       case HAL_USART_ABORT_COMPLETE_CB_ID :
00592         husart->AbortCpltCallback = HAL_USART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback         */
00593         break;
00594 
00595       case HAL_USART_MSPINIT_CB_ID :
00596         husart->MspInitCallback = HAL_USART_MspInit;                             /* Legacy weak MspInitCallback           */
00597         break;
00598 
00599       case HAL_USART_MSPDEINIT_CB_ID :
00600         husart->MspDeInitCallback = HAL_USART_MspDeInit;                         /* Legacy weak MspDeInitCallback         */
00601         break;
00602 
00603       default :
00604         /* Update the error code */
00605         husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00606 
00607         /* Return error status */
00608         status =  HAL_ERROR;
00609         break;
00610     }
00611   }
00612   else if (husart->State == HAL_USART_STATE_RESET)
00613   {
00614     switch (CallbackID)
00615     {
00616       case HAL_USART_MSPINIT_CB_ID :
00617         husart->MspInitCallback = HAL_USART_MspInit;
00618         break;
00619 
00620       case HAL_USART_MSPDEINIT_CB_ID :
00621         husart->MspDeInitCallback = HAL_USART_MspDeInit;
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
00634   {
00635     /* Update the error code */
00636     husart->ErrorCode |= HAL_USART_ERROR_INVALID_CALLBACK;
00637 
00638     /* Return error status */
00639     status =  HAL_ERROR;
00640   }
00641 
00642   /* Release Lock */
00643   __HAL_UNLOCK(husart);
00644 
00645   return status;
00646 }
00647 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
00648 
00649 /**
00650   * @}
00651   */
00652 
00653 /** @defgroup USART_Exported_Functions_Group2 IO operation functions
00654   *  @brief   USART Transmit and Receive functions
00655   *
00656 @verbatim
00657   ==============================================================================
00658                          ##### IO operation functions #####
00659   ==============================================================================
00660   [..]
00661     This subsection provides a set of functions allowing to manage the USART synchronous
00662     data transfers.
00663 
00664   [..]
00665     The USART supports master mode only: it cannot receive or send data related to an input
00666     clock (SCLK is always an output).
00667 
00668     (#) There are two modes of transfer:
00669         (++) Blocking mode: The communication is performed in polling mode.
00670              The HAL status of all data processing is returned by the same function
00671              after finishing transfer.
00672         (++) No-Blocking mode: The communication is performed using Interrupts
00673              or DMA, These API's return the HAL status.
00674              The end of the data processing will be indicated through the
00675              dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
00676              using DMA mode.
00677              The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback()
00678               user callbacks
00679              will be executed respectively at the end of the transmit or Receive process
00680              The HAL_USART_ErrorCallback() user callback will be executed when a communication
00681              error is detected
00682 
00683     (#) Blocking mode APIs are :
00684         (++) HAL_USART_Transmit() in simplex mode
00685         (++) HAL_USART_Receive() in full duplex receive only
00686         (++) HAL_USART_TransmitReceive() in full duplex mode
00687 
00688     (#) Non Blocking mode APIs with Interrupt are :
00689         (++) HAL_USART_Transmit_IT()in simplex mode
00690         (++) HAL_USART_Receive_IT() in full duplex receive only
00691         (++) HAL_USART_TransmitReceive_IT() in full duplex mode
00692         (++) HAL_USART_IRQHandler()
00693 
00694     (#) Non Blocking mode functions with DMA are :
00695         (++) HAL_USART_Transmit_DMA()in simplex mode
00696         (++) HAL_USART_Receive_DMA() in full duplex receive only
00697         (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
00698         (++) HAL_USART_DMAPause()
00699         (++) HAL_USART_DMAResume()
00700         (++) HAL_USART_DMAStop()
00701 
00702     (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
00703         (++) HAL_USART_TxHalfCpltCallback()
00704         (++) HAL_USART_TxCpltCallback()
00705         (++) HAL_USART_RxHalfCpltCallback()
00706         (++) HAL_USART_RxCpltCallback()
00707         (++) HAL_USART_ErrorCallback()
00708         (++) HAL_USART_TxRxCpltCallback()
00709 
00710     (#) Non-Blocking mode transfers could be aborted using Abort API's :
00711         (++) HAL_USART_Abort()
00712         (++) HAL_USART_Abort_IT()
00713 
00714     (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
00715         (++) HAL_USART_AbortCpltCallback()
00716 
00717     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
00718         Errors are handled as follows :
00719         (++) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
00720              to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
00721              Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
00722              and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
00723              If user wants to abort it, Abort services should be called by user.
00724         (++) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
00725              This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
00726              Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
00727 
00728 @endverbatim
00729   * @{
00730   */
00731 
00732 /**
00733   * @brief  Simplex Send an amount of data in blocking mode.
00734   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00735   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
00736   *         of u16 provided through pTxData.
00737   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
00738   *                 the configuration information for the specified USART module.
00739   * @param  pTxData Pointer to data buffer (u8 or u16 data elements).
00740   * @param  Size    Amount of data elements (u8 or u16) to be sent.
00741   * @param  Timeout Timeout duration.
00742   * @retval HAL status
00743   */
00744 HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
00745 {
00746   uint8_t  *ptxdata8bits;
00747   uint16_t *ptxdata16bits;
00748   uint32_t tickstart;
00749 
00750   if (husart->State == HAL_USART_STATE_READY)
00751   {
00752     if ((pTxData == NULL) || (Size == 0))
00753     {
00754       return  HAL_ERROR;
00755     }
00756 
00757     /* Process Locked */
00758     __HAL_LOCK(husart);
00759 
00760     husart->ErrorCode = HAL_USART_ERROR_NONE;
00761     husart->State = HAL_USART_STATE_BUSY_TX;
00762 
00763     /* Init tickstart for timeout management */
00764     tickstart = HAL_GetTick();
00765 
00766     husart->TxXferSize = Size;
00767     husart->TxXferCount = Size;
00768 
00769     /* In case of 9bits/No Parity transfer, pTxData needs to be handled as a uint16_t pointer */
00770     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00771     {
00772       ptxdata8bits  = NULL;
00773       ptxdata16bits = (uint16_t *) pTxData;
00774     }
00775     else
00776     {
00777       ptxdata8bits  = pTxData;
00778       ptxdata16bits = NULL;
00779     }
00780 
00781     while (husart->TxXferCount > 0U)
00782     {
00783       /* Wait for TXE flag in order to write data in DR */
00784       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00785       {
00786         return HAL_TIMEOUT;
00787       }
00788       if (ptxdata8bits == NULL)
00789       {
00790         husart->Instance->DR = (uint16_t)(*ptxdata16bits & (uint16_t)0x01FF);
00791         ptxdata16bits++;
00792       }
00793       else
00794       {
00795         husart->Instance->DR = (uint8_t)(*ptxdata8bits & (uint8_t)0xFF);
00796         ptxdata8bits++;
00797       }
00798 
00799       husart->TxXferCount--;
00800     }
00801 
00802     if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
00803     {
00804       return HAL_TIMEOUT;
00805     }
00806 
00807     husart->State = HAL_USART_STATE_READY;
00808 
00809     /* Process Unlocked */
00810     __HAL_UNLOCK(husart);
00811 
00812     return HAL_OK;
00813   }
00814   else
00815   {
00816     return HAL_BUSY;
00817   }
00818 }
00819 
00820 /**
00821   * @brief  Full-Duplex Receive an amount of data in blocking mode.
00822   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
00823   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00824   *         the received data is handled as a set of u16. In this case, Size must indicate the number
00825   *         of u16 available through pRxData.
00826   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
00827   *                 the configuration information for the specified USART module.
00828   * @param  pRxData Pointer to data buffer (u8 or u16 data elements).
00829   * @param  Size    Amount of data elements (u8 or u16) to be received.
00830   * @param  Timeout Timeout duration.
00831   * @retval HAL status
00832   */
00833 HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
00834 {
00835   uint8_t  *prxdata8bits;
00836   uint16_t *prxdata16bits;
00837   uint32_t tickstart;
00838 
00839   if (husart->State == HAL_USART_STATE_READY)
00840   {
00841     if ((pRxData == NULL) || (Size == 0))
00842     {
00843       return  HAL_ERROR;
00844     }
00845     /* Process Locked */
00846     __HAL_LOCK(husart);
00847 
00848     husart->ErrorCode = HAL_USART_ERROR_NONE;
00849     husart->State = HAL_USART_STATE_BUSY_RX;
00850 
00851     /* Init tickstart for timeout management */
00852     tickstart = HAL_GetTick();
00853 
00854     husart->RxXferSize = Size;
00855     husart->RxXferCount = Size;
00856 
00857     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
00858     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00859     {
00860       prxdata8bits  = NULL;
00861       prxdata16bits = (uint16_t *) pRxData;
00862     }
00863     else
00864     {
00865       prxdata8bits  = pRxData;
00866       prxdata16bits = NULL;
00867     }
00868 
00869     /* Check the remain data to be received */
00870     while (husart->RxXferCount > 0U)
00871     {
00872       /* Wait until TXE flag is set to send dummy byte in order to generate the
00873       * clock for the slave to send data.
00874       * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
00875       * can be written for all the cases. */
00876       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00877       {
00878         return HAL_TIMEOUT;
00879       }
00880       husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x0FF);
00881 
00882       /* Wait until RXNE flag is set to receive the byte */
00883       if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
00884       {
00885         return HAL_TIMEOUT;
00886       }
00887 
00888       if (prxdata8bits == NULL)
00889       {
00890         *prxdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
00891         prxdata16bits++;
00892       }
00893       else
00894       {
00895         if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
00896         {
00897           *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x0FF);
00898         }
00899         else
00900         {
00901           *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x07F);
00902         }
00903         prxdata8bits++;
00904       }
00905       husart->RxXferCount--;
00906     }
00907 
00908     husart->State = HAL_USART_STATE_READY;
00909 
00910     /* Process Unlocked */
00911     __HAL_UNLOCK(husart);
00912 
00913     return HAL_OK;
00914   }
00915   else
00916   {
00917     return HAL_BUSY;
00918   }
00919 }
00920 
00921 /**
00922   * @brief  Full-Duplex Send and Receive an amount of data in full-duplex mode (blocking mode).
00923   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
00924   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
00925   *         of u16 available through pTxData and through pRxData.
00926   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
00927   *                 the configuration information for the specified USART module.
00928   * @param  pTxData Pointer to TX data buffer (u8 or u16 data elements).
00929   * @param  pRxData Pointer to RX data buffer (u8 or u16 data elements).
00930   * @param  Size    Amount of data elements (u8 or u16) to be sent (same amount to be received).
00931   * @param  Timeout Timeout duration
00932   * @retval HAL status
00933   */
00934 HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
00935 {
00936   uint8_t  *prxdata8bits;
00937   uint16_t *prxdata16bits;
00938   uint8_t  *ptxdata8bits;
00939   uint16_t *ptxdata16bits;
00940   uint16_t rxdatacount;
00941   uint32_t tickstart;
00942 
00943   if (husart->State == HAL_USART_STATE_READY)
00944   {
00945     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
00946     {
00947       return  HAL_ERROR;
00948     }
00949 
00950     /* In case of 9bits/No Parity transfer, pTxData and pRxData buffers provided as input parameter
00951        should be aligned on a u16 frontier, as data to be filled into TDR/retrieved from RDR will be
00952        handled through a u16 cast. */
00953     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00954     {
00955       if (((((uint32_t)pTxData) & 1U) != 0U) || ((((uint32_t)pRxData) & 1U) != 0U))
00956       {
00957         return  HAL_ERROR;
00958       }
00959     }
00960     /* Process Locked */
00961     __HAL_LOCK(husart);
00962 
00963     husart->ErrorCode = HAL_USART_ERROR_NONE;
00964     husart->State = HAL_USART_STATE_BUSY_RX;
00965 
00966     /* Init tickstart for timeout management */
00967     tickstart = HAL_GetTick();
00968 
00969     husart->RxXferSize = Size;
00970     husart->TxXferSize = Size;
00971     husart->TxXferCount = Size;
00972     husart->RxXferCount = Size;
00973 
00974     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
00975     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
00976     {
00977       prxdata8bits  = NULL;
00978       ptxdata8bits  = NULL;
00979       ptxdata16bits = (uint16_t *) pTxData;
00980       prxdata16bits = (uint16_t *) pRxData;
00981     }
00982     else
00983     {
00984       prxdata8bits  = pRxData;
00985       ptxdata8bits  = pTxData;
00986       ptxdata16bits = NULL;
00987       prxdata16bits = NULL;
00988     }
00989 
00990     /* Check the remain data to be received */
00991     /* rxdatacount is a temporary variable for MISRAC2012-Rule-13.5 */
00992     rxdatacount = husart->RxXferCount;
00993     while ((husart->TxXferCount > 0U) || (rxdatacount > 0U))
00994     {
00995       if (husart->TxXferCount > 0U)
00996       {
00997         /* Wait for TXE flag in order to write data in DR */
00998         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
00999         {
01000           return HAL_TIMEOUT;
01001         }
01002 
01003         if (ptxdata8bits == NULL)
01004         {
01005           husart->Instance->DR = (uint16_t)(*ptxdata16bits & (uint16_t)0x01FF);
01006           ptxdata16bits++;
01007         }
01008         else
01009         {
01010           husart->Instance->DR = (uint8_t)(*ptxdata8bits & (uint8_t)0xFF);
01011           ptxdata8bits++; 
01012         }
01013 
01014         husart->TxXferCount--;
01015       }
01016 
01017       if (husart->RxXferCount > 0U)
01018       {  
01019         /* Wait for RXNE Flag */
01020         if (USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
01021         {
01022           return HAL_TIMEOUT;
01023         }
01024         if (prxdata8bits == NULL)
01025         {
01026           *prxdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
01027           prxdata16bits++;
01028         }
01029         else
01030         {
01031           if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
01032           {
01033             *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x0FF);
01034           }
01035           else
01036           {
01037             *prxdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x07F);
01038           }
01039 
01040           prxdata8bits++;
01041         }
01042 
01043         husart->RxXferCount--;
01044       }
01045       rxdatacount = husart->RxXferCount;
01046     }
01047 
01048     husart->State = HAL_USART_STATE_READY;
01049 
01050     /* Process Unlocked */
01051     __HAL_UNLOCK(husart);
01052 
01053     return HAL_OK;
01054   }
01055   else
01056   {
01057     return HAL_BUSY;
01058   }
01059 }
01060 
01061 /**
01062   * @brief  Simplex Send an amount of data in non-blocking mode.
01063   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01064   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
01065   *         of u16 provided through pTxData.
01066   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01067   *                 the configuration information for the specified USART module.
01068   * @param  pTxData Pointer to data buffer (u8 or u16 data elements).
01069   * @param  Size    Amount of data elements (u8 or u16) to be sent.
01070   * @retval HAL status
01071   * @note   The USART errors are not managed to avoid the overrun error.
01072   */
01073 HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
01074 {
01075   if (husart->State == HAL_USART_STATE_READY)
01076   {
01077     if ((pTxData == NULL) || (Size == 0))
01078     {
01079       return HAL_ERROR;
01080     }
01081 
01082     /* Process Locked */
01083     __HAL_LOCK(husart);
01084 
01085     husart->pTxBuffPtr = pTxData;
01086     husart->TxXferSize = Size;
01087     husart->TxXferCount = Size;
01088 
01089     husart->ErrorCode = HAL_USART_ERROR_NONE;
01090     husart->State = HAL_USART_STATE_BUSY_TX;
01091 
01092     /* The USART Error Interrupts: (Frame error, Noise error, Overrun error)
01093        are not managed by the USART transmit process to avoid the overrun interrupt
01094        when the USART mode is configured for transmit and receive "USART_MODE_TX_RX"
01095        to benefit for the frame error and noise interrupts the USART mode should be
01096        configured only for transmit "USART_MODE_TX"
01097        The __HAL_USART_ENABLE_IT(husart, USART_IT_ERR) can be used to enable the Frame error,
01098        Noise error interrupt */
01099 
01100     /* Process Unlocked */
01101     __HAL_UNLOCK(husart);
01102 
01103     /* Enable the USART Transmit Data Register Empty Interrupt */
01104     SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
01105 
01106     return HAL_OK;
01107   }
01108   else
01109   {
01110     return HAL_BUSY;
01111   }
01112 }
01113 
01114 /**
01115   * @brief  Simplex Receive an amount of data in non-blocking mode.
01116   * @note   To receive synchronous data, dummy data are simultaneously transmitted.
01117   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01118   *         the received data is handled as a set of u16. In this case, Size must indicate the number
01119   *         of u16 available through pRxData.
01120   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01121   *                 the configuration information for the specified USART module.
01122   * @param  pRxData Pointer to data buffer (u8 or u16 data elements).
01123   * @param  Size    Amount of data elements (u8 or u16) to be received.
01124   * @retval HAL status
01125   */
01126 HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
01127 {
01128   if (husart->State == HAL_USART_STATE_READY)
01129   {
01130     if ((pRxData == NULL) || (Size == 0))
01131     {
01132       return HAL_ERROR;
01133     }
01134     /* Process Locked */
01135     __HAL_LOCK(husart);
01136 
01137     husart->pRxBuffPtr = pRxData;
01138     husart->RxXferSize = Size;
01139     husart->RxXferCount = Size;
01140 
01141     husart->ErrorCode = HAL_USART_ERROR_NONE;
01142     husart->State = HAL_USART_STATE_BUSY_RX;
01143 
01144     /* Process Unlocked */
01145     __HAL_UNLOCK(husart);
01146 
01147     /* Enable the USART Parity Error and Data Register not empty Interrupts */
01148     SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
01149 
01150     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01151     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01152 
01153     /* Send dummy byte in order to generate the clock for the slave to send data */
01154     husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x01FF);
01155 
01156     return HAL_OK;
01157   }
01158   else
01159   {
01160     return HAL_BUSY;
01161   }
01162 }
01163 
01164 /**
01165   * @brief  Full-Duplex Send and Receive an amount of data in full-duplex mode (non-blocking).
01166   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01167   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
01168   *         of u16 available through pTxData and through pRxData.
01169   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01170   *                 the configuration information for the specified USART module.
01171   * @param  pTxData Pointer to TX data buffer (u8 or u16 data elements).
01172   * @param  pRxData Pointer to RX data buffer (u8 or u16 data elements).
01173   * @param  Size    Amount of data elements (u8 or u16) to be sent (same amount to be received).
01174   * @retval HAL status
01175   */
01176 HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData,  uint16_t Size)
01177 {
01178   if (husart->State == HAL_USART_STATE_READY)
01179   {
01180     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
01181     {
01182       return HAL_ERROR;
01183     }
01184     /* Process Locked */
01185     __HAL_LOCK(husart);
01186 
01187     husart->pRxBuffPtr = pRxData;
01188     husart->RxXferSize = Size;
01189     husart->RxXferCount = Size;
01190     husart->pTxBuffPtr = pTxData;
01191     husart->TxXferSize = Size;
01192     husart->TxXferCount = Size;
01193 
01194     husart->ErrorCode = HAL_USART_ERROR_NONE;
01195     husart->State = HAL_USART_STATE_BUSY_TX_RX;
01196 
01197     /* Process Unlocked */
01198     __HAL_UNLOCK(husart);
01199 
01200     /* Enable the USART Data Register not empty Interrupt */
01201     SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
01202 
01203     /* Enable the USART Parity Error Interrupt */
01204     SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01205 
01206     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01207     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01208 
01209     /* Enable the USART Transmit Data Register Empty Interrupt */
01210     SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
01211 
01212     return HAL_OK;
01213   }
01214   else
01215   {
01216     return HAL_BUSY;
01217   }
01218 }
01219 
01220 /**
01221   * @brief  Simplex Send an amount of data in DMA mode.
01222   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01223   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
01224   *         of u16 provided through pTxData.
01225   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01226   *                 the configuration information for the specified USART module.
01227   * @param  pTxData Pointer to data buffer (u8 or u16 data elements).
01228   * @param  Size    Amount of data elements (u8 or u16) to be sent.
01229   * @retval HAL status
01230   */
01231 HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
01232 {
01233   uint32_t *tmp;
01234 
01235   if (husart->State == HAL_USART_STATE_READY)
01236   {
01237     if ((pTxData == NULL) || (Size == 0))
01238     {
01239       return HAL_ERROR;
01240     }
01241     /* Process Locked */
01242     __HAL_LOCK(husart);
01243 
01244     husart->pTxBuffPtr = pTxData;
01245     husart->TxXferSize = Size;
01246     husart->TxXferCount = Size;
01247 
01248     husart->ErrorCode = HAL_USART_ERROR_NONE;
01249     husart->State = HAL_USART_STATE_BUSY_TX;
01250 
01251     /* Set the USART DMA transfer complete callback */
01252     husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
01253 
01254     /* Set the USART DMA Half transfer complete callback */
01255     husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
01256 
01257     /* Set the DMA error callback */
01258     husart->hdmatx->XferErrorCallback = USART_DMAError;
01259 
01260     /* Set the DMA abort callback */
01261     husart->hdmatx->XferAbortCallback = NULL;
01262 
01263     /* Enable the USART transmit DMA channel */
01264     tmp = (uint32_t *)&pTxData;
01265     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
01266 
01267     /* Clear the TC flag in the SR register by writing 0 to it */
01268     __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
01269 
01270     /* Process Unlocked */
01271     __HAL_UNLOCK(husart);
01272 
01273     /* Enable the DMA transfer for transmit request by setting the DMAT bit
01274     in the USART CR3 register */
01275     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01276 
01277     return HAL_OK;
01278   }
01279   else
01280   {
01281     return HAL_BUSY;
01282   }
01283 }
01284 
01285 /**
01286   * @brief  Full-Duplex Receive an amount of data in DMA mode.
01287   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01288   *         the received data is handled as a set of u16. In this case, Size must indicate the number
01289   *         of u16 available through pRxData.
01290   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01291   *                 the configuration information for the specified USART module.
01292   * @param  pRxData Pointer to data buffer (u8 or u16 data elements).
01293   * @param  Size    Amount of data elements (u8 or u16) to be received.
01294   * @retval HAL status
01295   * @note   The USART DMA transmit channel must be configured in order to generate the clock for the slave.
01296   * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
01297   */
01298 HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
01299 {
01300   uint32_t *tmp;
01301 
01302   if (husart->State == HAL_USART_STATE_READY)
01303   {
01304     if ((pRxData == NULL) || (Size == 0))
01305     {
01306       return HAL_ERROR;
01307     }
01308 
01309     /* Process Locked */
01310     __HAL_LOCK(husart);
01311 
01312     husart->pRxBuffPtr = pRxData;
01313     husart->RxXferSize = Size;
01314     husart->pTxBuffPtr = pRxData;
01315     husart->TxXferSize = Size;
01316 
01317     husart->ErrorCode = HAL_USART_ERROR_NONE;
01318     husart->State = HAL_USART_STATE_BUSY_RX;
01319 
01320     /* Set the USART DMA Rx transfer complete callback */
01321     husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
01322 
01323     /* Set the USART DMA Half transfer complete callback */
01324     husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
01325 
01326     /* Set the USART DMA Rx transfer error callback */
01327     husart->hdmarx->XferErrorCallback = USART_DMAError;
01328 
01329     /* Set the DMA abort callback */
01330     husart->hdmarx->XferAbortCallback = NULL;
01331 
01332     /* Set the USART Tx DMA transfer complete callback as NULL because the communication closing
01333     is performed in DMA reception complete callback  */
01334     husart->hdmatx->XferHalfCpltCallback = NULL;
01335     husart->hdmatx->XferCpltCallback = NULL;
01336 
01337     /* Set the DMA error callback */
01338     husart->hdmatx->XferErrorCallback = USART_DMAError;
01339 
01340     /* Set the DMA AbortCpltCallback */
01341     husart->hdmatx->XferAbortCallback = NULL;
01342 
01343     /* Enable the USART receive DMA channel */
01344     tmp = (uint32_t *)&pRxData;
01345     HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size);
01346 
01347     /* Enable the USART transmit DMA channel: the transmit channel is used in order
01348        to generate in the non-blocking mode the clock to the slave device,
01349        this mode isn't a simplex receive mode but a full-duplex receive one */
01350     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
01351 
01352     /* Clear the Overrun flag just before enabling the DMA Rx request: mandatory for the second transfer */
01353     __HAL_USART_CLEAR_OREFLAG(husart);
01354 
01355     /* Process Unlocked */
01356     __HAL_UNLOCK(husart);
01357 
01358     /* Enable the USART Parity Error Interrupt */
01359     SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01360 
01361     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01362     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01363 
01364     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
01365        in the USART CR3 register */
01366     SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01367 
01368     /* Enable the DMA transfer for transmit request by setting the DMAT bit
01369        in the USART CR3 register */
01370     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01371 
01372     return HAL_OK;
01373   }
01374   else
01375   {
01376     return HAL_BUSY;
01377   }
01378 }
01379 
01380 /**
01381   * @brief  Full-Duplex Transmit Receive an amount of data in DMA mode.
01382   * @note   When USART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
01383   *         the sent data and the received data are handled as sets of u16. In this case, Size must indicate the number
01384   *         of u16 available through pTxData and through pRxData.
01385   * @param  husart  Pointer to a USART_HandleTypeDef structure that contains
01386   *                 the configuration information for the specified USART module.
01387   * @param  pTxData Pointer to TX data buffer (u8 or u16 data elements).
01388   * @param  pRxData Pointer to RX data buffer (u8 or u16 data elements).
01389   * @param  Size    Amount of data elements (u8 or u16) to be received/sent.
01390   * @note   When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
01391   * @retval HAL status
01392   */
01393 HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
01394 {
01395   uint32_t *tmp;
01396 
01397   if (husart->State == HAL_USART_STATE_READY)
01398   {
01399     if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
01400     {
01401       return HAL_ERROR;
01402     }
01403     /* Process Locked */
01404     __HAL_LOCK(husart);
01405 
01406     husart->pRxBuffPtr = pRxData;
01407     husart->RxXferSize = Size;
01408     husart->pTxBuffPtr = pTxData;
01409     husart->TxXferSize = Size;
01410 
01411     husart->ErrorCode = HAL_USART_ERROR_NONE;
01412     husart->State = HAL_USART_STATE_BUSY_TX_RX;
01413 
01414     /* Set the USART DMA Rx transfer complete callback */
01415     husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
01416 
01417     /* Set the USART DMA Half transfer complete callback */
01418     husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
01419 
01420     /* Set the USART DMA Tx transfer complete callback */
01421     husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
01422 
01423     /* Set the USART DMA Half transfer complete callback */
01424     husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
01425 
01426     /* Set the USART DMA Tx transfer error callback */
01427     husart->hdmatx->XferErrorCallback = USART_DMAError;
01428 
01429     /* Set the USART DMA Rx transfer error callback */
01430     husart->hdmarx->XferErrorCallback = USART_DMAError;
01431 
01432     /* Set the DMA abort callback */
01433     husart->hdmarx->XferAbortCallback = NULL;
01434 
01435     /* Enable the USART receive DMA channel */
01436     tmp = (uint32_t *)&pRxData;
01437     HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->DR, *(uint32_t *)tmp, Size);
01438 
01439     /* Enable the USART transmit DMA channel */
01440     tmp = (uint32_t *)&pTxData;
01441     HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t *)tmp, (uint32_t)&husart->Instance->DR, Size);
01442 
01443     /* Clear the TC flag in the SR register by writing 0 to it */
01444     __HAL_USART_CLEAR_FLAG(husart, USART_FLAG_TC);
01445 
01446     /* Clear the Overrun flag: mandatory for the second transfer in circular mode */
01447     __HAL_USART_CLEAR_OREFLAG(husart);
01448 
01449     /* Process Unlocked */
01450     __HAL_UNLOCK(husart);
01451 
01452     /* Enable the USART Parity Error Interrupt */
01453     SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
01454 
01455     /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
01456     SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
01457 
01458     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
01459        in the USART CR3 register */
01460     SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01461 
01462     /* Enable the DMA transfer for transmit request by setting the DMAT bit
01463        in the USART CR3 register */
01464     SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01465 
01466     return HAL_OK;
01467   }
01468   else
01469   {
01470     return HAL_BUSY;
01471   }
01472 }
01473 
01474 /**
01475   * @brief  Pauses the DMA Transfer.
01476   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01477   *                the configuration information for the specified USART module.
01478   * @retval HAL status
01479   */
01480 HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
01481 {
01482   /* Process Locked */
01483   __HAL_LOCK(husart);
01484 
01485   /* Disable the USART DMA Tx request */
01486   CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01487 
01488   /* Process Unlocked */
01489   __HAL_UNLOCK(husart);
01490 
01491   return HAL_OK;
01492 }
01493 
01494 /**
01495   * @brief  Resumes the DMA Transfer.
01496   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01497   *                the configuration information for the specified USART module.
01498   * @retval HAL status
01499   */
01500 HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
01501 {
01502   /* Process Locked */
01503   __HAL_LOCK(husart);
01504 
01505   /* Enable the USART DMA Tx request */
01506   SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01507 
01508   /* Process Unlocked */
01509   __HAL_UNLOCK(husart);
01510 
01511   return HAL_OK;
01512 }
01513 
01514 /**
01515   * @brief  Stops the DMA Transfer.
01516   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01517   *                the configuration information for the specified USART module.
01518   * @retval HAL status
01519   */
01520 HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
01521 {
01522   uint32_t dmarequest = 0x00U;
01523   /* The Lock is not implemented on this API to allow the user application
01524      to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback():
01525      when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
01526      and the correspond call back is executed HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback()
01527      */
01528 
01529   /* Stop USART DMA Tx request if ongoing */
01530   dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
01531   if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
01532   {
01533     USART_EndTxTransfer(husart);
01534 
01535     /* Abort the USART DMA Tx channel */
01536     if (husart->hdmatx != NULL)
01537     {
01538       HAL_DMA_Abort(husart->hdmatx);
01539     }
01540 
01541     /* Disable the USART Tx DMA request */
01542     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01543   }
01544 
01545   /* Stop USART DMA Rx request if ongoing */
01546   dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
01547   if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
01548   {
01549     USART_EndRxTransfer(husart);
01550 
01551     /* Abort the USART DMA Rx channel */
01552     if (husart->hdmarx != NULL)
01553     {
01554       HAL_DMA_Abort(husart->hdmarx);
01555     }
01556 
01557     /* Disable the USART Rx DMA request */
01558     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01559   }
01560 
01561   return HAL_OK;
01562 }
01563 
01564 /**
01565   * @brief  Abort ongoing transfer (blocking mode).
01566   * @param  husart USART handle.
01567   * @note   This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
01568   *         as described by TransferType parameter) started in Interrupt or DMA mode.
01569   *         This procedure performs following operations :
01570   *           - Disable PPP Interrupts (depending of transfer direction)
01571   *           - Disable the DMA transfer in the peripheral register (if enabled)
01572   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
01573   *           - Set handle State to READY
01574   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
01575   * @retval HAL status
01576 */
01577 HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
01578 {
01579   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01580   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
01581   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
01582 
01583   /* Disable the USART DMA Tx request if enabled */
01584   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
01585   {
01586     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01587 
01588     /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
01589     if (husart->hdmatx != NULL)
01590     {
01591       /* Set the USART DMA Abort callback to Null.
01592          No call back execution at end of DMA abort procedure */
01593       husart->hdmatx->XferAbortCallback = NULL;
01594 
01595       HAL_DMA_Abort(husart->hdmatx);
01596     }
01597   }
01598 
01599   /* Disable the USART DMA Rx request if enabled */
01600   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01601   {
01602     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01603 
01604     /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
01605     if (husart->hdmarx != NULL)
01606     {
01607       /* Set the USART DMA Abort callback to Null.
01608          No call back execution at end of DMA abort procedure */
01609       husart->hdmarx->XferAbortCallback = NULL;
01610 
01611       HAL_DMA_Abort(husart->hdmarx);
01612     }
01613   }
01614 
01615   /* Reset Tx and Rx transfer counters */
01616   husart->TxXferCount = 0x00U;
01617   husart->RxXferCount = 0x00U;
01618 
01619   /* Restore husart->State to Ready */
01620   husart->State  = HAL_USART_STATE_READY;
01621 
01622   /* Reset Handle ErrorCode to No Error */
01623   husart->ErrorCode = HAL_USART_ERROR_NONE;
01624 
01625   return HAL_OK;
01626 }
01627 
01628 /**
01629   * @brief  Abort ongoing transfer (Interrupt mode).
01630   * @param  husart USART handle.
01631   * @note   This procedure could be used for aborting any ongoing transfer (either Tx or Rx,
01632   *         as described by TransferType parameter) started in Interrupt or DMA mode.
01633   *         This procedure performs following operations :
01634   *           - Disable PPP Interrupts (depending of transfer direction)
01635   *           - Disable the DMA transfer in the peripheral register (if enabled)
01636   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
01637   *           - Set handle State to READY
01638   *           - At abort completion, call user abort complete callback
01639   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
01640   *         considered as completed only when user abort complete callback is executed (not when exiting function).
01641   * @retval HAL status
01642 */
01643 HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
01644 {
01645   uint32_t AbortCplt = 0x01U;
01646 
01647   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
01648   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
01649   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
01650 
01651   /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
01652      before any call to DMA Abort functions */
01653   /* DMA Tx Handle is valid */
01654   if (husart->hdmatx != NULL)
01655   {
01656     /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
01657        Otherwise, set it to NULL */
01658     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
01659     {
01660       husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
01661     }
01662     else
01663     {
01664       husart->hdmatx->XferAbortCallback = NULL;
01665     }
01666   }
01667   /* DMA Rx Handle is valid */
01668   if (husart->hdmarx != NULL)
01669   {
01670     /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
01671        Otherwise, set it to NULL */
01672     if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01673     {
01674       husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
01675     }
01676     else
01677     {
01678       husart->hdmarx->XferAbortCallback = NULL;
01679     }
01680   }
01681 
01682   /* Disable the USART DMA Tx request if enabled */
01683   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
01684   {
01685     /* Disable DMA Tx at USART level */
01686     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
01687 
01688     /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
01689     if (husart->hdmatx != NULL)
01690     {
01691       /* USART Tx DMA Abort callback has already been initialised :
01692          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
01693 
01694       /* Abort DMA TX */
01695       if (HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
01696       {
01697         husart->hdmatx->XferAbortCallback = NULL;
01698       }
01699       else
01700       {
01701         AbortCplt = 0x00U;
01702       }
01703     }
01704   }
01705 
01706   /* Disable the USART DMA Rx request if enabled */
01707   if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01708   {
01709     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01710 
01711     /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
01712     if (husart->hdmarx != NULL)
01713     {
01714       /* USART Rx DMA Abort callback has already been initialised :
01715          will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
01716 
01717       /* Abort DMA RX */
01718       if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
01719       {
01720         husart->hdmarx->XferAbortCallback = NULL;
01721         AbortCplt = 0x01U;
01722       }
01723       else
01724       {
01725         AbortCplt = 0x00U;
01726       }
01727     }
01728   }
01729 
01730   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
01731   if (AbortCplt  == 0x01U)
01732   {
01733     /* Reset Tx and Rx transfer counters */
01734     husart->TxXferCount = 0x00U;
01735     husart->RxXferCount = 0x00U;
01736 
01737     /* Reset errorCode */
01738     husart->ErrorCode = HAL_USART_ERROR_NONE;
01739 
01740     /* Restore husart->State to Ready */
01741     husart->State  = HAL_USART_STATE_READY;
01742 
01743     /* As no DMA to be aborted, call directly user Abort complete callback */
01744 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
01745     /* Call registered Abort Complete Callback */
01746     husart->AbortCpltCallback(husart);
01747 #else
01748     /* Call legacy weak Abort Complete Callback */
01749     HAL_USART_AbortCpltCallback(husart);
01750 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
01751   }
01752 
01753   return HAL_OK;
01754 }
01755 
01756 /**
01757   * @brief  This function handles USART interrupt request.
01758   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01759   *                the configuration information for the specified USART module.
01760   * @retval None
01761   */
01762 void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
01763 {
01764   uint32_t isrflags = READ_REG(husart->Instance->SR);
01765   uint32_t cr1its   = READ_REG(husart->Instance->CR1);
01766   uint32_t cr3its   = READ_REG(husart->Instance->CR3);
01767   uint32_t errorflags = 0x00U;
01768   uint32_t dmarequest = 0x00U;
01769 
01770   /* If no error occurs */
01771   errorflags = (isrflags & (uint32_t)(USART_SR_PE | USART_SR_FE | USART_SR_ORE | USART_SR_NE));
01772   if (errorflags == RESET)
01773   {
01774     /* USART in mode Receiver -------------------------------------------------*/
01775     if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
01776     {
01777       if (husart->State == HAL_USART_STATE_BUSY_RX)
01778       {
01779         USART_Receive_IT(husart);
01780       }
01781       else
01782       {
01783         USART_TransmitReceive_IT(husart);
01784       }
01785       return;
01786     }
01787   }
01788   /* If some errors occur */
01789   if ((errorflags != RESET) && (((cr3its & USART_CR3_EIE) != RESET) || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
01790   {
01791     /* USART parity error interrupt occurred ----------------------------------*/
01792     if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
01793     {
01794       husart->ErrorCode |= HAL_USART_ERROR_PE;
01795     }
01796 
01797     /* USART noise error interrupt occurred --------------------------------*/
01798     if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
01799     {
01800       husart->ErrorCode |= HAL_USART_ERROR_NE;
01801     }
01802 
01803     /* USART frame error interrupt occurred --------------------------------*/
01804     if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
01805     {
01806       husart->ErrorCode |= HAL_USART_ERROR_FE;
01807     }
01808 
01809     /* USART Over-Run interrupt occurred -----------------------------------*/
01810     if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
01811     {
01812       husart->ErrorCode |= HAL_USART_ERROR_ORE;
01813     }
01814 
01815     if (husart->ErrorCode != HAL_USART_ERROR_NONE)
01816     {
01817       /* USART in mode Receiver -----------------------------------------------*/
01818       if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
01819       {
01820         if (husart->State == HAL_USART_STATE_BUSY_RX)
01821         {
01822           USART_Receive_IT(husart);
01823         }
01824         else
01825         {
01826           USART_TransmitReceive_IT(husart);
01827         }
01828       }
01829       /* If Overrun error occurs, or if any error occurs in DMA mode reception,
01830       consider error as blocking */
01831       dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
01832       if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) || dmarequest)
01833       {
01834         /* Set the USART state ready to be able to start again the process,
01835         Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
01836         USART_EndRxTransfer(husart);
01837 
01838         /* Disable the USART DMA Rx request if enabled */
01839         if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
01840         {
01841           CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
01842 
01843           /* Abort the USART DMA Rx channel */
01844           if (husart->hdmarx != NULL)
01845           {
01846             /* Set the USART DMA Abort callback :
01847             will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
01848             husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
01849 
01850             if (HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
01851             {
01852               /* Call Directly XferAbortCallback function in case of error */
01853               husart->hdmarx->XferAbortCallback(husart->hdmarx);
01854             }
01855           }
01856           else
01857           {
01858             /* Call user error callback */
01859 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
01860             /* Call registered Error Callback */
01861             husart->ErrorCallback(husart);
01862 #else
01863             /* Call legacy weak Error Callback */
01864             HAL_USART_ErrorCallback(husart);
01865 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
01866           }
01867         }
01868         else
01869         {
01870           /* Call user error callback */
01871 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
01872           /* Call registered Error Callback */
01873           husart->ErrorCallback(husart);
01874 #else
01875           /* Call legacy weak Error Callback */
01876           HAL_USART_ErrorCallback(husart);
01877 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
01878         }
01879       }
01880       else
01881       {
01882         /* Call user error callback */
01883 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
01884         /* Call registered Error Callback */
01885         husart->ErrorCallback(husart);
01886 #else
01887         /* Call legacy weak Error Callback */
01888         HAL_USART_ErrorCallback(husart);
01889 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
01890         husart->ErrorCode = HAL_USART_ERROR_NONE;
01891       }
01892     }
01893     return;
01894   }
01895 
01896   /* USART in mode Transmitter -----------------------------------------------*/
01897   if (((isrflags & USART_SR_TXE) != RESET) && ((cr1its & USART_CR1_TXEIE) != RESET))
01898   {
01899     if (husart->State == HAL_USART_STATE_BUSY_TX)
01900     {
01901       USART_Transmit_IT(husart);
01902     }
01903     else
01904     {
01905       USART_TransmitReceive_IT(husart);
01906     }
01907     return;
01908   }
01909 
01910   /* USART in mode Transmitter (transmission end) ----------------------------*/
01911   if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
01912   {
01913     USART_EndTransmit_IT(husart);
01914     return;
01915   }
01916 }
01917 
01918 /**
01919   * @brief  Tx Transfer completed callbacks.
01920   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01921   *                the configuration information for the specified USART module.
01922   * @retval None
01923   */
01924 __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
01925 {
01926   /* Prevent unused argument(s) compilation warning */
01927   UNUSED(husart);
01928   /* NOTE: This function should not be modified, when the callback is needed,
01929            the HAL_USART_TxCpltCallback could be implemented in the user file
01930    */
01931 }
01932 
01933 /**
01934   * @brief  Tx Half Transfer completed callbacks.
01935   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01936   *                the configuration information for the specified USART module.
01937   * @retval None
01938   */
01939 __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
01940 {
01941   /* Prevent unused argument(s) compilation warning */
01942   UNUSED(husart);
01943   /* NOTE: This function should not be modified, when the callback is needed,
01944            the HAL_USART_TxHalfCpltCallback could be implemented in the user file
01945    */
01946 }
01947 
01948 /**
01949   * @brief  Rx Transfer completed callbacks.
01950   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01951   *                the configuration information for the specified USART module.
01952   * @retval None
01953   */
01954 __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
01955 {
01956   /* Prevent unused argument(s) compilation warning */
01957   UNUSED(husart);
01958   /* NOTE: This function should not be modified, when the callback is needed,
01959            the HAL_USART_RxCpltCallback could be implemented in the user file
01960    */
01961 }
01962 
01963 /**
01964   * @brief  Rx Half Transfer completed callbacks.
01965   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01966   *                the configuration information for the specified USART module.
01967   * @retval None
01968   */
01969 __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
01970 {
01971   /* Prevent unused argument(s) compilation warning */
01972   UNUSED(husart);
01973   /* NOTE: This function should not be modified, when the callback is needed,
01974            the HAL_USART_RxHalfCpltCallback could be implemented in the user file
01975    */
01976 }
01977 
01978 /**
01979   * @brief  Tx/Rx Transfers completed callback for the non-blocking process.
01980   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01981   *                the configuration information for the specified USART module.
01982   * @retval None
01983   */
01984 __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
01985 {
01986   /* Prevent unused argument(s) compilation warning */
01987   UNUSED(husart);
01988   /* NOTE: This function should not be modified, when the callback is needed,
01989            the HAL_USART_TxRxCpltCallback could be implemented in the user file
01990    */
01991 }
01992 
01993 /**
01994   * @brief  USART error callbacks.
01995   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
01996   *                the configuration information for the specified USART module.
01997   * @retval None
01998   */
01999 __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
02000 {
02001   /* Prevent unused argument(s) compilation warning */
02002   UNUSED(husart);
02003   /* NOTE: This function should not be modified, when the callback is needed,
02004            the HAL_USART_ErrorCallback could be implemented in the user file
02005    */
02006 }
02007 
02008 /**
02009   * @brief  USART Abort Complete callback.
02010   * @param  husart USART handle.
02011   * @retval None
02012   */
02013 __weak void HAL_USART_AbortCpltCallback(USART_HandleTypeDef *husart)
02014 {
02015   /* Prevent unused argument(s) compilation warning */
02016   UNUSED(husart);
02017 
02018   /* NOTE : This function should not be modified, when the callback is needed,
02019             the HAL_USART_AbortCpltCallback can be implemented in the user file.
02020    */
02021 }
02022 
02023 /**
02024   * @}
02025   */
02026 
02027 /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
02028   *  @brief   USART State and Errors functions
02029   *
02030 @verbatim
02031   ==============================================================================
02032                   ##### Peripheral State and Errors functions #####
02033   ==============================================================================
02034   [..]
02035     This subsection provides a set of functions allowing to return the State of
02036     USART communication
02037     process, return Peripheral Errors occurred during communication process
02038      (+) HAL_USART_GetState() API can be helpful to check in run-time the state
02039          of the USART peripheral.
02040      (+) HAL_USART_GetError() check in run-time errors that could be occurred during
02041          communication.
02042 @endverbatim
02043   * @{
02044   */
02045 
02046 /**
02047   * @brief  Returns the USART state.
02048   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02049   *                the configuration information for the specified USART module.
02050   * @retval HAL state
02051   */
02052 HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
02053 {
02054   return husart->State;
02055 }
02056 
02057 /**
02058   * @brief  Return the USART error code
02059   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02060   *                the configuration information for the specified USART.
02061   * @retval USART Error Code
02062   */
02063 uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
02064 {
02065   return husart->ErrorCode;
02066 }
02067 
02068 /**
02069   * @}
02070   */
02071 
02072 /** @defgroup USART_Private_Functions USART Private Functions
02073  * @{
02074  */
02075 
02076 /**
02077   * @brief  Initialize the callbacks to their default values.
02078   * @param  husart USART handle.
02079   * @retval none
02080   */
02081 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02082 void USART_InitCallbacksToDefault(USART_HandleTypeDef *husart)
02083 {
02084   /* Init the USART Callback settings */
02085   husart->TxHalfCpltCallback        = HAL_USART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
02086   husart->TxCpltCallback            = HAL_USART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
02087   husart->RxHalfCpltCallback        = HAL_USART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
02088   husart->RxCpltCallback            = HAL_USART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
02089   husart->TxRxCpltCallback          = HAL_USART_TxRxCpltCallback;          /* Legacy weak TxRxCpltCallback          */
02090   husart->ErrorCallback             = HAL_USART_ErrorCallback;             /* Legacy weak ErrorCallback             */
02091   husart->AbortCpltCallback         = HAL_USART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
02092 }
02093 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02094 
02095 /**
02096   * @brief  DMA USART transmit process complete callback.
02097   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
02098   *              the configuration information for the specified DMA module.
02099   * @retval None
02100   */
02101 static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
02102 {
02103   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02104   /* DMA Normal mode */
02105   if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
02106   {
02107     husart->TxXferCount = 0U;
02108     if (husart->State == HAL_USART_STATE_BUSY_TX)
02109     {
02110       /* Disable the DMA transfer for transmit request by resetting the DMAT bit
02111          in the USART CR3 register */
02112       CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
02113 
02114       /* Enable the USART Transmit Complete Interrupt */
02115       SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
02116     }
02117   }
02118   /* DMA Circular mode */
02119   else
02120   {
02121     if (husart->State == HAL_USART_STATE_BUSY_TX)
02122     {
02123 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02124       /* Call registered Tx Complete Callback */
02125       husart->TxCpltCallback(husart);
02126 #else
02127       /* Call legacy weak Tx Complete Callback */
02128       HAL_USART_TxCpltCallback(husart);
02129 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02130     }
02131   }
02132 }
02133 
02134 /**
02135   * @brief  DMA USART transmit process half complete callback
02136   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
02137   *              the configuration information for the specified DMA module.
02138   * @retval None
02139   */
02140 static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
02141 {
02142   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02143 
02144 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02145   /* Call registered Tx Half Complete Callback */
02146   husart->TxHalfCpltCallback(husart);
02147 #else
02148   /* Call legacy weak Tx Half Complete Callback */
02149   HAL_USART_TxHalfCpltCallback(husart);
02150 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02151 }
02152 
02153 /**
02154   * @brief  DMA USART receive process complete callback.
02155   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
02156   *              the configuration information for the specified DMA module.
02157   * @retval None
02158   */
02159 static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
02160 {
02161   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02162   /* DMA Normal mode */
02163   if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
02164   {
02165     husart->RxXferCount = 0x00U;
02166 
02167     /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
02168     CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
02169     CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02170 
02171     /* Disable the DMA transfer for the Transmit/receiver request by clearing the DMAT/DMAR bit
02172          in the USART CR3 register */
02173     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
02174     CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
02175 
02176     /* The USART state is HAL_USART_STATE_BUSY_RX */
02177     if (husart->State == HAL_USART_STATE_BUSY_RX)
02178     {
02179 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02180       /* Call registered Rx Complete Callback */
02181       husart->RxCpltCallback(husart);
02182 #else
02183       /* Call legacy weak Rx Complete Callback */
02184       HAL_USART_RxCpltCallback(husart);
02185 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02186     }
02187     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
02188     else
02189     {
02190 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02191       /* Call registered Tx Rx Complete Callback */
02192       husart->TxRxCpltCallback(husart);
02193 #else
02194       /* Call legacy weak Tx Rx Complete Callback */
02195       HAL_USART_TxRxCpltCallback(husart);
02196 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02197     }
02198     husart->State = HAL_USART_STATE_READY;
02199   }
02200   /* DMA circular mode */
02201   else
02202   {
02203     if (husart->State == HAL_USART_STATE_BUSY_RX)
02204     {
02205 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02206       /* Call registered Rx Complete Callback */
02207       husart->RxCpltCallback(husart);
02208 #else
02209       /* Call legacy weak Rx Complete Callback */
02210       HAL_USART_RxCpltCallback(husart);
02211 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02212     }
02213     /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
02214     else
02215     {
02216 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02217       /* Call registered Tx Rx Complete Callback */
02218       husart->TxRxCpltCallback(husart);
02219 #else
02220       /* Call legacy weak Tx Rx Complete Callback */
02221       HAL_USART_TxRxCpltCallback(husart);
02222 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02223     }
02224   }
02225 }
02226 
02227 /**
02228   * @brief  DMA USART receive process half complete callback
02229   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
02230   *              the configuration information for the specified DMA module.
02231   * @retval None
02232   */
02233 static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
02234 {
02235   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02236 
02237 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02238   /* Call registered Rx Half Complete Callback */
02239   husart->RxHalfCpltCallback(husart);
02240 #else
02241   /* Call legacy weak Rx Half Complete Callback */
02242   HAL_USART_RxHalfCpltCallback(husart);
02243 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02244 }
02245 
02246 /**
02247   * @brief  DMA USART communication error callback.
02248   * @param  hdma Pointer to a DMA_HandleTypeDef structure that contains
02249   *              the configuration information for the specified DMA module.
02250   * @retval None
02251   */
02252 static void USART_DMAError(DMA_HandleTypeDef *hdma)
02253 {
02254   uint32_t dmarequest = 0x00U;
02255   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02256   husart->RxXferCount = 0x00U;
02257   husart->TxXferCount = 0x00U;
02258 
02259   /* Stop USART DMA Tx request if ongoing */
02260   dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT);
02261   if ((husart->State == HAL_USART_STATE_BUSY_TX) && dmarequest)
02262   {
02263     USART_EndTxTransfer(husart);
02264   }
02265 
02266   /* Stop USART DMA Rx request if ongoing */
02267   dmarequest = HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR);
02268   if ((husart->State == HAL_USART_STATE_BUSY_RX) && dmarequest)
02269   {
02270     USART_EndRxTransfer(husart);
02271   }
02272 
02273   husart->ErrorCode |= HAL_USART_ERROR_DMA;
02274   husart->State = HAL_USART_STATE_READY;
02275 
02276 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02277   /* Call registered Error Callback */
02278   husart->ErrorCallback(husart);
02279 #else
02280   /* Call legacy weak Error Callback */
02281   HAL_USART_ErrorCallback(husart);
02282 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02283 }
02284 
02285 /**
02286   * @brief  This function handles USART Communication Timeout.
02287   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02288   *                the configuration information for the specified USART module.
02289   * @param  Flag specifies the USART flag to check.
02290   * @param  Status The new Flag status (SET or RESET).
02291   * @param  Tickstart Tick start value.
02292   * @param  Timeout Timeout duration.
02293   * @retval HAL status
02294   */
02295 static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
02296 {
02297   /* Wait until flag is set */
02298   while ((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
02299   {
02300     /* Check for the Timeout */
02301     if (Timeout != HAL_MAX_DELAY)
02302     {
02303       if ((Timeout == 0U) || ((HAL_GetTick() - Tickstart) > Timeout))
02304       {
02305         /* Disable the USART Transmit Complete Interrupt */
02306         CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
02307 
02308         /* Disable the USART RXNE Interrupt */
02309         CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
02310 
02311         /* Disable the USART Parity Error Interrupt */
02312         CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
02313 
02314         /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
02315         CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02316 
02317         husart->State = HAL_USART_STATE_READY;
02318 
02319         /* Process Unlocked */
02320         __HAL_UNLOCK(husart);
02321 
02322         return HAL_TIMEOUT;
02323       }
02324     }
02325   }
02326   return HAL_OK;
02327 }
02328 
02329 /**
02330   * @brief  End ongoing Tx transfer on USART peripheral (following error detection or Transmit completion).
02331   * @param  husart USART handle.
02332   * @retval None
02333   */
02334 static void USART_EndTxTransfer(USART_HandleTypeDef *husart)
02335 {
02336   /* Disable TXEIE and TCIE interrupts */
02337   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
02338 
02339   /* At end of Tx process, restore husart->State to Ready */
02340   husart->State = HAL_USART_STATE_READY;
02341 }
02342 
02343 /**
02344   * @brief  End ongoing Rx transfer on USART peripheral (following error detection or Reception completion).
02345   * @param  husart USART handle.
02346   * @retval None
02347   */
02348 static void USART_EndRxTransfer(USART_HandleTypeDef *husart)
02349 {
02350   /* Disable RXNE, PE and ERR interrupts */
02351   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
02352   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02353 
02354   /* At end of Rx process, restore husart->State to Ready */
02355   husart->State = HAL_USART_STATE_READY;
02356 }
02357 
02358 /**
02359   * @brief  DMA USART communication abort callback, when initiated by HAL services on Error
02360   *         (To be called at end of DMA Abort procedure following error occurrence).
02361   * @param  hdma DMA handle.
02362   * @retval None
02363   */
02364 static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
02365 {
02366   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02367   husart->RxXferCount = 0x00U;
02368   husart->TxXferCount = 0x00U;
02369 
02370 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02371   /* Call registered Error Callback */
02372   husart->ErrorCallback(husart);
02373 #else
02374   /* Call legacy weak Error Callback */
02375   HAL_USART_ErrorCallback(husart);
02376 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02377 }
02378 
02379 /**
02380   * @brief  DMA USART Tx communication abort callback, when initiated by user
02381   *         (To be called at end of DMA Tx Abort procedure following user abort request).
02382   * @note   When this callback is executed, User Abort complete call back is called only if no
02383   *         Abort still ongoing for Rx DMA Handle.
02384   * @param  hdma DMA handle.
02385   * @retval None
02386   */
02387 static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
02388 {
02389   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02390 
02391   husart->hdmatx->XferAbortCallback = NULL;
02392 
02393   /* Check if an Abort process is still ongoing */
02394   if (husart->hdmarx != NULL)
02395   {
02396     if (husart->hdmarx->XferAbortCallback != NULL)
02397     {
02398       return;
02399     }
02400   }
02401 
02402   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02403   husart->TxXferCount = 0x00U;
02404   husart->RxXferCount = 0x00U;
02405 
02406   /* Reset errorCode */
02407   husart->ErrorCode = HAL_USART_ERROR_NONE;
02408 
02409   /* Restore husart->State to Ready */
02410   husart->State  = HAL_USART_STATE_READY;
02411 
02412   /* Call user Abort complete callback */
02413 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02414   /* Call registered Abort Complete Callback */
02415   husart->AbortCpltCallback(husart);
02416 #else
02417   /* Call legacy weak Abort Complete Callback */
02418   HAL_USART_AbortCpltCallback(husart);
02419 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02420 }
02421 
02422 /**
02423   * @brief  DMA USART Rx communication abort callback, when initiated by user
02424   *         (To be called at end of DMA Rx Abort procedure following user abort request).
02425   * @note   When this callback is executed, User Abort complete call back is called only if no
02426   *         Abort still ongoing for Tx DMA Handle.
02427   * @param  hdma DMA handle.
02428   * @retval None
02429   */
02430 static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
02431 {
02432   USART_HandleTypeDef *husart = (USART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
02433 
02434   husart->hdmarx->XferAbortCallback = NULL;
02435 
02436   /* Check if an Abort process is still ongoing */
02437   if (husart->hdmatx != NULL)
02438   {
02439     if (husart->hdmatx->XferAbortCallback != NULL)
02440     {
02441       return;
02442     }
02443   }
02444 
02445   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
02446   husart->TxXferCount = 0x00U;
02447   husart->RxXferCount = 0x00U;
02448 
02449   /* Reset errorCode */
02450   husart->ErrorCode = HAL_USART_ERROR_NONE;
02451 
02452   /* Restore husart->State to Ready */
02453   husart->State  = HAL_USART_STATE_READY;
02454 
02455   /* Call user Abort complete callback */
02456 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02457   /* Call registered Abort Complete Callback */
02458   husart->AbortCpltCallback(husart);
02459 #else
02460   /* Call legacy weak Abort Complete Callback */
02461   HAL_USART_AbortCpltCallback(husart);
02462 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02463 }
02464 
02465 /**
02466   * @brief  Simplex Send an amount of data in non-blocking mode.
02467   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02468   *                the configuration information for the specified USART module.
02469   * @retval HAL status
02470   * @note   The USART errors are not managed to avoid the overrun error.
02471   */
02472 static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
02473 {
02474   uint16_t *tmp;
02475 
02476   if (husart->State == HAL_USART_STATE_BUSY_TX)
02477   {
02478     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
02479     {
02480       tmp = (uint16_t *) husart->pTxBuffPtr;
02481       husart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
02482       husart->pTxBuffPtr += 2U;
02483     }
02484     else
02485     {
02486       husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
02487     }
02488 
02489     if (--husart->TxXferCount == 0U)
02490     {
02491       /* Disable the USART Transmit data register empty Interrupt */
02492       CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
02493 
02494       /* Enable the USART Transmit Complete Interrupt */
02495       SET_BIT(husart->Instance->CR1, USART_CR1_TCIE);
02496     }
02497     return HAL_OK;
02498   }
02499   else
02500   {
02501     return HAL_BUSY;
02502   }
02503 }
02504 
02505 /**
02506   * @brief  Wraps up transmission in non blocking mode.
02507   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02508   *                the configuration information for the specified USART module.
02509   * @retval HAL status
02510   */
02511 static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
02512 {
02513   /* Disable the USART Transmit Complete Interrupt */
02514   CLEAR_BIT(husart->Instance->CR1, USART_CR1_TCIE);
02515 
02516   /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
02517   CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02518 
02519   husart->State = HAL_USART_STATE_READY;
02520 
02521 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02522   /* Call registered Tx Complete Callback */
02523   husart->TxCpltCallback(husart);
02524 #else
02525   /* Call legacy weak Tx Complete Callback */
02526   HAL_USART_TxCpltCallback(husart);
02527 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02528 
02529   return HAL_OK;
02530 }
02531 
02532 /**
02533   * @brief  Simplex Receive an amount of data in non-blocking mode.
02534   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02535   *                the configuration information for the specified USART module.
02536   * @retval HAL status
02537   */
02538 static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
02539 {
02540   uint8_t  *pdata8bits;
02541   uint16_t *pdata16bits;
02542 
02543   if (husart->State == HAL_USART_STATE_BUSY_RX)
02544   {
02545     if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
02546     {
02547       pdata8bits  = NULL;
02548       pdata16bits = (uint16_t *) husart->pRxBuffPtr;
02549       *pdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
02550       husart->pRxBuffPtr += 2U;
02551     }
02552     else
02553     {
02554       pdata8bits = (uint8_t *) husart->pRxBuffPtr;
02555       pdata16bits  = NULL;
02556 
02557       if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
02558       {
02559         *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
02560       }
02561       else
02562       {
02563         *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
02564       }
02565 
02566       husart->pRxBuffPtr += 1U;
02567     }
02568 
02569     husart->RxXferCount--;
02570 
02571     if (husart->RxXferCount == 0U)
02572     {
02573       /* Disable the USART RXNE Interrupt */
02574       CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
02575 
02576       /* Disable the USART Parity Error Interrupt */
02577       CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
02578 
02579       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
02580       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02581 
02582       husart->State = HAL_USART_STATE_READY;
02583 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02584       /* Call registered Rx Complete Callback */
02585       husart->RxCpltCallback(husart);
02586 #else
02587       /* Call legacy weak Rx Complete Callback */
02588       HAL_USART_RxCpltCallback(husart);
02589 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02590 
02591       return HAL_OK;
02592     }
02593     else
02594     {
02595       /* Send dummy byte in order to generate the clock for the slave to send the next data.
02596       * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
02597       * can be written for all the cases. */
02598       husart->Instance->DR = (DUMMY_DATA & (uint16_t)0x0FF);
02599     }
02600     return HAL_OK;
02601   }
02602   else
02603   {
02604     return HAL_BUSY;
02605   }
02606 }
02607 
02608 /**
02609   * @brief  Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
02610   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02611   *                the configuration information for the specified USART module.
02612   * @retval HAL status
02613   */
02614 static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
02615 {
02616   uint8_t  *pdata8bits;
02617   uint16_t *pdata16bits;
02618 
02619   if (husart->State == HAL_USART_STATE_BUSY_TX_RX)
02620   {
02621     if (husart->TxXferCount != 0x00U)
02622     {
02623       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
02624       {
02625         if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
02626         {
02627           pdata8bits  = NULL;
02628           pdata16bits = (uint16_t *) husart->pTxBuffPtr;
02629           husart->Instance->DR = (uint16_t)(*pdata16bits & (uint16_t)0x01FF);
02630           husart->pTxBuffPtr += 2U;
02631         }
02632         else
02633         {
02634           husart->Instance->DR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0x00FF);
02635         }
02636 
02637         husart->TxXferCount--;
02638 
02639         /* Check the latest data transmitted */
02640         if (husart->TxXferCount == 0U)
02641         {
02642           CLEAR_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
02643         }
02644       }
02645     }
02646 
02647     if (husart->RxXferCount != 0x00U)
02648     {
02649       if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
02650       {
02651         if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
02652         {
02653            pdata8bits  = NULL;
02654            pdata16bits = (uint16_t *) husart->pRxBuffPtr;
02655            *pdata16bits = (uint16_t)(husart->Instance->DR & (uint16_t)0x01FF);
02656            husart->pRxBuffPtr += 2U;
02657         }
02658         else
02659         {
02660           pdata8bits = (uint8_t *) husart->pRxBuffPtr;
02661           pdata16bits  = NULL;
02662           if ((husart->Init.WordLength == USART_WORDLENGTH_9B) || ((husart->Init.WordLength == USART_WORDLENGTH_8B) && (husart->Init.Parity == USART_PARITY_NONE)))
02663           {
02664             *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x00FF);
02665           }
02666           else
02667           {
02668             *pdata8bits = (uint8_t)(husart->Instance->DR & (uint8_t)0x007F);
02669           }
02670           husart->pRxBuffPtr += 1U;
02671         }
02672 
02673         husart->RxXferCount--;
02674       }
02675     }
02676 
02677     /* Check the latest data received */
02678     if (husart->RxXferCount == 0U)
02679     {
02680       /* Disable the USART RXNE Interrupt */
02681       CLEAR_BIT(husart->Instance->CR1, USART_CR1_RXNEIE);
02682 
02683       /* Disable the USART Parity Error Interrupt */
02684       CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
02685 
02686       /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
02687       CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
02688 
02689       husart->State = HAL_USART_STATE_READY;
02690 
02691 #if (USE_HAL_USART_REGISTER_CALLBACKS == 1)
02692       /* Call registered Tx Rx Complete Callback */
02693       husart->TxRxCpltCallback(husart);
02694 #else
02695       /* Call legacy weak Tx Rx Complete Callback */
02696       HAL_USART_TxRxCpltCallback(husart);
02697 #endif /* USE_HAL_USART_REGISTER_CALLBACKS */
02698 
02699       return HAL_OK;
02700     }
02701 
02702     return HAL_OK;
02703   }
02704   else
02705   {
02706     return HAL_BUSY;
02707   }
02708 }
02709 
02710 /**
02711   * @brief  Configures the USART peripheral.
02712   * @param  husart Pointer to a USART_HandleTypeDef structure that contains
02713   *                the configuration information for the specified USART module.
02714   * @retval None
02715   */
02716 static void USART_SetConfig(USART_HandleTypeDef *husart)
02717 {
02718   uint32_t tmpreg = 0x00U;
02719   uint32_t pclk;
02720 
02721   /* Check the parameters */
02722   assert_param(IS_USART_INSTANCE(husart->Instance));
02723   assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
02724   assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
02725   assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
02726   assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
02727   assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
02728   assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
02729   assert_param(IS_USART_PARITY(husart->Init.Parity));
02730   assert_param(IS_USART_MODE(husart->Init.Mode));
02731 
02732   /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
02733      receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
02734   CLEAR_BIT(husart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
02735 
02736   /*---------------------------- USART CR2 Configuration ---------------------*/
02737   tmpreg = husart->Instance->CR2;
02738   /* Clear CLKEN, CPOL, CPHA and LBCL bits */
02739   tmpreg &= (uint32_t)~((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP));
02740   /* Configure the USART Clock, CPOL, CPHA and LastBit -----------------------*/
02741   /* Set CPOL bit according to husart->Init.CLKPolarity value */
02742   /* Set CPHA bit according to husart->Init.CLKPhase value */
02743   /* Set LBCL bit according to husart->Init.CLKLastBit value */
02744   /* Set Stop Bits: Set STOP[13:12] bits according to husart->Init.StopBits value */
02745   tmpreg |= (uint32_t)(USART_CLOCK_ENABLE | husart->Init.CLKPolarity |
02746                        husart->Init.CLKPhase | husart->Init.CLKLastBit | husart->Init.StopBits);
02747   /* Write to USART CR2 */
02748   WRITE_REG(husart->Instance->CR2, (uint32_t)tmpreg);
02749 
02750   /*-------------------------- USART CR1 Configuration -----------------------*/
02751   tmpreg = husart->Instance->CR1;
02752 
02753   /* Clear M, PCE, PS, TE and RE bits */
02754   tmpreg &= (uint32_t)~((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE));
02755 
02756   /* Configure the USART Word Length, Parity and mode:
02757      Set the M bits according to husart->Init.WordLength value
02758      Set PCE and PS bits according to husart->Init.Parity value
02759      Set TE and RE bits according to husart->Init.Mode value
02760    */
02761   tmpreg |= (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode;
02762 
02763   /* Write to USART CR1 */
02764   WRITE_REG(husart->Instance->CR1, (uint32_t)tmpreg);
02765 
02766   /*-------------------------- USART CR3 Configuration -----------------------*/
02767   /* Clear CTSE and RTSE bits */
02768   CLEAR_BIT(husart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
02769 
02770   /*-------------------------- USART BRR Configuration -----------------------*/
02771   if((husart->Instance == USART1))
02772   {
02773     pclk = HAL_RCC_GetPCLK2Freq();
02774     husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
02775   }
02776   else
02777   {
02778     pclk = HAL_RCC_GetPCLK1Freq();
02779     husart->Instance->BRR = USART_BRR(pclk, husart->Init.BaudRate);
02780   }
02781 }
02782 
02783 /**
02784   * @}
02785   */
02786 
02787 /**
02788   * @}
02789   */
02790 
02791 #endif /* HAL_USART_MODULE_ENABLED */
02792 /**
02793   * @}
02794   */
02795 
02796 /**
02797   * @}
02798   */
02799 
02800 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/