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