user3431800
user3431800

Reputation: 301

stm32f427 interrupt clear pending bit

I am new to stm32f427 board and I am digging a bit deeper. I think that on the internet, especially StackOverflow, people sort of agreed on this, just an example:

void EXTI9_5_IRQHandler(void) {

/* Make sure that interrupt flag is set */
if (EXTI_GetITStatus(EXTI_Line5) != RESET) {
    /* Do your stuff when PB5 is changed */
    

    /* Clear interrupt flag */
    EXTI_ClearITPendingBit(EXTI_Line5);
}
} 

This handler, as everyone explained, needs to do whatever you need inside and must clear the pending flag when exiting the interrupt, and people claim that the pending bit is the interrupt flag.

However, in my case and many cases like here, they clear this bit first but many people claim that doing this will kill your interrupt functionality since you will be clearing the bit as soon as you get it. However, this is not the case, and moreover, it's totally the opposite. If I don't do it clearly first, my code would never work.

I want to figure out the reason behind it. Could someone kindly explain?

Upvotes: 3

Views: 5677

Answers (2)

Tim Wensky
Tim Wensky

Reputation: 144

As mentioned before clearing the interrupt flag is not executed immediately. In other words the command needs a few clock cycles to reach the interrupt controller before the interrupt flag is finally cleared. In the meanwhile the CPU continues to execute the code of your interrupt handler in parallel. So, if you exit your ISR immediately after calling EXTI_ClearITPendingBit(…) the ISR is left before the interrupt flag is actually cleared. Unfortunately the result is that the interrupt handler will immediately be called again since the interrupt flag was not cleared, yet. As a matter of fact the interrupt handler will exactly be called twice, because the interrupt flag will for sure be cleared after the second call. I had the same problem a few years ago and it took me some time to figure out the reason. As a best practice I can recommend to clear the interrupt flag at the beginning of your ISR. It is not true that you are killing your interrupt functionality by doing this on an STM32x device (explicitly: this is not a general recommendation). The CORTEX-M core "knows" that it is in an ISR and does not "forget" it once you clear the interrupt flag. Explaining this in detail would go beyond the scope of this answer. But you can be absolutely sure your ISR is executed correctly if you do so. To let you know, I am (continuously) working with STM32 controllers for about 15 years now.

Upvotes: 2

0___________
0___________

Reputation: 68099

The flag should be cleared at the beginning . The clear operation needs some time to propagate across the bus. If you clear it just before exit from the handler you need to use the barrier instruction, read back the flag (it does not work in some cases) or leave enough time for the operation to propagate. Otherwise you may get the "ghost" interrupts.

Upvotes: 3

Related Questions