STM32F4xx HAL Documentation
Hardware Abstraction Layer for STM32F4 familiy
Loading...
Searching...
No Matches
stm32f4xx_hal_uart.c File Reference

UART HAL module driver. This file provides firmware functions to manage the following functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART). More...

#include "stm32f4xx_hal.h"
Include dependency graph for stm32f4xx_hal_uart.c:

Go to the source code of this file.

Functions

static void UART_EndTxTransfer (UART_HandleTypeDef *huart)
 End ongoing Tx transfer on UART peripheral (following error detection or Transmit completion).
 
static void UART_EndRxTransfer (UART_HandleTypeDef *huart)
 End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
 
static void UART_DMATransmitCplt (DMA_HandleTypeDef *hdma)
 Initialize the callbacks to their default values.
 
static void UART_DMAReceiveCplt (DMA_HandleTypeDef *hdma)
 DMA UART receive process complete callback.
 
static void UART_DMATxHalfCplt (DMA_HandleTypeDef *hdma)
 DMA UART transmit process half complete callback.
 
static void UART_DMARxHalfCplt (DMA_HandleTypeDef *hdma)
 DMA UART receive process half complete callback.
 
static void UART_DMAError (DMA_HandleTypeDef *hdma)
 DMA UART communication error callback.
 
static void UART_DMAAbortOnError (DMA_HandleTypeDef *hdma)
 DMA UART communication abort callback, when initiated by HAL services on Error (To be called at end of DMA Abort procedure following error occurrence).
 
static void UART_DMATxAbortCallback (DMA_HandleTypeDef *hdma)
 DMA UART Tx communication abort callback, when initiated by user (To be called at end of DMA Tx Abort procedure following user abort request).
 
static void UART_DMARxAbortCallback (DMA_HandleTypeDef *hdma)
 DMA UART Rx communication abort callback, when initiated by user (To be called at end of DMA Rx Abort procedure following user abort request).
 
static void UART_DMATxOnlyAbortCallback (DMA_HandleTypeDef *hdma)
 DMA UART Tx communication abort callback, when initiated by user by a call to HAL_UART_AbortTransmit_IT API (Abort only Tx transfer) (This callback is executed at end of DMA Tx Abort procedure following user abort request, and leads to user Tx Abort Complete callback execution).
 
static void UART_DMARxOnlyAbortCallback (DMA_HandleTypeDef *hdma)
 DMA UART Rx communication abort callback, when initiated by user by a call to HAL_UART_AbortReceive_IT API (Abort only Rx transfer) (This callback is executed at end of DMA Rx Abort procedure following user abort request, and leads to user Rx Abort Complete callback execution).
 
static HAL_StatusTypeDef UART_Transmit_IT (UART_HandleTypeDef *huart)
 Sends an amount of data in non blocking mode.
 
static HAL_StatusTypeDef UART_EndTransmit_IT (UART_HandleTypeDef *huart)
 Wraps up transmission in non blocking mode.
 
static HAL_StatusTypeDef UART_Receive_IT (UART_HandleTypeDef *huart)
 Receives an amount of data in non blocking mode.
 
