Understanding STM32 HAL Library Fundamentals

The STM32 HAL driver layer provides a simple, generic multi-instance set of APIs (application programming interfaces) to interact with the upper layer like applications, libraries, and stacks. In this tutorial, we will discuss HAL library fundamentals, architecture and understand the STM32 HAL project hierarchy. We also discuss about graphical configuration tool of ST for configuring HAL API functions.

You may also like reading:

What does the HAL mean for STM32 Microcontrollers?

HAL, short for Hardware Abstraction Layer, stands for a set of standardized APIs engineered to shield developers from the complexities inherent in STM32 microcontrollers.

It empowers developers to write code that is not only portable but can seamlessly traverse different STM32 devices with minimal adaptations. STMicroelectronics developed a software called STM32CubeMX for the graphical configuration of HAL layer APIs.

Type of HAL Module

  • Core HAL Modules: This module encompasses functions for system initialization, clock configuration, and exception handling, forming the foundational layer of the library. An example is CMSIS-CORE HAL APIs, we will talk about this later in the tutorial.
  • Peripheral HAL Modules: These modules furnish functions tailored to specific peripherals, like GPIO, USART, and SPI, streamlining the initializing, configuring, and controlling peripherals.

What is STM32CubeMX?

As we said earlier, STM32CubeMX is a graphical configuration tool provided by STMicroelectronics. This software is designed to smooth the development process for STM32 microcontrollers.

This user-friendly software offers a visual interface to configure peripherals, pin assignments, and middleware components, simplifying the complex task of setting up an STM32 microcontroller-based project.

STM32CubeMX Software

STM32CubeMX generates an initialization code base called STM32 HAL API  based on the user’s configurations, making it a valuable tool for both beginners and experienced developers.

By using this software developers accelerate their project setup phase, reducing the likelihood of errors and allowing them to focus more on application-specific logic and features.

STM32CubeMX HAL Project Architecture

STM32CubeMX HAL Project Architecture

If you first create an STM32CubeMX project, you will see a microcontroller startup code added by the CubeMX software. You also see various HAL driver files for various peripherals like I2C, SPI, UART, and ADC. All these driver files are called the  STM32Cube files.

The project also contains the CMSIS-CORE APIs added by the CubeMX software.HAL driver registers talk to the processor via the CMSIS-CORE APIs. As a developer, our job is to work with different peripherals of the microcontroller. So, to talk with the peripherals of the microcontroller we will take the help of the driver APIs exposed by the STM32 HAL Layer.

Understanding STM32 HAL Project Hierarchy

To show you the HAL project hierarchy, we already created a CubeMX project by using the STM32CubeIDE. In the below image of the STM32Cube project, you can see four important folders and they are Includes, Core,  Drivers, and Startup.

stm32 hal apis

The drivers folder has two subfolders one is CMSIS which contains the CMSIS-CORE APIs. ST does not write the CMSIS-CORE APIs. These APIs are given by the ARM. CMSIS APIs actually help the developer to write portable code so that they can use the same code for other ARM-based same family microcontrollers.

The second folder is the STM32 HAL Driver layer APIs and we will use those HAL layer APIs to develop our application. Please note that CubeMX software does not create all the peripheral divers. It only creates those divers that are required for the particular application.

Below the Drivers folder, you can see the startup folder. Startup code is very important to work with Microcontroller. So, CubeMX software automatically generates a suitable startup code for the project.

That’s it for this short introduction to the STM32 HAL Library APIs and CubeMX software. To learn more about STM32 and the uses of HAL library function APIs please check our STM32 related tutorials.

Mahamudul Hasan

I hold a B.Sc degree in Electrical & Electronic Engineering from Daffodil International University, Bangladesh. I am a Printed Circuit Board (PCB) designer and Microcontroller programmer with an avid interest in Embedded System Design and IoT. As an Engineer, I love taking challenges and love being part of the solution. My Linkedin Profile Link: https://linkedin.com/in/mheee

Recent Posts