Pipeline delay in IRQ flag clearing before returning from ISR?
Hello,
I have an ISR for TIM4 running on an STM32F2 microcontroller. The ISR triggers on a rising edge of the timer input channel, and its purpose is to record the microsecond timestamp of the last two rising edges in global variables for other parts of the firmware to use.
I notice that the ISR as written triggers twice on each rising edge. This is evident by last_re_us and second_last_re_us reading as equal, even when the PWM signal being input is only in the hundreds of hertz.
If I move the IRQ flag clearing to the beginning of the ISR, or if I add barriers before returning from the ISR, the issue goes away, and the ISR gets called only once. This is evident by last_re_us minus second_last_re_us giving the expected period of the PWM signal I am inputting.
Am I correct in understanding that this occurs because of the processor's pipeline delay causing the actual hardware writing of TIM4->SR = 0; to happen only after the ISR actually returns? What is the best practice for making sure that IRQ flag clearing actually completes before an ISR returns? I don't see use of barriers anywhere in example code for ISRs in general.
