I was reading about microcontroller interrupts and how they work, but I can't figure out how the context switch really happens. Can someone please explain it to me in more detail?
3 Answers
This is a pretty general question. There is a lot more detail involved than this general answer and many ways to achieve it.
A processor is essential a set of registers executing a program. When an interrupt occurs, the processor interrupts the present program flow and executes an interrupt service routine.
The first instructions of the ISR push the present contents of registers on the stack.
The middle of the ISR is where the context switches and the registers are used for a new purpose. In executing these instructions, the interrupt should be cleared.
The last instructions pop the old registers off the stack. When the registers are popped, the processor continues with original code, with no knowledge that the ISR was serviced.

- 7,851
- 2
- 18
- 34
-
+1 for "with no knowledge that the ISR was serviced". This becomes pretty important since it e.g. means that if a variable is set in the ISR (e.g. value read from an ADC) the other code has to assume that this variable can change virtually any time. This leads to concepts like buffering values in local variables or the "volatile" keyword in C. – 0x6d64 May 12 '15 at 10:09
Typically a microcontroller will have a register that it looks at periodically during its cycle. If one of the bits is set, it will store what it is doing and take a pre-defined action, that is, it will handle the interrupt.
So, when a peripheral requests an interrupt, it will set a specific bit. Then, on the next instruction cycle, the microcontroller will pause, and, depending on the priority of the interrupt, it will handle it, then return to what it was doing.

- 1,892
- 11
- 8
First of all you need to see this picture!
So, what's really happening here, not many days ago I understood how interrupts really work, maybe it's not pro knowledge about interrupts, but enough for you as a beginner to understand them too. Imagine that main program is executing all the time, you defined while(1) loop and main code executes infinite times.
Before interrupts start to work you need to do some steps and understand them. In ARM microcontrollers there is an peripheral called NVIC (Nested Vector Interrupt Controller), which works separately from ARM processor, also there is an table called Vector Table which points to interrupt functions (called ISRs - Interrupt Service Routines).
Interrupt working principle is based on some events, some bits (called flag bits) are set when something happens. For example, UART peripheral received character, timer which counts 30ms expired, button is pressed etc.
In the main function on the ARM microcontrollers there is an function which enables interrupts (required!) and when main function executing it constantly polls (enters) that interrupt function (with the help of Vector Table) and checks whether the significant flag bits are set. If yes then main program execution stops, ISR (some code) is executed and once routine is completed, it returns back to execution of main program, and after that everything repeats.
The best way to understand how it works is seeing code example:
BYTE receive_byte; // Global variable
int main(void) {
uart0(); //Initialize UART0 peripheral
NVIC_EnableIRQ(UART0_IRQn); //Enable UART0 interrupt
while(1) { // Infinite loop
} //while
} //main
void receive(void) {
receive_byte = LPC_UART0->RBR;
}
void send(void) {
LPC_UART0->THR = transmit_byte;
}
void UART0_IRQHandler(void) __irq {
BYTE tip = LPC_UART0->IIR & 0x06; // Masking bits we will check
if(tip == 0x04) receive(); //Checks if there is characted received in RBR (Receive Buffer Register - 1B) if yes go to receive(); function
else if (tip == 0x02) send(); //Checks if there is characted (1B) in THR (Transmit Holding Register) if yes go to send function
}
Interrupts configuration is mostly the same on every microcontroller, just register names for working with specified peripheral and registers for working with interrupts are different.

- 513
- 1
- 3
- 20