Skip to main content
Visitor II
April 27, 2022
Solved

I have a problem with the interrupt callback. The interrupt event is correctly detected but when the code enter into the callback function it excecute just the first line of the code wrot between the curly brackets.

  • April 27, 2022
  • 6 replies
  • 5598 views

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

if(GPIO_Pin == LVMS_DETECT_Pin)

{

HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, GPIO_PIN_SET);

HAL_Delay(2000);

HAL_GPIO_WritePin(AMS_LED_GPIO_Port, AMS_LED_Pin, GPIO_PIN_RESET);

}

}

When the trigger event happent to the LVMS_DETECT_Pin, the code enter into the callback and turn on the LED without turn it off after 2 seconds.

It is not the first time I noticed this behaviour, it jus execute the first line of the code.

I'm working on Nucleo F401RE.

    This topic has been closed for replies.
    Best answer by TDK
    Only if you need the functionality of systick in those interrupts, which is typically poor practice, but can work.

    6 replies

    Super User
    April 27, 2022

    Probably your SysTick interrupt is same/lower priority as your EXTI interrupt, so the code just stalls there during HAL_Delay.

    Having a 2s blocking delay in an interrupt isn't a good practice.

    In any case, debug your code. Run it, wait for the bad event to happen, hit pause, and see where the code is at.

    GMart.7Author
    Visitor II
    April 27, 2022

    The goal is to turn on the LED for 2 seconds.

    GMart.7Author
    Visitor II
    April 27, 2022

    0693W00000LzDg1QAF.png

    Super User
    April 27, 2022

    Looks like exactly what I guessed, right?. Make SysTick priority higher (numerically lower) than the EXTI priority.

    GMart.7Author
    Visitor II
    April 27, 2022

    0693W00000LzDsvQAF.pngThe Preemption Priority of Time Base: SysTick Timer was setted to 15 (default) meanwhile all the others to 0. 

    Now I set SysTick Timer Preemption Priority to 0 and the EXTI to 1.

    This solved the problem but i noticed that the LED remain turned up for something more than 3 second insted of 2.

    Super User
    April 27, 2022
    Probably for 4 seconds if the pin isn’t debounced.
    GMart.7Author
    Visitor II
    April 27, 2022

    The pin is normally low (connected to GND) and internally pulled down. To generate the event I connect it to 3V3 and then again to GND.

    At the end, is correct to say that the Sys Tick Timer must have always the higher priority with respect of the others interrupt?

    TDKAnswer
    Super User
    April 27, 2022
    Only if you need the functionality of systick in those interrupts, which is typically poor practice, but can work.
    GMart.7Author
    Visitor II
    April 27, 2022

    Thank you.

    Visitor II
    August 21, 2024

    Hi,

     

    Never use HAL_Delay() in interrupt service routines. It causes halt / reset of uC. ISRs must be as short as possible. Just set a flag, start a timer, then when the timer overflows, clear the flag. Use Timers for delays.