5.9.1 Interrupt Service Routines
Observe the following information and guidelines when writing interrupt functions (also known as Interrupt Service Routines, or ISRs), regardless of how they are processed.
Inside an interrupt function, clear the relevant interrupt flag once the interrupt source has been processed. Check your device data sheet to see what flags are relevant for your peripherals.
Do not re-enable interrupts inside the interrupt function. This is performed automatically
by the retfie
instruction when the ISR returns.
Keep the ISR as simple as possible. Complex code will typically use many registers, which might increase the size of the context switch code. Even devices that use shadow registers to perform the context switch in hardware might still need to have compiler-defined registers saved and restored in software.
Interrupt functions always use the non-reentrant function model. These functions ignore any option or function specifier that might otherwise specify reentrancy.
The compiler processes interrupt functions differently from other functions, generating code to save and restore any registers that are used by the function and that are not saved by the device hardware. These functions additionally use a special return instruction. It is recommended that interrupt functions be placed in C source files within the project; however, since they are marked as “root” functions in the program's call graph, they will always be linked into the program, even if they are placed in a library archive.
Interrupt functions must not be called directly from C code (due to the different return instruction that is used), but interrupt functions can call other functions, both user-defined and library functions.
The following sections describe how interrupt functions should be written and incorporated into your project for each interrupt vector arrangement.