Interrupt Handling
This deserves its own page, it’s pretty important to understand how interrupts actually work under the hood.
Essentially, in each Instruction Cycle, there is an additional interrupt stage that is added. If an interrupt is pending, then the Interrupt Handler is executed.
Big Advantage
No special code or programmer attention required. Everything happens in hardware.
You can see the big advantage of interrupts in the diagram below.
- In this case, since we are dealing with I/O, it is O
Read the detailed steps at p.41, but below is a high-level of how an interrupt is processed. Lots of similarities to Process Switch, since in both cases, we need to save the current context.
Step 4 is important to understand
Step 4: Pushing the PSW and the PC onto the control stack is to prepare to transfer control to the Interrupt Service Routine.
The diagram below also helps.
- This was confusing for me to understand at first, but just look at the arrows and you’ll understand
The Stack Pointer points to the top of the stack. When we push onto the stack, what we’re doing is just storing values from the current set of registers, since they will (potentially) be overwritten by new values once we are inside the Interrupt Service Routine.
Multiple Interrupts
In a real system, there are multiple interrupts: I/O, timers, ADC, PWM settings, RS232, CAN, EEPROM, I2C, etc.
Interrupt during interrupt processing?
This can happen. How do we handle it?
Approach 1:
- Disable interrupts in ISR
- No priority information
- Not useful for time critical elements
Approach 2:
- Priorities for ISR
- How to assign priorities
- Finite number of priority levels
The most straightforward way is to simply have it done sequentially. But this ignores priority. Below is an example with priority.