cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 interrupt service routine and volatile variables

EPora.1
Associate II

Hello,

I'm using an stm32f411 which is a cortex M4 device.

In my application I have an interrupt from an IMU in a 400hz frq. Every time the interrupt is called I want to read data from the sensor and process it using a filter function.

I have a few questions:

  1. What happens if the interrupt is called while it runs the service routine?
  2. How should I treat the function called in the routine and the variables in it?

The service routine is the only code executed(there is an empty while 1).

thanks!

Eyal

6 REPLIES 6
Uwe Bonnes
Principal III

1: Your user code gets interrupted, the IRQ runs and then returns to your user code.

2: Variables shared by user and IRQ code must be volatile or , as bad alternative, no optimazation may be used.

Make sure your user code is done when the next IRQ arrives, wait for interrupt WFI after the evaluation in user code is done.

Thanks for the response!

I think you misunderstood my first question. The question is what happens if the interrupt handler is running and while it happens the same interrupt is triggered.

Thanks

> The question is what happens if the interrupt handler is running and while it happens the same interrupt is triggered.

From the interrupt controller's point of view, most interrupts in STM32 are level-triggered, i.e. there is some status register bit (e.g. bits in TIMx_SR for timer interrupts) which gets set when the interrupt source "happens", and which must be cleared in the interrupt service routine (ISR) otherwise ISR will be entered again and again.

If the new interrupt "happens" before ISR clears given bit, that bit is already set so the new "occurence" of interrupt is ignored. If it "happens" after ISR clears given bit, that bit gets set again and causes the ISR to be re-entered immediately after it exits.

JW

OK I get it thanks!

in the handler I'm turning off the bit only at the end of the routine.

is it alright?

MM..1
Chief II

Is it alright? When you need ignore next occurence in service time , then YES otherwise no and need clear ASAP.

Or better in teory multiple interrupt possible in service time is create only increment variable in ISR and main while execute servicing and decrement...

OK thanks!

In my application I just want the code to run as fast as possible so it doesn't matter if the interrupt is ignored