0
0
Cnc-programmingHow-ToBeginner · 4 min read

How to Use HAL Library for STM32: Simple Guide

To use the HAL library for STM32, first include the HAL header files and initialize the HAL with HAL_Init(). Then configure the hardware peripherals using HAL functions like HAL_GPIO_Init() and control them with HAL APIs, which simplify direct register access.
📐

Syntax

The STM32 HAL library uses a set of functions and structures to initialize and control hardware peripherals. Key parts include:

  • HAL_Init(): Initializes the HAL library and system clock.
  • GPIO_InitTypeDef: Structure to configure GPIO pins.
  • HAL_GPIO_Init(GPIOx, &GPIO_InitStruct): Initializes GPIO port GPIOx with settings.
  • HAL_GPIO_WritePin(GPIOx, Pin, State): Sets a GPIO pin high or low.
c
HAL_Init();

GPIO_InitTypeDef GPIO_InitStruct = {0};

GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
💻

Example

This example shows how to toggle an LED connected to pin PA5 using the HAL library. It initializes the HAL, configures the pin as output, and toggles the LED every 500 milliseconds.

c
#include "stm32f4xx_hal.h"

int main(void) {
    HAL_Init();

    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    while (1) {
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
        HAL_Delay(500); // Delay 500 ms
    }
}
Output
The LED connected to PA5 blinks on and off every 500 milliseconds.
⚠️

Common Pitfalls

Common mistakes when using the HAL library include:

  • Not calling HAL_Init() before other HAL functions, which can cause hardware misbehavior.
  • Forgetting to enable the peripheral clock (e.g., __HAL_RCC_GPIOA_CLK_ENABLE()) before initializing GPIO pins.
  • Incorrectly configuring pin modes or speeds, leading to unexpected hardware behavior.
  • Not handling HAL error returns or interrupts properly.
c
/* Wrong: Missing clock enable */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // This will fail without clock enable

/* Correct: Enable clock before init */
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
📊

Quick Reference

Key HAL functions for STM32 GPIO control:

FunctionPurpose
HAL_Init()Initialize HAL library and system clock
__HAL_RCC_GPIOx_CLK_ENABLE()Enable clock for GPIO port x
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct)Configure GPIO pins
HAL_GPIO_WritePin(GPIOx, Pin, State)Set GPIO pin high or low
HAL_GPIO_TogglePin(GPIOx, Pin)Toggle GPIO pin state
HAL_Delay(ms)Delay in milliseconds
FunctionPurpose
HAL_Init()Initialize HAL library and system clock
__HAL_RCC_GPIOx_CLK_ENABLE()Enable clock for GPIO port x
HAL_GPIO_Init(GPIOx, &GPIO_InitStruct)Configure GPIO pins
HAL_GPIO_WritePin(GPIOx, Pin, State)Set GPIO pin high or low
HAL_GPIO_TogglePin(GPIOx, Pin)Toggle GPIO pin state
HAL_Delay(ms)Delay in milliseconds

Key Takeaways

Always call HAL_Init() before using other HAL functions to set up the system.
Enable the peripheral clock before initializing hardware like GPIO pins.
Use HAL structures and functions to configure and control STM32 peripherals easily.
Check and handle errors returned by HAL functions to avoid silent failures.
Use HAL_Delay() for simple timing delays in your application.