static HAL_StatusTypeDef UART_WaitOnFlagUntilTimeout (UART_HandleTypeDef *huart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
 This function handles UART Communication Timeout. It waits until a flag is no longer in the specified status.
 
static void UART_SetConfig (UART_HandleTypeDef *huart)
 Configures the UART peripheral.
 
HAL_StatusTypeDef HAL_UART_Init (UART_HandleTypeDef *huart)
 Initializes the UART mode according to the specified parameters in the UART_InitTypeDef and create the associated handle.
 
HAL_StatusTypeDef HAL_HalfDuplex_Init (UART_HandleTypeDef *huart)
 Initializes the half-duplex mode according to the specified parameters in the UART_InitTypeDef and create the associated handle.
 
HAL_StatusTypeDef HAL_LIN_Init (UART_HandleTypeDef *huart, uint32_t BreakDetectLength)
 Initializes the LIN mode according to the specified parameters in the UART_InitTypeDef and create the associated handle.
 
HAL_StatusTypeDef HAL_MultiProcessor_Init (UART_HandleTypeDef *huart, uint8_t Address, uint32_t WakeUpMethod)
 Initializes the Multi-Processor mode according to the specified parameters in the UART_InitTypeDef and create the associated handle.
 
HAL_StatusTypeDef HAL_UART_DeInit (UART_HandleTypeDef *huart)
 DeInitializes the UART peripheral.
 
void HAL_UART_MspInit (UART_HandleTypeDef *huart)
 UART MSP Init.
 
void HAL_UART_MspDeInit (UART_HandleTypeDef *huart)
 UART MSP DeInit.
 
HAL_StatusTypeDef HAL_UART_Transmit (UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size, uint32_t Timeout)
 Sends an amount of data in blocking mode.
 
HAL_StatusTypeDef HAL_UART_Receive (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)
 Receives an amount of data in blocking mode.
 
HAL_StatusTypeDef HAL_UART_Transmit_IT (UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
 Sends an amount of data in non blocking mode.
 
HAL_StatusTypeDef HAL_UART_Receive_IT (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Receives an amount of data in non blocking mode.
 
HAL_StatusTypeDef HAL_UART_Transmit_DMA (UART_HandleTypeDef *huart, const uint8_t *pData, uint16_t Size)
 Sends an amount of data in DMA mode.
 
HAL_StatusTypeDef HAL_UART_Receive_DMA (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Receives an amount of data in DMA mode.
 
HAL_StatusTypeDef HAL_UART_DMAPause (UART_HandleTypeDef *huart)
 Pauses the DMA Transfer.
 
HAL_StatusTypeDef HAL_UART_DMAResume (UART_HandleTypeDef *huart)
 Resumes the DMA Transfer.
 
HAL_StatusTypeDef HAL_UART_DMAStop (UART_HandleTypeDef *huart)
 Stops the DMA Transfer.
 
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint16_t *RxLen, uint32_t Timeout)
 Receive an amount of data in blocking mode till either the expected number of data is received or an IDLE event occurs.
 
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_IT (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Receive an amount of data in interrupt mode till either the expected number of data is received or an IDLE event occurs.
 
HAL_StatusTypeDef HAL_UARTEx_ReceiveToIdle_DMA (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Receive an amount of data in DMA mode till either the expected number of data is received or an IDLE event occurs.
 
HAL_UART_RxEventTypeTypeDef HAL_UARTEx_GetRxEventType (UART_HandleTypeDef *huart)
 Provide Rx Event type that has lead to RxEvent callback execution.
 
HAL_StatusTypeDef HAL_UART_Abort (UART_HandleTypeDef *huart)
 Abort ongoing transfers (blocking mode).
 
HAL_StatusTypeDef HAL_UART_AbortTransmit (UART_HandleTypeDef *huart)
 Abort ongoing Transmit transfer (blocking mode).
 
HAL_StatusTypeDef HAL_UART_AbortReceive (UART_HandleTypeDef *huart)
 Abort ongoing Receive transfer (blocking mode).
 
HAL_StatusTypeDef HAL_UART_Abort_IT (UART_HandleTypeDef *huart)
 Abort ongoing transfers (Interrupt mode).
 
HAL_StatusTypeDef HAL_UART_AbortTransmit_IT (UART_HandleTypeDef *huart)
 Abort ongoing Transmit transfer (Interrupt mode).
 
HAL_StatusTypeDef HAL_UART_AbortReceive_IT (UART_HandleTypeDef *huart)
 Abort ongoing Receive transfer (Interrupt mode).
 
void HAL_UART_IRQHandler (UART_HandleTypeDef *huart)
 This function handles UART interrupt request.
 
void HAL_UART_TxCpltCallback (UART_HandleTypeDef *huart)
 Tx Transfer completed callbacks.
 
void HAL_UART_TxHalfCpltCallback (UART_HandleTypeDef *huart)
 Tx Half Transfer completed callbacks.
 
void HAL_UART_RxCpltCallback (UART_HandleTypeDef *huart)
 Rx Transfer completed callbacks.
 
void HAL_UART_RxHalfCpltCallback (UART_HandleTypeDef *huart)
 Rx Half Transfer completed callbacks.
 
void HAL_UART_ErrorCallback (UART_HandleTypeDef *huart)
 UART error callbacks.
 
void HAL_UART_AbortCpltCallback (UART_HandleTypeDef *huart)
 UART Abort Complete callback.
 
void HAL_UART_AbortTransmitCpltCallback (UART_HandleTypeDef *huart)
 UART Abort Complete callback.
 
void HAL_UART_AbortReceiveCpltCallback (UART_HandleTypeDef *huart)
 UART Abort Receive Complete callback.
 
void HAL_UARTEx_RxEventCallback (UART_HandleTypeDef *huart, uint16_t Size)
 Reception Event Callback (Rx event notification called after use of advanced reception service).
 
HAL_StatusTypeDef HAL_LIN_SendBreak (UART_HandleTypeDef *huart)
 Transmits break characters.
 
HAL_StatusTypeDef HAL_MultiProcessor_EnterMuteMode (UART_HandleTypeDef *huart)
 Enters the UART in mute mode.
 
HAL_StatusTypeDef HAL_MultiProcessor_ExitMuteMode (UART_HandleTypeDef *huart)
 Exits the UART mute mode: wake up software.
 
HAL_StatusTypeDef HAL_HalfDuplex_EnableTransmitter (UART_HandleTypeDef *huart)
 Enables the UART transmitter and disables the UART receiver.
 
HAL_StatusTypeDef HAL_HalfDuplex_EnableReceiver (UART_HandleTypeDef *huart)
 Enables the UART receiver and disables the UART transmitter.
 
HAL_UART_StateTypeDef HAL_UART_GetState (const UART_HandleTypeDef *huart)
 Returns the UART state.
 
uint32_t HAL_UART_GetError (const UART_HandleTypeDef *huart)
 Return the UART error code.
 
HAL_StatusTypeDef UART_Start_Receive_IT (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Start Receive operation in interrupt mode.
 
HAL_StatusTypeDef UART_Start_Receive_DMA (UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size)
 Start Receive operation in DMA mode.
 

Detailed Description

UART HAL module driver. This file provides firmware functions to manage the following functionalities of the Universal Asynchronous Receiver Transmitter Peripheral (UART).

Author
MCD Application Team
  • Initialization and de-initialization functions
  • IO operation functions
  • Peripheral Control functions
  • Peripheral State and Errors functions
Attention

Copyright (c) 2016 STMicroelectronics. All rights reserved.

This software is licensed under terms that can be found in the LICENSE file in the root directory of this software component. If no LICENSE file comes with this software, it is provided AS-IS.

==============================================================================
                      ##### How to use this driver #####
==============================================================================
[..]
  The UART HAL driver can be used as follows:

  (#) Declare a UART_HandleTypeDef handle structure (eg. UART_HandleTypeDef huart).
  (#) Initialize the UART low level resources by implementing the HAL_UART_MspInit() API:
      (##) Enable the USARTx interface clock.
      (##) UART pins configuration:
          (+++) Enable the clock for the UART GPIOs.
          (+++) Configure the UART TX/RX pins as alternate function pull-up.
      (##) NVIC configuration if you need to use interrupt process (HAL_UART_Transmit_IT()
           and HAL_UART_Receive_IT() APIs):
          (+++) Configure the USARTx interrupt priority.
          (+++) Enable the NVIC USART IRQ handle.
      (##) DMA Configuration if you need to use DMA process (HAL_UART_Transmit_DMA()
           and HAL_UART_Receive_DMA() APIs):
          (+++) Declare a DMA handle structure for the Tx/Rx stream.
          (+++) Enable the DMAx interface clock.
          (+++) Configure the declared DMA handle structure with the required
                Tx/Rx parameters.
          (+++) Configure the DMA Tx/Rx stream.
          (+++) Associate the initialized DMA handle to the UART DMA Tx/Rx handle.
          (+++) Configure the priority and enable the NVIC for the transfer complete
                interrupt on the DMA Tx/Rx stream.
          (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
                (used for last byte sending completion detection in DMA non circular mode)

  (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
      flow control and Mode(Receiver/Transmitter) in the huart Init structure.

  (#) For the UART asynchronous mode, initialize the UART registers by calling
      the HAL_UART_Init() API.

  (#) For the UART Half duplex mode, initialize the UART registers by calling
      the HAL_HalfDuplex_Init() API.

  (#) For the LIN mode, initialize the UART registers by calling the HAL_LIN_Init() API.

  (#) For the Multi-Processor mode, initialize the UART registers by calling
      the HAL_MultiProcessor_Init() API.

   [..]
     (@) The specific UART interrupts (Transmission complete interrupt,
          RXNE interrupt and Error Interrupts) will be managed using the macros
          __HAL_UART_ENABLE_IT() and __HAL_UART_DISABLE_IT() inside the transmit
          and receive process.

   [..]
     (@) These APIs (HAL_UART_Init() and HAL_HalfDuplex_Init()) configure also the
          low level Hardware GPIO, CLOCK, CORTEX...etc) by calling the customized
          HAL_UART_MspInit() API.

  ##### Callback registration #####
  ==================================

  [..]
  The compilation define USE_HAL_UART_REGISTER_CALLBACKS when set to 1
  allows the user to configure dynamically the driver callbacks.

  [..]
  Use Function HAL_UART_RegisterCallback() to register a user callback.
  Function HAL_UART_RegisterCallback() allows to register following callbacks:
  (+) TxHalfCpltCallback        : Tx Half Complete Callback.
  (+) TxCpltCallback            : Tx Complete Callback.
  (+) RxHalfCpltCallback        : Rx Half Complete Callback.
  (+) RxCpltCallback            : Rx Complete Callback.
  (+) ErrorCallback             : Error Callback.
  (+) AbortCpltCallback         : Abort Complete Callback.
  (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  (+) MspInitCallback           : UART MspInit.
  (+) MspDeInitCallback         : UART MspDeInit.
  This function takes as parameters the HAL peripheral handle, the Callback ID
  and a pointer to the user callback function.

  [..]
  Use function HAL_UART_UnRegisterCallback() to reset a callback to the default
  weak (surcharged) function.
  HAL_UART_UnRegisterCallback() takes as parameters the HAL peripheral handle,
  and the Callback ID.
  This function allows to reset following callbacks:
  (+) TxHalfCpltCallback        : Tx Half Complete Callback.
  (+) TxCpltCallback            : Tx Complete Callback.
  (+) RxHalfCpltCallback        : Rx Half Complete Callback.
  (+) RxCpltCallback            : Rx Complete Callback.
  (+) ErrorCallback             : Error Callback.
  (+) AbortCpltCallback         : Abort Complete Callback.
  (+) AbortTransmitCpltCallback : Abort Transmit Complete Callback.
  (+) AbortReceiveCpltCallback  : Abort Receive Complete Callback.
  (+) MspInitCallback           : UART MspInit.
  (+) MspDeInitCallback         : UART MspDeInit.

  [..]
  For specific callback RxEventCallback, use dedicated registration/reset functions:
  respectively HAL_UART_RegisterRxEventCallback() , HAL_UART_UnRegisterRxEventCallback().

  [..]
  By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_RESET
  all callbacks are set to the corresponding weak (surcharged) functions:
  examples HAL_UART_TxCpltCallback(), HAL_UART_RxHalfCpltCallback().
  Exception done for MspInit and MspDeInit functions that are respectively
  reset to the legacy weak (surcharged) functions in the HAL_UART_Init()
  and HAL_UART_DeInit() only when these callbacks are null (not registered beforehand).
  If not, MspInit or MspDeInit are not null, the HAL_UART_Init() and HAL_UART_DeInit()
  keep and use the user MspInit/MspDeInit callbacks (registered beforehand).

  [..]
  Callbacks can be registered/unregistered in HAL_UART_STATE_READY state only.
  Exception done MspInit/MspDeInit that can be registered/unregistered
  in HAL_UART_STATE_READY or HAL_UART_STATE_RESET state, thus registered (user)
  MspInit/DeInit callbacks can be used during the Init/DeInit.
  In that case first register the MspInit/MspDeInit user callbacks
  using HAL_UART_RegisterCallback() before calling HAL_UART_DeInit()
  or HAL_UART_Init() function.

  [..]
  When The compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0 or
  not defined, the callback registration feature is not available
  and weak (surcharged) callbacks are used.

   [..]
      Three operation modes are available within this driver :

   *** Polling mode IO operation ***
   =================================
   [..]
     (+) Send an amount of data in blocking mode using HAL_UART_Transmit()
     (+) Receive an amount of data in blocking mode using HAL_UART_Receive()

   *** Interrupt mode IO operation ***
   ===================================
   [..]
     (+) Send an amount of data in non blocking mode using HAL_UART_Transmit_IT()
     (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_TxCpltCallback
     (+) Receive an amount of data in non blocking mode using HAL_UART_Receive_IT()
     (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_RxCpltCallback
     (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
          add his own code by customization of function pointer HAL_UART_ErrorCallback

   *** DMA mode IO operation ***
   ==============================
   [..]
     (+) Send an amount of data in non blocking mode (DMA) using HAL_UART_Transmit_DMA()
     (+) At transmission end of half transfer HAL_UART_TxHalfCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_TxHalfCpltCallback
     (+) At transmission end of transfer HAL_UART_TxCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_TxCpltCallback
     (+) Receive an amount of data in non blocking mode (DMA) using HAL_UART_Receive_DMA()
     (+) At reception end of half transfer HAL_UART_RxHalfCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_RxHalfCpltCallback
     (+) At reception end of transfer HAL_UART_RxCpltCallback is executed and user can
          add his own code by customization of function pointer HAL_UART_RxCpltCallback
     (+) In case of transfer Error, HAL_UART_ErrorCallback() function is executed and user can
          add his own code by customization of function pointer HAL_UART_ErrorCallback
     (+) Pause the DMA Transfer using HAL_UART_DMAPause()
     (+) Resume the DMA Transfer using HAL_UART_DMAResume()
     (+) Stop the DMA Transfer using HAL_UART_DMAStop()


  [..] This subsection also provides a set of additional functions providing enhanced reception
  services to user. (For example, these functions allow application to handle use cases
  where number of data to be received is unknown).

  (#) Compared to standard reception services which only consider number of received
      data elements as reception completion criteria, these functions also consider additional events
      as triggers for updating reception status to caller :
     (+) Detection of inactivity period (RX line has not been active for a given period).
        (++) RX inactivity detected by IDLE event, i.e. RX line has been in idle state (normally high state)
             for 1 frame time, after last received byte.

  (#) There are two mode of transfer:
     (+) Blocking mode: The reception is performed in polling mode, until either expected number of data is received,
         or till IDLE event occurs. Reception is handled only during function execution.
         When function exits, no data reception could occur. HAL status and number of actually received data elements,
         are returned by function after finishing transfer.
     (+) Non-Blocking mode: The reception is performed using Interrupts or DMA.
         These API's return the HAL status.
         The end of the data processing will be indicated through the
         dedicated UART IRQ when using Interrupt mode or the DMA IRQ when using DMA mode.
         The HAL_UARTEx_RxEventCallback() user callback will be executed during Receive process
         The HAL_UART_ErrorCallback()user callback will be executed when a reception error is detected.

  (#) Blocking mode API:
      (+) HAL_UARTEx_ReceiveToIdle()

  (#) Non-Blocking mode API with Interrupt:
      (+) HAL_UARTEx_ReceiveToIdle_IT()

  (#) Non-Blocking mode API with DMA:
      (+) HAL_UARTEx_ReceiveToIdle_DMA()


   *** UART HAL driver macros list ***
   =============================================
   [..]
     Below the list of most used macros in UART HAL driver.

    (+) __HAL_UART_ENABLE: Enable the UART peripheral
    (+) __HAL_UART_DISABLE: Disable the UART peripheral
    (+) __HAL_UART_GET_FLAG : Check whether the specified UART flag is set or not
    (+) __HAL_UART_CLEAR_FLAG : Clear the specified UART pending flag
    (+) __HAL_UART_ENABLE_IT: Enable the specified UART interrupt
    (+) __HAL_UART_DISABLE_IT: Disable the specified UART interrupt
    (+) __HAL_UART_GET_IT_SOURCE: Check whether the specified UART interrupt has occurred or not

   [..]
     (@) You can refer to the UART HAL driver header file for more useful macros

[..] (@) Additional remark: If the parity is enabled, then the MSB bit of the data written in the data register is transmitted but is changed by the parity bit. Depending on the frame length defined by the M bit (8-bits or 9-bits), the possible UART frame formats are as listed in the following table: +----------------------------------------------------------—+ | M bit | PCE bit | UART frame | |------------------—|------------------------------------—| | 0 | 0 | | SB | 8 bit data | STB | | |------—|--------—|------------------------------------—| | 0 | 1 | | SB | 7 bit data | PB | STB | | |------—|--------—|------------------------------------—| | 1 | 0 | | SB | 9 bit data | STB | | |------—|--------—|------------------------------------—| | 1 | 1 | | SB | 8 bit data | PB | STB | | +----------------------------------------------------------—+

Definition in file stm32f4xx_hal_uart.c.