Getting started with EXTI

Revision as of 15:22, 24 October 2022 by Registered User

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

1. External interrupt/event controller (EXTI)

The external interrupt/event controller consists of up to 23 edge detectors for generating event/interrupt requests. 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 an LED when a user button is pressed

2.1. Objective

  • Configure the GPIO that is connected to the user Button as External Interrupt (EXTI) with falling edge trigger.
  • Learn how to configure the Interrupt Controller : the NVIC

2.2. How

  • Configure a GPIO and EXTI pin in STM32CubeIDE and generate code.
  • Add into a project: a callback function, and a function to turn on an LED.
  • Verify the correct functionality by pressing a button that turns on an LED.

2.3. Create project in STM32CubeIDE

  • File > New > STM32 Project in main panel.
  • Select NUCLEO-L476RG in board Selector.

File:GPIO3 EXTI.png

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

File:LedPinGPIO1.png

  • Configure a button pin as GPIO_EXTIX (PA0 on NucleoF439ZI). For other boards check their user manual.

File:Button.png

  • GPIO configuration check
  • Enable the interrupt for EXTI
  • Save your project and generate the code for your IDE.

2.4. HAL Library workflow summary

File:EXTI HAL flowchart.png

2.5. Open your IDE

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
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_0) {
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
  } else {
      __NOP();
  }
}
/* USER CODE END 4 */

2.6. Compile and Flash

Compile the above code, then load it to the board flash memory.

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