Electronic – Preemptive Interrupts and “Context Switching”

interruptsmicrocontroller

In RTOS, when a Higher Priority task becomes Ready to Run, it stops or preempts currently running (Lower Priority) task and start executing itself. Before switching tasks, RTOS saves required data related to lower priority task (Context Switching)

Coming to my question, Like tasks, Interrupts also have priorities. When a Lower priority interrupt is executing and a Higher priority interrupt comes, What happens?
Does it stores all data on Stack etc. just like RTOS? What will happen to my variables used in ISR?

(Note that, this scenario is possible in a Non-RTOS based application too!)

Best Answer

This is basically entirely platform and compiler dependent. Some MCUs/CPUs have hardware for saving some or part of the stack, some do not.

Additionally, Stack saving/loading is compiler-determined on some platforms. At least on atmel's xmega MCUs, context saving is up to the code, rather then the hardware. You can therefore write naked ISRs that could muck with the previous context without too much trouble.


Basically, there is no general rule aside from "read the docs of the CPU in question".