Do I need to do anything to C source code that contains a #pragma interrupt_level directive with XC8 compiler?

With the MPLAB® XC8 compiler, this pragma is used to indicate that the function duplication that would normally take place when a function is called from main-line and interrupt code should not be performed, again because the programmer must have taken steps to ensure that reentrancy issues have been dealt with.

The duplication of the function can be inhibited by the use of a special pragma. The #pragma interrupt_level directive can be used to prevent function duplication of functions called from main-line and interrupt code.

Let’s assume that the function main calls a function called input. This function is also called by an interrupt function. The examination of the assembly list file will show assembly code for both the original and duplicate function outputs. The output corresponding to the C function input() will use the assembly label _input. The corresponding label used by the duplicate function will be i1_input.

This should only be done 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.

Note: Never re-enable interrupts inside the interrupt function itself. Interrupts are automatically re-enabled by hardware on the execution of the RETFIE instruction. Re-enabling interrupts inside an interrupt function can result in code failure.

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.

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

The value 1 specified to the pragma "interrupt_level" indicates which interrupt the function will not duplicate.
For mid-range devices, the level should always be one; for PIC18 devices it can be one or two for the low- or high-priority interrupt functions, respectively.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.