Skip to main content
Associate
April 3, 2025
Solved

HAL_GPIO_EXTI_Callback() not being triggered

  • April 3, 2025
  • 1 reply
  • 771 views

I connected a external button on my PCB board and am trying to use it to toggle another GPIO Pin (PA8) with HAL_GPIO_EXTI_Callback()

I connected the button to PC4 on my NUCLEO board and is configured as follow:

/*Configure GPIO pin : PC4 */
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

In the User Code 4 Section:

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == GPIO_PIN_4){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);
}
}


In stm32g0xx_it.c:

void EXTI4_15_IRQHandler(void)
{
 /* USER CODE BEGIN EXTI4_15_IRQn 0 */
 /* USER CODE END EXTI4_15_IRQn 0 */
 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); // PC4
 HAL_GPIO_EXTI_IRQHandler(B1_Pin);
 /* USER CODE BEGIN EXTI4_15_IRQn 1 */

 /* USER CODE END EXTI4_15_IRQn 1 */
}​

What I dont understand is when I push the button using this configuration it works:
In main while-loop:
if(triggerDetected==1)
 {
 if((HAL_GetTick()-triggerTick)>debounceTime)
 {
 // Toggle relay (if energy units > 0)
 if (units_left > 0.0f) {
 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8); // drives relay and D2
 } else {
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); // force off if no units
 }
 //Reset trigger
 triggerDetected=0;
 }
 }
 
In stm32g0xx_it.c:
void EXTI4_15_IRQHandler(void)
{
 /* USER CODE BEGIN EXTI4_15_IRQn 0 */
triggerDetected = 1;
triggerTick = HAL_GetTick();
 /* USER CODE END EXTI4_15_IRQn 0 */
 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); // PC4
 HAL_GPIO_EXTI_IRQHandler(B1_Pin);
 /* USER CODE BEGIN EXTI4_15_IRQn 1 */

 /* USER CODE END EXTI4_15_IRQn 1 */
}

When I handle the PC4 interrupt directly in the interrupt handler, PA8 toggles, but it doesn't work if i handle it in the callback function.

Any idea to fix this please?
Best answer by mƎALLEm

Hello @blade_runner_2004 and welcome to the ST community,

First, you need to use </> button to share your code. See this post. (updated your post accordingly).

Second, on G0 product there are separated callbacks for Falling and rising edges:

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)

You can refer to this example: https://github.com/STMicroelectronics/STM32CubeG0/blob/master/Projects/NUCLEO-G0B1RE/Examples/GPIO/GPIO_EXTI/Src/main.c

You didn't mention if you enabled the NVIC.

PS: this thread has no relation with CubeIDE it will be moved to the embedded software forum board.

Hope that helps.

1 reply

mƎALLEm
mƎALLEmAnswer
Technical Moderator
April 3, 2025

Hello @blade_runner_2004 and welcome to the ST community,

First, you need to use </> button to share your code. See this post. (updated your post accordingly).

Second, on G0 product there are separated callbacks for Falling and rising edges:

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)

You can refer to this example: https://github.com/STMicroelectronics/STM32CubeG0/blob/master/Projects/NUCLEO-G0B1RE/Examples/GPIO/GPIO_EXTI/Src/main.c

You didn't mention if you enabled the NVIC.

PS: this thread has no relation with CubeIDE it will be moved to the embedded software forum board.

Hope that helps.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate
April 3, 2025

Thank You

 

mƎALLEm
Technical Moderator
April 3, 2025

If your question has been answered please accept my comment where I answered your question as solution.

Thanks

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.