What should be expected if the code in the timer interrupt ISR does not finish before the interrupt is called again? For example Timer0 ISR(TIMER0_COMPA_vect){} on the Uno. This is for debugging purposes in a complex program. Thanks.
2 Answers
- the timer will set the Timer Interrupt Flag (TIFR0 register)
- The ISR will be called, and the flag will be cleared
- you do some long calculations or something
- the timer will match OCR0A, and set the interrupt flag again.
- you calculations are done, and the ISR function will end.
- the uC will jump back to the main loop code, and execute
21 instructions. - The uC will see the flag, and call the ISR again. As per step 2.
PS please note that by default the ISR macro will turn off global interrupt (cli) at the beginning of the function, and set it back at the end. You can re-enable interrupts inside the ISR, but you have to be very careful here, as interrupts can be nested, and cause stack-overflows.
You can re-enable interrupts inside the ISR, but that only means other interrupt may interrupt this ISR, but never the same vector. (i.e. only one ISR per vector can run at the same time). So in your case the timer ISR can't be interrupted by the same timer interrupt.
-
1Congrats, that's a pretty good summary of what happens in this situation. Aug 14, 2016 at 19:38
-
1.- My understanding is that the uC will execute a single instruction (not 2) of the main code before entering the ISR again. 2.- Where did you read that only one ISR per vector can run at the same time? It's the first time I read anything like this. Aug 15, 2016 at 8:24
-
Note that if you re-enable interrupts inside the ISR, then the same interrupt definitely can re-enter the same vector. This is one reason why re-enabling interrupts inside an ISR needs careful thought... You said it yourself: (step 2) - "...the flag will be cleared." Aug 15, 2016 at 9:18
-
Apparently I was wrong in the last paragraph. I'm not sure where I got that from, but it doesn't appear anywhere in the datasheet.– GerbenAug 15, 2016 at 9:35
It won't be called until the current interrupt is finished. As soon as the current interrupt function returns the pending interrupt would trigger and call the ISR again.