STM32 Tutorial 07 - GPIO Interrupts (EXTI) Using HAL (And FreeRTOS)
STM32 Tutorial 07 - GPIO Interrupts (EXTI) Using HAL (And FreeRTOS)
The STM32 microcontroller family offers multiple GPIO interrupt pins. The STM32CubeMX Software
comes in handy when configuring the parameters of these pins. However, the actual usage of
So we can test that the interrupt is working. Type the following into the endless loop of the default
task.
This simply shows, that the endless loop keeps running when we trigger an EXTI interrupt.
Now to execute something on an EXTI interrupt event, you could simply add your code between the
user code blocks. This code is then executed with every EXTI event on any enabled EXTI channel.
But you might want to do different things, when different interrupt pins are triggered.
You can filter the interrupt event with a simple if-statement (see on line 76).
It is important to place this if statement before clearing the interrupt flags (line 80).
73 void EXTI4_15_IRQHandler(void)
74 {
75 /* USER CODE BEGIN EXTI4_15_IRQn 0 */
76 if(__HAL_GPIO_EXTI_GET_FLAG(GPIO_PIN_13)){
77 LD2_GPIO_Port -> ODR ^= LD2_Pin; // toggle LD2 LED
78 }
79 /* USER CODE END EXTI4_15_IRQn 0 */
80 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13);
81 /* USER CODE BEGIN EXTI4_15_IRQn 1 */
82
83 /* USER CODE END EXTI4_15_IRQn 1 */
84 }
85
The instructions within the if-statement is only executed when an EXTI interrupt on EXTI channel 13
occurs.
1 __HAL_GPIO_EXTI_GET_FLAG(GPIO_PIN_x)
This tutorial is very basic and might not show the best way to use the STM32 environment.
It still might help you get into the whole HAL philosophy of STM if you are coming from another
platform. This document is free of copyright under the Creative Commons Zero attribution.
Simon Burkhardt
electronical engineering
simonmartin.ch
History:
V1.0 tested the code, created this document
V1.0.1 added contact info and cc0 notice