Getting started with EXTI

This article explains the EXTI and its use, with examples.

1 What is an external interrupt/event controller (EXTI)

The EXTI (EXTernal Interrupt/Event) controller consists of up to 40 edge detectors for generating event/interrupt requests on STM32L47x/L48x devices. Each input line can be independently configured to select the type (interrupt or event) and the corresponding trigger event (rising, falling, or both).

2 Configure EXTI to turn on a LED when a user button is pressed

2.1 Objective

Learn how to use the external interrupt and turn ON a LED when user button is pressed.

  • Configure the GPIO that is connected to the user Button as External Interrupt (EXTI) with falling edge trigger using STM32CubeIDE
  • Learn how to configure the Interrupt Controller : the NVIC
  • Verify the correct functionality by pressing a button that turns on a LED

2.2 Create the project in STM32CubeIDE

  • File > New > STM32 Project in main panel.

create STM32CubeIDE project.png

In this example the NUCLEO-L476RG board is used.

  • Select NUCLEO-L476RG in board Selector.

Select NUCLEO-L476RG board.png

  • Save the project

2.3 Configure GPIO

  • Configure the LED pin as GPIO_Output (PA5 on NucleoFL476RG). For other boards check their user manual.

Visual LedPinGPIO1.png Scheme LED GPIO1.png

  • Configure a button pin as GPIO_EXTI (PC13 on NucleoL467RG). For other boards check their user manual.

Visual PC13.png Scheme Blue button PC13.png

  • Check GPIO configuration
Info white.png Information
Make sure to select 'External interrupt mode with Falling edge trigger detection as GPIO Mode!

Setup Pc13.png

  • Enable the interrupt for EXTI

Setup NVIC.png

2.4 Generate project and edit main.c

  • The easiest way to generate the code is to save your current project: Ctrl + S

The code is generated so you can see it in the left side of the screen in the project explorer
File tree EXTI.png

2.4.1 HAL Library workflow summary

The HAL library provides a high-level access to STM32 peripherals like the EXTI.
The HAL_EXTI0_IRQHandler and EXTI0_IRQHandler are inside stm32l4xx_it.c file.
You must define the Callback function in the main.c : HAL_GPIO_EXTI_Callback.
.

EXTI HAL flowchart.png

2.4.2 Configure the Interrupt

Create a function to handle the EXTI interrupts:

  • HAL callback function for EXTI: void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • To turn an LED on we need to use the function: HAL_GPIO_WritePin

Put the functions into main.c

Info white.png Information
Insert your code between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == GPIO_PIN_13) {
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
  } else {
      __NOP();
  }
}
/* USER CODE END 4 */

2.5 Compile and flash

  • Click on Build button Built.png
  • Click on Debug button (to run step by step) Debug.png
  • Or on Run button (to execute) Run.png

=> When you press the blue button on your board the LED must switch on.