Dear all,
I would like to understand more about nested interrupts since it looks to me they break my code and I'm not sure why.
I have three 'items' in my software: a serial port, a fifo and a timer. The serial port is an external hardware that sends back an interrupt when it's free to send another byte, so I decided to have an interrupt service routine (ISR) that reads from the fifo and puts the byte into the serial port. With this choice the main program should start the first byte transmission from the fifo and then everything is handled by the ISR. Therefore the code looks like this:
int main() {
> ...
> while(1) {
> write_to_fifo(data);
> if (state != TRANSMITTING) {
> if (read_from_fifo(&byte) == OK) {
> write_to_sport(byte);
> state = TRANSMITTING;
> }
> }
> }
> }
the variable 'state' is needed in order to prevent the main to write into the serial port while the ISR is already doing this. The ISR would look like the following:
> void isr_sport(int signal) {
>
> if (read_from_fifo(&byte) == OK) write_to_sport(byte);
> else state == DONE;
> }
When the fifo will be empty the ISR will change the state to DONE such that main can start a new transmission again.
This flow works perfectly as expected but now I decided to add a timer interrupt which is simply incrementing my 'system time' variable, nothing else. If the 'interrupt nesting' is disabled everything works as expected, but if it is enabled something breaks. After some bytes transmitted to the serial port the program simply runs through the main, as if the 'state' variable was not set to DONE when the ISR emptied the fifo.
My simple 'naive' model for this is that while the serial port ISR was running it was interrupted by the timer interrupt (higher priority) but once the timer ISR was completed, instead of returning to the serial port ISR it returned directly to the main, missing the instruction to change the state. At this point there's no chance for the ISR to change the 'state' variable and the main will wait forever a never changing state.
But is this 'possible'? I started looking at the assembler for the interrupt dispatcher and interrupt handling routines but still didn't manage to find a potential flaw (even though I admit I did not fully understand everything there).
Any suggestion or pointer is more than welcome. Thanks a lot,
Al
p.s.: the target is an ADSP21020 and the compiler is g21k.