Interrupt service routines

The PIC18 devices use two kind of interrupts: low priority interrupts, and high priority interrupts. The corresponding Interrupt Service Routines (ISR) respectively start at addresses 0x18 and 0x8. cpik automatically put a branch instruction at these addresses, so the code of the ISRs can be located anywhere in memory. Two empty interrupt service routines are provided by the run-time library16. A user who plan to use interrupts must provide a specific interrupt service routine that can be written in C, using the (non-standard) __interrupt__ keyword as following:
__interrupt__ void hi_pri_ISR()
{
  /* interrupt service code */
}
The used-defined routine will shadow the default one, because user libraries are scanned before rtl.slb.

The role of the __interrupt__ keyword is to insure that

The body of an ISR routine can be written in pure assembly language, using the __asm__ directive. In this case, all previously mentioned registers can be freely altered, as long as FSR0 (the software stack pointer) is not altered when the ISR exits.

When the interrupt code is written in C (or mix of C and asm code), registers used by the run-time library and user code will be saved if a proper pragma saved_regs has be seen by the compiler before the source code of the ISR.

A standard saved_regs pragma is provided in the <interrupt.h> header, so, generally, nothing special must be done if interrupt.h is included. However, I recommend to verify that the registers specified by the saved_regs pragma match the registers that are actually used in the ISR.

See section 10.4.5 about the saved_regs pragma for details.

Note: the SAVE_REGS and RESTORE_REGS macro that were defined in the <interrupt.h> header prior version 0.7.3 have been suppressed and cannot be used anymore. Any call of these macros should be removed from your source code and the equivalent saved_regs pragma should be inserted before the concerned ISR.



Footnotes

... library16
The run time library is the file /usr/share/cpik/<version>/lib/rtl.slb
... stack17
FSR0 is not saved because it is the stack pointer itself.
... return 018
The reftie 1 instruction is not used because it is explicitly mentioned as bogus by errata documents from Microchip.
Alain Gibaud 2015-07-09