Subversion Repositories EngineBay2

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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