15.5 Interrupt Service Routine Context Saving

Interrupts, by their very nature, can occur at unpredictable times. Therefore, the interrupted code must be able to resume with the same machine state that was present when the interrupt occurred.

To properly handle a return from interrupt, the setup (prologue) code for an ISR function automatically saves the compiler-managed working and special function registers on the stack for later restoration at the end of the ISR. You can use the optional save parameter of the interrupt attribute to specify additional variables and SFRs to be saved and restored.

To reduce the amount of context that must be preserved, which will improve interrupt latency, use the 14.1.2.4 context attribute with the ISR.

void __attribute__((interrupt, context)) _INT1Interrupt()
  • When the context attribute is added, the compiler will not generate instructions to save/restore the context.
  • When the nocontext attribute is added, or when neither context nor nocontext attributes are specified, the compiler will generate instructions to save/restore the context.

The context register set is automatically, chosen using the given interrupt’s priority level,.i.e., an interrupt with IPL = 5 will use Context set 5, and an interrupt with IPL = 6 will use context set 6.