cancel
Showing results for 
Search instead for 
Did you mean: 

How to correctly enable innerrupt on Falling edge of built-in button of Nucleo-G474RE ?

username007
Associate II

Hello, I'm trying to follow the MOOC STM32CubeIDE tutorial on External interrupts with HAL (https://www.youtube.com/watch?v=w_81fHydEoE&list=PLnMKNibPkDnFCosVVv98U5dCulE6T3Iy8&index=6) but on Nucleo-G474RE board with STM32CubeIDE 1.13.1.

My confusion starts after the generation of code and checking the content of `stm32g4xx_hal_gpio.c` file. According to a tutorial, the section related to interrupts on falling edge supposed to look like this:

```

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)

{

/* EXTI line interrupt detected */

if (__HAL_GPIO_EXTI_GET_RISING_IT(GPIO_Pin) != 0x00u)

{

__HAL_GPIO_EXTI_CLEAR_RISING_IT(GPIO_Pin);

HAL_GPIO_EXTI_Rising_Callback(GPIO_Pin);

}

if (__HAL_GPIO_EXTI_GET_FALLING_IT(GPIO_Pin) != 0x00u)

{

__HAL_GPIO_EXTI_CLEAR_FALLING_IT(GPIO_Pin);

HAL_GPIO_EXTI_Falling_Callback(GPIO_Pin);

}

}

 

__weak void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)

{

UNUSED(GPIO_Pin);

}

```

Instead, the generated code is:

```

void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)

{

/* EXTI line interrupt detected */

if (__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != 0x00u)

{

__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);

HAL_GPIO_EXTI_Callback(GPIO_Pin);

}

}

 

__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

UNUSED(GPIO_Pin);

}

```

How to make it correct or how to use the resulting callback on falling edge only?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
username007
Associate II

OK. I managed to use the callback by this:

```

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
// check if interruption is caused by the button
if(GPIO_Pin == B1_Pin){
// if yes, toggle the pin state
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
};

```

 

But what to do if I want to assign different actions to the Rising and Falling edges? If I just change the type of interrupt to "External Interrupt mode with Rising/Falling edge trigger detection" that there still no other Interrupr functions except the "HAL_GPIO_EXTI_Callback"

View solution in original post

4 REPLIES 4
username007
Associate II

OK. I managed to use the callback by this:

```

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
// check if interruption is caused by the button
if(GPIO_Pin == B1_Pin){
// if yes, toggle the pin state
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
}
};

```

 

But what to do if I want to assign different actions to the Rising and Falling edges? If I just change the type of interrupt to "External Interrupt mode with Rising/Falling edge trigger detection" that there still no other Interrupr functions except the "HAL_GPIO_EXTI_Callback"

Pavel A.
Evangelist III

What if you simply read the GPIO state in the generated callback?

If high, it was a rising edge. Else, falling edge. Too simple?

 

No, it works. I just thought that if such functionality existed for one MCU it might also exist for another one; and it might be faster/better than reading the Pin state once again and thus giving it the time to change value it if is quick.

Hello @username007

I'm not sure that I correctly understood your question, but if you mean to ask about other interrupt sources (besides GPIO), you can ofc use a timer, UART, an ADC, or any other peripheral that generates interrupts, and the process will always be the same!  you need to enable the interrupt and perform any processing in its interrupt handler.

Also, maybe this wiki could help you understand more the EXTI interrupt 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.