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
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.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
-
Open STM32CubeIDE
-
Create a new project for the STM32G081RBT6 that is on the board
-
Give a name to the project
-
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:Left click on PA0 and select GPIO_EXTI0Now 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:Now click on GPIOAdd the following configuration:
- Change the trigger detection to rising edge
- Enable Internal Pull-down
- Add user label
-
Now repeat the same exercise for the rest of the GPIOs: PC3, PC8, PC7 and PC2
When finished, you should have the following configurations:
-
Now enable the Interrupts for all EXTI lines used.
Go to the NVIC Tab of the GPIO and enable the three EXTI Interrupts:
-
Generate Code
Save the project, that will also generate the code.
-
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 */
-
Build the project, enter debug mode and run the code
After the project is built, enter a debug session.Add the variable Button_Pressed to the Live Watch Window.Run the code:You should now see the Button_Pressed being updated every time you press the different keys of the Joystick.
5. Links
STM32CubeIDE - Integrated Development Environment for STM32 - STMicroelectronicsSTM32G081B-EVAL - Evaluation board with STM32G081RB MCU - STMicroelectronicsSTM32G081xB advanced Arm®-based 32-bit MCU - DatasheetSTM32G0x1 advanced Arm®-based 32-bit MCUs - Reference manual