0% found this document useful (0 votes)
14 views

HAL Function

The document provides an overview of commonly used HAL functions in STM32 Cube IDE, categorized by peripherals such as GPIO, UART, Timer, ADC, I2C/SPI, and System Control. Each function includes its purpose, usage context, and example code snippets. It emphasizes the importance of consulting the HAL documentation for specific STM32 variants for detailed usage and parameters.

Uploaded by

Joe Juki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

HAL Function

The document provides an overview of commonly used HAL functions in STM32 Cube IDE, categorized by peripherals such as GPIO, UART, Timer, ADC, I2C/SPI, and System Control. Each function includes its purpose, usage context, and example code snippets. It emphasizes the importance of consulting the HAL documentation for specific STM32 variants for detailed usage and parameters.

Uploaded by

Joe Juki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Below is a list of commonly used HAL (Hardware Abstraction Layer) functions in STM32 Cube IDE,

organized by peripheral/module, along with explanations and examples of their usage:

1. GPIO Functions

Used for controlling General-Purpose Input/Output pins (e.g., LEDs, buttons).

HAL_GPIO_Init()

• Purpose: Initialize a GPIO pin (configure mode, pull-up/down, speed, etc.).

• When to Use: During peripheral initialization (e.g., in MX_GPIO_Init()).

• Example:
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_5; // Example: PA5
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);

HAL_GPIO_WritePin()

• Purpose: Set a GPIO pin to HIGH or LOW.

• When to Use: To turn an LED on/off or drive a signal.

• Example:
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); // Set PA5 HIGH
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); // Set PA5 LOW

HAL_GPIO_TogglePin()

• Purpose: Toggle the state of a GPIO pin (HIGH ↔ LOW).

• When to Use: Blinking an LED without tracking its state.

• Example:
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Toggle PA5
HAL_GPIO_ReadPin()

• Purpose: Read the state of a GPIO pin.

• When to Use: Check if a button is pressed or read a digital input.

• Example:
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET) {
// PA0 is HIGH (e.g., button pressed)
}

2. UART Functions

Used for serial communication (e.g., debugging, sending/receiving data).

HAL_UART_Transmit()

• Purpose: Send data over UART in blocking mode.

• When to Use: Transmit a string or buffer (e.g., send "Hello World" to a PC).

• Example:
char msg[] = "Hello World\r\n";
HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 100); // Timeout:
100 ms

HAL_UART_Receive()

• Purpose: Receive data over UART in blocking mode.

• When to Use: Read a fixed number of bytes (e.g., wait for user input).

• Example:
uint8_t buffer[10];
HAL_UART_Receive(&huart2, buffer, 10, 100); // Wait for 10 bytes
HAL_UART_Transmit_IT() / HAL_UART_Receive_IT()

• Purpose: Transmit/receive data in non-blocking (interrupt) mode.

• When to Use: Avoid blocking the CPU during UART operations.

• Example:
// Start non-blocking transmission
HAL_UART_Transmit_IT(&huart2, (uint8_t*)msg, strlen(msg));

// Implement the TX complete callback:


void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
// Transmission complete!
}

3. Timer Functions

Used for PWM, delays, or periodic interrupts.

HAL_TIM_Base_Start()

• Purpose: Start a timer in base mode.

• When to Use: Generate periodic interrupts or measure time.

• Example:
HAL_TIM_Base_Start(&htim2); // Start TIM2

HAL_TIM_PWM_Start()

• Purpose: Start PWM generation on a timer channel.

• When to Use: Control servo motors, LED brightness, etc.

• Example:
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); // Start PWM on TIM3 Channel 1

HAL_TIM_IC_Start_IT()

• Purpose: Start input capture with interrupts.

• When to Use: Measure pulse width (e.g., for an ultrasonic sensor).

• Example:
HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_2); // Measure on TIM4 Channel 2
4. ADC Functions

Used for analog-to-digital conversion (e.g., reading sensors).

HAL_ADC_Start() / HAL_ADC_PollForConversion()

• Purpose: Start ADC conversion and poll for results.

• When to Use: Read analog values in blocking mode.

• Example:
HAL_ADC_Start(&hadc1); // Start ADC1
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK) {
uint16_t adc_value = HAL_ADC_GetValue(&hadc1); // Read result
}

HAL_ADC_Start_IT()

• Purpose: Start ADC conversion in interrupt mode.

• When to Use: Read analog values asynchronously.

• Example:
HAL_ADC_Start_IT(&hadc1); // Start ADC1 with interrupt
// Implement callback:
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) {
uint16_t adc_value = HAL_ADC_GetValue(hadc); // Read result
}

5. I2C/SPI Functions

Used for communication with sensors, EEPROMs, etc.

HAL_I2C_Master_Transmit()

• Purpose: Send data over I2C in master mode.

• When to Use: Write to an I2C device (e.g., OLED display).

• Example:
uint8_t data[] = {0x00, 0x01};
HAL_I2C_Master_Transmit(&hi2c1, 0x50<<1, data, 2, 100); // Send to
device address 0x50
HAL_SPI_TransmitReceive()

• Purpose: Transmit and receive data over SPI.

• When to Use: Read/write to SPI devices (e.g., SD cards, sensors).

• Example:
uint8_t tx_data[] = {0xAA};
uint8_t rx_data[1];
HAL_SPI_TransmitReceive(&hspi1, tx_data, rx_data, 1, 100); // Full-
duplex SPI

6. System Control Functions

HAL_Delay()

• Purpose: Blocking delay in milliseconds.

• When to Use: Simple timing (avoid in interrupts!).

• Example:
HAL_Delay(1000); // Delay for 1 second

HAL_GetTick()

• Purpose: Get the current system tick (ms since startup).

• When to Use: Measure time intervals non-blockingly.

• Example:
uint32_t start = HAL_GetTick();
while (HAL_GetTick() - start < 1000) {
// Wait for 1 second without blocking
}
7. Interrupt Handlers

HAL_GPIO_EXTI_IRQHandler()

• Purpose: Handle EXTI line interrupts (e.g., button presses).

• When to Use: Triggered when an external interrupt occurs.

• Example:
// In stm32XXxx_it.c:
void EXTI0_IRQHandler(void) {
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0); // Handle PA0 interrupt
}

// Implement callback:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == GPIO_PIN_0) {
// PA0 button pressed
}
}

Summary

• GPIO: Toggle/Write/Read pins.

• UART/SPI/I2C: Blocking and non-blocking communication.

• Timers: PWM, input capture, and timekeeping.

• ADC: Read analog sensors.

• HAL_Delay(): Simple delays (avoid in production code!).

• Interrupts: Handle external events efficiently.

Always check the HAL documentation for your specific STM32 variant for detailed usage and
parameters!

You might also like