5.9.7.1 Disabling Duplication

The automatic duplication of non-reentrant functions called from more than one call graph can be inhibited by the use of a special pragma.

Duplication should only be disabled if the source code guarantees that an interrupt cannot occur while the function is being called from any main-line code. Typically this would be achieved by disabling interrupts before calling the function. It is not sufficient to disable the interrupts inside the function after it has been called; if an interrupt occurs when executing the function, the code can fail. See 5.9.5 Enabling Interrupts for more information on how interrupts can be disabled.

The pragma is:

#pragma interrupt_level 1

The pragma should be placed before the definition of the function that is not to be duplicated. The pragma will only affect the first function whose definition follows.

For example, if the function read() is only ever called from main-line code when the interrupts are disabled, then duplication of the function can be prevented if it is also called from an interrupt function as follows.

#pragma interrupt_level 1
int read(char device)
{
    // ...
}

In main-line code, this function would typically be called as follows:

di();  // turn off interrupts
read(IN_CH1);
ei();  // re-enable interrupts

The value specified with the pragma indicates for which interrupt the function will not be duplicated. For Mid-range devices, the level should always be 1; for PIC18 devices it can be 1 or 2 for the low- or high-priority interrupt functions, respectively. To disable duplication for both interrupt priorities, use the pragma twice to specify both levels 1 and 2. The following function will not be duplicated if it is also called from the low- and high-priority interrupt functions.

#pragma interrupt_level 1
#pragma interrupt_level 2
int timestwo(int a) {
    return a * 2;
}