Subversion Repositories DashDisplay

Rev

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

  1. /**
  2.   ******************************************************************************
  3.   * @file    stm32f1xx_hal_uart.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.1
  6.   * @date    31-July-2015
  7.   * @brief   UART HAL module driver.
  8.   *          This file provides firmware functions to manage the following
  9.   *          functionalities of the Universal Asynchronous Receiver Transmitter (UART) peripheral:
  10.   *           + Initialization and de-initialization functions
  11.   *           + IO operation functions
  12.   *           + Peripheral Control functions
  13.   *           + Peripheral State and Errors functions  
  14.   @verbatim
  15.   ==============================================================================
  16.                         ##### How to use this driver #####
  17.   ==============================================================================
  18.   [..]
  19.     The UART HAL driver can be used as follows:
  20.    
  21.     (#) Declare a UART_HandleTypeDef handle structure.
  22.  
  23.     (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
  24.         (##) Enable the USARTx interface clock.
  25.         (##) UART pins configuration:
  26.             (+++) Enable the clock for the UART GPIOs.
  27.              (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  28.         (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
  29.              and HAL_UART_Receive_IT() APIs):
  30.             (+++) Configure the USARTx interrupt priority.
  31.             (+++) Enable the NVIC USART IRQ handle.
  32.         (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
  33.              and HAL_UART_Receive_DMA() APIs):
  34.             (+++) Declare a DMA handle structure for the Tx/Rx channel.
  35.             (+++) Enable the DMAx interface clock.
  36.             (+++) Configure the declared DMA handle structure with the required
  37.                   Tx/Rx parameters.                
  38.             (+++) Configure the DMA Tx/Rx channel.
  39.             (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
  40.             (+++) Configure the priority and enable the NVIC for the transfer complete
  41.                   interrupt on the DMA Tx/Rx channel.
  42.             (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  43.                   (used for last byte sending completion detection in DMA non circular mode)
  44.  
  45.     (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  46.         flow control and Mode(Receiver/Transmitter) in the huart Init structure.
  47.  
  48.     (#) For the UART asynchronous mode, initialize the UART registers by calling
  49.         the HAL_UART_Init() API.
  50.  
  51.     (#) For the UART Half duplex mode, initialize the UART registers by calling
  52.         the HAL_HalfDuplex_Init() API.
  53.  
  54.     (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.
  55.  
  56.     (#) For the Multi-Processor mode, initialize the UART registers by calling
  57.         the HAL_MultiProcessor_Init() API.
  58.  
  59.      [..]
  60.        (@) The specific UART interrupts (Transmission complete interrupt,
  61.             RXNE interrupt and Error Interrupts) will be managed using the macros
  62.             __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
  63.             and receive process.
  64.  
  65.      [..]
  66.        (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
  67.             low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customed
  68.             HAL_UART_MspInit() API.
  69.  
  70.      [..]
  71.         Three operation modes are available within this driver :
  72.  
  73.      *** Polling mode IO operation ***
  74.      =================================
  75.      [..]    
  76.        (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
  77.        (+) Receive an amount of data in blocking mode using HAL_UART_Receive()
  78.        
  79.      *** Interrupt mode IO operation ***
  80.      ===================================
  81.      [..]
  82.        (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
  83.        (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  84.             add his own code by customization of function pointer HAL_UART_TxCpltCallback
  85.        (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
  86.        (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  87.             add his own code by customization of function pointer HAL_UART_RxCpltCallback
  88.        (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  89.             add his own code by customization of function pointer HAL_UART_ErrorCallback
  90.  
  91.      *** DMA mode IO operation ***
  92.      ==============================
  93.      [..]
  94.        (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
  95.        (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
  96.             add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
  97.        (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
  98.             add his own code by customization of function pointer HAL_UART_TxCpltCallback
  99.        (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
  100.        (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
  101.             add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
  102.        (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
  103.             add his own code by customization of function pointer HAL_UART_RxCpltCallback
  104.        (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
  105.             add his own code by customization of function pointer HAL_UART_ErrorCallback
  106.        (+) Pause the DMA Transfer using HAL_UART_DMAPause()
  107.        (+) Resume the DMA Transfer using HAL_UART_DMAResume()
  108.        (+) Stop the DMA Transfer using HAL_UART_DMAStop()
  109.  
  110.      *** UART HAL driver macros list ***
  111.      =============================================
  112.      [..]
  113.        Below the list of most used macros in UART HAL driver.
  114.  
  115.       (+) __HAL_UART_ENABLE: Enable the UART peripheral
  116.       (+) __HAL_UART_DISABLE: Disable the UART peripheral
  117.       (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
  118.       (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
  119.       (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
  120.       (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
  121.       (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not
  122.  
  123.      [..]
  124.        (@) You can refer to the UART HAL driver header file for more useful macros
  125.      
  126.   @endverbatim
  127.   ******************************************************************************
  128.   * @attention
  129.   *
  130.   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  131.   *
  132.   * Redistribution and use in source and binary forms, with or without modification,
  133.   * are permitted provided that the following conditions are met:
  134.   *   1. Redistributions of source code must retain the above copyright notice,
  135.   *      this list of conditions and the following disclaimer.
  136.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  137.   *      this list of conditions and the following disclaimer in the documentation
  138.   *      and/or other materials provided with the distribution.
  139.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  140.   *      may be used to endorse or promote products derived from this software
  141.   *      without specific prior written permission.
  142.   *
  143.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  144.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  145.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  146.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  147.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  148.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  149.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  150.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  151.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  152.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  153.   *
  154.   ******************************************************************************
  155.   */
  156.  
  157. /* Includes ------------------------------------------------------------------*/
  158. #include "stm32f1xx_hal.h"
  159.  
  160. /** @addtogroup STM32F1xx_HAL_Driver
  161.   * @{
  162.   */
  163.  
  164. /** @defgroup UART UART
  165.   * @brief HAL UART module driver
  166.   * @{
  167.   */
  168. #ifdef HAL_UART_MODULE_ENABLED
  169.    
  170. /* Private typedef -----------------------------------------------------------*/
  171. /* Private define ------------------------------------------------------------*/
  172. /* Private macros ------------------------------------------------------------*/
  173. /* Private variables ---------------------------------------------------------*/
  174. /* Private function prototypes -----------------------------------------------*/
  175. /** @addtogroup UART_Private_Functions   UART Private Functions
  176.   * @{
  177.   */
  178. static void UART_SetConfig (UART_HandleTypeDef *huart);
  179. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart);
  180. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart);
  181. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart);
  182. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  183. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  184. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  185. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  186. static void UART_DMAError(DMA_HandleTypeDef *hdma);
  187. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  188. /**
  189.   * @}
  190.   */
  191.  
  192. /* Exported functions ---------------------------------------------------------*/
  193.  
  194. /** @defgroup UART_Exported_Functions UART Exported Functions
  195.   * @{
  196.   */
  197.  
  198. /** @defgroup UART_Exported_Functions_Group1 Initialization and de-initialization functions
  199.   *  @brief    Initialization and Configuration functions
  200.   *
  201. @verbatim
  202. ===============================================================================
  203.             ##### Initialization and Configuration functions #####
  204.  ===============================================================================  
  205.     [..]
  206.     This subsection provides a set of functions allowing to initialize the USARTx or the UARTy
  207.     in asynchronous mode.
  208.       (+) For the asynchronous mode only these parameters can be configured:
  209.         (++) Baud Rate
  210.         (++) Word Length
  211.         (++) Stop Bit
  212.         (++) Parity: If the parity is enabled, then the MSB bit of the data written
  213.              in the data register is transmitted but is changed by the parity bit.
  214.              Depending on the frame length defined by the M bit (8-bits or 9-bits),
  215.              the possible UART frame formats are as listed in the following table:
  216.         (+++)    +-------------------------------------------------------------+    
  217.         (+++)    |   M bit |  PCE bit  |            UART frame                 |
  218.         (+++)    |---------------------|---------------------------------------|            
  219.         (+++)    |    0    |    0      |    | SB | 8 bit data | STB |          |
  220.         (+++)    |---------|-----------|---------------------------------------|  
  221.         (+++)    |    0    |    1      |    | SB | 7 bit data | PB | STB |     |
  222.         (+++)    |---------|-----------|---------------------------------------|  
  223.         (+++)    |    1    |    0      |    | SB | 9 bit data | STB |          |
  224.         (+++)    |---------|-----------|---------------------------------------|  
  225.         (+++)    |    1    |    1      |    | SB | 8 bit data | PB | STB |     |
  226.         (+++)    +-------------------------------------------------------------+            
  227.         (++) Hardware flow control
  228.         (++) Receiver/transmitter modes
  229.     [..]
  230.     The HAL_UART_Init(), HAL_HalfDuplex_Init(), HAL_LIN_Init() and HAL_MultiProcessor_Init() APIs
  231.     follow respectively the UART asynchronous, UART Half duplex, LIN and Multi-Processor
  232.     configuration procedures (details for the procedures are available in reference manuals
  233.     (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  234.  
  235.  
  236. @endverbatim
  237.   * @{
  238.   */
  239.  
  240. /**
  241.   * @brief  Initializes the UART mode according to the specified parameters in
  242.   *         the UART_InitTypeDef and create the associated handle.
  243.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  244.   *                the configuration information for the specified UART module.
  245.   * @retval HAL status
  246.   */
  247. HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart)
  248. {
  249.   /* Check the UART handle allocation */
  250.   if(huart == NULL)
  251.   {
  252.     return HAL_ERROR;
  253.   }
  254.  
  255.   /* Check the parameters */
  256.   if(huart->Init.HwFlowCtl != UART_HWCONTROL_NONE)
  257.   {
  258.     /* The hardware flow control is available only for USART1, USART2, USART3 */
  259.     assert_param(IS_UART_HWFLOW_INSTANCE(huart->Instance));
  260.     assert_param(IS_UART_HARDWARE_FLOW_CONTROL(huart->Init.HwFlowCtl));
  261.   }
  262.   else
  263.   {
  264.     assert_param(IS_UART_INSTANCE(huart->Instance));
  265.   }
  266.   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  267.   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  268.  
  269.   if(huart->State == HAL_UART_STATE_RESET)
  270.   {  
  271.     /* Allocate lock resource and initialize it */
  272.     huart->Lock = HAL_UNLOCKED;
  273.    
  274.     /* Init the low level hardware */
  275.     HAL_UART_MspInit(huart);
  276.   }
  277.  
  278.   huart->State = HAL_UART_STATE_BUSY;
  279.  
  280.   /* Disable the peripheral */
  281.   __HAL_UART_DISABLE(huart);
  282.  
  283.   /* Set the UART Communication parameters */
  284.   UART_SetConfig(huart);
  285.  
  286.   /* In asynchronous mode, the following bits must be kept cleared:
  287.      - LINEN and CLKEN bits in the USART_CR2 register,
  288.      - SCEN, HDSEL and IREN  bits in the USART_CR3 register.*/
  289.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  290.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  291.  
  292.   /* Enable the peripheral */
  293.   __HAL_UART_ENABLE(huart);
  294.  
  295.   /* Initialize the UART state */
  296.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  297.   huart->State= HAL_UART_STATE_READY;
  298.  
  299.   return HAL_OK;
  300. }
  301.  
  302. /**
  303.   * @brief  Initializes the half-duplex mode according to the specified
  304.   *         parameters in the UART_InitTypeDef and create the associated handle.
  305.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  306.   *                the configuration information for the specified UART module.
  307.   * @retval HAL status
  308.   */
  309. HAL_StatusTypeDef HAL_HalfDuplex_Init(UART_HandleTypeDef *huart)
  310. {
  311.   /* Check the UART handle allocation */
  312.   if(huart == NULL)
  313.   {
  314.     return HAL_ERROR;
  315.   }
  316.  
  317.   /* Check UART instance */
  318.   assert_param(IS_UART_HALFDUPLEX_INSTANCE(huart->Instance));
  319.   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  320.   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  321.  
  322.   if(huart->State == HAL_UART_STATE_RESET)
  323.   {  
  324.     /* Allocate lock resource and initialize it */
  325.     huart->Lock = HAL_UNLOCKED;
  326.  
  327.     /* Init the low level hardware */
  328.     HAL_UART_MspInit(huart);
  329.   }
  330.  
  331.   huart->State = HAL_UART_STATE_BUSY;
  332.  
  333.   /* Disable the peripheral */
  334.   __HAL_UART_DISABLE(huart);
  335.  
  336.   /* Set the UART Communication parameters */
  337.   UART_SetConfig(huart);
  338.  
  339.   /* In half-duplex mode, the following bits must be kept cleared:
  340.      - LINEN and CLKEN bits in the USART_CR2 register,
  341.      - SCEN and IREN bits in the USART_CR3 register.*/
  342.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  343.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_IREN | USART_CR3_SCEN));
  344.  
  345.   /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  346.   SET_BIT(huart->Instance->CR3, USART_CR3_HDSEL);
  347.  
  348.   /* Enable the peripheral */
  349.   __HAL_UART_ENABLE(huart);
  350.  
  351.   /* Initialize the UART state*/
  352.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  353.   huart->State= HAL_UART_STATE_READY;
  354.  
  355.   return HAL_OK;
  356. }
  357.  
  358. /**
  359.   * @brief  Initializes the LIN mode according to the specified
  360.   *         parameters in the UART_InitTypeDef and create the associated handle.
  361.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  362.   *                the configuration information for the specified UART module.
  363.   * @param  BreakDetectLength: Specifies the LIN break detection length.
  364.   *         This parameter can be one of the following values:
  365.   *            @arg UART_LINBREAKDETECTLENGTH_10B: 10-bit break detection
  366.   *            @arg UART_LINBREAKDETECTLENGTH_11B: 11-bit break detection
  367.   * @retval HAL status
  368.   */
  369. HAL_StatusTypeDef HAL_LIN_Init(UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
  370. {
  371.   /* Check the UART handle allocation */
  372.   if(huart == NULL)
  373.   {
  374.     return HAL_ERROR;
  375.   }
  376.  
  377.   /* Check the LIN UART instance */  
  378.   assert_param(IS_UART_LIN_INSTANCE(huart->Instance));
  379.   /* Check the Break detection length parameter */
  380.   assert_param(IS_UART_LIN_BREAK_DETECT_LENGTH(BreakDetectLength));
  381.   assert_param(IS_UART_LIN_WORD_LENGTH(huart->Init.WordLength));
  382.   assert_param(IS_UART_LIN_OVERSAMPLING(huart->Init.OverSampling));
  383.  
  384.   if(huart->State == HAL_UART_STATE_RESET)
  385.   {  
  386.     /* Allocate lock resource and initialize it */
  387.     huart->Lock = HAL_UNLOCKED;  
  388.  
  389.     /* Init the low level hardware */
  390.     HAL_UART_MspInit(huart);
  391.   }
  392.  
  393.   huart->State = HAL_UART_STATE_BUSY;
  394.  
  395.   /* Disable the peripheral */
  396.   __HAL_UART_DISABLE(huart);
  397.  
  398.   /* Set the UART Communication parameters */
  399.   UART_SetConfig(huart);
  400.  
  401.   /* In LIN mode, the following bits must be kept cleared:
  402.      - CLKEN bits in the USART_CR2 register,
  403.      - SCEN and IREN bits in the USART_CR3 register.*/
  404.   CLEAR_BIT(huart->Instance->CR2, USART_CR2_CLKEN);
  405.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN | USART_CR3_SCEN));
  406.  
  407.   /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  408.   SET_BIT(huart->Instance->CR2, USART_CR2_LINEN);
  409.  
  410.   /* Set the USART LIN Break detection length. */
  411.   MODIFY_REG(huart->Instance->CR2, USART_CR2_LBDL, BreakDetectLength);
  412.  
  413.   /* Enable the peripheral */
  414.   __HAL_UART_ENABLE(huart);
  415.  
  416.   /* Initialize the UART state*/
  417.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  418.   huart->State= HAL_UART_STATE_READY;
  419.  
  420.   return HAL_OK;
  421. }
  422.  
  423. /**
  424.   * @brief  Initializes the Multi-Processor mode according to the specified
  425.   *         parameters in the UART_InitTypeDef and create the associated handle.
  426.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  427.   *                the configuration information for the specified UART module.
  428.   * @param  Address: UART node address
  429.   * @param  WakeUpMethod: specifies the UART wakeup method.
  430.   *         This parameter can be one of the following values:
  431.   *            @arg UART_WAKEUPMETHOD_IDLELINE: Wakeup by an idle line detection
  432.   *            @arg UART_WAKEUPMETHOD_ADDRESSMARK: Wakeup by an address mark
  433.   * @retval HAL status
  434.   */
  435. HAL_StatusTypeDef HAL_MultiProcessor_Init(UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
  436. {
  437.   /* Check the UART handle allocation */
  438.   if(huart == NULL)
  439.   {
  440.     return HAL_ERROR;
  441.   }
  442.  
  443.   /* Check UART instance capabilities */  
  444.   assert_param(IS_UART_MULTIPROCESSOR_INSTANCE(huart->Instance));
  445.  
  446.   /* Check the Address & wake up method parameters */
  447.   assert_param(IS_UART_WAKEUPMETHOD(WakeUpMethod));
  448.   assert_param(IS_UART_ADDRESS(Address));
  449.   assert_param(IS_UART_WORD_LENGTH(huart->Init.WordLength));
  450.   assert_param(IS_UART_OVERSAMPLING(huart->Init.OverSampling));
  451.  
  452.   if(huart->State == HAL_UART_STATE_RESET)
  453.   {  
  454.     /* Allocate lock resource and initialize it */
  455.     huart->Lock = HAL_UNLOCKED;
  456.  
  457.     /* Init the low level hardware */
  458.     HAL_UART_MspInit(huart);
  459.   }
  460.  
  461.   huart->State = HAL_UART_STATE_BUSY;
  462.  
  463.   /* Disable the peripheral */
  464.   __HAL_UART_DISABLE(huart);
  465.  
  466.   /* Set the UART Communication parameters */
  467.   UART_SetConfig(huart);
  468.  
  469.   /* In Multi-Processor mode, the following bits must be kept cleared:
  470.      - LINEN and CLKEN bits in the USART_CR2 register,
  471.      - SCEN, HDSEL and IREN  bits in the USART_CR3 register */
  472.   CLEAR_BIT(huart->Instance->CR2, (USART_CR2_LINEN | USART_CR2_CLKEN));
  473.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN));
  474.  
  475.   /* Set the USART address node */
  476.   MODIFY_REG(huart->Instance->CR2, USART_CR2_ADD, Address);
  477.  
  478.   /* Set the wake up method by setting the WAKE bit in the CR1 register */
  479.   MODIFY_REG(huart->Instance->CR1, USART_CR1_WAKE, WakeUpMethod);
  480.  
  481.   /* Enable the peripheral */
  482.   __HAL_UART_ENABLE(huart);
  483.  
  484.   /* Initialize the UART state */
  485.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  486.   huart->State= HAL_UART_STATE_READY;
  487.  
  488.   return HAL_OK;
  489. }
  490.  
  491. /**
  492.   * @brief  DeInitializes the UART peripheral.
  493.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  494.   *                the configuration information for the specified UART module.
  495.   * @retval HAL status
  496.   */
  497. HAL_StatusTypeDef HAL_UART_DeInit(UART_HandleTypeDef *huart)
  498. {
  499.   /* Check the UART handle allocation */
  500.   if(huart == NULL)
  501.   {
  502.     return HAL_ERROR;
  503.   }
  504.  
  505.   /* Check the parameters */
  506.   assert_param(IS_UART_INSTANCE(huart->Instance));
  507.  
  508.   huart->State = HAL_UART_STATE_BUSY;
  509.  
  510.   /* Disable the Peripheral */
  511.   __HAL_UART_DISABLE(huart);
  512.  
  513.   huart->Instance->CR1 = 0x0;
  514.   huart->Instance->CR2 = 0x0;
  515.   huart->Instance->CR3 = 0x0;
  516.  
  517.   /* DeInit the low level hardware */
  518.   HAL_UART_MspDeInit(huart);
  519.  
  520.   huart->ErrorCode = HAL_UART_ERROR_NONE;
  521.   huart->State = HAL_UART_STATE_RESET;
  522.  
  523.   /* Process Unlock */
  524.   __HAL_UNLOCK(huart);
  525.  
  526.   return HAL_OK;
  527. }
  528.  
  529. /**
  530.   * @brief  UART MSP Init.
  531.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  532.   *                the configuration information for the specified UART module.
  533.   * @retval None
  534.   */
  535.  __weak void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  536. {
  537.   /* NOTE: This function should not be modified, when the callback is needed,
  538.            the HAL_UART_MspInit can be implemented in the user file
  539.    */
  540. }
  541.  
  542. /**
  543.   * @brief  UART MSP DeInit.
  544.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  545.   *                the configuration information for the specified UART module.
  546.   * @retval None
  547.   */
  548.  __weak void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  549. {
  550.   /* NOTE: This function should not be modified, when the callback is needed,
  551.            the HAL_UART_MspDeInit can be implemented in the user file
  552.    */
  553. }
  554.  
  555. /**
  556.   * @}
  557.   */
  558.  
  559. /** @defgroup UART_Exported_Functions_Group2 IO operation functions
  560.   *  @brief UART Transmit and Receive functions
  561.   *
  562. @verbatim
  563.   ==============================================================================
  564.                       ##### IO operation functions #####
  565.   ==============================================================================  
  566.   [..]
  567.     This subsection provides a set of functions allowing to manage the UART asynchronous
  568.     and Half duplex data transfers.
  569.  
  570.     (#) There are two modes of transfer:
  571.        (++) Blocking mode: The communication is performed in polling mode.
  572.             The HAL status of all data processing is returned by the same function
  573.             after finishing transfer.  
  574.        (++) Non blocking mode: The communication is performed using Interrupts
  575.             or DMA, these APIs return the HAL status.
  576.             The end of the data processing will be indicated through the
  577.             dedicated UART IRQ when using Interrupt mode or the DMA IRQ when
  578.             using DMA mode.
  579.             The HAL_UART_TxCpltCallback(), HAL_UART_RxCpltCallback() user callbacks
  580.             will be executed respectively at the end of the transmit or receive process.
  581.             The HAL_UART_ErrorCallback() user callback will be executed when
  582.             a communication error is detected.
  583.  
  584.     (#) Blocking mode APIs are:
  585.         (++) HAL_UART_Transmit()
  586.         (++) HAL_UART_Receive()
  587.  
  588.     (#) Non Blocking mode APIs with Interrupt are:
  589.         (++) HAL_UART_Transmit_IT()
  590.         (++) HAL_UART_Receive_IT()
  591.         (++) HAL_UART_IRQHandler()
  592.  
  593.     (#) Non Blocking mode functions with DMA are:
  594.         (++) HAL_UART_Transmit_DMA()
  595.         (++) HAL_UART_Receive_DMA()
  596.         (++) HAL_UART_DMAPause()
  597.         (++) HAL_UART_DMAResume()
  598.         (++) HAL_UART_DMAStop()
  599.  
  600.     (#) A set of Transfer Complete Callbacks are provided in non blocking mode:
  601.         (++) HAL_UART_TxHalfCpltCallback()
  602.         (++) HAL_UART_TxCpltCallback()
  603.         (++) HAL_UART_RxHalfCpltCallback()
  604.         (++) HAL_UART_RxCpltCallback()
  605.         (++) HAL_UART_ErrorCallback()
  606.  
  607.     [..]
  608.       (@) In the Half duplex communication, it is forbidden to run the transmit
  609.           and receive process in parallel, the UART state HAL_UART_STATE_BUSY_TX_RX
  610.           can't be useful.
  611.      
  612. @endverbatim
  613.   * @{
  614.   */
  615.  
  616. /**
  617.   * @brief  Sends an amount of data in blocking mode.
  618.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  619.   *                the configuration information for the specified UART module.
  620.   * @param  pData: Pointer to data buffer
  621.   * @param  Size: Amount of data to be sent
  622.   * @param  Timeout: Timeout duration  
  623.   * @retval HAL status
  624.   */
  625. HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  626. {
  627.   uint16_t* tmp;
  628.   uint32_t tmp_state = 0;
  629.  
  630.   tmp_state = huart->State;
  631.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX))
  632.   {
  633.     if((pData == NULL) || (Size == 0))
  634.     {
  635.       return  HAL_ERROR;
  636.     }
  637.  
  638.     /* Process Locked */
  639.     __HAL_LOCK(huart);
  640.  
  641.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  642.     /* Check if a non-blocking receive process is ongoing or not */
  643.     if(huart->State == HAL_UART_STATE_BUSY_RX)
  644.     {
  645.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  646.     }
  647.     else
  648.     {
  649.       huart->State = HAL_UART_STATE_BUSY_TX;
  650.     }
  651.  
  652.     huart->TxXferSize = Size;
  653.     huart->TxXferCount = Size;
  654.     while(huart->TxXferCount > 0)
  655.     {
  656.       huart->TxXferCount--;
  657.       if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  658.       {
  659.         if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  660.         {
  661.           return HAL_TIMEOUT;
  662.         }
  663.         tmp = (uint16_t*) pData;
  664.         huart->Instance->DR = (*tmp & (uint16_t)0x01FF);
  665.         if(huart->Init.Parity == UART_PARITY_NONE)
  666.         {
  667.           pData +=2;
  668.         }
  669.         else
  670.         {
  671.           pData +=1;
  672.         }
  673.       }
  674.       else
  675.       {
  676.         if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TXE, RESET, Timeout) != HAL_OK)
  677.         {
  678.           return HAL_TIMEOUT;
  679.         }
  680.         huart->Instance->DR = (*pData++ & (uint8_t)0xFF);
  681.       }
  682.     }
  683.  
  684.     if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_TC, RESET, Timeout) != HAL_OK)
  685.     {
  686.       return HAL_TIMEOUT;
  687.     }
  688.  
  689.     /* Check if a non-blocking receive process is ongoing or not */
  690.     if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  691.     {
  692.       huart->State = HAL_UART_STATE_BUSY_RX;
  693.     }
  694.     else
  695.     {
  696.       huart->State = HAL_UART_STATE_READY;
  697.     }
  698.  
  699.     /* Process Unlocked */
  700.     __HAL_UNLOCK(huart);
  701.  
  702.     return HAL_OK;
  703.   }
  704.   else
  705.   {
  706.     return HAL_BUSY;
  707.   }
  708. }
  709.  
  710. /**
  711.   * @brief  Receives an amount of data in blocking mode.
  712.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  713.   *                the configuration information for the specified UART module.
  714.   * @param  pData: Pointer to data buffer
  715.   * @param  Size: Amount of data to be received
  716.   * @param  Timeout: Timeout duration
  717.   * @retval HAL status
  718.   */
  719. HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  720. {
  721.   uint16_t* tmp;
  722.   uint32_t  tmp_state = 0;
  723.  
  724.   tmp_state = huart->State;
  725.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX))
  726.   {
  727.     if((pData == NULL ) || (Size == 0))
  728.     {
  729.       return  HAL_ERROR;
  730.     }
  731.  
  732.     /* Process Locked */
  733.     __HAL_LOCK(huart);
  734.  
  735.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  736.     /* Check if a non-blocking transmit process is ongoing or not */
  737.     if(huart->State == HAL_UART_STATE_BUSY_TX)
  738.     {
  739.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  740.     }
  741.     else
  742.     {
  743.       huart->State = HAL_UART_STATE_BUSY_RX;
  744.     }
  745.  
  746.     huart->RxXferSize = Size;
  747.     huart->RxXferCount = Size;
  748.  
  749.     /* Check the remain data to be received */
  750.     while(huart->RxXferCount > 0)
  751.     {
  752.       huart->RxXferCount--;
  753.       if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  754.       {
  755.         if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  756.         {
  757.           return HAL_TIMEOUT;
  758.         }
  759.         tmp = (uint16_t*) pData ;
  760.         if(huart->Init.Parity == UART_PARITY_NONE)
  761.         {
  762.           *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  763.           pData +=2;
  764.         }
  765.         else
  766.         {
  767.           *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  768.           pData +=1;
  769.         }
  770.  
  771.       }
  772.       else
  773.       {
  774.         if(UART_WaitOnFlagUntilTimeout(huart, UART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  775.         {
  776.           return HAL_TIMEOUT;
  777.         }
  778.         if(huart->Init.Parity == UART_PARITY_NONE)
  779.         {
  780.           *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  781.         }
  782.         else
  783.         {
  784.           *pData++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  785.         }
  786.  
  787.       }
  788.     }
  789.  
  790.     /* Check if a non-blocking transmit process is ongoing or not */
  791.     if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  792.     {
  793.       huart->State = HAL_UART_STATE_BUSY_TX;
  794.     }
  795.     else
  796.     {
  797.       huart->State = HAL_UART_STATE_READY;
  798.     }
  799.     /* Process Unlocked */
  800.     __HAL_UNLOCK(huart);
  801.  
  802.     return HAL_OK;
  803.   }
  804.   else
  805.   {
  806.     return HAL_BUSY;
  807.   }
  808. }
  809.  
  810. /**
  811.   * @brief  Sends an amount of data in non blocking mode.
  812.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  813.   *                the configuration information for the specified UART module.
  814.   * @param  pData: Pointer to data buffer
  815.   * @param  Size: Amount of data to be sent
  816.   * @retval HAL status
  817.   */
  818. HAL_StatusTypeDef HAL_UART_Transmit_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  819. {
  820.   uint32_t tmp_state = 0;
  821.  
  822.   tmp_state = huart->State;
  823.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX))
  824.   {
  825.     if((pData == NULL ) || (Size == 0))
  826.     {
  827.       return HAL_ERROR;
  828.     }
  829.    
  830.     /* Process Locked */
  831.     __HAL_LOCK(huart);
  832.    
  833.     huart->pTxBuffPtr = pData;
  834.     huart->TxXferSize = Size;
  835.     huart->TxXferCount = Size;
  836.  
  837.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  838.     /* Check if a receive process is ongoing or not */
  839.     if(huart->State == HAL_UART_STATE_BUSY_RX)
  840.     {
  841.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  842.     }
  843.     else
  844.     {
  845.       huart->State = HAL_UART_STATE_BUSY_TX;
  846.     }
  847.  
  848.     /* Process Unlocked */
  849.     __HAL_UNLOCK(huart);
  850.  
  851.     /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  852.     __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
  853.  
  854.     /* Enable the UART Transmit data register empty Interrupt */
  855.     __HAL_UART_ENABLE_IT(huart, UART_IT_TXE);
  856.    
  857.     return HAL_OK;
  858.   }
  859.   else
  860.   {
  861.     return HAL_BUSY;
  862.   }
  863. }
  864.  
  865. /**
  866.   * @brief  Receives an amount of data in non blocking mode
  867.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  868.   *                the configuration information for the specified UART module.
  869.   * @param  pData: Pointer to data buffer
  870.   * @param  Size: Amount of data to be received
  871.   * @retval HAL status
  872.   */
  873. HAL_StatusTypeDef HAL_UART_Receive_IT(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  874. {
  875.   uint32_t tmp_state = 0;
  876.  
  877.   tmp_state = huart->State;
  878.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX))
  879.   {
  880.     if((pData == NULL ) || (Size == 0))
  881.     {
  882.       return HAL_ERROR;
  883.     }
  884.  
  885.     /* Process Locked */
  886.     __HAL_LOCK(huart);
  887.  
  888.     huart->pRxBuffPtr = pData;
  889.     huart->RxXferSize = Size;
  890.     huart->RxXferCount = Size;
  891.  
  892.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  893.     /* Check if a transmit process is ongoing or not */
  894.     if(huart->State == HAL_UART_STATE_BUSY_TX)
  895.     {
  896.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  897.     }
  898.     else
  899.     {
  900.       huart->State = HAL_UART_STATE_BUSY_RX;
  901.     }
  902.  
  903.     /* Process Unlocked */
  904.     __HAL_UNLOCK(huart);
  905.  
  906.     /* Enable the UART Parity Error Interrupt */
  907.     __HAL_UART_ENABLE_IT(huart, UART_IT_PE);
  908.  
  909.     /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  910.     __HAL_UART_ENABLE_IT(huart, UART_IT_ERR);
  911.  
  912.     /* Enable the UART Data Register not empty Interrupt */
  913.     __HAL_UART_ENABLE_IT(huart, UART_IT_RXNE);
  914.  
  915.     return HAL_OK;
  916.   }
  917.   else
  918.   {
  919.     return HAL_BUSY;
  920.   }
  921. }
  922.  
  923. /**
  924.   * @brief  Sends an amount of data in non blocking mode.
  925.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  926.   *                the configuration information for the specified UART module.
  927.   * @param  pData: Pointer to data buffer
  928.   * @param  Size: Amount of data to be sent
  929.   * @retval HAL status
  930.   */
  931. HAL_StatusTypeDef HAL_UART_Transmit_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  932. {
  933.   uint32_t *tmp;
  934.   uint32_t tmp_state = 0;
  935.  
  936.   tmp_state = huart->State;
  937.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_RX))
  938.   {
  939.     if((pData == NULL ) || (Size == 0))
  940.     {
  941.       return HAL_ERROR;
  942.     }
  943.  
  944.     /* Process Locked */
  945.     __HAL_LOCK(huart);
  946.  
  947.     huart->pTxBuffPtr = pData;
  948.     huart->TxXferSize = Size;
  949.     huart->TxXferCount = Size;
  950.  
  951.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  952.     /* Check if a receive process is ongoing or not */
  953.     if(huart->State == HAL_UART_STATE_BUSY_RX)
  954.     {
  955.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  956.     }
  957.     else
  958.     {
  959.       huart->State = HAL_UART_STATE_BUSY_TX;
  960.     }
  961.  
  962.     /* Set the UART DMA transfer complete callback */
  963.     huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt;
  964.  
  965.     /* Set the UART DMA Half transfer complete callback */
  966.     huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt;
  967.  
  968.     /* Set the DMA error callback */
  969.     huart->hdmatx->XferErrorCallback = UART_DMAError;
  970.  
  971.     /* Enable the UART transmit DMA channel */
  972.     tmp = (uint32_t*)&pData;
  973.     HAL_DMA_Start_IT(huart->hdmatx, *(uint32_t*)tmp, (uint32_t)&huart->Instance->DR, Size);
  974.  
  975.     /* Clear the TC flag in the SR register by writing 0 to it */
  976.     __HAL_UART_CLEAR_FLAG(huart, UART_FLAG_TC);
  977.  
  978.     /* Enable the DMA transfer for transmit request by setting the DMAT bit
  979.        in the UART CR3 register */
  980.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  981.  
  982.     /* Process Unlocked */
  983.     __HAL_UNLOCK(huart);
  984.  
  985.     return HAL_OK;
  986.   }
  987.   else
  988.   {
  989.     return HAL_BUSY;
  990.   }
  991. }
  992.  
  993. /**
  994.   * @brief  Receives an amount of data in non blocking mode.
  995.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  996.   *                the configuration information for the specified UART module.
  997.   * @param  pData: Pointer to data buffer
  998.   * @param  Size: Amount of data to be received
  999.   * @note   When the UART parity is enabled (PCE = 1), the received data contain
  1000.   *         the parity bit (MSB position)    
  1001.   * @retval HAL status
  1002.   */
  1003. HAL_StatusTypeDef HAL_UART_Receive_DMA(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
  1004. {
  1005.   uint32_t *tmp;
  1006.   uint32_t tmp_state = 0;
  1007.  
  1008.   tmp_state = huart->State;
  1009.   if((tmp_state == HAL_UART_STATE_READY) || (tmp_state == HAL_UART_STATE_BUSY_TX))
  1010.   {
  1011.     if((pData == NULL ) || (Size == 0))
  1012.     {
  1013.       return HAL_ERROR;
  1014.     }
  1015.  
  1016.     /* Process Locked */
  1017.     __HAL_LOCK(huart);
  1018.  
  1019.     huart->pRxBuffPtr = pData;
  1020.     huart->RxXferSize = Size;
  1021.  
  1022.     huart->ErrorCode = HAL_UART_ERROR_NONE;
  1023.     /* Check if a transmit process is ongoing or not */
  1024.     if(huart->State == HAL_UART_STATE_BUSY_TX)
  1025.     {
  1026.       huart->State = HAL_UART_STATE_BUSY_TX_RX;
  1027.     }
  1028.     else
  1029.     {
  1030.       huart->State = HAL_UART_STATE_BUSY_RX;
  1031.     }
  1032.  
  1033.     /* Set the UART DMA transfer complete callback */
  1034.     huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt;
  1035.  
  1036.     /* Set the UART DMA Half transfer complete callback */
  1037.     huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt;
  1038.  
  1039.     /* Set the DMA error callback */
  1040.     huart->hdmarx->XferErrorCallback = UART_DMAError;
  1041.  
  1042.     /* Enable the DMA channel */
  1043.     tmp = (uint32_t*)&pData;
  1044.     HAL_DMA_Start_IT(huart->hdmarx, (uint32_t)&huart->Instance->DR, *(uint32_t*)tmp, Size);
  1045.  
  1046.     /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1047.        in the UART CR3 register */
  1048.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1049.  
  1050.     /* Process Unlocked */
  1051.     __HAL_UNLOCK(huart);
  1052.  
  1053.     return HAL_OK;
  1054.   }
  1055.   else
  1056.   {
  1057.     return HAL_BUSY;
  1058.   }
  1059. }
  1060.    
  1061. /**
  1062.   * @brief Pauses the DMA Transfer.
  1063.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1064.   *                the configuration information for the specified UART module.
  1065.   * @retval HAL status
  1066.   */
  1067. HAL_StatusTypeDef HAL_UART_DMAPause(UART_HandleTypeDef *huart)
  1068. {
  1069.   /* Process Locked */
  1070.   __HAL_LOCK(huart);
  1071.  
  1072.   if(huart->State == HAL_UART_STATE_BUSY_TX)
  1073.   {
  1074.     /* Disable the UART DMA Tx request */
  1075.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1076.   }
  1077.   else if(huart->State == HAL_UART_STATE_BUSY_RX)
  1078.   {
  1079.     /* Disable the UART DMA Rx request */
  1080.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1081.   }
  1082.   else if (huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1083.   {
  1084.     /* Disable the UART DMA Tx & Rx requests */
  1085.     CLEAR_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
  1086.   }
  1087.   else
  1088.   {
  1089.     /* Process Unlocked */
  1090.     __HAL_UNLOCK(huart);
  1091.  
  1092.     return HAL_ERROR;
  1093.   }
  1094.  
  1095.   /* Process Unlocked */
  1096.   __HAL_UNLOCK(huart);
  1097.  
  1098.   return HAL_OK;
  1099. }
  1100.  
  1101. /**
  1102.   * @brief Resumes the DMA Transfer.
  1103.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1104.   *                the configuration information for the specified UART module.
  1105.   * @retval HAL status
  1106.   */
  1107. HAL_StatusTypeDef HAL_UART_DMAResume(UART_HandleTypeDef *huart)
  1108. {
  1109.   /* Process Locked */
  1110.   __HAL_LOCK(huart);
  1111.  
  1112.   if(huart->State == HAL_UART_STATE_BUSY_TX)
  1113.   {
  1114.     /* Enable the UART DMA Tx request */
  1115.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1116.   }
  1117.   else if(huart->State == HAL_UART_STATE_BUSY_RX)
  1118.   {
  1119.     /* Clear the Overrun flag before resumming the Rx transfer*/
  1120.     __HAL_UART_CLEAR_OREFLAG(huart);
  1121.     /* Enable the UART DMA Rx request */
  1122.     SET_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1123.   }
  1124.   else if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1125.   {
  1126.     /* Clear the Overrun flag before resumming the Rx transfer*/
  1127.     __HAL_UART_CLEAR_OREFLAG(huart);
  1128.     /* Enable the UART DMA Tx & Rx request */
  1129.     SET_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
  1130.   }
  1131.   else
  1132.   {
  1133.     /* Process Unlocked */
  1134.     __HAL_UNLOCK(huart);
  1135.  
  1136.     return HAL_ERROR;
  1137.   }
  1138.  
  1139.   /* Process Unlocked */
  1140.   __HAL_UNLOCK(huart);
  1141.  
  1142.   return HAL_OK;
  1143. }
  1144.  
  1145. /**
  1146.   * @brief Stops the DMA Transfer.
  1147.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1148.   *                the configuration information for the specified UART module.
  1149.   * @retval HAL status
  1150.   */
  1151. HAL_StatusTypeDef HAL_UART_DMAStop(UART_HandleTypeDef *huart)
  1152. {
  1153.   /* The Lock is not implemented on this API to allow the user application
  1154.      to call the HAL UART API under callbacks HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback():
  1155.      when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  1156.      and the correspond call back is executed HAL_UART_TxCpltCallback() / HAL_UART_RxCpltCallback()
  1157.      */
  1158.  
  1159.   /* Disable the UART Tx/Rx DMA requests */
  1160.   CLEAR_BIT(huart->Instance->CR3, (USART_CR3_DMAT | USART_CR3_DMAR));
  1161.  
  1162.   /* Abort the UART DMA tx channel */
  1163.   if(huart->hdmatx != NULL)
  1164.   {
  1165.     HAL_DMA_Abort(huart->hdmatx);
  1166.   }
  1167.   /* Abort the UART DMA rx channel */
  1168.   if(huart->hdmarx != NULL)
  1169.   {
  1170.     HAL_DMA_Abort(huart->hdmarx);
  1171.   }
  1172.  
  1173.   huart->State = HAL_UART_STATE_READY;
  1174.  
  1175.   return HAL_OK;
  1176. }
  1177.  
  1178. /**
  1179.   * @brief  This function handles UART interrupt request.
  1180.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1181.   *                the configuration information for the specified UART module.
  1182.   * @retval None
  1183.   */
  1184. void HAL_UART_IRQHandler(UART_HandleTypeDef *huart)
  1185. {
  1186.   uint32_t tmp_flag = 0, tmp_it_source = 0;
  1187.  
  1188.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_PE);
  1189.   tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_PE);  
  1190.   /* UART parity error interrupt occurred ------------------------------------*/
  1191.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1192.   {
  1193.     __HAL_UART_CLEAR_PEFLAG(huart);
  1194.    
  1195.     huart->ErrorCode |= HAL_UART_ERROR_PE;
  1196.   }
  1197.  
  1198.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_FE);
  1199.   tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_ERR);
  1200.   /* UART frame error interrupt occurred -------------------------------------*/
  1201.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1202.   {
  1203.     __HAL_UART_CLEAR_FEFLAG(huart);
  1204.    
  1205.     huart->ErrorCode |= HAL_UART_ERROR_FE;
  1206.   }
  1207.  
  1208.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_NE);
  1209.   /* UART noise error interrupt occurred -------------------------------------*/
  1210.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1211.   {
  1212.     __HAL_UART_CLEAR_NEFLAG(huart);
  1213.    
  1214.     huart->ErrorCode |= HAL_UART_ERROR_NE;
  1215.   }
  1216.  
  1217.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_ORE);
  1218.   /* UART Over-Run interrupt occurred ----------------------------------------*/
  1219.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1220.   {
  1221.     __HAL_UART_CLEAR_OREFLAG(huart);
  1222.    
  1223.     huart->ErrorCode |= HAL_UART_ERROR_ORE;
  1224.   }
  1225.  
  1226.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE);
  1227.   tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_RXNE);
  1228.   /* UART in mode Receiver ---------------------------------------------------*/
  1229.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1230.   {
  1231.     UART_Receive_IT(huart);
  1232.   }
  1233.  
  1234.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_TXE);
  1235.   tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TXE);
  1236.   /* UART in mode Transmitter ------------------------------------------------*/
  1237.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1238.   {
  1239.     UART_Transmit_IT(huart);
  1240.   }
  1241.  
  1242.   tmp_flag = __HAL_UART_GET_FLAG(huart, UART_FLAG_TC);
  1243.   tmp_it_source = __HAL_UART_GET_IT_SOURCE(huart, UART_IT_TC);
  1244.   /* UART in mode Transmitter end --------------------------------------------*/
  1245.   if((tmp_flag != RESET) && (tmp_it_source != RESET))
  1246.   {
  1247.     UART_EndTransmit_IT(huart);
  1248.   }  
  1249.  
  1250.   if(huart->ErrorCode != HAL_UART_ERROR_NONE)
  1251.   {
  1252.     /* Set the UART state ready to be able to start again the process */
  1253.     huart->State = HAL_UART_STATE_READY;
  1254.    
  1255.     HAL_UART_ErrorCallback(huart);
  1256.   }  
  1257. }
  1258.  
  1259. /**
  1260.   * @brief  Tx Transfer completed callbacks.
  1261.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1262.   *                the configuration information for the specified UART module.
  1263.   * @retval None
  1264.   */
  1265.  __weak void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  1266. {
  1267.   /* NOTE: This function should not be modified, when the callback is needed,
  1268.            the HAL_UART_TxCpltCallback can be implemented in the user file
  1269.    */
  1270. }
  1271.  
  1272. /**
  1273.   * @brief  Tx Half Transfer completed callbacks.
  1274.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1275.   *                the configuration information for the specified UART module.
  1276.   * @retval None
  1277.   */
  1278.  __weak void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
  1279. {
  1280.   /* NOTE: This function should not be modified, when the callback is needed,
  1281.            the HAL_UART_TxHalfCpltCallback can be implemented in the user file
  1282.    */
  1283. }
  1284.  
  1285. /**
  1286.   * @brief  Rx Transfer completed callbacks.
  1287.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1288.   *                the configuration information for the specified UART module.
  1289.   * @retval None
  1290.   */
  1291. __weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  1292. {
  1293.   /* NOTE: This function should not be modified, when the callback is needed,
  1294.            the HAL_UART_RxCpltCallback can be implemented in the user file
  1295.    */
  1296. }
  1297.  
  1298. /**
  1299.   * @brief  Rx Half Transfer completed callbacks.
  1300.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1301.   *                the configuration information for the specified UART module.
  1302.   * @retval None
  1303.   */
  1304. __weak void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart)
  1305. {
  1306.   /* NOTE: This function should not be modified, when the callback is needed,
  1307.            the HAL_UART_RxHalfCpltCallback can be implemented in the user file
  1308.    */
  1309. }
  1310.  
  1311. /**
  1312.   * @brief  UART error callbacks.
  1313.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1314.   *                the configuration information for the specified UART module.
  1315.   * @retval None
  1316.   */
  1317.  __weak void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
  1318. {
  1319.   /* NOTE: This function should not be modified, when the callback is needed,
  1320.            the HAL_UART_ErrorCallback can be implemented in the user file
  1321.    */
  1322. }
  1323.  
  1324. /**
  1325.   * @}
  1326.   */
  1327.  
  1328. /** @defgroup UART_Exported_Functions_Group3 Peripheral Control functions
  1329.   *  @brief   UART control functions
  1330.   *
  1331. @verbatim  
  1332.   ==============================================================================
  1333.                       ##### Peripheral Control functions #####
  1334.   ==============================================================================  
  1335.   [..]
  1336.     This subsection provides a set of functions allowing to control the UART:
  1337.     (+) HAL_LIN_SendBreak() API can be helpful to transmit the break character.
  1338.     (+) HAL_MultiProcessor_EnterMuteMode() API can be helpful to enter the UART in mute mode.
  1339.     (+) HAL_MultiProcessor_ExitMuteMode() API can be helpful to exit the UART mute mode by software.
  1340.     (+) HAL_HalfDuplex_EnableTransmitter() API to enable the UART transmitter and disables the UART receiver in Half Duplex mode
  1341.     (+) HAL_HalfDuplex_EnableReceiver() API to enable the UART receiver and disables the UART transmitter in Half Duplex mode
  1342.    
  1343. @endverbatim
  1344.   * @{
  1345.   */
  1346.  
  1347. /**
  1348.   * @brief  Transmits break characters.
  1349.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1350.   *                the configuration information for the specified UART module.
  1351.   * @retval HAL status
  1352.   */
  1353. HAL_StatusTypeDef HAL_LIN_SendBreak(UART_HandleTypeDef *huart)
  1354. {
  1355.   /* Check the parameters */
  1356.   assert_param(IS_UART_INSTANCE(huart->Instance));
  1357.  
  1358.   /* Process Locked */
  1359.   __HAL_LOCK(huart);
  1360.  
  1361.   huart->State = HAL_UART_STATE_BUSY;
  1362.  
  1363.   /* Send break characters */
  1364.   SET_BIT(huart->Instance->CR1, USART_CR1_SBK);
  1365.  
  1366.   huart->State = HAL_UART_STATE_READY;
  1367.  
  1368.   /* Process Unlocked */
  1369.   __HAL_UNLOCK(huart);
  1370.  
  1371.   return HAL_OK;
  1372. }
  1373.  
  1374. /**
  1375.   * @brief  Enters the UART in mute mode.
  1376.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1377.   *                the configuration information for the specified UART module.
  1378.   * @retval HAL status
  1379.   */
  1380. HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode(UART_HandleTypeDef *huart)
  1381. {
  1382.   /* Check the parameters */
  1383.   assert_param(IS_UART_INSTANCE(huart->Instance));
  1384.  
  1385.   /* Process Locked */
  1386.   __HAL_LOCK(huart);
  1387.  
  1388.   huart->State = HAL_UART_STATE_BUSY;
  1389.  
  1390.   /* Enable the USART mute mode  by setting the RWU bit in the CR1 register */
  1391.   SET_BIT(huart->Instance->CR1, USART_CR1_RWU);
  1392.  
  1393.   huart->State = HAL_UART_STATE_READY;
  1394.  
  1395.   /* Process Unlocked */
  1396.   __HAL_UNLOCK(huart);
  1397.  
  1398.   return HAL_OK;
  1399. }
  1400.  
  1401. /**
  1402.   * @brief  Exits the UART mute mode: wake up software.
  1403.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1404.   *                the configuration information for the specified UART module.
  1405.   * @retval HAL status
  1406.   */
  1407. HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode(UART_HandleTypeDef *huart)
  1408. {
  1409.   /* Check the parameters */
  1410.   assert_param(IS_UART_INSTANCE(huart->Instance));
  1411.  
  1412.   /* Process Locked */
  1413.   __HAL_LOCK(huart);
  1414.  
  1415.   huart->State = HAL_UART_STATE_BUSY;
  1416.  
  1417.   /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
  1418.   CLEAR_BIT(huart->Instance->CR1, USART_CR1_RWU);
  1419.  
  1420.   huart->State = HAL_UART_STATE_READY;
  1421.  
  1422.   /* Process Unlocked */
  1423.   __HAL_UNLOCK(huart);
  1424.  
  1425.   return HAL_OK;
  1426. }
  1427.  
  1428. /**
  1429.   * @brief  Enables the UART transmitter and disables the UART receiver.
  1430.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1431.   *                the configuration information for the specified UART module.
  1432.   * @retval HAL status
  1433.   */
  1434. HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter(UART_HandleTypeDef *huart)
  1435. {
  1436.   /* Process Locked */
  1437.   __HAL_LOCK(huart);
  1438.  
  1439.   huart->State = HAL_UART_STATE_BUSY;
  1440.  
  1441.   /*-------------------------- USART CR1 Configuration -----------------------*/
  1442.   /* Clear TE and RE bits */
  1443.   /* Enable the USART's transmit interface by setting the TE bit in the USART CR1 register */
  1444.   MODIFY_REG(huart->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE), USART_CR1_TE);
  1445.  
  1446.   huart->State = HAL_UART_STATE_READY;
  1447.  
  1448.   /* Process Unlocked */
  1449.   __HAL_UNLOCK(huart);
  1450.  
  1451.   return HAL_OK;
  1452. }
  1453.  
  1454. /**
  1455.   * @brief  Enables the UART receiver and disables the UART transmitter.
  1456.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1457.   *                the configuration information for the specified UART module.
  1458.   * @retval HAL status
  1459.   */
  1460. HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver(UART_HandleTypeDef *huart)
  1461. {
  1462.   /* Process Locked */
  1463.   __HAL_LOCK(huart);
  1464.  
  1465.   huart->State = HAL_UART_STATE_BUSY;
  1466.  
  1467.   /*-------------------------- USART CR1 Configuration -----------------------*/
  1468.   /* Clear TE and RE bits */
  1469.   /* Enable the USART's receive interface by setting the RE bit in the USART CR1 register */
  1470.   MODIFY_REG(huart->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE), USART_CR1_RE);
  1471.  
  1472.   huart->State = HAL_UART_STATE_READY;
  1473.  
  1474.   /* Process Unlocked */
  1475.   __HAL_UNLOCK(huart);
  1476.  
  1477.   return HAL_OK;
  1478. }
  1479.  
  1480. /**
  1481.   * @}
  1482.   */
  1483.  
  1484. /** @defgroup UART_Exported_Functions_Group4 Peripheral State and Errors functions
  1485.   *  @brief   UART State and Errors functions
  1486.   *
  1487. @verbatim  
  1488.   ==============================================================================
  1489.                  ##### Peripheral State and Errors functions #####
  1490.   ==============================================================================  
  1491.  [..]
  1492.    This subsection provides a set of functions allowing to return the State of
  1493.    UART communication process, return Peripheral Errors occurred during communication
  1494.    process
  1495.    (+) HAL_UART_GetState() API can be helpful to check in run-time the state of the UART peripheral.
  1496.    (+) HAL_UART_GetError() check in run-time errors that could be occurred during communication.
  1497.  
  1498. @endverbatim
  1499.   * @{
  1500.   */
  1501.  
  1502. /**
  1503.   * @brief  Returns the UART state.
  1504.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1505.   *                the configuration information for the specified UART module.
  1506.   * @retval HAL state
  1507.   */
  1508. HAL_UART_StateTypeDef HAL_UART_GetState(UART_HandleTypeDef *huart)
  1509. {
  1510.   return huart->State;
  1511. }
  1512.  
  1513. /**
  1514. * @brief  Return the UART error code
  1515. * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1516.   *              the configuration information for the specified UART.
  1517. * @retval UART Error Code
  1518. */
  1519. uint32_t HAL_UART_GetError(UART_HandleTypeDef *huart)
  1520. {
  1521.   return huart->ErrorCode;
  1522. }
  1523.  
  1524. /**
  1525.   * @}
  1526.   */
  1527.  
  1528. /**
  1529.   * @}
  1530.   */
  1531.  
  1532. /** @defgroup UART_Private_Functions   UART Private Functions
  1533.   *  @brief   UART Private functions
  1534.   * @{
  1535.   */
  1536. /**
  1537.   * @brief  DMA UART transmit process complete callback.
  1538.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1539.   *               the configuration information for the specified DMA module.
  1540.   * @retval None
  1541.   */
  1542. static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma)    
  1543. {
  1544.   UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1545.   /* DMA Normal mode*/
  1546.   if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1547.   {
  1548.     huart->TxXferCount = 0;
  1549.  
  1550.     /* Disable the DMA transfer for transmit request by setting the DMAT bit
  1551.        in the UART CR3 register */
  1552.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAT);
  1553.  
  1554.     /* Enable the UART Transmit Complete Interrupt */    
  1555.     __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1556.   }
  1557.   /* DMA Circular mode */
  1558.   else
  1559.   {
  1560.     HAL_UART_TxCpltCallback(huart);
  1561.   }
  1562. }
  1563.  
  1564. /**
  1565.   * @brief DMA UART transmit process half complete callback
  1566.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1567.   *               the configuration information for the specified DMA module.
  1568.   * @retval None
  1569.   */
  1570. static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1571. {
  1572.   UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1573.  
  1574.   HAL_UART_TxHalfCpltCallback(huart);
  1575. }
  1576.  
  1577. /**
  1578.   * @brief  DMA UART receive process complete callback.
  1579.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1580.   *               the configuration information for the specified DMA module.
  1581.   * @retval None
  1582.   */
  1583. static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)  
  1584. {
  1585.   UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1586.   /* DMA Normal mode*/
  1587.   if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1588.   {
  1589.     huart->RxXferCount = 0;
  1590.  
  1591.     /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  1592.        in the UART CR3 register */
  1593.     CLEAR_BIT(huart->Instance->CR3, USART_CR3_DMAR);
  1594.  
  1595.     /* Check if a transmit process is ongoing or not */
  1596.     if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1597.     {
  1598.       huart->State = HAL_UART_STATE_BUSY_TX;
  1599.     }
  1600.     else
  1601.     {
  1602.       huart->State = HAL_UART_STATE_READY;
  1603.     }
  1604.   }
  1605.   HAL_UART_RxCpltCallback(huart);
  1606. }
  1607.  
  1608. /**
  1609.   * @brief DMA UART receive process half complete callback
  1610.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1611.   *               the configuration information for the specified DMA module.
  1612.   * @retval None
  1613.   */
  1614. static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1615. {
  1616.   UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1617.  
  1618.   HAL_UART_RxHalfCpltCallback(huart);
  1619. }
  1620.  
  1621. /**
  1622.   * @brief  DMA UART communication error callback.
  1623.   * @param  hdma: Pointer to a DMA_HandleTypeDef structure that contains
  1624.   *               the configuration information for the specified DMA module.
  1625.   * @retval None
  1626.   */
  1627. static void UART_DMAError(DMA_HandleTypeDef *hdma)  
  1628. {
  1629.   UART_HandleTypeDef* huart = ( UART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1630.   huart->RxXferCount = 0;
  1631.   huart->TxXferCount = 0;
  1632.   huart->State= HAL_UART_STATE_READY;
  1633.   huart->ErrorCode |= HAL_UART_ERROR_DMA;
  1634.   HAL_UART_ErrorCallback(huart);
  1635. }
  1636.  
  1637. /**
  1638.   * @brief  This function handles UART Communication Timeout.
  1639.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1640.   *                the configuration information for the specified UART module.
  1641.   * @param  Flag: specifies the UART flag to check.
  1642.   * @param  Status: The new Flag status (SET or RESET).
  1643.   * @param  Timeout: Timeout duration
  1644.   * @retval HAL status
  1645.   */
  1646. static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout(UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1647. {
  1648.   uint32_t tickstart = 0;
  1649.  
  1650.   /* Get tick */
  1651.   tickstart = HAL_GetTick();
  1652.  
  1653.   /* Wait until flag is set */
  1654.   if(Status == RESET)
  1655.   {
  1656.     while(__HAL_UART_GET_FLAG(huart, Flag) == RESET)
  1657.     {
  1658.       /* Check for the Timeout */
  1659.       if(Timeout != HAL_MAX_DELAY)
  1660.       {
  1661.         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1662.         {
  1663.           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1664.           __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1665.           __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1666.           __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1667.           __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1668.  
  1669.           huart->State= HAL_UART_STATE_READY;
  1670.  
  1671.           /* Process Unlocked */
  1672.           __HAL_UNLOCK(huart);
  1673.  
  1674.           return HAL_TIMEOUT;
  1675.         }
  1676.       }
  1677.     }
  1678.   }
  1679.   else
  1680.   {
  1681.     while(__HAL_UART_GET_FLAG(huart, Flag) != RESET)
  1682.     {
  1683.       /* Check for the Timeout */
  1684.       if(Timeout != HAL_MAX_DELAY)
  1685.       {
  1686.         if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  1687.         {
  1688.           /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1689.           __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1690.           __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1691.           __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1692.           __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1693.  
  1694.           huart->State= HAL_UART_STATE_READY;
  1695.  
  1696.           /* Process Unlocked */
  1697.           __HAL_UNLOCK(huart);
  1698.  
  1699.           return HAL_TIMEOUT;
  1700.         }
  1701.       }
  1702.     }
  1703.   }
  1704.   return HAL_OK;
  1705. }
  1706.  
  1707. /**
  1708.   * @brief  Sends an amount of data in non blocking mode.
  1709.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1710.   *                the configuration information for the specified UART module.
  1711.   * @retval HAL status
  1712.   */
  1713. static HAL_StatusTypeDef UART_Transmit_IT(UART_HandleTypeDef *huart)
  1714. {
  1715.   uint16_t* tmp;
  1716.   uint32_t tmp_state = 0;
  1717.  
  1718.   tmp_state = huart->State;
  1719.   if((tmp_state == HAL_UART_STATE_BUSY_TX) || (tmp_state == HAL_UART_STATE_BUSY_TX_RX))
  1720.   {
  1721.     if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  1722.     {
  1723.       tmp = (uint16_t*) huart->pTxBuffPtr;
  1724.       huart->Instance->DR = (uint16_t)(*tmp & (uint16_t)0x01FF);
  1725.       if(huart->Init.Parity == UART_PARITY_NONE)
  1726.       {
  1727.         huart->pTxBuffPtr += 2;
  1728.       }
  1729.       else
  1730.       {
  1731.         huart->pTxBuffPtr += 1;
  1732.       }
  1733.     }
  1734.     else
  1735.     {
  1736.       huart->Instance->DR = (uint8_t)(*huart->pTxBuffPtr++ & (uint8_t)0x00FF);
  1737.     }
  1738.  
  1739.     if(--huart->TxXferCount == 0)
  1740.     {
  1741.       /* Disable the UART Transmit Complete Interrupt */
  1742.       __HAL_UART_DISABLE_IT(huart, UART_IT_TXE);
  1743.  
  1744.       /* Enable the UART Transmit Complete Interrupt */    
  1745.       __HAL_UART_ENABLE_IT(huart, UART_IT_TC);
  1746.     }
  1747.     return HAL_OK;
  1748.   }
  1749.   else
  1750.   {
  1751.     return HAL_BUSY;
  1752.   }
  1753. }
  1754.  
  1755.  
  1756. /**
  1757.   * @brief  Wraps up transmission in non blocking mode.
  1758.   * @param  huart: pointer to a UART_HandleTypeDef structure that contains
  1759.   *                the configuration information for the specified UART module.
  1760.   * @retval HAL status
  1761.   */
  1762. static HAL_StatusTypeDef UART_EndTransmit_IT(UART_HandleTypeDef *huart)
  1763. {
  1764.   /* Disable the UART Transmit Complete Interrupt */    
  1765.   __HAL_UART_DISABLE_IT(huart, UART_IT_TC);
  1766.  
  1767.   /* Check if a receive process is ongoing or not */
  1768.   if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1769.   {
  1770.     huart->State = HAL_UART_STATE_BUSY_RX;
  1771.   }
  1772.   else
  1773.   {
  1774.     /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1775.     __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1776.  
  1777.     huart->State = HAL_UART_STATE_READY;
  1778.   }
  1779.  
  1780.   HAL_UART_TxCpltCallback(huart);
  1781.  
  1782.   return HAL_OK;
  1783. }
  1784.  
  1785. /**
  1786.   * @brief  Receives an amount of data in non blocking mode
  1787.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1788.   *                the configuration information for the specified UART module.
  1789.   * @retval HAL status
  1790.   */
  1791. static HAL_StatusTypeDef UART_Receive_IT(UART_HandleTypeDef *huart)
  1792. {
  1793.   uint16_t* tmp;
  1794.   uint32_t tmp_state = 0;
  1795.  
  1796.   tmp_state = huart->State;
  1797.   if((tmp_state == HAL_UART_STATE_BUSY_RX) || (tmp_state == HAL_UART_STATE_BUSY_TX_RX))
  1798.   {
  1799.     if(huart->Init.WordLength == UART_WORDLENGTH_9B)
  1800.     {
  1801.       tmp = (uint16_t*) huart->pRxBuffPtr;
  1802.       if(huart->Init.Parity == UART_PARITY_NONE)
  1803.       {
  1804.         *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x01FF);
  1805.         huart->pRxBuffPtr += 2;
  1806.       }
  1807.       else
  1808.       {
  1809.         *tmp = (uint16_t)(huart->Instance->DR & (uint16_t)0x00FF);
  1810.         huart->pRxBuffPtr += 1;
  1811.       }
  1812.     }
  1813.     else
  1814.     {
  1815.       if(huart->Init.Parity == UART_PARITY_NONE)
  1816.       {
  1817.         *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x00FF);
  1818.       }
  1819.       else
  1820.       {
  1821.         *huart->pRxBuffPtr++ = (uint8_t)(huart->Instance->DR & (uint8_t)0x007F);
  1822.       }
  1823.     }
  1824.  
  1825.     if(--huart->RxXferCount == 0)
  1826.     {
  1827.       __HAL_UART_DISABLE_IT(huart, UART_IT_RXNE);
  1828.  
  1829.       /* Check if a transmit process is ongoing or not */
  1830.       if(huart->State == HAL_UART_STATE_BUSY_TX_RX)
  1831.       {
  1832.         huart->State = HAL_UART_STATE_BUSY_TX;
  1833.       }
  1834.       else
  1835.       {
  1836.         /* Disable the UART Parity Error Interrupt */
  1837.         __HAL_UART_DISABLE_IT(huart, UART_IT_PE);
  1838.  
  1839.         /* Disable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  1840.         __HAL_UART_DISABLE_IT(huart, UART_IT_ERR);
  1841.  
  1842.         huart->State = HAL_UART_STATE_READY;
  1843.       }
  1844.       HAL_UART_RxCpltCallback(huart);
  1845.  
  1846.       return HAL_OK;
  1847.     }
  1848.     return HAL_OK;
  1849.   }
  1850.   else
  1851.   {
  1852.     return HAL_BUSY;
  1853.   }
  1854. }
  1855.  
  1856. /**
  1857.   * @brief  Configures the UART peripheral.
  1858.   * @param  huart: Pointer to a UART_HandleTypeDef structure that contains
  1859.   *                the configuration information for the specified UART module.
  1860.   * @retval None
  1861.   */
  1862. static void UART_SetConfig(UART_HandleTypeDef *huart)
  1863. {
  1864.   uint32_t tmpreg = 0x00;
  1865.  
  1866.   /* Check the parameters */
  1867.   assert_param(IS_UART_BAUDRATE(huart->Init.BaudRate));  
  1868.   assert_param(IS_UART_STOPBITS(huart->Init.StopBits));
  1869.   assert_param(IS_UART_PARITY(huart->Init.Parity));
  1870.   assert_param(IS_UART_MODE(huart->Init.Mode));
  1871.  
  1872.   /*------- UART-associated USART registers setting : CR2 Configuration ------*/
  1873.   /* Configure the UART Stop Bits: Set STOP[13:12] bits according
  1874.    * to huart->Init.StopBits value */
  1875.   MODIFY_REG(huart->Instance->CR2, USART_CR2_STOP, huart->Init.StopBits);
  1876.  
  1877.   /*------- UART-associated USART registers setting : CR1 Configuration ------*/
  1878.   /* Configure the UART Word Length, Parity and mode:
  1879.      Set the M bits according to huart->Init.WordLength value
  1880.      Set PCE and PS bits according to huart->Init.Parity value
  1881.      Set TE and RE bits according to huart->Init.Mode value */
  1882.   tmpreg = (uint32_t)huart->Init.WordLength | huart->Init.Parity | huart->Init.Mode ;
  1883.   MODIFY_REG(huart->Instance->CR1,
  1884.              (uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE),
  1885.              tmpreg);
  1886.  
  1887.   /*------- UART-associated USART registers setting : CR3 Configuration ------*/
  1888.   /* Configure the UART HFC: Set CTSE and RTSE bits according to huart->Init.HwFlowCtl value */
  1889.   MODIFY_REG(huart->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE), huart->Init.HwFlowCtl);
  1890.  
  1891.   /*------- UART-associated USART registers setting : BRR Configuration ------*/
  1892.   if((huart->Instance == USART1))
  1893.   {
  1894.     huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK2Freq(), huart->Init.BaudRate);
  1895.   }
  1896.   else
  1897.   {
  1898.     huart->Instance->BRR = UART_BRR_SAMPLING16(HAL_RCC_GetPCLK1Freq(), huart->Init.BaudRate);
  1899.   }
  1900. }
  1901. /**
  1902.   * @}
  1903.   */
  1904.  
  1905. #endif /* HAL_UART_MODULE_ENABLED */
  1906. /**
  1907.   * @}
  1908.   */
  1909.  
  1910. /**
  1911.   * @}
  1912.   */
  1913.  
  1914. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  1915.