3.5.6 How Can I Make My Interrupt Routine Faster?
Consider suggestions made in 3.5.2 How Can I Make My Code Smaller? (code size) for any interrupt code. Smaller code is often faster code.
In addition to the code you write in the ISR, there is the code the compiler produces to switch context. This is executed immediately after an interrupt occurs and immediately before the interrupt returns, so must be included in the time taken to process an interrupt. This code is optimal in that only registers used in the ISR will be saved by this code. Thus, the fewer registers used in your ISR will mean potentially less context switch code to be executed.
Generally simpler code will require fewer resources than more complicated expressions. Use the assembly list file to see which registers are being used by the compiler in the interrupt code. For information on the list file, see the MPLAB® XC32 Assembler, Linker and Utilities User’s Guide (DS50002186).
Avoid calling other functions from the ISR. In addition to the extra overhead of the function call, the compiler also saves all general purpose registers that may or may not be used by the called function. Consider having the ISR simply set a flag and return. The flag can then be checked in main-line code to handle the interrupt. This has the advantage of moving the complicated interrupt-processing code out of the ISR so that it no longer contributes to its register usage. Always use the volatile
qualifier (see 8.9.2 Volatile Type Qualifier for variables shared by the interrupt and main-line code, see 3.4.4 How Do I Share Data Between Interrupt and
Main-Line Code?.
If your target device supports tightly coupled memory (see 7.5 Tightly-Coupled Memories) it can help improve the determinism and performance of an ISR.