Subversion Repositories testOled

Rev

Blame | Last modification | View Log | Download | RSS feed

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