Subversion Repositories FuelGauge

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f0xx_hal_uart.c
  4.   * @author  MCD Application Team
  5.   * @brief   UART HAL module driver.
  6.   *          This file provides firmware functions to manage the following
  7.   *          functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).
  8.   *           + Initialization and de-initialization functions
  9.   *           + IO operation functions
  10.   *           + Peripheral Control functions
  11.   *
  12.   *
  13.   @verbatim
  14.  ===============================================================================
  15.                         ##### How to use this driver #####
  16.  ===============================================================================
  17.   [..]
  18.     The UART HAL driver can be used as follows:
  19.  
  20.     (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  21.     (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  22.         (++) Enable the USARTx interface clock.
  23.         (++) UART pins configuration:
  24.             (+++) Enable the clock for the UART GPIOs.
  25.             (+++) Configure these UART pins as alternate function pull-up.
  26.         (++) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  27.              and HAL_UART_Receive_IT() APIs):
  28.             (+++) Configure the USARTx interrupt priority.
  29.             (+++) Enable the NVIC USART IRQ handle.
  30.         (++) UART interrupts handling:
  31.               -@@-  The specific UART interrupts (Transmission complete interrupt,
  32.                 RXNE interrupt, RX/TX FIFOs related interrupts and Error Interrupts)
  33.                 are managed using the macros __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT()
  34.                 inside the transmit and receive processes.
  35.         (++) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  36.              and HAL_UART_Receive_DMA() APIs):
  37.             (+++) Declare a DMA handle structure for the Tx/Rx channel.
  38.             (+++) Enable the DMAx interface clock.
  39.             (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  40.             (+++) Configure the DMA Tx/Rx channel.
  41.             (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  42.             (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  43.  
  44.     (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  45.         flow control and Mode (Receiver/Transmitter) in the huart handle Init structure.
  46.  
  47.     (#) If required, program UART advanced features (TX/RX pins swap, auto Baud rate detection,...)
  48.         in the huart handle AdvancedInit structure.
  49.  
  50.     (#) For the UART asynchronous mode, initialize the UART registers by calling
  51.         the HAL_UART_Init() API.
  52.  
  53.     (#) For the UART Half duplex mode, initialize the UART registers by calling
  54.         the HAL_HalfDuplex_Init() API.
  55.  
  56.     (#) For the UART LIN (Local Interconnection Network) mode, initialize the UART registers
  57.         by calling the HAL_LIN_Init() API.
  58.  
  59.     (#) For the UART Multiprocessor mode, initialize the UART registers
  60.         by calling the HAL_MultiProcessor_Init() API.
  61.  
  62.     (#) For the UART RS485 Driver Enabled mode, initialize the UART registers
  63.         by calling the HAL_RS485Ex_Init() API.
  64.  
  65.     [..]
  66.     (@) These API's (HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init(), HAL_MultiProcessor_Init(),
  67.         also configure the low level Hardware GPIO, CLOCK, CORTEX...etc) by
  68.         calling the customized HAL_UART_MspInit() API.
  69.  
  70.     ##### Callback registration #####
  71.     ==================================
  72.  
  73.     [..]
  74.     The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
  75.     allows the user to configure dynamically the driver callbacks.
  76.  
  77.     [..]
  78.     Use Function @ref HAL_UART_RegisterCallback() to register a user callback.
  79.     Function @ref HAL_UART_RegisterCallback() allows to register following callbacks:
  80.     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
  81.     (+) TxCpltCallback            : Tx Complete Callback.
  82.     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
  83.     (+) RxCpltCallback            : Rx Complete Callback.
  84.     (+) ErrorCallback             : Error Callback.
  85.     (+) AbortCpltCallback         : Abort Complete Callback.
  86.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  87.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  88.     (+) WakeupCallback            : Wakeup Callback.
  89.     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
  90.     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
  91.     (+) MspInitCallback           : UART MspInit.
  92.     (+) MspDeInitCallback         : UART MspDeInit.
  93.     This function takes as parameters the HAL peripheral handle, the Callback ID
  94.     and a pointer to the user callback function.
  95.  
  96.     [..]
  97.     Use function @ref HAL_UART_UnRegisterCallback() to reset a callback to the default
  98.     weak (surcharged) function.
  99.     @ref HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  100.     and the Callback ID.
  101.     This function allows to reset following callbacks:
  102.     (+) TxHalfCpltCallback        : Tx Half Complete Callback.
  103.     (+) TxCpltCallback            : Tx Complete Callback.
  104.     (+) RxHalfCpltCallback        : Rx Half Complete Callback.
  105.     (+) RxCpltCallback            : Rx Complete Callback.
  106.     (+) ErrorCallback             : Error Callback.
  107.     (+) AbortCpltCallback         : Abort Complete Callback.
  108.     (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  109.     (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  110.     (+) WakeupCallback            : Wakeup Callback.
  111.     (+) RxFifoFullCallback        : Rx Fifo Full Callback.
  112.     (+) TxFifoEmptyCallback       : Tx Fifo Empty Callback.
  113.     (+) MspInitCallback           : UART MspInit.
  114.     (+) MspDeInitCallback         : UART MspDeInit.
  115.  
  116.     [..]
  117.     By default, after the @ref HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
  118.     all callbacks are set to the corresponding weak (surcharged) functions:
  119.     examples @ref HAL_UART_TxCpltCallback(), @ref HAL_UART_RxHalfCpltCallback().
  120.     Exception done for MspInit and MspDeInit functions that are respectively
  121.     reset to the legacy weak (surcharged) functions in the @ref HAL_UART_Init()
  122.     and @ref HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
  123.     If not, MspInit or MspDeInit are not null, the @ref HAL_UART_Init() and @ref HAL_UART_DeInit()
  124.     keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
  125.  
  126.     [..]
  127.     Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
  128.     Exception done MspInit/MspDeInit that can be registered/unregistered
  129.     in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
  130.     MspInit/DeInit callbacks can be used during the Init/DeInit.
  131.     In that case first register the MspInit/MspDeInit user callbacks
  132.     using @ref HAL_UART_RegisterCallback() before calling @ref HAL_UART_DeInit()
  133.     or @ref HAL_UART_Init() function.
  134.  
  135.     [..]
  136.     When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
  137.     not defined, the callback registration feature is not available
  138.     and weak (surcharged) callbacks are used.
  139.  
  140.  
  141.   @endverbatim
  142.   ******************************************************************************
  143.   * @attention
  144.   *
  145.   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  146.   * All rights reserved.</center></h2>
  147.   *
  148.   * This software component is licensed by ST under BSD 3-Clause license,
  149.   * the "License"; You may not use this file except in compliance with the
  150.   * License. You may obtain a copy of the License at:
  151.   *                        opensource.org/licenses/BSD-3-Clause
  152.   *
  153.   ******************************************************************************
  154.   */
  155.  
  156. /* Includes ------------------------------------------------------------------*/
  157. #include "stm32f0xx_hal.h"
  158.  
  159. /** @addtogroup STM32F0xx_HAL_Driver
  160.   * @{
  161.   */
  162.  
  163. /** @defgroup UART UART
  164.   * @brief HAL UART module driver
  165.   * @{
  166.   */
  167.  
  168. #ifdef HAL_UART_MODULE_ENABLED
  169.  
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private define ------------------------------------------------------------*/
  172. /** @defgroup UART_Private_Constants UART Private Constants
  173.   * @{
  174.   */
  175. #define USART_CR1_FIELDS  ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  176.                                       USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 )) /*!< UART or USART CR1 fields of parameters set by UART_SetConfig API */
  177.  
  178. #define USART_CR3_FIELDS  ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE | USART_CR3_ONEBIT))  /*!< UART or USART CR3 fields of parameters set by UART_SetConfig API */
  179.  
  180.  
  181. #define UART_BRR_MIN    0x10U        /* UART BRR minimum authorized value */
  182. #define UART_BRR_MAX    0x0000FFFFU  /* UART BRR maximum authorized value */
  183.  
  184. /**
  185.   * @}
  186.   */
  187.  
  188. /* Private macros ------------------------------------------------------------*/
  189. /* Private variables ---------------------------------------------------------*/
  190. /* Private function prototypes -----------------------------------------------*/
  191. /** @addtogroup UART_Private_Functions
  192.   * @{
  193.   */
  194. static void UART_EndTxTransfer(UART_HandleTypeDef *huart);
  195. static void UART_EndRxTransfer(UART_HandleTypeDef *huart);
  196. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  197. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  198. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  199. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  200. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  201. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  202. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  203. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  204. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  205. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  206. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart);
  207. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart);
  208. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  209. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart);
  210. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart);
  211. /**
  212.   * @}
  213.   */
  214.  
  215. /* Exported functions --------------------------------------------------------*/
  216.  
  217. /** @defgroup UART_Exported_Functions UART Exported Functions
  218.   * @{
  219.   */
  220.  
  221. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  222.   *  @brief    Initialization and Configuration functions
  223.   *
  224. @verbatim
  225. ===============================================================================
  226.             ##### Initialization and Configuration functions #####
  227.  ===============================================================================
  228.     [..]
  229.     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  230.     in asynchronous mode.
  231.       (+) For the asynchronous mode the parameters below can be configured:
  232.         (++) Baud Rate
  233.         (++) Word Length
  234.         (++) Stop Bit
  235.         (++) Parity: If the parity is enabled, then the MSB bit of the data written
  236.              in the data register is transmitted but is changed by the parity bit.
  237.         (++) Hardware flow control
  238.         (++) Receiver/transmitter modes
  239.         (++) Over Sampling Method
  240.         (++) One-Bit Sampling Method
  241.       (+) For the asynchronous mode, the following advanced features can be configured as well:
  242.         (++) TX and/or RX pin level inversion
  243.         (++) data logical level inversion
  244.         (++) RX and TX pins swap
  245.         (++) RX overrun detection disabling
  246.         (++) DMA disabling on RX error
  247.         (++) MSB first on communication line
  248.         (++) auto Baud rate detection
  249.     [..]
  250.     The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init()and HAL_MultiProcessor_Init()API
  251.     follow respectively the UART asynchronous, UART Half duplex, UART LIN mode
  252.     and UART multiprocessor mode configuration procedures (details for the procedures
  253.     are available in reference manual).
  254.  
  255. @endverbatim
  256.  
  257.   Depending on the frame length defined by the M1 and M0 bits (7-bit,
  258.   8-bit or 9-bit), the possible UART formats are listed in the
  259.   following table.
  260.  
  261.   Table 1. UART frame format.
  262.     +-----------------------------------------------------------------------+
  263.     |  M1 bit |  M0 bit |  PCE bit  |             UART frame                |
  264.     |---------|---------|-----------|---------------------------------------|
  265.     |    0    |    0    |    0      |    | SB |    8 bit data   | STB |     |
  266.     |---------|---------|-----------|---------------------------------------|
  267.     |    0    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
  268.     |---------|---------|-----------|---------------------------------------|
  269.     |    0    |    1    |    0      |    | SB |    9 bit data   | STB |     |
  270.     |---------|---------|-----------|---------------------------------------|
  271.     |    0    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
  272.     |---------|---------|-----------|---------------------------------------|
  273.     |    1    |    0    |    0      |    | SB |    7 bit data   | STB |     |
  274.     |---------|---------|-----------|---------------------------------------|
  275.     |    1    |    0    |    1      |    | SB | 6 bit data | PB | STB |     |
  276.     +-----------------------------------------------------------------------+
  277.  
  278.   * @{
  279.   */
  280.  
  281. /**
  282.   * @brief Initialize the UART mode according to the specified
  283.   *        parameters in the UART_InitTypeDef and initialize the associated handle.
  284.   * @param huart UART handle.
  285.   * @retval HAL status
  286.   */
  287. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  288. {
  289.   /* Check the UART handle allocation */
  290.   if (huart == NULL)
  291.   {
  292.     return HAL_ERROR;
  293.   }
  294.  
  295.   if (huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  296.   {
  297.     /* Check the parameters */
  298.     assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  299.   }
  300.   else
  301.   {
  302.     /* Check the parameters */
  303.     assert_param(IS_UART_INSTANCE(huart->Instance));
  304.   }
  305.  
  306.   if (huart->gState == HAL_UART_STATE_RESET)
  307.   {
  308.     /* Allocate lock resource and initialize it */
  309.     huart->Lock = HAL_UNLOCKED;
  310.  
  311. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  312.     UART_InitCallbacksToDefault(huart);
  313.  
  314.     if (huart->MspInitCallback == NULL)
  315.     {
  316.       huart->MspInitCallback = HAL_UART_MspInit;
  317.     }
  318.  
  319.     /* Init the low level hardware */
  320.     huart->MspInitCallback(huart);
  321. #else
  322.     /* Init the low level hardware : GPIO, CLOCK */
  323.     HAL_UART_MspInit(huart);
  324. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  325.   }
  326.  
  327.   huart->gState = HAL_UART_STATE_BUSY;
  328.  
  329.   __HAL_UART_DISABLE(huart);
  330.  
  331.   /* Set the UART Communication parameters */
  332.   if (UART_SetConfig(huart) == HAL_ERROR)
  333.   {
  334.     return HAL_ERROR;
  335.   }
  336.  
  337.   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  338.   {
  339.     UART_AdvFeatureConfig(huart);
  340.   }
  341.  
  342.   /* In asynchronous mode, the following bits must be kept cleared:
  343.   - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  344.   - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported)  bits in the USART_CR3 register.*/
  345. #if defined (USART_CR2_LINEN)
  346.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  347. #else
  348.   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  349. #endif /* USART_CR2_LINEN */
  350. #if defined (USART_CR3_SCEN)
  351. #if defined (USART_CR3_IREN)
  352.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  353. #else
  354.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  355. #endif /* USART_CR3_IREN */
  356. #else
  357. #if defined (USART_CR3_IREN)
  358.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  359. #else
  360.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  361. #endif /* USART_CR3_IREN*/
  362. #endif /* USART_CR3_SCEN */
  363.  
  364.   __HAL_UART_ENABLE(huart);
  365.  
  366.   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  367.   return (UART_CheckIdleState(huart));
  368. }
  369.  
  370. /**
  371.   * @brief Initialize the half-duplex mode according to the specified
  372.   *        parameters in the UART_InitTypeDef and creates the associated handle.
  373.   * @param huart UART handle.
  374.   * @retval HAL status
  375.   */
  376. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  377. {
  378.   /* Check the UART handle allocation */
  379.   if (huart == NULL)
  380.   {
  381.     return HAL_ERROR;
  382.   }
  383.  
  384.   /* Check UART instance */
  385.   assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  386.  
  387.   if (huart->gState == HAL_UART_STATE_RESET)
  388.   {
  389.     /* Allocate lock resource and initialize it */
  390.     huart->Lock = HAL_UNLOCKED;
  391.  
  392. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  393.     UART_InitCallbacksToDefault(huart);
  394.  
  395.     if (huart->MspInitCallback == NULL)
  396.     {
  397.       huart->MspInitCallback = HAL_UART_MspInit;
  398.     }
  399.  
  400.     /* Init the low level hardware */
  401.     huart->MspInitCallback(huart);
  402. #else
  403.     /* Init the low level hardware : GPIO, CLOCK */
  404.     HAL_UART_MspInit(huart);
  405. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  406.   }
  407.  
  408.   huart->gState = HAL_UART_STATE_BUSY;
  409.  
  410.   __HAL_UART_DISABLE(huart);
  411.  
  412.   /* Set the UART Communication parameters */
  413.   if (UART_SetConfig(huart) == HAL_ERROR)
  414.   {
  415.     return HAL_ERROR;
  416.   }
  417.  
  418.   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  419.   {
  420.     UART_AdvFeatureConfig(huart);
  421.   }
  422.  
  423.   /* In half-duplex mode, the following bits must be kept cleared:
  424.   - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  425.   - SCEN (if Smartcard is supported) and IREN (if IrDA is supported) bits in the USART_CR3 register.*/
  426. #if defined (USART_CR2_LINEN)
  427.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  428. #else
  429.   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  430. #endif /* USART_CR2_LINEN */
  431. #if defined (USART_CR3_SCEN)
  432. #if defined (USART_CR3_IREN)
  433.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  434. #else
  435.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_SCEN);
  436. #endif /* USART_CR3_IREN */
  437. #else
  438. #if defined (USART_CR3_IREN)
  439.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_IREN);
  440. #endif /* USART_CR3_IREN */
  441. #endif /* USART_CR3_SCEN */
  442.  
  443.   /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  444.   SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  445.  
  446.   __HAL_UART_ENABLE(huart);
  447.  
  448.   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  449.   return (UART_CheckIdleState(huart));
  450. }
  451.  
  452.  
  453. #if   defined(USART_CR2_LINEN)
  454. /**
  455.   * @brief Initialize the LIN mode according to the specified
  456.   *        parameters in the UART_InitTypeDef and creates the associated handle.
  457.   * @param huart             UART handle.
  458.   * @param BreakDetectLength Specifies the LIN break detection length.
  459.   *        This parameter can be one of the following values:
  460.   *          @arg @ref UART_LINBREAKDETECTLENGTH_10B 10-bit break detection
  461.   *          @arg @ref UART_LINBREAKDETECTLENGTH_11B 11-bit break detection
  462.   * @retval HAL status
  463.   */
  464. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  465. {
  466.   /* Check the UART handle allocation */
  467.   if (huart == NULL)
  468.   {
  469.     return HAL_ERROR;
  470.   }
  471.  
  472.   /* Check the LIN UART instance */
  473.   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  474.   /* Check the Break detection length parameter */
  475.   assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  476.  
  477.   /* LIN mode limited to 16-bit oversampling only */
  478.   if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  479.   {
  480.     return HAL_ERROR;
  481.   }
  482.   /* LIN mode limited to 8-bit data length */
  483.   if (huart->Init.WordLength != UART_WORDLENGTH_8B)
  484.   {
  485.     return HAL_ERROR;
  486.   }
  487.  
  488.   if (huart->gState == HAL_UART_STATE_RESET)
  489.   {
  490.     /* Allocate lock resource and initialize it */
  491.     huart->Lock = HAL_UNLOCKED;
  492.  
  493. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  494.     UART_InitCallbacksToDefault(huart);
  495.  
  496.     if (huart->MspInitCallback == NULL)
  497.     {
  498.       huart->MspInitCallback = HAL_UART_MspInit;
  499.     }
  500.  
  501.     /* Init the low level hardware */
  502.     huart->MspInitCallback(huart);
  503. #else
  504.     /* Init the low level hardware : GPIO, CLOCK */
  505.     HAL_UART_MspInit(huart);
  506. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  507.   }
  508.  
  509.   huart->gState = HAL_UART_STATE_BUSY;
  510.  
  511.   __HAL_UART_DISABLE(huart);
  512.  
  513.   /* Set the UART Communication parameters */
  514.   if (UART_SetConfig(huart) == HAL_ERROR)
  515.   {
  516.     return HAL_ERROR;
  517.   }
  518.  
  519.   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  520.   {
  521.     UART_AdvFeatureConfig(huart);
  522.   }
  523.  
  524.   /* In LIN mode, the following bits must be kept cleared:
  525.   - LINEN and CLKEN bits in the USART_CR2 register,
  526.   - SCEN(if Smartcard is supported) and IREN(if IrDA is supported) bits in the USART_CR3 register.*/
  527.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  528. #if defined (USART_CR3_SCEN)
  529. #if defined (USART_CR3_IREN)
  530.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  531. #else
  532.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  533. #endif /* USART_CR3_IREN */
  534. #else
  535. #if defined (USART_CR3_IREN)
  536.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  537. #else
  538.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  539. #endif /* USART_CR3_IREN*/
  540. #endif /* USART_CR3_SCEN */
  541.  
  542.   /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  543.   SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  544.  
  545.   /* Set the USART LIN Break detection length. */
  546.   MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  547.  
  548.   __HAL_UART_ENABLE(huart);
  549.  
  550.   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  551.   return (UART_CheckIdleState(huart));
  552. }
  553. #endif /* USART_CR2_LINEN */
  554.  
  555.  
  556. /**
  557.   * @brief Initialize the multiprocessor mode according to the specified
  558.   *        parameters in the UART_InitTypeDef and initialize the associated handle.
  559.   * @param huart        UART handle.
  560.   * @param Address      UART node address (4-, 6-, 7- or 8-bit long).
  561.   * @param WakeUpMethod Specifies the UART wakeup method.
  562.   *        This parameter can be one of the following values:
  563.   *          @arg @ref UART_WAKEUPMETHOD_IDLELINE WakeUp by an idle line detection
  564.   *          @arg @ref UART_WAKEUPMETHOD_ADDRESSMARK WakeUp by an address mark
  565.   * @note  If the user resorts to idle line detection wake up, the Address parameter
  566.   *        is useless and ignored by the initialization function.
  567.   * @note  If the user resorts to address mark wake up, the address length detection
  568.   *        is configured by default to 4 bits only. For the UART to be able to
  569.   *        manage 6-, 7- or 8-bit long addresses detection, the API
  570.   *        HAL_MultiProcessorEx_AddressLength_Set() must be called after
  571.   *        HAL_MultiProcessor_Init().
  572.   * @retval HAL status
  573.   */
  574. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  575. {
  576.   /* Check the UART handle allocation */
  577.   if (huart == NULL)
  578.   {
  579.     return HAL_ERROR;
  580.   }
  581.  
  582.   /* Check the wake up method parameter */
  583.   assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  584.  
  585.   if (huart->gState == HAL_UART_STATE_RESET)
  586.   {
  587.     /* Allocate lock resource and initialize it */
  588.     huart->Lock = HAL_UNLOCKED;
  589.  
  590. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  591.     UART_InitCallbacksToDefault(huart);
  592.  
  593.     if (huart->MspInitCallback == NULL)
  594.     {
  595.       huart->MspInitCallback = HAL_UART_MspInit;
  596.     }
  597.  
  598.     /* Init the low level hardware */
  599.     huart->MspInitCallback(huart);
  600. #else
  601.     /* Init the low level hardware : GPIO, CLOCK */
  602.     HAL_UART_MspInit(huart);
  603. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  604.   }
  605.  
  606.   huart->gState = HAL_UART_STATE_BUSY;
  607.  
  608.   __HAL_UART_DISABLE(huart);
  609.  
  610.   /* Set the UART Communication parameters */
  611.   if (UART_SetConfig(huart) == HAL_ERROR)
  612.   {
  613.     return HAL_ERROR;
  614.   }
  615.  
  616.   if (huart->AdvancedInit.AdvFeatureInit != UART_ADVFEATURE_NO_INIT)
  617.   {
  618.     UART_AdvFeatureConfig(huart);
  619.   }
  620.  
  621.   /* In multiprocessor mode, the following bits must be kept cleared:
  622.   - LINEN (if LIN is supported) and CLKEN bits in the USART_CR2 register,
  623.   - SCEN (if Smartcard is supported), HDSEL and IREN (if IrDA is supported) bits in the USART_CR3 register. */
  624. #if defined (USART_CR2_LINEN)
  625.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  626. #else
  627.   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  628. #endif /* USART_CR2_LINEN */
  629. #if defined (USART_CR3_SCEN)
  630. #if defined (USART_CR3_IREN)
  631.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  632. #else
  633.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL));
  634. #endif /* USART_CR3_IREN */
  635. #else
  636. #if defined (USART_CR3_IREN)
  637.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  638. #else
  639.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  640. #endif /* USART_CR3_IREN */
  641. #endif /* USART_CR3_SCEN */
  642.  
  643.   if (WakeUpMethod == UART_WAKEUPMETHOD_ADDRESSMARK)
  644.   {
  645.     /* If address mark wake up method is chosen, set the USART address node */
  646.     MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, ((uint32_t)Address << UART_CR2_ADDRESS_LSB_POS));
  647.   }
  648.  
  649.   /* Set the wake up method by setting the WAKE bit in the CR1 register */
  650.   MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  651.  
  652.   __HAL_UART_ENABLE(huart);
  653.  
  654.   /* TEACK and/or REACK to check before moving huart->gState and huart->RxState to Ready */
  655.   return (UART_CheckIdleState(huart));
  656. }
  657.  
  658.  
  659. /**
  660.   * @brief DeInitialize the UART peripheral.
  661.   * @param huart UART handle.
  662.   * @retval HAL status
  663.   */
  664. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  665. {
  666.   /* Check the UART handle allocation */
  667.   if (huart == NULL)
  668.   {
  669.     return HAL_ERROR;
  670.   }
  671.  
  672.   /* Check the parameters */
  673.   assert_param(IS_UART_INSTANCE(huart->Instance));
  674.  
  675.   huart->gState = HAL_UART_STATE_BUSY;
  676.  
  677.   __HAL_UART_DISABLE(huart);
  678.  
  679.   huart->Instance->CR1 = 0x0U;
  680.   huart->Instance->CR2 = 0x0U;
  681.   huart->Instance->CR3 = 0x0U;
  682.  
  683. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  684.   if (huart->MspDeInitCallback == NULL)
  685.   {
  686.     huart->MspDeInitCallback = HAL_UART_MspDeInit;
  687.   }
  688.   /* DeInit the low level hardware */
  689.   huart->MspDeInitCallback(huart);
  690. #else
  691.   /* DeInit the low level hardware */
  692.   HAL_UART_MspDeInit(huart);
  693. #endif /* (USE_HAL_UART_REGISTER_CALLBACKS) */
  694.  
  695.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  696.   huart->gState = HAL_UART_STATE_RESET;
  697.   huart->RxState = HAL_UART_STATE_RESET;
  698.  
  699.   __HAL_UNLOCK(huart);
  700.  
  701.   return HAL_OK;
  702. }
  703.  
  704. /**
  705.   * @brief Initialize the UART MSP.
  706.   * @param huart UART handle.
  707.   * @retval None
  708.   */
  709. __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  710. {
  711.   /* Prevent unused argument(s) compilation warning */
  712.   UNUSED(huart);
  713.  
  714.   /* NOTE : This function should not be modified, when the callback is needed,
  715.             the HAL_UART_MspInit can be implemented in the user file
  716.    */
  717. }
  718.  
  719. /**
  720.   * @brief DeInitialize the UART MSP.
  721.   * @param huart UART handle.
  722.   * @retval None
  723.   */
  724. __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  725. {
  726.   /* Prevent unused argument(s) compilation warning */
  727.   UNUSED(huart);
  728.  
  729.   /* NOTE : This function should not be modified, when the callback is needed,
  730.             the HAL_UART_MspDeInit can be implemented in the user file
  731.    */
  732. }
  733.  
  734. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  735. /**
  736.   * @brief  Register a User UART Callback
  737.   *         To be used instead of the weak predefined callback
  738.   * @param  huart uart handle
  739.   * @param  CallbackID ID of the callback to be registered
  740.   *         This parameter can be one of the following values:
  741.   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  742.   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  743.   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  744.   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  745.   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  746.   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  747.   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  748.   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  749.   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  750.   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  751.   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  752.   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  753.   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  754.   * @param  pCallback pointer to the Callback function
  755.   * @retval HAL status
  756.   */
  757. HAL_StatusTypeDef HAL_UART_RegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID,
  758.                                             pUART_CallbackTypeDef pCallback)
  759. {
  760.   HAL_StatusTypeDef status = HAL_OK;
  761.  
  762.   if (pCallback == NULL)
  763.   {
  764.     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  765.  
  766.     return HAL_ERROR;
  767.   }
  768.  
  769.   __HAL_LOCK(huart);
  770.  
  771.   if (huart->gState == HAL_UART_STATE_READY)
  772.   {
  773.     switch (CallbackID)
  774.     {
  775.       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  776.         huart->TxHalfCpltCallback = pCallback;
  777.         break;
  778.  
  779.       case HAL_UART_TX_COMPLETE_CB_ID :
  780.         huart->TxCpltCallback = pCallback;
  781.         break;
  782.  
  783.       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  784.         huart->RxHalfCpltCallback = pCallback;
  785.         break;
  786.  
  787.       case HAL_UART_RX_COMPLETE_CB_ID :
  788.         huart->RxCpltCallback = pCallback;
  789.         break;
  790.  
  791.       case HAL_UART_ERROR_CB_ID :
  792.         huart->ErrorCallback = pCallback;
  793.         break;
  794.  
  795.       case HAL_UART_ABORT_COMPLETE_CB_ID :
  796.         huart->AbortCpltCallback = pCallback;
  797.         break;
  798.  
  799.       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  800.         huart->AbortTransmitCpltCallback = pCallback;
  801.         break;
  802.  
  803.       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  804.         huart->AbortReceiveCpltCallback = pCallback;
  805.         break;
  806.  
  807.       case HAL_UART_WAKEUP_CB_ID :
  808.         huart->WakeupCallback = pCallback;
  809.         break;
  810.  
  811.  
  812.       case HAL_UART_MSPINIT_CB_ID :
  813.         huart->MspInitCallback = pCallback;
  814.         break;
  815.  
  816.       case HAL_UART_MSPDEINIT_CB_ID :
  817.         huart->MspDeInitCallback = pCallback;
  818.         break;
  819.  
  820.       default :
  821.         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  822.  
  823.         status =  HAL_ERROR;
  824.         break;
  825.     }
  826.   }
  827.   else if (huart->gState == HAL_UART_STATE_RESET)
  828.   {
  829.     switch (CallbackID)
  830.     {
  831.       case HAL_UART_MSPINIT_CB_ID :
  832.         huart->MspInitCallback = pCallback;
  833.         break;
  834.  
  835.       case HAL_UART_MSPDEINIT_CB_ID :
  836.         huart->MspDeInitCallback = pCallback;
  837.         break;
  838.  
  839.       default :
  840.         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  841.  
  842.         status =  HAL_ERROR;
  843.         break;
  844.     }
  845.   }
  846.   else
  847.   {
  848.     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  849.  
  850.     status =  HAL_ERROR;
  851.   }
  852.  
  853.   __HAL_UNLOCK(huart);
  854.  
  855.   return status;
  856. }
  857.  
  858. /**
  859.   * @brief  Unregister an UART Callback
  860.   *         UART callaback is redirected to the weak predefined callback
  861.   * @param  huart uart handle
  862.   * @param  CallbackID ID of the callback to be unregistered
  863.   *         This parameter can be one of the following values:
  864.   *           @arg @ref HAL_UART_TX_HALFCOMPLETE_CB_ID Tx Half Complete Callback ID
  865.   *           @arg @ref HAL_UART_TX_COMPLETE_CB_ID Tx Complete Callback ID
  866.   *           @arg @ref HAL_UART_RX_HALFCOMPLETE_CB_ID Rx Half Complete Callback ID
  867.   *           @arg @ref HAL_UART_RX_COMPLETE_CB_ID Rx Complete Callback ID
  868.   *           @arg @ref HAL_UART_ERROR_CB_ID Error Callback ID
  869.   *           @arg @ref HAL_UART_ABORT_COMPLETE_CB_ID Abort Complete Callback ID
  870.   *           @arg @ref HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID Abort Transmit Complete Callback ID
  871.   *           @arg @ref HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID Abort Receive Complete Callback ID
  872.   *           @arg @ref HAL_UART_WAKEUP_CB_ID Wakeup Callback ID
  873.   *           @arg @ref HAL_UART_RX_FIFO_FULL_CB_ID Rx Fifo Full Callback ID
  874.   *           @arg @ref HAL_UART_TX_FIFO_EMPTY_CB_ID Tx Fifo Empty Callback ID
  875.   *           @arg @ref HAL_UART_MSPINIT_CB_ID MspInit Callback ID
  876.   *           @arg @ref HAL_UART_MSPDEINIT_CB_ID MspDeInit Callback ID
  877.   * @retval HAL status
  878.   */
  879. HAL_StatusTypeDef HAL_UART_UnRegisterCallback(UART_HandleTypeDef *huart, HAL_UART_CallbackIDTypeDef CallbackID)
  880. {
  881.   HAL_StatusTypeDef status = HAL_OK;
  882.  
  883.   __HAL_LOCK(huart);
  884.  
  885.   if (HAL_UART_STATE_READY == huart->gState)
  886.   {
  887.     switch (CallbackID)
  888.     {
  889.       case HAL_UART_TX_HALFCOMPLETE_CB_ID :
  890.         huart->TxHalfCpltCallback = HAL_UART_TxHalfCpltCallback;               /* Legacy weak  TxHalfCpltCallback       */
  891.         break;
  892.  
  893.       case HAL_UART_TX_COMPLETE_CB_ID :
  894.         huart->TxCpltCallback = HAL_UART_TxCpltCallback;                       /* Legacy weak TxCpltCallback            */
  895.         break;
  896.  
  897.       case HAL_UART_RX_HALFCOMPLETE_CB_ID :
  898.         huart->RxHalfCpltCallback = HAL_UART_RxHalfCpltCallback;               /* Legacy weak RxHalfCpltCallback        */
  899.         break;
  900.  
  901.       case HAL_UART_RX_COMPLETE_CB_ID :
  902.         huart->RxCpltCallback = HAL_UART_RxCpltCallback;                       /* Legacy weak RxCpltCallback            */
  903.         break;
  904.  
  905.       case HAL_UART_ERROR_CB_ID :
  906.         huart->ErrorCallback = HAL_UART_ErrorCallback;                         /* Legacy weak ErrorCallback             */
  907.         break;
  908.  
  909.       case HAL_UART_ABORT_COMPLETE_CB_ID :
  910.         huart->AbortCpltCallback = HAL_UART_AbortCpltCallback;                 /* Legacy weak AbortCpltCallback         */
  911.         break;
  912.  
  913.       case HAL_UART_ABORT_TRANSMIT_COMPLETE_CB_ID :
  914.         huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  915.         break;
  916.  
  917.       case HAL_UART_ABORT_RECEIVE_COMPLETE_CB_ID :
  918.         huart->AbortReceiveCpltCallback = HAL_UART_AbortReceiveCpltCallback;   /* Legacy weak AbortReceiveCpltCallback  */
  919.         break;
  920.  
  921. #if defined(USART_CR1_UESM)
  922.       case HAL_UART_WAKEUP_CB_ID :
  923.         huart->WakeupCallback = HAL_UARTEx_WakeupCallback;                     /* Legacy weak WakeupCallback            */
  924.         break;
  925.  
  926. #endif /* USART_CR1_UESM */
  927.       case HAL_UART_MSPINIT_CB_ID :
  928.         huart->MspInitCallback = HAL_UART_MspInit;                             /* Legacy weak MspInitCallback           */
  929.         break;
  930.  
  931.       case HAL_UART_MSPDEINIT_CB_ID :
  932.         huart->MspDeInitCallback = HAL_UART_MspDeInit;                         /* Legacy weak MspDeInitCallback         */
  933.         break;
  934.  
  935.       default :
  936.         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  937.  
  938.         status =  HAL_ERROR;
  939.         break;
  940.     }
  941.   }
  942.   else if (HAL_UART_STATE_RESET == huart->gState)
  943.   {
  944.     switch (CallbackID)
  945.     {
  946.       case HAL_UART_MSPINIT_CB_ID :
  947.         huart->MspInitCallback = HAL_UART_MspInit;
  948.         break;
  949.  
  950.       case HAL_UART_MSPDEINIT_CB_ID :
  951.         huart->MspDeInitCallback = HAL_UART_MspDeInit;
  952.         break;
  953.  
  954.       default :
  955.         huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  956.  
  957.         status =  HAL_ERROR;
  958.         break;
  959.     }
  960.   }
  961.   else
  962.   {
  963.     huart->ErrorCode |= HAL_UART_ERROR_INVALID_CALLBACK;
  964.  
  965.     status =  HAL_ERROR;
  966.   }
  967.  
  968.   __HAL_UNLOCK(huart);
  969.  
  970.   return status;
  971. }
  972. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  973.  
  974. /**
  975.   * @}
  976.   */
  977.  
  978. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  979.   * @brief UART Transmit/Receive functions
  980.   *
  981. @verbatim
  982.  ===============================================================================
  983.                       ##### IO operation functions #####
  984.  ===============================================================================
  985.     This subsection provides a set of functions allowing to manage the UART asynchronous
  986.     and Half duplex data transfers.
  987.  
  988.     (#) There are two mode of transfer:
  989.        (+) Blocking mode: The communication is performed in polling mode.
  990.            The HAL status of all data processing is returned by the same function
  991.            after finishing transfer.
  992.        (+) Non-Blocking mode: The communication is performed using Interrupts
  993.            or DMA, These API's return the HAL status.
  994.            The end of the data processing will be indicated through the
  995.            dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  996.            using DMA mode.
  997.            The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  998.            will be executed respectively at the end of the transmit or Receive process
  999.            The HAL_UART_ErrorCallback()user callback will be executed when a communication error is detected
  1000.  
  1001.     (#) Blocking mode API's are :
  1002.         (+) HAL_UART_Transmit()
  1003.         (+) HAL_UART_Receive()
  1004.  
  1005.     (#) Non-Blocking mode API's with Interrupt are :
  1006.         (+) HAL_UART_Transmit_IT()
  1007.         (+) HAL_UART_Receive_IT()
  1008.         (+) HAL_UART_IRQHandler()
  1009.  
  1010.     (#) Non-Blocking mode API's with DMA are :
  1011.         (+) HAL_UART_Transmit_DMA()
  1012.         (+) HAL_UART_Receive_DMA()
  1013.         (+) HAL_UART_DMAPause()
  1014.         (+) HAL_UART_DMAResume()
  1015.         (+) HAL_UART_DMAStop()
  1016.  
  1017.     (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  1018.         (+) HAL_UART_TxHalfCpltCallback()
  1019.         (+) HAL_UART_TxCpltCallback()
  1020.         (+) HAL_UART_RxHalfCpltCallback()
  1021.         (+) HAL_UART_RxCpltCallback()
  1022.         (+) HAL_UART_ErrorCallback()
  1023.  
  1024.     (#) Non-Blocking mode transfers could be aborted using Abort API's :
  1025.         (+) HAL_UART_Abort()
  1026.         (+) HAL_UART_AbortTransmit()
  1027.         (+) HAL_UART_AbortReceive()
  1028.         (+) HAL_UART_Abort_IT()
  1029.         (+) HAL_UART_AbortTransmit_IT()
  1030.         (+) HAL_UART_AbortReceive_IT()
  1031.  
  1032.     (#) For Abort services based on interrupts (HAL_UART_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  1033.         (+) HAL_UART_AbortCpltCallback()
  1034.         (+) HAL_UART_AbortTransmitCpltCallback()
  1035.         (+) HAL_UART_AbortReceiveCpltCallback()
  1036.  
  1037.     (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  1038.         Errors are handled as follows :
  1039.        (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  1040.            to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  1041.            Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  1042.            and HAL_UART_ErrorCallback() user callback is executed. Transfer is kept ongoing on UART side.
  1043.            If user wants to abort it, Abort services should be called by user.
  1044.        (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  1045.            This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  1046.            Error code is set to allow user to identify error type, and HAL_UART_ErrorCallback() user callback is executed.
  1047.  
  1048.     -@- In the Half duplex communication, it is forbidden to run the transmit
  1049.         and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX can't be useful.
  1050.  
  1051. @endverbatim
  1052.   * @{
  1053.   */
  1054.  
  1055. /**
  1056.   * @brief Send an amount of data in blocking mode.
  1057.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1058.   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  1059.   *         of u16 provided through pData.
  1060.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1061.   *         address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  1062.   *         (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  1063.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1064.   * @param huart   UART handle.
  1065.   * @param pData   Pointer to data buffer (u8 or u16 data elements).
  1066.   * @param Size    Amount of data elements (u8 or u16) to be sent.
  1067.   * @param Timeout Timeout duration.
  1068.   * @retval HAL status
  1069.   */
  1070. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1071. {
  1072.   uint8_t  *pdata8bits;
  1073.   uint16_t *pdata16bits;
  1074.   uint32_t tickstart;
  1075.  
  1076.   /* Check that a Tx process is not already ongoing */
  1077.   if (huart->gState == HAL_UART_STATE_READY)
  1078.   {
  1079.     if ((pData == NULL) || (Size == 0U))
  1080.     {
  1081.       return  HAL_ERROR;
  1082.     }
  1083.  
  1084.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1085.        should be aligned on a u16 frontier, as data to be filled into TDR will be
  1086.        handled through a u16 cast. */
  1087.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1088.     {
  1089.       if ((((uint32_t)pData) & 1U) != 0U)
  1090.       {
  1091.         return  HAL_ERROR;
  1092.       }
  1093.     }
  1094.  
  1095.     __HAL_LOCK(huart);
  1096.  
  1097.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1098.     huart->gState = HAL_UART_STATE_BUSY_TX;
  1099.  
  1100.     /* Init tickstart for timeout managment*/
  1101.     tickstart = HAL_GetTick();
  1102.  
  1103.     huart->TxXferSize  = Size;
  1104.     huart->TxXferCount = Size;
  1105.  
  1106.     /* In case of 9bits/No Parity transfer, pData needs to be handled as a uint16_t pointer */
  1107.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1108.     {
  1109.       pdata8bits  = NULL;
  1110.       pdata16bits = (uint16_t *) pData;
  1111.     }
  1112.     else
  1113.     {
  1114.       pdata8bits  = pData;
  1115.       pdata16bits = NULL;
  1116.     }
  1117.  
  1118.     while (huart->TxXferCount > 0U)
  1119.     {
  1120.       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  1121.       {
  1122.         return HAL_TIMEOUT;
  1123.       }
  1124.       if (pdata8bits == NULL)
  1125.       {
  1126.         huart->Instance->TDR = (uint16_t)(*pdata16bits & 0x01FFU);
  1127.         pdata16bits++;
  1128.       }
  1129.       else
  1130.       {
  1131.         huart->Instance->TDR = (uint8_t)(*pdata8bits & 0xFFU);
  1132.         pdata8bits++;
  1133.       }
  1134.       huart->TxXferCount--;
  1135.     }
  1136.  
  1137.     if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  1138.     {
  1139.       return HAL_TIMEOUT;
  1140.     }
  1141.  
  1142.     /* At end of Tx process, restore huart->gState to Ready */
  1143.     huart->gState = HAL_UART_STATE_READY;
  1144.  
  1145.     __HAL_UNLOCK(huart);
  1146.  
  1147.     return HAL_OK;
  1148.   }
  1149.   else
  1150.   {
  1151.     return HAL_BUSY;
  1152.   }
  1153. }
  1154.  
  1155. /**
  1156.   * @brief Receive an amount of data in blocking mode.
  1157.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1158.   *         the received data is handled as a set of u16. In this case, Size must indicate the number
  1159.   *         of u16 available through pData.
  1160.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1161.   *         address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  1162.   *         (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  1163.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1164.   * @param huart   UART handle.
  1165.   * @param pData   Pointer to data buffer (u8 or u16 data elements).
  1166.   * @param Size    Amount of data elements (u8 or u16) to be received.
  1167.   * @param Timeout Timeout duration.
  1168.   * @retval HAL status
  1169.   */
  1170. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  1171. {
  1172.   uint8_t  *pdata8bits;
  1173.   uint16_t *pdata16bits;
  1174.   uint16_t uhMask;
  1175.   uint32_t tickstart;
  1176.  
  1177.   /* Check that a Rx process is not already ongoing */
  1178.   if (huart->RxState == HAL_UART_STATE_READY)
  1179.   {
  1180.     if ((pData == NULL) || (Size == 0U))
  1181.     {
  1182.       return  HAL_ERROR;
  1183.     }
  1184.  
  1185.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1186.        should be aligned on a u16 frontier, as data to be received from RDR will be
  1187.        handled through a u16 cast. */
  1188.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1189.     {
  1190.       if ((((uint32_t)pData) & 1U) != 0U)
  1191.       {
  1192.         return  HAL_ERROR;
  1193.       }
  1194.     }
  1195.  
  1196.     __HAL_LOCK(huart);
  1197.  
  1198.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1199.     huart->RxState = HAL_UART_STATE_BUSY_RX;
  1200.  
  1201.     /* Init tickstart for timeout managment*/
  1202.     tickstart = HAL_GetTick();
  1203.  
  1204.     huart->RxXferSize  = Size;
  1205.     huart->RxXferCount = Size;
  1206.  
  1207.     /* Computation of UART mask to apply to RDR register */
  1208.     UART_MASK_COMPUTATION(huart);
  1209.     uhMask = huart->Mask;
  1210.  
  1211.     /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
  1212.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1213.     {
  1214.       pdata8bits  = NULL;
  1215.       pdata16bits = (uint16_t *) pData;
  1216.     }
  1217.     else
  1218.     {
  1219.       pdata8bits  = pData;
  1220.       pdata16bits = NULL;
  1221.     }
  1222.  
  1223.     /* as long as data have to be received */
  1224.     while (huart->RxXferCount > 0U)
  1225.     {
  1226.       if (UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  1227.       {
  1228.         return HAL_TIMEOUT;
  1229.       }
  1230.       if (pdata8bits == NULL)
  1231.       {
  1232.         *pdata16bits = (uint16_t)(huart->Instance->RDR & uhMask);
  1233.         pdata16bits++;
  1234.       }
  1235.       else
  1236.       {
  1237.         *pdata8bits = (uint8_t)(huart->Instance->RDR & (uint8_t)uhMask);
  1238.         pdata8bits++;
  1239.       }
  1240.       huart->RxXferCount--;
  1241.     }
  1242.  
  1243.     /* At end of Rx process, restore huart->RxState to Ready */
  1244.     huart->RxState = HAL_UART_STATE_READY;
  1245.  
  1246.     __HAL_UNLOCK(huart);
  1247.  
  1248.     return HAL_OK;
  1249.   }
  1250.   else
  1251.   {
  1252.     return HAL_BUSY;
  1253.   }
  1254. }
  1255.  
  1256. /**
  1257.   * @brief Send an amount of data in interrupt mode.
  1258.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1259.   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  1260.   *         of u16 provided through pData.
  1261.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1262.   *         address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  1263.   *         (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
  1264.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1265.   * @param huart UART handle.
  1266.   * @param pData Pointer to data buffer (u8 or u16 data elements).
  1267.   * @param Size  Amount of data elements (u8 or u16) to be sent.
  1268.   * @retval HAL status
  1269.   */
  1270. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1271. {
  1272.   /* Check that a Tx process is not already ongoing */
  1273.   if (huart->gState == HAL_UART_STATE_READY)
  1274.   {
  1275.     if ((pData == NULL) || (Size == 0U))
  1276.     {
  1277.       return HAL_ERROR;
  1278.     }
  1279.  
  1280.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1281.        should be aligned on a u16 frontier, as data to be filled into TDR will be
  1282.        handled through a u16 cast. */
  1283.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1284.     {
  1285.       if ((((uint32_t)pData) & 1U) != 0U)
  1286.       {
  1287.         return  HAL_ERROR;
  1288.       }
  1289.     }
  1290.  
  1291.     __HAL_LOCK(huart);
  1292.  
  1293.     huart->pTxBuffPtr  = pData;
  1294.     huart->TxXferSize  = Size;
  1295.     huart->TxXferCount = Size;
  1296.     huart->TxISR       = NULL;
  1297.  
  1298.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1299.     huart->gState = HAL_UART_STATE_BUSY_TX;
  1300.  
  1301.     /* Set the Tx ISR function pointer according to the data word length */
  1302.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1303.     {
  1304.       huart->TxISR = UART_TxISR_16BIT;
  1305.     }
  1306.     else
  1307.     {
  1308.       huart->TxISR = UART_TxISR_8BIT;
  1309.     }
  1310.  
  1311.     __HAL_UNLOCK(huart);
  1312.  
  1313.     /* Enable the Transmit Data Register Empty interrupt */
  1314.     SET_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  1315.  
  1316.     return HAL_OK;
  1317.   }
  1318.   else
  1319.   {
  1320.     return HAL_BUSY;
  1321.   }
  1322. }
  1323.  
  1324. /**
  1325.   * @brief Receive an amount of data in interrupt mode.
  1326.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1327.   *         the received data is handled as a set of u16. In this case, Size must indicate the number
  1328.   *         of u16 available through pData.
  1329.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1330.   *         address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  1331.   *         (as received data will be handled using u16 pointer cast). Depending on compilation chain,
  1332.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1333.   * @param huart UART handle.
  1334.   * @param pData Pointer to data buffer (u8 or u16 data elements).
  1335.   * @param Size  Amount of data elements (u8 or u16) to be received.
  1336.   * @retval HAL status
  1337.   */
  1338. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1339. {
  1340.   /* Check that a Rx process is not already ongoing */
  1341.   if (huart->RxState == HAL_UART_STATE_READY)
  1342.   {
  1343.     if ((pData == NULL) || (Size == 0U))
  1344.     {
  1345.       return HAL_ERROR;
  1346.     }
  1347.  
  1348.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1349.        should be aligned on a u16 frontier, as data to be received from RDR will be
  1350.        handled through a u16 cast. */
  1351.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1352.     {
  1353.       if ((((uint32_t)pData) & 1U) != 0U)
  1354.       {
  1355.         return  HAL_ERROR;
  1356.       }
  1357.     }
  1358.  
  1359.     __HAL_LOCK(huart);
  1360.  
  1361.     huart->pRxBuffPtr  = pData;
  1362.     huart->RxXferSize  = Size;
  1363.     huart->RxXferCount = Size;
  1364.     huart->RxISR       = NULL;
  1365.  
  1366.     /* Computation of UART mask to apply to RDR register */
  1367.     UART_MASK_COMPUTATION(huart);
  1368.  
  1369.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1370.     huart->RxState = HAL_UART_STATE_BUSY_RX;
  1371.  
  1372.     /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1373.     SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1374.  
  1375.     /* Set the Rx ISR function pointer according to the data word length */
  1376.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1377.     {
  1378.       huart->RxISR = UART_RxISR_16BIT;
  1379.     }
  1380.     else
  1381.     {
  1382.       huart->RxISR = UART_RxISR_8BIT;
  1383.     }
  1384.  
  1385.     __HAL_UNLOCK(huart);
  1386.  
  1387.     /* Enable the UART Parity Error interrupt and Data Register Not Empty interrupt */
  1388.     SET_BIT(huart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  1389.  
  1390.     return HAL_OK;
  1391.   }
  1392.   else
  1393.   {
  1394.     return HAL_BUSY;
  1395.   }
  1396. }
  1397.  
  1398. /**
  1399.   * @brief Send an amount of data in DMA mode.
  1400.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1401.   *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  1402.   *         of u16 provided through pData.
  1403.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1404.   *         address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
  1405.   *         (as sent data will be handled by DMA from halfword frontier). Depending on compilation chain,
  1406.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1407.   * @param huart UART handle.
  1408.   * @param pData Pointer to data buffer (u8 or u16 data elements).
  1409.   * @param Size  Amount of data elements (u8 or u16) to be sent.
  1410.   * @retval HAL status
  1411.   */
  1412. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1413. {
  1414.   /* Check that a Tx process is not already ongoing */
  1415.   if (huart->gState == HAL_UART_STATE_READY)
  1416.   {
  1417.     if ((pData == NULL) || (Size == 0U))
  1418.     {
  1419.       return HAL_ERROR;
  1420.     }
  1421.  
  1422.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1423.        should be aligned on a u16 frontier, as data copy into TDR will be
  1424.        handled by DMA from a u16 frontier. */
  1425.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1426.     {
  1427.       if ((((uint32_t)pData) & 1U) != 0U)
  1428.       {
  1429.         return  HAL_ERROR;
  1430.       }
  1431.     }
  1432.  
  1433.     __HAL_LOCK(huart);
  1434.  
  1435.     huart->pTxBuffPtr  = pData;
  1436.     huart->TxXferSize  = Size;
  1437.     huart->TxXferCount = Size;
  1438.  
  1439.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1440.     huart->gState = HAL_UART_STATE_BUSY_TX;
  1441.  
  1442.     if (huart->hdmatx != NULL)
  1443.     {
  1444.       /* Set the UART DMA transfer complete callback */
  1445.       huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  1446.  
  1447.       /* Set the UART DMA Half transfer complete callback */
  1448.       huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  1449.  
  1450.       /* Set the DMA error callback */
  1451.       huart->hdmatx->XferErrorCallback = UART_DMAError;
  1452.  
  1453.       /* Set the DMA abort callback */
  1454.       huart->hdmatx->XferAbortCallback = NULL;
  1455.  
  1456.       /* Enable the UART transmit DMA channel */
  1457.       if (HAL_DMA_Start_IT(huart->hdmatx, (uint32_t)huart->pTxBuffPtr, (uint32_t)&huart->Instance->TDR, Size) != HAL_OK)
  1458.       {
  1459.         /* Set error code to DMA */
  1460.         huart->ErrorCode = HAL_UART_ERROR_DMA;
  1461.  
  1462.         __HAL_UNLOCK(huart);
  1463.  
  1464.         /* Restore huart->gState to ready */
  1465.         huart->gState = HAL_UART_STATE_READY;
  1466.  
  1467.         return HAL_ERROR;
  1468.       }
  1469.     }
  1470.     /* Clear the TC flag in the ICR register */
  1471.     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_TCF);
  1472.  
  1473.     __HAL_UNLOCK(huart);
  1474.  
  1475.     /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1476.     in the UART CR3 register */
  1477.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1478.  
  1479.     return HAL_OK;
  1480.   }
  1481.   else
  1482.   {
  1483.     return HAL_BUSY;
  1484.   }
  1485. }
  1486.  
  1487. /**
  1488.   * @brief Receive an amount of data in DMA mode.
  1489.   * @note   When the UART parity is enabled (PCE = 1), the received data contain
  1490.   *         the parity bit (MSB position).
  1491.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1492.   *         the received data is handled as a set of u16. In this case, Size must indicate the number
  1493.   *         of u16 available through pData.
  1494.   * @note   When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
  1495.   *         address of user data buffer for storing data to be received, should be aligned on a half word frontier (16 bits)
  1496.   *         (as received data will be handled by DMA from halfword frontier). Depending on compilation chain,
  1497.   *         use of specific alignment compilation directives or pragmas might be required to ensure proper alignment for pData.
  1498.   * @param huart UART handle.
  1499.   * @param pData Pointer to data buffer (u8 or u16 data elements).
  1500.   * @param Size  Amount of data elements (u8 or u16) to be received.
  1501.   * @retval HAL status
  1502.   */
  1503. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1504. {
  1505.   /* Check that a Rx process is not already ongoing */
  1506.   if (huart->RxState == HAL_UART_STATE_READY)
  1507.   {
  1508.     if ((pData == NULL) || (Size == 0U))
  1509.     {
  1510.       return HAL_ERROR;
  1511.     }
  1512.  
  1513.     /* In case of 9bits/No Parity transfer, pData buffer provided as input parameter
  1514.        should be aligned on a u16 frontier, as data copy from RDR will be
  1515.        handled by DMA from a u16 frontier. */
  1516.     if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
  1517.     {
  1518.       if ((((uint32_t)pData) & 1U) != 0U)
  1519.       {
  1520.         return  HAL_ERROR;
  1521.       }
  1522.     }
  1523.  
  1524.     __HAL_LOCK(huart);
  1525.  
  1526.     huart->pRxBuffPtr = pData;
  1527.     huart->RxXferSize = Size;
  1528.  
  1529.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1530.     huart->RxState = HAL_UART_STATE_BUSY_RX;
  1531.  
  1532.     if (huart->hdmarx != NULL)
  1533.     {
  1534.       /* Set the UART DMA transfer complete callback */
  1535.       huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  1536.  
  1537.       /* Set the UART DMA Half transfer complete callback */
  1538.       huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  1539.  
  1540.       /* Set the DMA error callback */
  1541.       huart->hdmarx->XferErrorCallback = UART_DMAError;
  1542.  
  1543.       /* Set the DMA abort callback */
  1544.       huart->hdmarx->XferAbortCallback = NULL;
  1545.  
  1546.       /* Enable the DMA channel */
  1547.       if (HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->RDR, (uint32_t)huart->pRxBuffPtr, Size) != HAL_OK)
  1548.       {
  1549.         /* Set error code to DMA */
  1550.         huart->ErrorCode = HAL_UART_ERROR_DMA;
  1551.  
  1552.         __HAL_UNLOCK(huart);
  1553.  
  1554.         /* Restore huart->gState to ready */
  1555.         huart->gState = HAL_UART_STATE_READY;
  1556.  
  1557.         return HAL_ERROR;
  1558.       }
  1559.     }
  1560.     __HAL_UNLOCK(huart);
  1561.  
  1562.     /* Enable the UART Parity Error Interrupt */
  1563.     SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1564.  
  1565.     /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1566.     SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1567.  
  1568.     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1569.     in the UART CR3 register */
  1570.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1571.  
  1572.     return HAL_OK;
  1573.   }
  1574.   else
  1575.   {
  1576.     return HAL_BUSY;
  1577.   }
  1578. }
  1579.  
  1580. /**
  1581.   * @brief Pause the DMA Transfer.
  1582.   * @param huart UART handle.
  1583.   * @retval HAL status
  1584.   */
  1585. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  1586. {
  1587.   const HAL_UART_StateTypeDef gstate = huart->gState;
  1588.   const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1589.  
  1590.   __HAL_LOCK(huart);
  1591.  
  1592.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1593.       (gstate == HAL_UART_STATE_BUSY_TX))
  1594.   {
  1595.     /* Disable the UART DMA Tx request */
  1596.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1597.   }
  1598.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1599.       (rxstate == HAL_UART_STATE_BUSY_RX))
  1600.   {
  1601.     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1602.     CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1603.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1604.  
  1605.     /* Disable the UART DMA Rx request */
  1606.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1607.   }
  1608.  
  1609.   __HAL_UNLOCK(huart);
  1610.  
  1611.   return HAL_OK;
  1612. }
  1613.  
  1614. /**
  1615.   * @brief Resume the DMA Transfer.
  1616.   * @param huart UART handle.
  1617.   * @retval HAL status
  1618.   */
  1619. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  1620. {
  1621.   __HAL_LOCK(huart);
  1622.  
  1623.   if (huart->gState == HAL_UART_STATE_BUSY_TX)
  1624.   {
  1625.     /* Enable the UART DMA Tx request */
  1626.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1627.   }
  1628.   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  1629.   {
  1630.     /* Clear the Overrun flag before resuming the Rx transfer */
  1631.     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  1632.  
  1633.     /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1634.     SET_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  1635.     SET_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1636.  
  1637.     /* Enable the UART DMA Rx request */
  1638.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1639.   }
  1640.  
  1641.   __HAL_UNLOCK(huart);
  1642.  
  1643.   return HAL_OK;
  1644. }
  1645.  
  1646. /**
  1647.   * @brief Stop the DMA Transfer.
  1648.   * @param huart UART handle.
  1649.   * @retval HAL status
  1650.   */
  1651. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  1652. {
  1653.   /* The Lock is not implemented on this API to allow the user application
  1654.      to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback() /
  1655.      HAL_UART_TxHalfCpltCallback / HAL_UART_RxHalfCpltCallback:
  1656.      indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1657.      interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1658.      the stream and the corresponding call back is executed. */
  1659.  
  1660.   const HAL_UART_StateTypeDef gstate = huart->gState;
  1661.   const HAL_UART_StateTypeDef rxstate = huart->RxState;
  1662.  
  1663.   /* Stop UART DMA Tx request if ongoing */
  1664.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  1665.       (gstate == HAL_UART_STATE_BUSY_TX))
  1666.   {
  1667.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1668.  
  1669.     /* Abort the UART DMA Tx channel */
  1670.     if (huart->hdmatx != NULL)
  1671.     {
  1672.       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1673.       {
  1674.         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1675.         {
  1676.           /* Set error code to DMA */
  1677.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1678.  
  1679.           return HAL_TIMEOUT;
  1680.         }
  1681.       }
  1682.     }
  1683.  
  1684.     UART_EndTxTransfer(huart);
  1685.   }
  1686.  
  1687.   /* Stop UART DMA Rx request if ongoing */
  1688.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  1689.       (rxstate == HAL_UART_STATE_BUSY_RX))
  1690.   {
  1691.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1692.  
  1693.     /* Abort the UART DMA Rx channel */
  1694.     if (huart->hdmarx != NULL)
  1695.     {
  1696.       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1697.       {
  1698.         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1699.         {
  1700.           /* Set error code to DMA */
  1701.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1702.  
  1703.           return HAL_TIMEOUT;
  1704.         }
  1705.       }
  1706.     }
  1707.  
  1708.     UART_EndRxTransfer(huart);
  1709.   }
  1710.  
  1711.   return HAL_OK;
  1712. }
  1713.  
  1714. /**
  1715.   * @brief  Abort ongoing transfers (blocking mode).
  1716.   * @param  huart UART handle.
  1717.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1718.   *         This procedure performs following operations :
  1719.   *           - Disable UART Interrupts (Tx and Rx)
  1720.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1721.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1722.   *           - Set handle State to READY
  1723.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1724.   * @retval HAL status
  1725.   */
  1726. HAL_StatusTypeDef HAL_UART_Abort(UART_HandleTypeDef *huart)
  1727. {
  1728.   /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1729.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1730.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1731.  
  1732.   /* Disable the UART DMA Tx request if enabled */
  1733.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1734.   {
  1735.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1736.  
  1737.     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1738.     if (huart->hdmatx != NULL)
  1739.     {
  1740.       /* Set the UART DMA Abort callback to Null.
  1741.          No call back execution at end of DMA abort procedure */
  1742.       huart->hdmatx->XferAbortCallback = NULL;
  1743.  
  1744.       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1745.       {
  1746.         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1747.         {
  1748.           /* Set error code to DMA */
  1749.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1750.  
  1751.           return HAL_TIMEOUT;
  1752.         }
  1753.       }
  1754.     }
  1755.   }
  1756.  
  1757.   /* Disable the UART DMA Rx request if enabled */
  1758.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1759.   {
  1760.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1761.  
  1762.     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1763.     if (huart->hdmarx != NULL)
  1764.     {
  1765.       /* Set the UART DMA Abort callback to Null.
  1766.          No call back execution at end of DMA abort procedure */
  1767.       huart->hdmarx->XferAbortCallback = NULL;
  1768.  
  1769.       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1770.       {
  1771.         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1772.         {
  1773.           /* Set error code to DMA */
  1774.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1775.  
  1776.           return HAL_TIMEOUT;
  1777.         }
  1778.       }
  1779.     }
  1780.   }
  1781.  
  1782.   /* Reset Tx and Rx transfer counters */
  1783.   huart->TxXferCount = 0U;
  1784.   huart->RxXferCount = 0U;
  1785.  
  1786.   /* Clear the Error flags in the ICR register */
  1787.   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1788.  
  1789.  
  1790.   /* Discard the received data */
  1791.   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1792.  
  1793.   /* Restore huart->gState and huart->RxState to Ready */
  1794.   huart->gState  = HAL_UART_STATE_READY;
  1795.   huart->RxState = HAL_UART_STATE_READY;
  1796.  
  1797.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  1798.  
  1799.   return HAL_OK;
  1800. }
  1801.  
  1802. /**
  1803.   * @brief  Abort ongoing Transmit transfer (blocking mode).
  1804.   * @param  huart UART handle.
  1805.   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1806.   *         This procedure performs following operations :
  1807.   *           - Disable UART Interrupts (Tx)
  1808.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1809.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1810.   *           - Set handle State to READY
  1811.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1812.   * @retval HAL status
  1813.   */
  1814. HAL_StatusTypeDef HAL_UART_AbortTransmit(UART_HandleTypeDef *huart)
  1815. {
  1816.   /* Disable TXEIE and TCIE interrupts */
  1817.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1818.  
  1819.   /* Disable the UART DMA Tx request if enabled */
  1820.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1821.   {
  1822.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1823.  
  1824.     /* Abort the UART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1825.     if (huart->hdmatx != NULL)
  1826.     {
  1827.       /* Set the UART DMA Abort callback to Null.
  1828.          No call back execution at end of DMA abort procedure */
  1829.       huart->hdmatx->XferAbortCallback = NULL;
  1830.  
  1831.       if (HAL_DMA_Abort(huart->hdmatx) != HAL_OK)
  1832.       {
  1833.         if (HAL_DMA_GetError(huart->hdmatx) == HAL_DMA_ERROR_TIMEOUT)
  1834.         {
  1835.           /* Set error code to DMA */
  1836.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1837.  
  1838.           return HAL_TIMEOUT;
  1839.         }
  1840.       }
  1841.     }
  1842.   }
  1843.  
  1844.   /* Reset Tx transfer counter */
  1845.   huart->TxXferCount = 0U;
  1846.  
  1847.  
  1848.   /* Restore huart->gState to Ready */
  1849.   huart->gState = HAL_UART_STATE_READY;
  1850.  
  1851.   return HAL_OK;
  1852. }
  1853.  
  1854. /**
  1855.   * @brief  Abort ongoing Receive transfer (blocking mode).
  1856.   * @param  huart UART handle.
  1857.   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1858.   *         This procedure performs following operations :
  1859.   *           - Disable UART Interrupts (Rx)
  1860.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1861.   *           - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1862.   *           - Set handle State to READY
  1863.   * @note   This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1864.   * @retval HAL status
  1865.   */
  1866. HAL_StatusTypeDef HAL_UART_AbortReceive(UART_HandleTypeDef *huart)
  1867. {
  1868.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1869.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1870.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1871.  
  1872.   /* Disable the UART DMA Rx request if enabled */
  1873.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1874.   {
  1875.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1876.  
  1877.     /* Abort the UART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1878.     if (huart->hdmarx != NULL)
  1879.     {
  1880.       /* Set the UART DMA Abort callback to Null.
  1881.          No call back execution at end of DMA abort procedure */
  1882.       huart->hdmarx->XferAbortCallback = NULL;
  1883.  
  1884.       if (HAL_DMA_Abort(huart->hdmarx) != HAL_OK)
  1885.       {
  1886.         if (HAL_DMA_GetError(huart->hdmarx) == HAL_DMA_ERROR_TIMEOUT)
  1887.         {
  1888.           /* Set error code to DMA */
  1889.           huart->ErrorCode = HAL_UART_ERROR_DMA;
  1890.  
  1891.           return HAL_TIMEOUT;
  1892.         }
  1893.       }
  1894.     }
  1895.   }
  1896.  
  1897.   /* Reset Rx transfer counter */
  1898.   huart->RxXferCount = 0U;
  1899.  
  1900.   /* Clear the Error flags in the ICR register */
  1901.   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  1902.  
  1903.   /* Discard the received data */
  1904.   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  1905.  
  1906.   /* Restore huart->RxState to Ready */
  1907.   huart->RxState = HAL_UART_STATE_READY;
  1908.  
  1909.   return HAL_OK;
  1910. }
  1911.  
  1912. /**
  1913.   * @brief  Abort ongoing transfers (Interrupt mode).
  1914.   * @param  huart UART handle.
  1915.   * @note   This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1916.   *         This procedure performs following operations :
  1917.   *           - Disable UART Interrupts (Tx and Rx)
  1918.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  1919.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1920.   *           - Set handle State to READY
  1921.   *           - At abort completion, call user abort complete callback
  1922.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1923.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  1924.   * @retval HAL status
  1925.   */
  1926. HAL_StatusTypeDef HAL_UART_Abort_IT(UART_HandleTypeDef *huart)
  1927. {
  1928.   uint32_t abortcplt = 1U;
  1929.  
  1930.   /* Disable interrupts */
  1931.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1932.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  1933.  
  1934.   /* If DMA Tx and/or DMA Rx Handles are associated to UART Handle, DMA Abort complete callbacks should be initialised
  1935.      before any call to DMA Abort functions */
  1936.   /* DMA Tx Handle is valid */
  1937.   if (huart->hdmatx != NULL)
  1938.   {
  1939.     /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
  1940.        Otherwise, set it to NULL */
  1941.     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1942.     {
  1943.       huart->hdmatx->XferAbortCallback = UART_DMATxAbortCallback;
  1944.     }
  1945.     else
  1946.     {
  1947.       huart->hdmatx->XferAbortCallback = NULL;
  1948.     }
  1949.   }
  1950.   /* DMA Rx Handle is valid */
  1951.   if (huart->hdmarx != NULL)
  1952.   {
  1953.     /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
  1954.        Otherwise, set it to NULL */
  1955.     if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1956.     {
  1957.       huart->hdmarx->XferAbortCallback = UART_DMARxAbortCallback;
  1958.     }
  1959.     else
  1960.     {
  1961.       huart->hdmarx->XferAbortCallback = NULL;
  1962.     }
  1963.   }
  1964.  
  1965.   /* Disable the UART DMA Tx request if enabled */
  1966.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  1967.   {
  1968.     /* Disable DMA Tx at UART level */
  1969.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1970.  
  1971.     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1972.     if (huart->hdmatx != NULL)
  1973.     {
  1974.       /* UART Tx DMA Abort callback has already been initialised :
  1975.          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1976.  
  1977.       /* Abort DMA TX */
  1978.       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  1979.       {
  1980.         huart->hdmatx->XferAbortCallback = NULL;
  1981.       }
  1982.       else
  1983.       {
  1984.         abortcplt = 0U;
  1985.       }
  1986.     }
  1987.   }
  1988.  
  1989.   /* Disable the UART DMA Rx request if enabled */
  1990.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  1991.   {
  1992.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1993.  
  1994.     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1995.     if (huart->hdmarx != NULL)
  1996.     {
  1997.       /* UART Rx DMA Abort callback has already been initialised :
  1998.          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  1999.  
  2000.       /* Abort DMA RX */
  2001.       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  2002.       {
  2003.         huart->hdmarx->XferAbortCallback = NULL;
  2004.         abortcplt = 1U;
  2005.       }
  2006.       else
  2007.       {
  2008.         abortcplt = 0U;
  2009.       }
  2010.     }
  2011.   }
  2012.  
  2013.   /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  2014.   if (abortcplt == 1U)
  2015.   {
  2016.     /* Reset Tx and Rx transfer counters */
  2017.     huart->TxXferCount = 0U;
  2018.     huart->RxXferCount = 0U;
  2019.  
  2020.     /* Clear ISR function pointers */
  2021.     huart->RxISR = NULL;
  2022.     huart->TxISR = NULL;
  2023.  
  2024.     /* Reset errorCode */
  2025.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  2026.  
  2027.     /* Clear the Error flags in the ICR register */
  2028.     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2029.  
  2030.  
  2031.     /* Discard the received data */
  2032.     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  2033.  
  2034.     /* Restore huart->gState and huart->RxState to Ready */
  2035.     huart->gState  = HAL_UART_STATE_READY;
  2036.     huart->RxState = HAL_UART_STATE_READY;
  2037.  
  2038.     /* As no DMA to be aborted, call directly user Abort complete callback */
  2039. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2040.     /* Call registered Abort complete callback */
  2041.     huart->AbortCpltCallback(huart);
  2042. #else
  2043.     /* Call legacy weak Abort complete callback */
  2044.     HAL_UART_AbortCpltCallback(huart);
  2045. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2046.   }
  2047.  
  2048.   return HAL_OK;
  2049. }
  2050.  
  2051. /**
  2052.   * @brief  Abort ongoing Transmit transfer (Interrupt mode).
  2053.   * @param  huart UART handle.
  2054.   * @note   This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  2055.   *         This procedure performs following operations :
  2056.   *           - Disable UART Interrupts (Tx)
  2057.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  2058.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  2059.   *           - Set handle State to READY
  2060.   *           - At abort completion, call user abort complete callback
  2061.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  2062.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  2063.   * @retval HAL status
  2064.   */
  2065. HAL_StatusTypeDef HAL_UART_AbortTransmit_IT(UART_HandleTypeDef *huart)
  2066. {
  2067.   /* Disable interrupts */
  2068.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  2069.  
  2070.   /* Disable the UART DMA Tx request if enabled */
  2071.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT))
  2072.   {
  2073.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  2074.  
  2075.     /* Abort the UART DMA Tx channel : use non blocking DMA Abort API (callback) */
  2076.     if (huart->hdmatx != NULL)
  2077.     {
  2078.       /* Set the UART DMA Abort callback :
  2079.          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  2080.       huart->hdmatx->XferAbortCallback = UART_DMATxOnlyAbortCallback;
  2081.  
  2082.       /* Abort DMA TX */
  2083.       if (HAL_DMA_Abort_IT(huart->hdmatx) != HAL_OK)
  2084.       {
  2085.         /* Call Directly huart->hdmatx->XferAbortCallback function in case of error */
  2086.         huart->hdmatx->XferAbortCallback(huart->hdmatx);
  2087.       }
  2088.     }
  2089.     else
  2090.     {
  2091.       /* Reset Tx transfer counter */
  2092.       huart->TxXferCount = 0U;
  2093.  
  2094.       /* Clear TxISR function pointers */
  2095.       huart->TxISR = NULL;
  2096.  
  2097.       /* Restore huart->gState to Ready */
  2098.       huart->gState = HAL_UART_STATE_READY;
  2099.  
  2100.       /* As no DMA to be aborted, call directly user Abort complete callback */
  2101. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2102.       /* Call registered Abort Transmit Complete Callback */
  2103.       huart->AbortTransmitCpltCallback(huart);
  2104. #else
  2105.       /* Call legacy weak Abort Transmit Complete Callback */
  2106.       HAL_UART_AbortTransmitCpltCallback(huart);
  2107. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2108.     }
  2109.   }
  2110.   else
  2111.   {
  2112.     /* Reset Tx transfer counter */
  2113.     huart->TxXferCount = 0U;
  2114.  
  2115.     /* Clear TxISR function pointers */
  2116.     huart->TxISR = NULL;
  2117.  
  2118.  
  2119.     /* Restore huart->gState to Ready */
  2120.     huart->gState = HAL_UART_STATE_READY;
  2121.  
  2122.     /* As no DMA to be aborted, call directly user Abort complete callback */
  2123. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2124.     /* Call registered Abort Transmit Complete Callback */
  2125.     huart->AbortTransmitCpltCallback(huart);
  2126. #else
  2127.     /* Call legacy weak Abort Transmit Complete Callback */
  2128.     HAL_UART_AbortTransmitCpltCallback(huart);
  2129. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2130.   }
  2131.  
  2132.   return HAL_OK;
  2133. }
  2134.  
  2135. /**
  2136.   * @brief  Abort ongoing Receive transfer (Interrupt mode).
  2137.   * @param  huart UART handle.
  2138.   * @note   This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  2139.   *         This procedure performs following operations :
  2140.   *           - Disable UART Interrupts (Rx)
  2141.   *           - Disable the DMA transfer in the peripheral register (if enabled)
  2142.   *           - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  2143.   *           - Set handle State to READY
  2144.   *           - At abort completion, call user abort complete callback
  2145.   * @note   This procedure is executed in Interrupt mode, meaning that abort procedure could be
  2146.   *         considered as completed only when user abort complete callback is executed (not when exiting function).
  2147.   * @retval HAL status
  2148.   */
  2149. HAL_StatusTypeDef HAL_UART_AbortReceive_IT(UART_HandleTypeDef *huart)
  2150. {
  2151.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  2152.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2153.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  2154.  
  2155.   /* Disable the UART DMA Rx request if enabled */
  2156.   if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  2157.   {
  2158.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2159.  
  2160.     /* Abort the UART DMA Rx channel : use non blocking DMA Abort API (callback) */
  2161.     if (huart->hdmarx != NULL)
  2162.     {
  2163.       /* Set the UART DMA Abort callback :
  2164.          will lead to call HAL_UART_AbortCpltCallback() at end of DMA abort procedure */
  2165.       huart->hdmarx->XferAbortCallback = UART_DMARxOnlyAbortCallback;
  2166.  
  2167.       /* Abort DMA RX */
  2168.       if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  2169.       {
  2170.         /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  2171.         huart->hdmarx->XferAbortCallback(huart->hdmarx);
  2172.       }
  2173.     }
  2174.     else
  2175.     {
  2176.       /* Reset Rx transfer counter */
  2177.       huart->RxXferCount = 0U;
  2178.  
  2179.       /* Clear RxISR function pointer */
  2180.       huart->pRxBuffPtr = NULL;
  2181.  
  2182.       /* Clear the Error flags in the ICR register */
  2183.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2184.  
  2185.       /* Discard the received data */
  2186.       __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  2187.  
  2188.       /* Restore huart->RxState to Ready */
  2189.       huart->RxState = HAL_UART_STATE_READY;
  2190.  
  2191.       /* As no DMA to be aborted, call directly user Abort complete callback */
  2192. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2193.       /* Call registered Abort Receive Complete Callback */
  2194.       huart->AbortReceiveCpltCallback(huart);
  2195. #else
  2196.       /* Call legacy weak Abort Receive Complete Callback */
  2197.       HAL_UART_AbortReceiveCpltCallback(huart);
  2198. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2199.     }
  2200.   }
  2201.   else
  2202.   {
  2203.     /* Reset Rx transfer counter */
  2204.     huart->RxXferCount = 0U;
  2205.  
  2206.     /* Clear RxISR function pointer */
  2207.     huart->pRxBuffPtr = NULL;
  2208.  
  2209.     /* Clear the Error flags in the ICR register */
  2210.     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  2211.  
  2212.     /* Restore huart->RxState to Ready */
  2213.     huart->RxState = HAL_UART_STATE_READY;
  2214.  
  2215.     /* As no DMA to be aborted, call directly user Abort complete callback */
  2216. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2217.     /* Call registered Abort Receive Complete Callback */
  2218.     huart->AbortReceiveCpltCallback(huart);
  2219. #else
  2220.     /* Call legacy weak Abort Receive Complete Callback */
  2221.     HAL_UART_AbortReceiveCpltCallback(huart);
  2222. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2223.   }
  2224.  
  2225.   return HAL_OK;
  2226. }
  2227.  
  2228. /**
  2229.   * @brief Handle UART interrupt request.
  2230.   * @param huart UART handle.
  2231.   * @retval None
  2232.   */
  2233. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  2234. {
  2235.   uint32_t isrflags   = READ_REG(huart->Instance->ISR);
  2236.   uint32_t cr1its     = READ_REG(huart->Instance->CR1);
  2237.   uint32_t cr3its     = READ_REG(huart->Instance->CR3);
  2238.  
  2239.   uint32_t errorflags;
  2240.   uint32_t errorcode;
  2241.  
  2242.   /* If no error occurs */
  2243.   errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  2244.   if (errorflags == 0U)
  2245.   {
  2246.     /* UART in mode Receiver ---------------------------------------------------*/
  2247.     if (((isrflags & USART_ISR_RXNE) != 0U)
  2248.         && ((cr1its & USART_CR1_RXNEIE) != 0U))
  2249.     {
  2250.       if (huart->RxISR != NULL)
  2251.       {
  2252.         huart->RxISR(huart);
  2253.       }
  2254.       return;
  2255.     }
  2256.   }
  2257.  
  2258.   /* If some errors occur */
  2259.   if ((errorflags != 0U)
  2260.       && (((cr3its & USART_CR3_EIE) != 0U)
  2261.           || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != 0U)))
  2262.   {
  2263.     /* UART parity error interrupt occurred -------------------------------------*/
  2264.     if (((isrflags & USART_ISR_PE) != 0U) && ((cr1its & USART_CR1_PEIE) != 0U))
  2265.     {
  2266.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_PEF);
  2267.  
  2268.       huart->ErrorCode |= HAL_UART_ERROR_PE;
  2269.     }
  2270.  
  2271.     /* UART frame error interrupt occurred --------------------------------------*/
  2272.     if (((isrflags & USART_ISR_FE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  2273.     {
  2274.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_FEF);
  2275.  
  2276.       huart->ErrorCode |= HAL_UART_ERROR_FE;
  2277.     }
  2278.  
  2279.     /* UART noise error interrupt occurred --------------------------------------*/
  2280.     if (((isrflags & USART_ISR_NE) != 0U) && ((cr3its & USART_CR3_EIE) != 0U))
  2281.     {
  2282.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_NEF);
  2283.  
  2284.       huart->ErrorCode |= HAL_UART_ERROR_NE;
  2285.     }
  2286.  
  2287.     /* UART Over-Run interrupt occurred -----------------------------------------*/
  2288.     if (((isrflags & USART_ISR_ORE) != 0U)
  2289.         && (((cr1its & USART_CR1_RXNEIE) != 0U) ||
  2290.             ((cr3its & USART_CR3_EIE) != 0U)))
  2291.     {
  2292.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF);
  2293.  
  2294.       huart->ErrorCode |= HAL_UART_ERROR_ORE;
  2295.     }
  2296.  
  2297.     /* UART Receiver Timeout interrupt occurred ---------------------------------*/
  2298.     if (((isrflags & USART_ISR_RTOF) != 0U) && ((cr1its & USART_CR1_RTOIE) != 0U))
  2299.     {
  2300.       __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  2301.  
  2302.       huart->ErrorCode |= HAL_UART_ERROR_RTO;
  2303.     }
  2304.  
  2305.     /* Call UART Error Call back function if need be ----------------------------*/
  2306.     if (huart->ErrorCode != HAL_UART_ERROR_NONE)
  2307.     {
  2308.       /* UART in mode Receiver --------------------------------------------------*/
  2309.       if (((isrflags & USART_ISR_RXNE) != 0U)
  2310.           && ((cr1its & USART_CR1_RXNEIE) != 0U))
  2311.       {
  2312.         if (huart->RxISR != NULL)
  2313.         {
  2314.           huart->RxISR(huart);
  2315.         }
  2316.       }
  2317.  
  2318.       /* If Error is to be considered as blocking :
  2319.           - Receiver Timeout error in Reception
  2320.           - Overrun error in Reception
  2321.           - any error occurs in DMA mode reception
  2322.       */
  2323.       errorcode = huart->ErrorCode;
  2324.       if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) ||
  2325.           ((errorcode & (HAL_UART_ERROR_RTO | HAL_UART_ERROR_ORE)) != 0U))
  2326.       {
  2327.         /* Blocking error : transfer is aborted
  2328.            Set the UART state ready to be able to start again the process,
  2329.            Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  2330.         UART_EndRxTransfer(huart);
  2331.  
  2332.         /* Disable the UART DMA Rx request if enabled */
  2333.         if (HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR))
  2334.         {
  2335.           CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  2336.  
  2337.           /* Abort the UART DMA Rx channel */
  2338.           if (huart->hdmarx != NULL)
  2339.           {
  2340.             /* Set the UART DMA Abort callback :
  2341.                will lead to call HAL_UART_ErrorCallback() at end of DMA abort procedure */
  2342.             huart->hdmarx->XferAbortCallback = UART_DMAAbortOnError;
  2343.  
  2344.             /* Abort DMA RX */
  2345.             if (HAL_DMA_Abort_IT(huart->hdmarx) != HAL_OK)
  2346.             {
  2347.               /* Call Directly huart->hdmarx->XferAbortCallback function in case of error */
  2348.               huart->hdmarx->XferAbortCallback(huart->hdmarx);
  2349.             }
  2350.           }
  2351.           else
  2352.           {
  2353.             /* Call user error callback */
  2354. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2355.             /*Call registered error callback*/
  2356.             huart->ErrorCallback(huart);
  2357. #else
  2358.             /*Call legacy weak error callback*/
  2359.             HAL_UART_ErrorCallback(huart);
  2360. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2361.  
  2362.           }
  2363.         }
  2364.         else
  2365.         {
  2366.           /* Call user error callback */
  2367. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2368.           /*Call registered error callback*/
  2369.           huart->ErrorCallback(huart);
  2370. #else
  2371.           /*Call legacy weak error callback*/
  2372.           HAL_UART_ErrorCallback(huart);
  2373. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2374.         }
  2375.       }
  2376.       else
  2377.       {
  2378.         /* Non Blocking error : transfer could go on.
  2379.            Error is notified to user through user error callback */
  2380. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2381.         /*Call registered error callback*/
  2382.         huart->ErrorCallback(huart);
  2383. #else
  2384.         /*Call legacy weak error callback*/
  2385.         HAL_UART_ErrorCallback(huart);
  2386. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2387.         huart->ErrorCode = HAL_UART_ERROR_NONE;
  2388.       }
  2389.     }
  2390.     return;
  2391.  
  2392.   } /* End if some error occurs */
  2393. #if defined(USART_CR1_UESM)
  2394.  
  2395.   /* UART wakeup from Stop mode interrupt occurred ---------------------------*/
  2396.   if (((isrflags & USART_ISR_WUF) != 0U) && ((cr3its & USART_CR3_WUFIE) != 0U))
  2397.   {
  2398.     __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_WUF);
  2399.  
  2400.     /* UART Rx state is not reset as a reception process might be ongoing.
  2401.        If UART handle state fields need to be reset to READY, this could be done in Wakeup callback */
  2402.  
  2403. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2404.     /* Call registered Wakeup Callback */
  2405.     huart->WakeupCallback(huart);
  2406. #else
  2407.     /* Call legacy weak Wakeup Callback */
  2408.     HAL_UARTEx_WakeupCallback(huart);
  2409. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2410.     return;
  2411.   }
  2412. #endif /* USART_CR1_UESM */
  2413.  
  2414.   /* UART in mode Transmitter ------------------------------------------------*/
  2415.   if (((isrflags & USART_ISR_TXE) != 0U)
  2416.       && ((cr1its & USART_CR1_TXEIE) != 0U))
  2417.   {
  2418.     if (huart->TxISR != NULL)
  2419.     {
  2420.       huart->TxISR(huart);
  2421.     }
  2422.     return;
  2423.   }
  2424.  
  2425.   /* UART in mode Transmitter (transmission end) -----------------------------*/
  2426.   if (((isrflags & USART_ISR_TC) != 0U) && ((cr1its & USART_CR1_TCIE) != 0U))
  2427.   {
  2428.     UART_EndTransmit_IT(huart);
  2429.     return;
  2430.   }
  2431.  
  2432. }
  2433.  
  2434. /**
  2435.   * @brief Tx Transfer completed callback.
  2436.   * @param huart UART handle.
  2437.   * @retval None
  2438.   */
  2439. __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  2440. {
  2441.   /* Prevent unused argument(s) compilation warning */
  2442.   UNUSED(huart);
  2443.  
  2444.   /* NOTE : This function should not be modified, when the callback is needed,
  2445.             the HAL_UART_TxCpltCallback can be implemented in the user file.
  2446.    */
  2447. }
  2448.  
  2449. /**
  2450.   * @brief  Tx Half Transfer completed callback.
  2451.   * @param  huart UART handle.
  2452.   * @retval None
  2453.   */
  2454. __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  2455. {
  2456.   /* Prevent unused argument(s) compilation warning */
  2457.   UNUSED(huart);
  2458.  
  2459.   /* NOTE: This function should not be modified, when the callback is needed,
  2460.            the HAL_UART_TxHalfCpltCallback can be implemented in the user file.
  2461.    */
  2462. }
  2463.  
  2464. /**
  2465.   * @brief  Rx Transfer completed callback.
  2466.   * @param  huart UART handle.
  2467.   * @retval None
  2468.   */
  2469. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  2470. {
  2471.   /* Prevent unused argument(s) compilation warning */
  2472.   UNUSED(huart);
  2473.  
  2474.   /* NOTE : This function should not be modified, when the callback is needed,
  2475.             the HAL_UART_RxCpltCallback can be implemented in the user file.
  2476.    */
  2477. }
  2478.  
  2479. /**
  2480.   * @brief  Rx Half Transfer completed callback.
  2481.   * @param  huart UART handle.
  2482.   * @retval None
  2483.   */
  2484. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  2485. {
  2486.   /* Prevent unused argument(s) compilation warning */
  2487.   UNUSED(huart);
  2488.  
  2489.   /* NOTE: This function should not be modified, when the callback is needed,
  2490.            the HAL_UART_RxHalfCpltCallback can be implemented in the user file.
  2491.    */
  2492. }
  2493.  
  2494. /**
  2495.   * @brief  UART error callback.
  2496.   * @param  huart UART handle.
  2497.   * @retval None
  2498.   */
  2499. __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  2500. {
  2501.   /* Prevent unused argument(s) compilation warning */
  2502.   UNUSED(huart);
  2503.  
  2504.   /* NOTE : This function should not be modified, when the callback is needed,
  2505.             the HAL_UART_ErrorCallback can be implemented in the user file.
  2506.    */
  2507. }
  2508.  
  2509. /**
  2510.   * @brief  UART Abort Complete callback.
  2511.   * @param  huart UART handle.
  2512.   * @retval None
  2513.   */
  2514. __weak void HAL_UART_AbortCpltCallback(UART_HandleTypeDef *huart)
  2515. {
  2516.   /* Prevent unused argument(s) compilation warning */
  2517.   UNUSED(huart);
  2518.  
  2519.   /* NOTE : This function should not be modified, when the callback is needed,
  2520.             the HAL_UART_AbortCpltCallback can be implemented in the user file.
  2521.    */
  2522. }
  2523.  
  2524. /**
  2525.   * @brief  UART Abort Complete callback.
  2526.   * @param  huart UART handle.
  2527.   * @retval None
  2528.   */
  2529. __weak void HAL_UART_AbortTransmitCpltCallback(UART_HandleTypeDef *huart)
  2530. {
  2531.   /* Prevent unused argument(s) compilation warning */
  2532.   UNUSED(huart);
  2533.  
  2534.   /* NOTE : This function should not be modified, when the callback is needed,
  2535.             the HAL_UART_AbortTransmitCpltCallback can be implemented in the user file.
  2536.    */
  2537. }
  2538.  
  2539. /**
  2540.   * @brief  UART Abort Receive Complete callback.
  2541.   * @param  huart UART handle.
  2542.   * @retval None
  2543.   */
  2544. __weak void HAL_UART_AbortReceiveCpltCallback(UART_HandleTypeDef *huart)
  2545. {
  2546.   /* Prevent unused argument(s) compilation warning */
  2547.   UNUSED(huart);
  2548.  
  2549.   /* NOTE : This function should not be modified, when the callback is needed,
  2550.             the HAL_UART_AbortReceiveCpltCallback can be implemented in the user file.
  2551.    */
  2552. }
  2553.  
  2554. /**
  2555.   * @}
  2556.   */
  2557.  
  2558. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  2559.   *  @brief   UART control functions
  2560.   *
  2561. @verbatim
  2562.  ===============================================================================
  2563.                       ##### Peripheral Control functions #####
  2564.  ===============================================================================
  2565.     [..]
  2566.     This subsection provides a set of functions allowing to control the UART.
  2567.      (+) HAL_UART_ReceiverTimeout_Config() API allows to configure the receiver timeout value on the fly
  2568.      (+) HAL_UART_EnableReceiverTimeout() API enables the receiver timeout feature
  2569.      (+) HAL_UART_DisableReceiverTimeout() API disables the receiver timeout feature
  2570.      (+) HAL_MultiProcessor_EnableMuteMode() API enables mute mode
  2571.      (+) HAL_MultiProcessor_DisableMuteMode() API disables mute mode
  2572.      (+) HAL_MultiProcessor_EnterMuteMode() API enters mute mode
  2573.      (+) UART_SetConfig() API configures the UART peripheral
  2574.      (+) UART_AdvFeatureConfig() API optionally configures the UART advanced features
  2575.      (+) UART_CheckIdleState() API ensures that TEACK and/or REACK are set after initialization
  2576.      (+) HAL_HalfDuplex_EnableTransmitter() API disables receiver and enables transmitter
  2577.      (+) HAL_HalfDuplex_EnableReceiver() API disables transmitter and enables receiver
  2578.      (+) HAL_LIN_SendBreak() API transmits the break characters
  2579. @endverbatim
  2580.   * @{
  2581.   */
  2582.  
  2583. /**
  2584.   * @brief  Update on the fly the receiver timeout value in RTOR register.
  2585.   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  2586.   *                    the configuration information for the specified UART module.
  2587.   * @param  TimeoutValue receiver timeout value in number of baud blocks. The timeout
  2588.   *                     value must be less or equal to 0x0FFFFFFFF.
  2589.   * @retval None
  2590.   */
  2591. void HAL_UART_ReceiverTimeout_Config(UART_HandleTypeDef *huart, uint32_t TimeoutValue)
  2592. {
  2593.   assert_param(IS_UART_RECEIVER_TIMEOUT_VALUE(TimeoutValue));
  2594.   MODIFY_REG(huart->Instance->RTOR, USART_RTOR_RTO, TimeoutValue);
  2595. }
  2596.  
  2597. /**
  2598.   * @brief  Enable the UART receiver timeout feature.
  2599.   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  2600.   *                    the configuration information for the specified UART module.
  2601.   * @retval HAL status
  2602.   */
  2603. HAL_StatusTypeDef HAL_UART_EnableReceiverTimeout(UART_HandleTypeDef *huart)
  2604. {
  2605.   if (huart->gState == HAL_UART_STATE_READY)
  2606.   {
  2607.     /* Process Locked */
  2608.     __HAL_LOCK(huart);
  2609.  
  2610.     huart->gState = HAL_UART_STATE_BUSY;
  2611.  
  2612.     /* Set the USART RTOEN bit */
  2613.     SET_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2614.  
  2615.     huart->gState = HAL_UART_STATE_READY;
  2616.  
  2617.     /* Process Unlocked */
  2618.     __HAL_UNLOCK(huart);
  2619.  
  2620.     return HAL_OK;
  2621.   }
  2622.   else
  2623.   {
  2624.     return HAL_BUSY;
  2625.   }
  2626. }
  2627.  
  2628. /**
  2629.   * @brief  Disable the UART receiver timeout feature.
  2630.   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  2631.   *                    the configuration information for the specified UART module.
  2632.   * @retval HAL status
  2633.   */
  2634. HAL_StatusTypeDef HAL_UART_DisableReceiverTimeout(UART_HandleTypeDef *huart)
  2635. {
  2636.   if (huart->gState == HAL_UART_STATE_READY)
  2637.   {
  2638.     /* Process Locked */
  2639.     __HAL_LOCK(huart);
  2640.  
  2641.     huart->gState = HAL_UART_STATE_BUSY;
  2642.  
  2643.     /* Clear the USART RTOEN bit */
  2644.     CLEAR_BIT(huart->Instance->CR2, USART_CR2_RTOEN);
  2645.  
  2646.     huart->gState = HAL_UART_STATE_READY;
  2647.  
  2648.     /* Process Unlocked */
  2649.     __HAL_UNLOCK(huart);
  2650.  
  2651.     return HAL_OK;
  2652.   }
  2653.   else
  2654.   {
  2655.     return HAL_BUSY;
  2656.   }
  2657. }
  2658.  
  2659. /**
  2660.   * @brief  Enable UART in mute mode (does not mean UART enters mute mode;
  2661.   *         to enter mute mode, HAL_MultiProcessor_EnterMuteMode() API must be called).
  2662.   * @param  huart UART handle.
  2663.   * @retval HAL status
  2664.   */
  2665. HAL_StatusTypeDef HAL_MultiProcessor_EnableMuteMode(UART_HandleTypeDef *huart)
  2666. {
  2667.   __HAL_LOCK(huart);
  2668.  
  2669.   huart->gState = HAL_UART_STATE_BUSY;
  2670.  
  2671.   /* Enable USART mute mode by setting the MME bit in the CR1 register */
  2672.   SET_BIT(huart->Instance->CR1, USART_CR1_MME);
  2673.  
  2674.   huart->gState = HAL_UART_STATE_READY;
  2675.  
  2676.   return (UART_CheckIdleState(huart));
  2677. }
  2678.  
  2679. /**
  2680.   * @brief  Disable UART mute mode (does not mean the UART actually exits mute mode
  2681.   *         as it may not have been in mute mode at this very moment).
  2682.   * @param  huart UART handle.
  2683.   * @retval HAL status
  2684.   */
  2685. HAL_StatusTypeDef HAL_MultiProcessor_DisableMuteMode(UART_HandleTypeDef *huart)
  2686. {
  2687.   __HAL_LOCK(huart);
  2688.  
  2689.   huart->gState = HAL_UART_STATE_BUSY;
  2690.  
  2691.   /* Disable USART mute mode by clearing the MME bit in the CR1 register */
  2692.   CLEAR_BIT(huart->Instance->CR1, USART_CR1_MME);
  2693.  
  2694.   huart->gState = HAL_UART_STATE_READY;
  2695.  
  2696.   return (UART_CheckIdleState(huart));
  2697. }
  2698.  
  2699. /**
  2700.   * @brief Enter UART mute mode (means UART actually enters mute mode).
  2701.   * @note  To exit from mute mode, HAL_MultiProcessor_DisableMuteMode() API must be called.
  2702.   * @param huart UART handle.
  2703.   * @retval None
  2704.   */
  2705. void HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  2706. {
  2707.   __HAL_UART_SEND_REQ(huart, UART_MUTE_MODE_REQUEST);
  2708. }
  2709.  
  2710. /**
  2711.   * @brief  Enable the UART transmitter and disable the UART receiver.
  2712.   * @param  huart UART handle.
  2713.   * @retval HAL status
  2714.   */
  2715. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  2716. {
  2717.   __HAL_LOCK(huart);
  2718.   huart->gState = HAL_UART_STATE_BUSY;
  2719.  
  2720.   /* Clear TE and RE bits */
  2721.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2722.  
  2723.   /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  2724.   SET_BIT(huart->Instance->CR1, USART_CR1_TE);
  2725.  
  2726.   huart->gState = HAL_UART_STATE_READY;
  2727.  
  2728.   __HAL_UNLOCK(huart);
  2729.  
  2730.   return HAL_OK;
  2731. }
  2732.  
  2733. /**
  2734.   * @brief  Enable the UART receiver and disable the UART transmitter.
  2735.   * @param  huart UART handle.
  2736.   * @retval HAL status.
  2737.   */
  2738. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  2739. {
  2740.   __HAL_LOCK(huart);
  2741.   huart->gState = HAL_UART_STATE_BUSY;
  2742.  
  2743.   /* Clear TE and RE bits */
  2744.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TE | USART_CR1_RE));
  2745.  
  2746.   /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  2747.   SET_BIT(huart->Instance->CR1, USART_CR1_RE);
  2748.  
  2749.   huart->gState = HAL_UART_STATE_READY;
  2750.  
  2751.   __HAL_UNLOCK(huart);
  2752.  
  2753.   return HAL_OK;
  2754. }
  2755.  
  2756.  
  2757. #if   defined(USART_CR2_LINEN)
  2758. /**
  2759.   * @brief  Transmit break characters.
  2760.   * @param  huart UART handle.
  2761.   * @retval HAL status
  2762.   */
  2763. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  2764. {
  2765.   /* Check the parameters */
  2766.   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  2767.  
  2768.   __HAL_LOCK(huart);
  2769.  
  2770.   huart->gState = HAL_UART_STATE_BUSY;
  2771.  
  2772.   /* Send break characters */
  2773.   __HAL_UART_SEND_REQ(huart, UART_SENDBREAK_REQUEST);
  2774.  
  2775.   huart->gState = HAL_UART_STATE_READY;
  2776.  
  2777.   __HAL_UNLOCK(huart);
  2778.  
  2779.   return HAL_OK;
  2780. }
  2781. #endif /* USART_CR2_LINEN */
  2782.  
  2783. /**
  2784.   * @}
  2785.   */
  2786.  
  2787. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Error functions
  2788.   *  @brief   UART Peripheral State functions
  2789.   *
  2790. @verbatim
  2791.   ==============================================================================
  2792.             ##### Peripheral State and Error functions #####
  2793.   ==============================================================================
  2794.     [..]
  2795.     This subsection provides functions allowing to :
  2796.       (+) Return the UART handle state.
  2797.       (+) Return the UART handle error code
  2798.  
  2799. @endverbatim
  2800.   * @{
  2801.   */
  2802.  
  2803. /**
  2804.   * @brief Return the UART handle state.
  2805.   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  2806.   *               the configuration information for the specified UART.
  2807.   * @retval HAL state
  2808.   */
  2809. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  2810. {
  2811.   uint32_t temp1;
  2812.   uint32_t temp2;
  2813.   temp1 = huart->gState;
  2814.   temp2 = huart->RxState;
  2815.  
  2816.   return (HAL_UART_StateTypeDef)(temp1 | temp2);
  2817. }
  2818.  
  2819. /**
  2820.   * @brief  Return the UART handle error code.
  2821.   * @param  huart Pointer to a UART_HandleTypeDef structure that contains
  2822.   *               the configuration information for the specified UART.
  2823.   * @retval UART Error Code
  2824.   */
  2825. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  2826. {
  2827.   return huart->ErrorCode;
  2828. }
  2829. /**
  2830.   * @}
  2831.   */
  2832.  
  2833. /**
  2834.   * @}
  2835.   */
  2836.  
  2837. /** @defgroup UART_Private_Functions UART Private Functions
  2838.   * @{
  2839.   */
  2840.  
  2841. /**
  2842.   * @brief  Initialize the callbacks to their default values.
  2843.   * @param  huart UART handle.
  2844.   * @retval none
  2845.   */
  2846. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  2847. void UART_InitCallbacksToDefault(UART_HandleTypeDef *huart)
  2848. {
  2849.   /* Init the UART Callback settings */
  2850.   huart->TxHalfCpltCallback        = HAL_UART_TxHalfCpltCallback;        /* Legacy weak TxHalfCpltCallback        */
  2851.   huart->TxCpltCallback            = HAL_UART_TxCpltCallback;            /* Legacy weak TxCpltCallback            */
  2852.   huart->RxHalfCpltCallback        = HAL_UART_RxHalfCpltCallback;        /* Legacy weak RxHalfCpltCallback        */
  2853.   huart->RxCpltCallback            = HAL_UART_RxCpltCallback;            /* Legacy weak RxCpltCallback            */
  2854.   huart->ErrorCallback             = HAL_UART_ErrorCallback;             /* Legacy weak ErrorCallback             */
  2855.   huart->AbortCpltCallback         = HAL_UART_AbortCpltCallback;         /* Legacy weak AbortCpltCallback         */
  2856.   huart->AbortTransmitCpltCallback = HAL_UART_AbortTransmitCpltCallback; /* Legacy weak AbortTransmitCpltCallback */
  2857.   huart->AbortReceiveCpltCallback  = HAL_UART_AbortReceiveCpltCallback;  /* Legacy weak AbortReceiveCpltCallback  */
  2858. #if defined(USART_CR1_UESM)
  2859.   huart->WakeupCallback            = HAL_UARTEx_WakeupCallback;          /* Legacy weak WakeupCallback            */
  2860. #endif /* USART_CR1_UESM */
  2861.  
  2862. }
  2863. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  2864.  
  2865. /**
  2866.   * @brief Configure the UART peripheral.
  2867.   * @param huart UART handle.
  2868.   * @retval HAL status
  2869.   */
  2870. HAL_StatusTypeDef UART_SetConfig(UART_HandleTypeDef *huart)
  2871. {
  2872.   uint32_t tmpreg;
  2873.   uint16_t brrtemp;
  2874.   UART_ClockSourceTypeDef clocksource;
  2875.   uint32_t usartdiv                   = 0x00000000U;
  2876.   HAL_StatusTypeDef ret               = HAL_OK;
  2877.   uint32_t pclk;
  2878.  
  2879.   /* Check the parameters */
  2880.   assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));
  2881.   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  2882.   assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  2883.   assert_param(IS_UART_ONE_BIT_SAMPLE(huart->Init.OneBitSampling));
  2884.  
  2885.   assert_param(IS_UART_PARITY(huart->Init.Parity));
  2886.   assert_param(IS_UART_MODE(huart->Init.Mode));
  2887.   assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  2888.   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  2889.  
  2890.   /*-------------------------- USART CR1 Configuration -----------------------*/
  2891.   /* Clear M, PCE, PS, TE, RE and OVER8 bits and configure
  2892.   *  the UART Word Length, Parity, Mode and oversampling:
  2893.   *  set the M bits according to huart->Init.WordLength value
  2894.   *  set PCE and PS bits according to huart->Init.Parity value
  2895.   *  set TE and RE bits according to huart->Init.Mode value
  2896.   *  set OVER8 bit according to huart->Init.OverSampling value */
  2897.   tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode | huart->Init.OverSampling ;
  2898.   MODIFY_REG(huart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  2899.  
  2900.   /*-------------------------- USART CR2 Configuration -----------------------*/
  2901.   /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  2902.   * to huart->Init.StopBits value */
  2903.   MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  2904.  
  2905.   /*-------------------------- USART CR3 Configuration -----------------------*/
  2906.   /* Configure
  2907.   * - UART HardWare Flow Control: set CTSE and RTSE bits according
  2908.   *   to huart->Init.HwFlowCtl value
  2909.   * - one-bit sampling method versus three samples' majority rule according
  2910.   *   to huart->Init.OneBitSampling (not applicable to LPUART) */
  2911.   tmpreg = (uint32_t)huart->Init.HwFlowCtl;
  2912.  
  2913.   tmpreg |= huart->Init.OneBitSampling;
  2914.   MODIFY_REG(huart->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  2915.  
  2916.  
  2917.   /*-------------------------- USART BRR Configuration -----------------------*/
  2918.   UART_GETCLOCKSOURCE(huart, clocksource);
  2919.  
  2920.   if (huart->Init.OverSampling == UART_OVERSAMPLING_8)
  2921.   {
  2922.     switch (clocksource)
  2923.     {
  2924.       case UART_CLOCKSOURCE_PCLK1:
  2925.         pclk = HAL_RCC_GetPCLK1Freq();
  2926.         usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate));
  2927.         break;
  2928.       case UART_CLOCKSOURCE_HSI:
  2929.         usartdiv = (uint16_t)(UART_DIV_SAMPLING8(HSI_VALUE, huart->Init.BaudRate));
  2930.         break;
  2931.       case UART_CLOCKSOURCE_SYSCLK:
  2932.         pclk = HAL_RCC_GetSysClockFreq();
  2933.         usartdiv = (uint16_t)(UART_DIV_SAMPLING8(pclk, huart->Init.BaudRate));
  2934.         break;
  2935.       case UART_CLOCKSOURCE_LSE:
  2936.         usartdiv = (uint16_t)(UART_DIV_SAMPLING8(LSE_VALUE, huart->Init.BaudRate));
  2937.         break;
  2938.       default:
  2939.         ret = HAL_ERROR;
  2940.         break;
  2941.     }
  2942.  
  2943.     /* USARTDIV must be greater than or equal to 0d16 */
  2944.     if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2945.     {
  2946.       brrtemp = (uint16_t)(usartdiv & 0xFFF0U);
  2947.       brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  2948.       huart->Instance->BRR = brrtemp;
  2949.     }
  2950.     else
  2951.     {
  2952.       ret = HAL_ERROR;
  2953.     }
  2954.   }
  2955.   else
  2956.   {
  2957.     switch (clocksource)
  2958.     {
  2959.       case UART_CLOCKSOURCE_PCLK1:
  2960.         pclk = HAL_RCC_GetPCLK1Freq();
  2961.         usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate));
  2962.         break;
  2963.       case UART_CLOCKSOURCE_HSI:
  2964.         usartdiv = (uint16_t)(UART_DIV_SAMPLING16(HSI_VALUE, huart->Init.BaudRate));
  2965.         break;
  2966.       case UART_CLOCKSOURCE_SYSCLK:
  2967.         pclk = HAL_RCC_GetSysClockFreq();
  2968.         usartdiv = (uint16_t)(UART_DIV_SAMPLING16(pclk, huart->Init.BaudRate));
  2969.         break;
  2970.       case UART_CLOCKSOURCE_LSE:
  2971.         usartdiv = (uint16_t)(UART_DIV_SAMPLING16(LSE_VALUE, huart->Init.BaudRate));
  2972.         break;
  2973.       default:
  2974.         ret = HAL_ERROR;
  2975.         break;
  2976.     }
  2977.  
  2978.     /* USARTDIV must be greater than or equal to 0d16 */
  2979.     if ((usartdiv >= UART_BRR_MIN) && (usartdiv <= UART_BRR_MAX))
  2980.     {
  2981.       huart->Instance->BRR = usartdiv;
  2982.     }
  2983.     else
  2984.     {
  2985.       ret = HAL_ERROR;
  2986.     }
  2987.   }
  2988.  
  2989.  
  2990.   /* Clear ISR function pointers */
  2991.   huart->RxISR = NULL;
  2992.   huart->TxISR = NULL;
  2993.  
  2994.   return ret;
  2995. }
  2996.  
  2997. /**
  2998.   * @brief Configure the UART peripheral advanced features.
  2999.   * @param huart UART handle.
  3000.   * @retval None
  3001.   */
  3002. void UART_AdvFeatureConfig(UART_HandleTypeDef *huart)
  3003. {
  3004.   /* Check whether the set of advanced features to configure is properly set */
  3005.   assert_param(IS_UART_ADVFEATURE_INIT(huart->AdvancedInit.AdvFeatureInit));
  3006.  
  3007.   /* if required, configure TX pin active level inversion */
  3008.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_TXINVERT_INIT))
  3009.   {
  3010.     assert_param(IS_UART_ADVFEATURE_TXINV(huart->AdvancedInit.TxPinLevelInvert));
  3011.     MODIFY_REG(huart->Instance->CR2, USART_CR2_TXINV, huart->AdvancedInit.TxPinLevelInvert);
  3012.   }
  3013.  
  3014.   /* if required, configure RX pin active level inversion */
  3015.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXINVERT_INIT))
  3016.   {
  3017.     assert_param(IS_UART_ADVFEATURE_RXINV(huart->AdvancedInit.RxPinLevelInvert));
  3018.     MODIFY_REG(huart->Instance->CR2, USART_CR2_RXINV, huart->AdvancedInit.RxPinLevelInvert);
  3019.   }
  3020.  
  3021.   /* if required, configure data inversion */
  3022.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DATAINVERT_INIT))
  3023.   {
  3024.     assert_param(IS_UART_ADVFEATURE_DATAINV(huart->AdvancedInit.DataInvert));
  3025.     MODIFY_REG(huart->Instance->CR2, USART_CR2_DATAINV, huart->AdvancedInit.DataInvert);
  3026.   }
  3027.  
  3028.   /* if required, configure RX/TX pins swap */
  3029.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_SWAP_INIT))
  3030.   {
  3031.     assert_param(IS_UART_ADVFEATURE_SWAP(huart->AdvancedInit.Swap));
  3032.     MODIFY_REG(huart->Instance->CR2, USART_CR2_SWAP, huart->AdvancedInit.Swap);
  3033.   }
  3034.  
  3035.   /* if required, configure RX overrun detection disabling */
  3036.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  3037.   {
  3038.     assert_param(IS_UART_OVERRUN(huart->AdvancedInit.OverrunDisable));
  3039.     MODIFY_REG(huart->Instance->CR3, USART_CR3_OVRDIS, huart->AdvancedInit.OverrunDisable);
  3040.   }
  3041.  
  3042.   /* if required, configure DMA disabling on reception error */
  3043.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_DMADISABLEONERROR_INIT))
  3044.   {
  3045.     assert_param(IS_UART_ADVFEATURE_DMAONRXERROR(huart->AdvancedInit.DMADisableonRxError));
  3046.     MODIFY_REG(huart->Instance->CR3, USART_CR3_DDRE, huart->AdvancedInit.DMADisableonRxError);
  3047.   }
  3048.  
  3049.   /* if required, configure auto Baud rate detection scheme */
  3050.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_AUTOBAUDRATE_INIT))
  3051.   {
  3052.     assert_param(IS_USART_AUTOBAUDRATE_DETECTION_INSTANCE(huart->Instance));
  3053.     assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATE(huart->AdvancedInit.AutoBaudRateEnable));
  3054.     MODIFY_REG(huart->Instance->CR2, USART_CR2_ABREN, huart->AdvancedInit.AutoBaudRateEnable);
  3055.     /* set auto Baudrate detection parameters if detection is enabled */
  3056.     if (huart->AdvancedInit.AutoBaudRateEnable == UART_ADVFEATURE_AUTOBAUDRATE_ENABLE)
  3057.     {
  3058.       assert_param(IS_UART_ADVFEATURE_AUTOBAUDRATEMODE(huart->AdvancedInit.AutoBaudRateMode));
  3059.       MODIFY_REG(huart->Instance->CR2, USART_CR2_ABRMODE, huart->AdvancedInit.AutoBaudRateMode);
  3060.     }
  3061.   }
  3062.  
  3063.   /* if required, configure MSB first on communication line */
  3064.   if (HAL_IS_BIT_SET(huart->AdvancedInit.AdvFeatureInit, UART_ADVFEATURE_MSBFIRST_INIT))
  3065.   {
  3066.     assert_param(IS_UART_ADVFEATURE_MSBFIRST(huart->AdvancedInit.MSBFirst));
  3067.     MODIFY_REG(huart->Instance->CR2, USART_CR2_MSBFIRST, huart->AdvancedInit.MSBFirst);
  3068.   }
  3069. }
  3070.  
  3071. /**
  3072.   * @brief Check the UART Idle State.
  3073.   * @param huart UART handle.
  3074.   * @retval HAL status
  3075.   */
  3076. HAL_StatusTypeDef UART_CheckIdleState(UART_HandleTypeDef *huart)
  3077. {
  3078.   uint32_t tickstart;
  3079.  
  3080.   /* Initialize the UART ErrorCode */
  3081.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  3082.  
  3083.   /* Init tickstart for timeout managment*/
  3084.   tickstart = HAL_GetTick();
  3085.  
  3086.   /* Check if the Transmitter is enabled */
  3087.   if ((huart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  3088.   {
  3089.     /* Wait until TEACK flag is set */
  3090.     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_TEACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  3091.     {
  3092.       /* Timeout occurred */
  3093.       return HAL_TIMEOUT;
  3094.     }
  3095.   }
  3096.  
  3097.   /* Check if the Receiver is enabled */
  3098.   if ((huart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  3099.   {
  3100.     /* Wait until REACK flag is set */
  3101.     if (UART_WaitOnFlagUntilTimeout(huart, USART_ISR_REACK, RESET, tickstart, HAL_UART_TIMEOUT_VALUE) != HAL_OK)
  3102.     {
  3103.       /* Timeout occurred */
  3104.       return HAL_TIMEOUT;
  3105.     }
  3106.   }
  3107.  
  3108.   /* Initialize the UART State */
  3109.   huart->gState = HAL_UART_STATE_READY;
  3110.   huart->RxState = HAL_UART_STATE_READY;
  3111.  
  3112.   __HAL_UNLOCK(huart);
  3113.  
  3114.   return HAL_OK;
  3115. }
  3116.  
  3117. /**
  3118.   * @brief  Handle UART Communication Timeout.
  3119.   * @param huart     UART handle.
  3120.   * @param Flag      Specifies the UART flag to check
  3121.   * @param Status    Flag status (SET or RESET)
  3122.   * @param Tickstart Tick start value
  3123.   * @param Timeout   Timeout duration
  3124.   * @retval HAL status
  3125.   */
  3126. HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status,
  3127.                                               uint32_t Tickstart, uint32_t Timeout)
  3128. {
  3129.   /* Wait until flag is set */
  3130.   while ((__HAL_UART_GET_FLAG(huart, Flag) ? SET : RESET) == Status)
  3131.   {
  3132.     /* Check for the Timeout */
  3133.     if (Timeout != HAL_MAX_DELAY)
  3134.     {
  3135.       if (((HAL_GetTick() - Tickstart) > Timeout) || (Timeout == 0U))
  3136.       {
  3137.         /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  3138.         CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  3139.         CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3140.  
  3141.         huart->gState = HAL_UART_STATE_READY;
  3142.         huart->RxState = HAL_UART_STATE_READY;
  3143.  
  3144.         __HAL_UNLOCK(huart);
  3145.  
  3146.         return HAL_TIMEOUT;
  3147.       }
  3148.  
  3149.       if (READ_BIT(huart->Instance->CR1, USART_CR1_RE) != 0U)
  3150.       {
  3151.         if (__HAL_UART_GET_FLAG(huart, UART_FLAG_RTOF) == SET)
  3152.         {
  3153.           /* Clear Receiver Timeout flag*/
  3154.           __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_RTOF);
  3155.          
  3156.           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  3157.           CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  3158.           CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3159.  
  3160.           huart->gState = HAL_UART_STATE_READY;
  3161.           huart->RxState = HAL_UART_STATE_READY;
  3162.           huart->ErrorCode = HAL_UART_ERROR_RTO;
  3163.          
  3164.           /* Process Unlocked */
  3165.           __HAL_UNLOCK(huart);
  3166.          
  3167.           return HAL_TIMEOUT;
  3168.         }
  3169.       }
  3170.     }
  3171.   }
  3172.   return HAL_OK;
  3173. }
  3174.  
  3175.  
  3176. /**
  3177.   * @brief  End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
  3178.   * @param  huart UART handle.
  3179.   * @retval None
  3180.   */
  3181. static void UART_EndTxTransfer(UART_HandleTypeDef *huart)
  3182. {
  3183.   /* Disable TXEIE and TCIE interrupts */
  3184.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  3185.  
  3186.   /* At end of Tx process, restore huart->gState to Ready */
  3187.   huart->gState = HAL_UART_STATE_READY;
  3188. }
  3189.  
  3190.  
  3191. /**
  3192.   * @brief  End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  3193.   * @param  huart UART handle.
  3194.   * @retval None
  3195.   */
  3196. static void UART_EndRxTransfer(UART_HandleTypeDef *huart)
  3197. {
  3198.   /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  3199.   CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  3200.   CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3201.  
  3202.   /* At end of Rx process, restore huart->RxState to Ready */
  3203.   huart->RxState = HAL_UART_STATE_READY;
  3204.  
  3205.   /* Reset RxIsr function pointer */
  3206.   huart->RxISR = NULL;
  3207. }
  3208.  
  3209.  
  3210. /**
  3211.   * @brief DMA UART transmit process complete callback.
  3212.   * @param hdma DMA handle.
  3213.   * @retval None
  3214.   */
  3215. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  3216. {
  3217.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3218.  
  3219.   /* DMA Normal mode */
  3220.   if (hdma->Init.Mode != DMA_CIRCULAR)
  3221.   {
  3222.     huart->TxXferCount = 0U;
  3223.  
  3224.     /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  3225.        in the UART CR3 register */
  3226.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  3227.  
  3228.     /* Enable the UART Transmit Complete Interrupt */
  3229.     SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3230.   }
  3231.   /* DMA Circular mode */
  3232.   else
  3233.   {
  3234. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3235.     /*Call registered Tx complete callback*/
  3236.     huart->TxCpltCallback(huart);
  3237. #else
  3238.     /*Call legacy weak Tx complete callback*/
  3239.     HAL_UART_TxCpltCallback(huart);
  3240. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3241.   }
  3242. }
  3243.  
  3244. /**
  3245.   * @brief DMA UART transmit process half complete callback.
  3246.   * @param hdma DMA handle.
  3247.   * @retval None
  3248.   */
  3249. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  3250. {
  3251.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3252.  
  3253. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3254.   /*Call registered Tx Half complete callback*/
  3255.   huart->TxHalfCpltCallback(huart);
  3256. #else
  3257.   /*Call legacy weak Tx Half complete callback*/
  3258.   HAL_UART_TxHalfCpltCallback(huart);
  3259. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3260. }
  3261.  
  3262. /**
  3263.   * @brief DMA UART receive process complete callback.
  3264.   * @param hdma DMA handle.
  3265.   * @retval None
  3266.   */
  3267. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  3268. {
  3269.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3270.  
  3271.   /* DMA Normal mode */
  3272.   if (hdma->Init.Mode != DMA_CIRCULAR)
  3273.   {
  3274.     huart->RxXferCount = 0U;
  3275.  
  3276.     /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  3277.     CLEAR_BIT(huart->Instance->CR1, USART_CR1_PEIE);
  3278.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3279.  
  3280.     /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  3281.        in the UART CR3 register */
  3282.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  3283.  
  3284.     /* At end of Rx process, restore huart->RxState to Ready */
  3285.     huart->RxState = HAL_UART_STATE_READY;
  3286.   }
  3287.  
  3288. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3289.   /*Call registered Rx complete callback*/
  3290.   huart->RxCpltCallback(huart);
  3291. #else
  3292.   /*Call legacy weak Rx complete callback*/
  3293.   HAL_UART_RxCpltCallback(huart);
  3294. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3295. }
  3296.  
  3297. /**
  3298.   * @brief DMA UART receive process half complete callback.
  3299.   * @param hdma DMA handle.
  3300.   * @retval None
  3301.   */
  3302. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  3303. {
  3304.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3305.  
  3306. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3307.   /*Call registered Rx Half complete callback*/
  3308.   huart->RxHalfCpltCallback(huart);
  3309. #else
  3310.   /*Call legacy weak Rx Half complete callback*/
  3311.   HAL_UART_RxHalfCpltCallback(huart);
  3312. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3313. }
  3314.  
  3315. /**
  3316.   * @brief DMA UART communication error callback.
  3317.   * @param hdma DMA handle.
  3318.   * @retval None
  3319.   */
  3320. static void UART_DMAError(DMA_HandleTypeDef *hdma)
  3321. {
  3322.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3323.  
  3324.   const HAL_UART_StateTypeDef gstate = huart->gState;
  3325.   const HAL_UART_StateTypeDef rxstate = huart->RxState;
  3326.  
  3327.   /* Stop UART DMA Tx request if ongoing */
  3328.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAT)) &&
  3329.       (gstate == HAL_UART_STATE_BUSY_TX))
  3330.   {
  3331.     huart->TxXferCount = 0U;
  3332.     UART_EndTxTransfer(huart);
  3333.   }
  3334.  
  3335.   /* Stop UART DMA Rx request if ongoing */
  3336.   if ((HAL_IS_BIT_SET(huart->Instance->CR3, USART_CR3_DMAR)) &&
  3337.       (rxstate == HAL_UART_STATE_BUSY_RX))
  3338.   {
  3339.     huart->RxXferCount = 0U;
  3340.     UART_EndRxTransfer(huart);
  3341.   }
  3342.  
  3343.   huart->ErrorCode |= HAL_UART_ERROR_DMA;
  3344.  
  3345. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3346.   /*Call registered error callback*/
  3347.   huart->ErrorCallback(huart);
  3348. #else
  3349.   /*Call legacy weak error callback*/
  3350.   HAL_UART_ErrorCallback(huart);
  3351. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3352. }
  3353.  
  3354. /**
  3355.   * @brief  DMA UART communication abort callback, when initiated by HAL services on Error
  3356.   *         (To be called at end of DMA Abort procedure following error occurrence).
  3357.   * @param  hdma DMA handle.
  3358.   * @retval None
  3359.   */
  3360. static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  3361. {
  3362.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3363.   huart->RxXferCount = 0U;
  3364.   huart->TxXferCount = 0U;
  3365.  
  3366. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3367.   /*Call registered error callback*/
  3368.   huart->ErrorCallback(huart);
  3369. #else
  3370.   /*Call legacy weak error callback*/
  3371.   HAL_UART_ErrorCallback(huart);
  3372. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3373. }
  3374.  
  3375. /**
  3376.   * @brief  DMA UART Tx communication abort callback, when initiated by user
  3377.   *         (To be called at end of DMA Tx Abort procedure following user abort request).
  3378.   * @note   When this callback is executed, User Abort complete call back is called only if no
  3379.   *         Abort still ongoing for Rx DMA Handle.
  3380.   * @param  hdma DMA handle.
  3381.   * @retval None
  3382.   */
  3383. static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  3384. {
  3385.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3386.  
  3387.   huart->hdmatx->XferAbortCallback = NULL;
  3388.  
  3389.   /* Check if an Abort process is still ongoing */
  3390.   if (huart->hdmarx != NULL)
  3391.   {
  3392.     if (huart->hdmarx->XferAbortCallback != NULL)
  3393.     {
  3394.       return;
  3395.     }
  3396.   }
  3397.  
  3398.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3399.   huart->TxXferCount = 0U;
  3400.   huart->RxXferCount = 0U;
  3401.  
  3402.   /* Reset errorCode */
  3403.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  3404.  
  3405.   /* Clear the Error flags in the ICR register */
  3406.   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3407.  
  3408.  
  3409.   /* Restore huart->gState and huart->RxState to Ready */
  3410.   huart->gState  = HAL_UART_STATE_READY;
  3411.   huart->RxState = HAL_UART_STATE_READY;
  3412.  
  3413.   /* Call user Abort complete callback */
  3414. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3415.   /* Call registered Abort complete callback */
  3416.   huart->AbortCpltCallback(huart);
  3417. #else
  3418.   /* Call legacy weak Abort complete callback */
  3419.   HAL_UART_AbortCpltCallback(huart);
  3420. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3421. }
  3422.  
  3423.  
  3424. /**
  3425.   * @brief  DMA UART Rx communication abort callback, when initiated by user
  3426.   *         (To be called at end of DMA Rx Abort procedure following user abort request).
  3427.   * @note   When this callback is executed, User Abort complete call back is called only if no
  3428.   *         Abort still ongoing for Tx DMA Handle.
  3429.   * @param  hdma DMA handle.
  3430.   * @retval None
  3431.   */
  3432. static void UART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  3433. {
  3434.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3435.  
  3436.   huart->hdmarx->XferAbortCallback = NULL;
  3437.  
  3438.   /* Check if an Abort process is still ongoing */
  3439.   if (huart->hdmatx != NULL)
  3440.   {
  3441.     if (huart->hdmatx->XferAbortCallback != NULL)
  3442.     {
  3443.       return;
  3444.     }
  3445.   }
  3446.  
  3447.   /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  3448.   huart->TxXferCount = 0U;
  3449.   huart->RxXferCount = 0U;
  3450.  
  3451.   /* Reset errorCode */
  3452.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  3453.  
  3454.   /* Clear the Error flags in the ICR register */
  3455.   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3456.  
  3457.   /* Discard the received data */
  3458.   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3459.  
  3460.   /* Restore huart->gState and huart->RxState to Ready */
  3461.   huart->gState  = HAL_UART_STATE_READY;
  3462.   huart->RxState = HAL_UART_STATE_READY;
  3463.  
  3464.   /* Call user Abort complete callback */
  3465. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3466.   /* Call registered Abort complete callback */
  3467.   huart->AbortCpltCallback(huart);
  3468. #else
  3469.   /* Call legacy weak Abort complete callback */
  3470.   HAL_UART_AbortCpltCallback(huart);
  3471. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3472. }
  3473.  
  3474.  
  3475. /**
  3476.   * @brief  DMA UART Tx communication abort callback, when initiated by user by a call to
  3477.   *         HAL_UART_AbortTransmit_IT API (Abort only Tx transfer)
  3478.   *         (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  3479.   *         and leads to user Tx Abort Complete callback execution).
  3480.   * @param  hdma DMA handle.
  3481.   * @retval None
  3482.   */
  3483. static void UART_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3484. {
  3485.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)(hdma->Parent);
  3486.  
  3487.   huart->TxXferCount = 0U;
  3488.  
  3489.  
  3490.   /* Restore huart->gState to Ready */
  3491.   huart->gState = HAL_UART_STATE_READY;
  3492.  
  3493.   /* Call user Abort complete callback */
  3494. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3495.   /* Call registered Abort Transmit Complete Callback */
  3496.   huart->AbortTransmitCpltCallback(huart);
  3497. #else
  3498.   /* Call legacy weak Abort Transmit Complete Callback */
  3499.   HAL_UART_AbortTransmitCpltCallback(huart);
  3500. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3501. }
  3502.  
  3503. /**
  3504.   * @brief  DMA UART Rx communication abort callback, when initiated by user by a call to
  3505.   *         HAL_UART_AbortReceive_IT API (Abort only Rx transfer)
  3506.   *         (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  3507.   *         and leads to user Rx Abort Complete callback execution).
  3508.   * @param  hdma DMA handle.
  3509.   * @retval None
  3510.   */
  3511. static void UART_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  3512. {
  3513.   UART_HandleTypeDef *huart = (UART_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  3514.  
  3515.   huart->RxXferCount = 0U;
  3516.  
  3517.   /* Clear the Error flags in the ICR register */
  3518.   __HAL_UART_CLEAR_FLAG(huart, UART_CLEAR_OREF | UART_CLEAR_NEF | UART_CLEAR_PEF | UART_CLEAR_FEF);
  3519.  
  3520.   /* Discard the received data */
  3521.   __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3522.  
  3523.   /* Restore huart->RxState to Ready */
  3524.   huart->RxState = HAL_UART_STATE_READY;
  3525.  
  3526.   /* Call user Abort complete callback */
  3527. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3528.   /* Call registered Abort Receive Complete Callback */
  3529.   huart->AbortReceiveCpltCallback(huart);
  3530. #else
  3531.   /* Call legacy weak Abort Receive Complete Callback */
  3532.   HAL_UART_AbortReceiveCpltCallback(huart);
  3533. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3534. }
  3535.  
  3536. /**
  3537.   * @brief TX interrrupt handler for 7 or 8 bits data word length .
  3538.   * @note   Function is called under interruption only, once
  3539.   *         interruptions have been enabled by HAL_UART_Transmit_IT().
  3540.   * @param huart UART handle.
  3541.   * @retval None
  3542.   */
  3543. static void UART_TxISR_8BIT(UART_HandleTypeDef *huart)
  3544. {
  3545.   /* Check that a Tx process is ongoing */
  3546.   if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3547.   {
  3548.     if (huart->TxXferCount == 0U)
  3549.     {
  3550.       /* Disable the UART Transmit Data Register Empty Interrupt */
  3551.       CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  3552.  
  3553.       /* Enable the UART Transmit Complete Interrupt */
  3554.       SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3555.     }
  3556.     else
  3557.     {
  3558.       huart->Instance->TDR = (uint8_t)(*huart->pTxBuffPtr & (uint8_t)0xFF);
  3559.       huart->pTxBuffPtr++;
  3560.       huart->TxXferCount--;
  3561.     }
  3562.   }
  3563. }
  3564.  
  3565. /**
  3566.   * @brief TX interrrupt handler for 9 bits data word length.
  3567.   * @note   Function is called under interruption only, once
  3568.   *         interruptions have been enabled by HAL_UART_Transmit_IT().
  3569.   * @param huart UART handle.
  3570.   * @retval None
  3571.   */
  3572. static void UART_TxISR_16BIT(UART_HandleTypeDef *huart)
  3573. {
  3574.   uint16_t *tmp;
  3575.  
  3576.   /* Check that a Tx process is ongoing */
  3577.   if (huart->gState == HAL_UART_STATE_BUSY_TX)
  3578.   {
  3579.     if (huart->TxXferCount == 0U)
  3580.     {
  3581.       /* Disable the UART Transmit Data Register Empty Interrupt */
  3582.       CLEAR_BIT(huart->Instance->CR1, USART_CR1_TXEIE);
  3583.  
  3584.       /* Enable the UART Transmit Complete Interrupt */
  3585.       SET_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3586.     }
  3587.     else
  3588.     {
  3589.       tmp = (uint16_t *) huart->pTxBuffPtr;
  3590.       huart->Instance->TDR = (((uint32_t)(*tmp)) & 0x01FFUL);
  3591.       huart->pTxBuffPtr += 2U;
  3592.       huart->TxXferCount--;
  3593.     }
  3594.   }
  3595. }
  3596.  
  3597.  
  3598. /**
  3599.   * @brief  Wrap up transmission in non-blocking mode.
  3600.   * @param  huart pointer to a UART_HandleTypeDef structure that contains
  3601.   *                the configuration information for the specified UART module.
  3602.   * @retval None
  3603.   */
  3604. static void UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  3605. {
  3606.   /* Disable the UART Transmit Complete Interrupt */
  3607.   CLEAR_BIT(huart->Instance->CR1, USART_CR1_TCIE);
  3608.  
  3609.   /* Tx process is ended, restore huart->gState to Ready */
  3610.   huart->gState = HAL_UART_STATE_READY;
  3611.  
  3612.   /* Cleat TxISR function pointer */
  3613.   huart->TxISR = NULL;
  3614.  
  3615. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3616.   /*Call registered Tx complete callback*/
  3617.   huart->TxCpltCallback(huart);
  3618. #else
  3619.   /*Call legacy weak Tx complete callback*/
  3620.   HAL_UART_TxCpltCallback(huart);
  3621. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3622. }
  3623.  
  3624. /**
  3625.   * @brief RX interrrupt handler for 7 or 8 bits data word length .
  3626.   * @param huart UART handle.
  3627.   * @retval None
  3628.   */
  3629. static void UART_RxISR_8BIT(UART_HandleTypeDef *huart)
  3630. {
  3631.   uint16_t uhMask = huart->Mask;
  3632.   uint16_t  uhdata;
  3633.  
  3634.   /* Check that a Rx process is ongoing */
  3635.   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3636.   {
  3637.     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3638.     *huart->pRxBuffPtr = (uint8_t)(uhdata & (uint8_t)uhMask);
  3639.     huart->pRxBuffPtr++;
  3640.     huart->RxXferCount--;
  3641.  
  3642.     if (huart->RxXferCount == 0U)
  3643.     {
  3644.       /* Disable the UART Parity Error Interrupt and RXNE interrupts */
  3645.       CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  3646.  
  3647.       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3648.       CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3649.  
  3650.       /* Rx process is completed, restore huart->RxState to Ready */
  3651.       huart->RxState = HAL_UART_STATE_READY;
  3652.  
  3653.       /* Clear RxISR function pointer */
  3654.       huart->RxISR = NULL;
  3655.  
  3656. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3657.       /*Call registered Rx complete callback*/
  3658.       huart->RxCpltCallback(huart);
  3659. #else
  3660.       /*Call legacy weak Rx complete callback*/
  3661.       HAL_UART_RxCpltCallback(huart);
  3662. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3663.     }
  3664.   }
  3665.   else
  3666.   {
  3667.     /* Clear RXNE interrupt flag */
  3668.     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3669.   }
  3670. }
  3671.  
  3672. /**
  3673.   * @brief RX interrrupt handler for 9 bits data word length .
  3674.   * @note   Function is called under interruption only, once
  3675.   *         interruptions have been enabled by HAL_UART_Receive_IT()
  3676.   * @param huart UART handle.
  3677.   * @retval None
  3678.   */
  3679. static void UART_RxISR_16BIT(UART_HandleTypeDef *huart)
  3680. {
  3681.   uint16_t *tmp;
  3682.   uint16_t uhMask = huart->Mask;
  3683.   uint16_t  uhdata;
  3684.  
  3685.   /* Check that a Rx process is ongoing */
  3686.   if (huart->RxState == HAL_UART_STATE_BUSY_RX)
  3687.   {
  3688.     uhdata = (uint16_t) READ_REG(huart->Instance->RDR);
  3689.     tmp = (uint16_t *) huart->pRxBuffPtr ;
  3690.     *tmp = (uint16_t)(uhdata & uhMask);
  3691.     huart->pRxBuffPtr += 2U;
  3692.     huart->RxXferCount--;
  3693.  
  3694.     if (huart->RxXferCount == 0U)
  3695.     {
  3696.       /* Disable the UART Parity Error Interrupt and RXNE interrupt*/
  3697.       CLEAR_BIT(huart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  3698.  
  3699.       /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  3700.       CLEAR_BIT(huart->Instance->CR3, USART_CR3_EIE);
  3701.  
  3702.       /* Rx process is completed, restore huart->RxState to Ready */
  3703.       huart->RxState = HAL_UART_STATE_READY;
  3704.  
  3705.       /* Clear RxISR function pointer */
  3706.       huart->RxISR = NULL;
  3707.  
  3708. #if (USE_HAL_UART_REGISTER_CALLBACKS == 1)
  3709.       /*Call registered Rx complete callback*/
  3710.       huart->RxCpltCallback(huart);
  3711. #else
  3712.       /*Call legacy weak Rx complete callback*/
  3713.       HAL_UART_RxCpltCallback(huart);
  3714. #endif /* USE_HAL_UART_REGISTER_CALLBACKS */
  3715.     }
  3716.   }
  3717.   else
  3718.   {
  3719.     /* Clear RXNE interrupt flag */
  3720.     __HAL_UART_SEND_REQ(huart, UART_RXDATA_FLUSH_REQUEST);
  3721.   }
  3722. }
  3723.  
  3724.  
  3725. /**
  3726.   * @}
  3727.   */
  3728.  
  3729. #endif /* HAL_UART_MODULE_ENABLED */
  3730. /**
  3731.   * @}
  3732.   */
  3733.  
  3734. /**
  3735.   * @}
  3736.   */
  3737.  
  3738. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  3739.