cancel
Showing results for 
Search instead for 
Did you mean: 

How to manage multiple EXTIs with the STM32Cube HAL

ST AME Support NF
ST Employee

How to manage multiple EXTIs with the STM32Cube HAL

1. Introduction

It is very common to have multiple GPIOs used as External Interrupt (EXTI) sources in an embedded system. But how to manage them in an application code? This is exactly what is going to be covered in this article.


2. Prerequisites

  • Hardware
    • Micro USB cable used to power the Nucleo board from a host machine and to load the code into the STM32.
    • STM32G081B-EVAL
1847.png
  • Software: STM32CubeIDE
 

3. Theory

Let’s say we want to manage a joystick that has at least five output signals: Left, Right, Down, Up and select. We will manage it with five GPIOs on our STM32G0 configured as EXTIs. We will use the STM32G081-EVAL that has a joystick on it connected to five GPIOs of the STM32G081.
1848.png
We will configure PA0, PC3, PC8, PC7 and PC2 as EXTI with pull-down and configure the polarity of the EXTI as rising edge. We will then see what code is needed to manage these five External Interrupts.

 

4. Steps 

  1. Open STM32CubeIDE

  2. Create a new project for the STM32G081RBT6 that is on the board


1849.png
 
  1. Give a name to the project

1850.png
 
  1. Configure EXTIs

Let’s start with PA0. We will configure PA0 as an external Interrupt (EXTI) with internal pull-up.
We will also add a user label “JOY_SEL”. To do this, from the pinout view we will search for PA0 in here:
1851.png
Left click on PA0 and select GPIO_EXTI0
1852.png
Now we will enable the internal pull-up and add the user label and change the polarity of the External interrupt edge to falling edge. To do this click on System View here:
1853.png
Now click on GPIO
1854.png
Add the following configuration:
  • Change the trigger detection to rising edge
  • Enable Internal Pull-down
  • Add user label

1856.png
 
  1. Now repeat the same exercise for the rest of the GPIOs: PC3, PC8, PC7 and PC2

When finished, you should have the following configurations:
1858.png
 
  1. Now enable the Interrupts for all EXTI lines used.

Go to the NVIC Tab of the GPIO and enable the three EXTI Interrupts:
1861.png
 
  1. Generate Code

Save the project, that will also generate the code.
 
  1. Add code to manage the five EXTIs in main.c

Declare a variable that will tell which key was pressed
/* USER CODE BEGIN PV */
uint16_t Button_Pressed = RESET;
/* USER CODE END PV */

Now add the Call back function for managing the five keys:
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
    switch(GPIO_Pin)
  {
  case JOY_DOWN_Pin :
         Button_Pressed = JOY_DOWN_Pin;
           break;
  case JOY_UP_Pin :
         Button_Pressed = JOY_UP_Pin;
           break;
  case JOY_SEL_Pin :
         Button_Pressed = JOY_SEL_Pin;
           break;
  case JOY_RIGHT_Pin :
         Button_Pressed = JOY_RIGHT_Pin;
           break;
  case JOY_LEFT_Pin :
         Button_Pressed = JOY_LEFT_Pin;
           break;
  default :
    break;
  }
}
/* USER CODE END 4 */
 
  1. Build the project, enter debug mode and run the code

1863.png
After the project is built, enter a debug session.
1865.png
Add the variable Button_Pressed to the Live Watch Window.
Run the code:
1867.png
You should now see the Button_Pressed being updated every time you press the different keys of the Joystick.
1868.png
 

5. Links

STM32CubeIDE - Integrated Development Environment for STM32 - STMicroelectronics
STM32G081B-EVAL - Evaluation board with STM32G081RB MCU - STMicroelectronics
STM32G081xB advanced Arm®-based 32-bit MCU - Datasheet
STM32G0x1 advanced Arm®-based 32-bit MCUs - Reference manual
 
Version history
Last update:
‎2021-12-01 09:18 PM
Updated by: