STM32 HAL Library Notes with Diagrams

USART, interrupt functions, callback functions, & handle/handler

Yu-Cheng (Morton) Kuo
Nerd For Tech
7 min readNov 20, 2023

--

The STM32 Hardware Abstraction Layer (HAL) provides a simple, generic, and handy multi-instance set of APIs to interact with the upper layers like the user application, libraries and stacks.

The contents here were the notes I took when I was learning STM32 HAL referencing the reference manuals of STM32F407VET6 & STM32F303ZET6 and online learning resources.

Outline
(1) USART
(2) Idle Line
(3) Interrupt Functions VS. Callback Functions
(4) Interrupt Functions
(5) Callback Functions
(6) Handle VS. Handler
(7) Comparison: Handle / Handler / Interrupt Function / Callback Function
(8) In STM32 HAL, is a callback function also an interrupt function?
(9) References

(1) USART

Referenced to the reference manuals of STM32F407VET6 & STM32F303ZET6 and online learning resources.

(2) Idle Line

Referenced to the reference manuals of STM32F407VET6 & STM32F303ZET6.

(3) Interrupt Functions VS. Callback Functions

Made by myself.

(4) Interrupt Functions

All made by myself.

(5) Callback Functions

All made by myself.

(6) Handle VS. Handler

All made by myself.

(7) Comparison: Handle / Handler / Interrupt Function / Callback Function

(This part comes from ChatGPT-4. Since it did a great job explaining the intricate relationships between all these concpets, I left this part to it.)

In the context of STM32 microcontroller programming, especially when using HAL (Hardware Abstraction Layer) libraries, you come across several terms that relate to how the microcontroller handles operations and interrupts. Here’s an explanation and comparison of these terms:

Handles

  • Definition: A handle is a reference to a resource. In STM32 programming, a handle is typically a structure that contains information about a particular peripheral. For instance, USART_HandleTypeDef is a structure that contains all the configuration information for a specific UART module.
  • Usage: Handles are used by the HAL library functions to manage and access the resources of the microcontroller peripherals.

Handlers

  • Definition: A handler is a function designed to deal with specific events or conditions. Handlers are usually associated with interrupts.
  • Usage: In STM32 MCUs, when an interrupt occurs, the corresponding Interrupt Service Routine (ISR) or “handler” is called. This handler function is predefined to handle specific types of interrupts, like TIM_IRQHandler for timer interrupts or USART_IRQHandler for UART interrupts.

Interrupt Functions

  • Definition: These are the actual functions that are executed when an interrupt occurs. They are a type of handler specifically designed to respond to hardware interrupts.
  • Usage: Each interrupt has its own interrupt function. For example, if a timer overflow generates an interrupt, the TIMx_IRQHandler function is executed, where x is the timer number. This function must be defined in your code, and it will be called automatically when the interrupt occurs.

Callback Functions

  • Definition: A callback function is a user-defined function that is “called back” at a certain point in the execution of a program. In the context of STM32, callback functions are typically used in association with peripheral operations like completing a UART transmission or an ADC conversion.
  • Usage: Callback functions are passed as arguments to other functions and are intended to be called when an event occurs, like the completion of an asynchronous operation. For instance, when a UART transmission is complete, a transmission-complete callback function can be called to notify the application that data has been sent.

Relationship and Usage

In STM32 HAL development, handlers and interrupt functions are often used interchangeably, as they both refer to the functions that handle interrupts. Here’s how they can be related:

  • Handlers Containing Interrupt Functions: A handler is the function that is called in response to an interrupt. Within this handler, you may perform a variety of tasks, which can include calling other functions, which are essentially “interrupt functions.”
  • Handlers and Callback Functions: Callback functions can be called from within handlers to separate the interrupt-level processing (which should be quick and efficient) from higher-level processing. For instance, inside a USART_IRQHandler, you might call a callback function to process the received data.
  • Nested Relationship: A handler for an interrupt might execute a callback function as part of its operation. For example, once an interrupt signals that a UART byte has been received, the corresponding handler would manage the immediate requirements of that interrupt (like reading the data register to clear the interrupt flag). It might then call a callback function that you’ve defined to handle the data at an application level.

In summary, in STM32 MCU programming:

  • A handle is a data structure that holds configuration details.
  • A handler is a function that deals with a particular kind of event, typically an interrupt.
  • An interrupt function is a specific type of handler that deals with hardware interrupts.
  • A callback function is a user-defined function that is called at a specific point in a process, often from within a handler.

(8) In STM32 HAL, is a callback function also an interrupt function?

(This part comes from ChatGPT-4. Since it did a great job explaining the intricate relationships between all these concpets, I left this part to it.)

In the STM32 HAL (Hardware Abstraction Layer), a callback function is not the same as an interrupt function, but it is often related to interrupts, particularly in asynchronous operations. Here’s the distinction:

Interrupt Functions

  • Purpose: Interrupt functions, or Interrupt Service Routines (ISRs), are low-level functions that respond to various interrupt events, such as timers, UART receive/transmit complete, ADC conversion complete, etc.
  • Execution: They are executed immediately when the interrupt occurs, at the interrupt priority level, and are part of the interrupt handling mechanism of the microcontroller.
  • Context: ISRs are defined within the context of the core’s response to hardware interrupts and are intended to be as quick and efficient as possible to maintain system responsiveness.

Callback Functions

  • Purpose: Callback functions in STM32 HAL are high-level functions provided by the HAL library that are called in response to certain events. They are typically used to notify the user application when a certain non-blocking operation has completed, like the end of a UART transmission or the completion of an ADC conversion.
  • Execution: A callback function is called by the HAL within the context of an ISR (usually at the end of the ISR execution) but is meant to inform the user application of an event. Callback functions are not directly part of the interrupt handling mechanism embedded in the hardware; instead, they are a feature of the HAL software.
  • Context: They are defined by the user and can be used to handle higher-level application tasks that are not as time-sensitive as the operations in an ISR.

Example

Here’s an example scenario in the STM32 HAL:

  1. Hardware Event: A UART peripheral receives a byte of data, triggering a UART interrupt.
  2. Interrupt Function Execution: The UART ISR (USARTx_IRQHandler) is automatically executed. This function is part of the STM32's interrupt handling and defined in the startup file or the HAL driver.
  3. HAL Handling: Within the ISR, the HAL processes the interrupt, reads the received byte to clear the interrupt flag, and performs any other necessary hardware-level processing.
  4. Callback Invocation: After handling the hardware-level events, the HAL then calls a user-defined callback function, such as HAL_UART_RxCpltCallback(), if reception was completed.
  5. User Application Response: Inside this callback function, which you have defined in your application, you can write code to process the received byte. This processing happens outside the ‘real-time’ constraints of the ISR.

In conclusion, callback functions are generally not interrupt functions, but they are often called by interrupt functions provided by the HAL when certain conditions are met, allowing for a structured response to hardware events.

--

--

Yu-Cheng (Morton) Kuo
Nerd For Tech

CS/DS blog with C/C++/Embedded Systems/Python. Embedded Software Engineer. Email: yc.kuo.28@gmail.com