If I disable interrupts (for example noInterrupts
or cli
) and enable them (interrupts
or sei
for example) later, would interrupts which would have executed in the window in between fire by the execution of the latter? I guess not, and haven't confirmed or denied it. If not it is always an option to manually disable timers or components with similar concerns with respect to the interrupt status? It makes sense that AVR-based Arduino boards would behave similarly. It seems plausible that the bits for the force interrupt execution will turn on and turned off when the jump occurs. Thanks for reaching out.
-
your question is not about Arduino ... it is a general question about the AVR microcontroller ... better fit at electronics.stackexchange.com/questions– jsotolaCommented Mar 11, 2023 at 17:33
-
Thanks, it seems that I made the same mistake as before, I thought that noInterrupts and interrupts would justify it.– markojCommented Mar 11, 2023 at 19:32
-
2@jsotola this is a reasonable question - otherwise you could hive off almost all questions about Arduino saying they are about AVR microcontrollers.– Nick Gammon ♦Commented Mar 11, 2023 at 20:41
1 Answer
See How do interrupts work on the Arduino Uno and similar boards?
Yes, most interrupts will be remembered and the ISR will be executed when interrupts are enabled again, plus one more instruction has been executed. If that didn't happen it would be very bad, as you may turn off interrupts briefly and you wouldn't want to miss the thing that the interrupt was for (eg. incoming serial data).
To stop that happening you need to clear the "interrupt has happened" bit for the individual interrupts. This is documented in the datasheet for each interrupt.
The order in which outstanding interrupts will be serviced is the interrupt priority, that is lower-numbered interrupts will be serviced first. The call to the ISR causes the processor to clear the "interrupt has happened" bit for that interrupt.
-
Note that each interrupt has a single 'interrupt occurred' bit, so that if a particular interrupt fires multiple times during the CLI-SEI window, it will only be serviced once outside the window. So the handler has to check all possible triggers... Commented Mar 12, 2023 at 14:29
-
@user10489 Can you elaborate on what you mean by "all possible triggers?". The interrupt bit will be set only once in that window. Once it is set it can't be "double set". It is important for ISRs to handle their respective interrupts quickly, so as not to unnecessarily delay the processing of other interrupts.– Nick Gammon ♦Commented Mar 13, 2023 at 5:25
-
Right. If an interrupt is multiple triggered but only fires once, your handler will only be called once, but might have to handle all the things that would have caused it to fire. For example, if you have an interrupt for characters received by the uart, you might end up reading multiple characters. Commented Mar 13, 2023 at 11:32
-
Yes, in that case, the UART has a (small) buffer so reading until it is empty would be advisable.– Nick Gammon ♦Commented Mar 13, 2023 at 20:40
-
As a note to somebody who might find this useful, it seems that the interrupts flags are those force bits, like the force compare A and B in the timer registers.– markojCommented Jun 22, 2023 at 16:16