17.9 ISR Considerations

There are a few things to consider when writing an interrupt service routine.

As with all compilers, limiting the number of registers used by the interrupt function, or any functions called by the interrupt function, may result in less context switch code being generated and executed by the compiler, see 17.6 Latency. Keeping interrupt functions small and simple will help you achieve this.

When interrupt latency is a concern, avoid calling other functions from your ISR. You may be able to replace a function call with a volatile flag that is handled by your application's main control loop.

If you are building with link-time optimizations (the -flto option), you might need to take special steps to ensure that code associated with interrupts is not removed. These optimizations for the most part work on the whole program. When the whole program is analyzed, interrupt functions will be found to be not called by any other function, so the compiler believes it can remove them. To prevent this from occurring, interrupt functions must be marked with the used attribute to inform the compiler that they are not redundant. The same attribute should be used with data objects that are used by interrupt functions. As an alternative, consider building source files containing interrupt functions with link-time optimizations disabled.