HAL UART How to Use

group UART_How_To_Use

How to use the UART HAL module driver

Use the UART HAL driver as follows:

1- Declare a hal_uart_handle_t handle structure, for example:

hal_uart_handle_t huart;

2- Configure the low-level hardware (GPIO, CLOCK, NVIC, etc.):

  • Enable the USARTx interface clock if you have not set USE_HAL_UART_CLK_ENABLE_MODEL to HAL_CLK_ENABLE_PERIPH_ONLY or HAL_CLK_ENABLE_PERIPH_PWR_SYSTEM (in those cases HAL_UART_Init() will enable the clock).

  • UART pins configuration:

    • Enable the clock for the UART GPIOs

    • Configure these UART pins as alternate function.

  • Configure the NVIC when using the interrupt process (HAL_UART_Transmit_IT() and HAL_UART_Receive_IT(), … APIs):

    • Configure the USARTx interrupt priority.

    • Enable the NVIC USARTx IRQ Channel.

  • Configure the DMA when using the DMA process (HAL_UART_Transmit_DMA() and HAL_UART_Receive_DMA(), … APIs):

    • Declare a DMA handle structure for the Tx or Rx channel.

    • Enable the DMAx interface clock.

    • Configure the declared DMA handle structure with the required Tx or Rx parameters.

    • Associate the initialized DMA handle with the UART handle with HAL_UART_SetTxDMA() or HAL_UART_SetRxDMA().

    • For each DMA channel (Tx and Rx), configure the corresponding NVIC line priority and enable it.

3- Initialize the UART driver by selecting a USARTx instance and calling HAL_UART_Init().

Depending on USE_HAL_UART_CLK_ENABLE_MODEL, HAL_UART_Init() can enable the USARTx clock. For example: HAL_UART_Init(&huart, HAL_UART1);

4- Declare a hal_uart_config_t structure, fill it, and then call HAL_UART_SetConfig(). For example:

hal_uart_config_t my_config;

  • In the configuration structure, Program the baud rate, word length, stop bit, parity, prescaler value, hardware flow control, direction (Receiver/Transmitter), oversampling, and one-bit sampling.

Apply the configuration by calling HAL_UART_SetConfig(&huart, &my_config).

5- If required, enable a specific mode on the UART:

Or call UART advanced features (TX/RX pins swap, auto baud rate detection, …) with a set of different configuration functions.

6- Transfer APIs (Transmit and Receive)

For USARTx I/O operations, polling, interrupt, and DMA are available within this driver.

7- Callback registration

When the compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 1U, configure the driver callbacks dynamically via your own method:

To unregister a callback, register the default callback via the registration function.

By default, after the HAL_UART_Init() and when the state is HAL_UART_STATE_INIT, all callbacks are set to the corresponding default weak functions.

Register callbacks when the handle global_state is HAL_UART_STATE_INIT or HAL_UART_STATE_CONFIGURED.

When the compilation define USE_HAL_UART_REGISTER_CALLBACKS is set to 0U or not defined, the callback registration feature is not available and weak callbacks are used, represented by the default value in the table above.

8- Acquire/Release the HAL UART handle

  • When the compilation flag USE_HAL_MUTEX is set to 1, a multi-thread user application can acquire the whole UART HAL handle to execute a transmit or a receive process or a sequence of transmit/receive. When the given process or sequence ends, release the UART HAL handle.

    • The HAL acquire/release operations are based on the HAL OS abstraction layer (stm32_hal_os.c/.h osal):

Note

In the following documentation, consider USARTx as a placeholder for every UART instance, USART instance, and LPUART instance.

Note

In DMA Tx configuration, also enable the USARTx IRQ to complete the DMA transfer.