# Vault 7: Projects

This publication series is about specific projects related to the Vault 7 main publication.

### Protego

# MPLAB XC16 C-Compiler

- 1
- 2
…
- 191
- 192
- 193
- 194
- 195
…
- 365
- 366

Interrupts
 2012-2013 Microchip Technology Inc. DS50002071C-page 193
14.3 WRITING AN INTERRUPT SERVICE ROUTINE
Following the guidelines in this section, you can write all of your application code,
including your interrupt service routines, using only C language constructs.
All ISR code will be placed into a named section that starts with .isr. A function with
a section attribute will prepend .isr to the name given. Code compiled with
-ffunction-sections will also prepend .isr to the section name.
If you have created your own linker script file, and that file is older than an MPLAB C30
v3.30 project, you will need to modify your linker script as per the Readme_XC16.html
file found in the docs subdirectory of the MPLAB XC16 install directory.
14.3.1 Guidelines for Writing ISRs
The following guidelines are suggested for writing ISRs:
 declare ISRs with no parameters and a void return type (mandatory)
 do not let ISRs be called by main line code (mandatory)
 do not let ISRs call other functions (recommended)
A 16-bit device ISR is like any other C function in that it can have local variables and
access global variables. However, an ISR needs to be declared with no parameters
and no return value. This is necessary because the ISR, in response to a hardware
interrupt or trap, is invoked asynchronously to the mainline C program (that is, it is not
called in the normal way, so parameters and return values dont apply).
ISRs should only be invoked through a hardware interrupt or trap and not from other C
functions. An ISR uses the return from interrupt (RETFIE) instruction to exit from the
function rather than the normal RETURN instruction. Using a RETFIE instruction out of
context can corrupt processor resources, such as the Status register.
Finally, ISRs should avoid calling other functions. This is recommended because of
latency issues. See Section 14.6 Nesting Interrupts for more information.
14.3.2 Syntax for Writing ISRs
To declare a C function as an interrupt handler, tag the function with the interrupt attri-
bute (see Section 13.2.2 Function Attributes for a description of the
__attribute__ keyword). The syntax of the interrupt attribute is:
__
attribute__((interrupt [(
[ save(symbol-list)]
[, irq(irqid)]
[, altirq(altirqid)]
[, preprologue(asm)]
)]
))
The interrupt attribute name and the parameter names may be written with a pair
of underscore characters before and after the name. Thus, interrupt and
__interrupt__ are equivalent, as are save and __save__.
The optional save parameter names a list of one or more variables that are to be saved
and restored on entry to and exit from the ISR. The list of names is written inside paren-
theses, with the names separated by commas.
You should arrange to save global variables that may be modified in an ISR if you do
not want the value to be exported. Global variables accessed by an ISR should be
qualified volatile.

- 1
- 2
…
- 191
- 192
- 193
- 194
- 195
…
- 365
- 366