Microchip Technology SW006021-1N User Manual
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 58
2012 Microchip Technology Inc.
3.5.5
How Do I Share Data Between Interrupt and Main-line Code?
Variables accessed from both interrupt and main-line code can easily become cor-
rupted or mis-read by the program. The volatile qualifier (see Section 5.4.7.2 “Vol-
atile Type Qualifier”) tells the compiler to avoid performing optimizations on such
variables. This will fix some of the issues associated with this problem.
rupted or mis-read by the program. The volatile qualifier (see Section 5.4.7.2 “Vol-
atile Type Qualifier”) tells the compiler to avoid performing optimizations on such
variables. This will fix some of the issues associated with this problem.
The other issues relates to whether the compiler/device can access the data atomically.
With 8-bit PIC devices, this is rarely the case. An atomic access is one where the entire
variable is accessed in only one instruction. Such access is uninterruptable. You can
determine if a variable is being accessed atomically by looking at the assembly code
the compiler produces in the assembly list file, see Section 6.6 “Assembly List
Files”. If the variable is accessed in one instruction, it is atomic. Since the way vari-
ables are accessed can vary from statement to statement it is usually best to avoid
these issues entirely by disabling interrupts prior to the variable being accessed in
main-line code, then re-enable the interrupts afterwards, see Section 5.9.4 “Enabling
Interrupts”.
With 8-bit PIC devices, this is rarely the case. An atomic access is one where the entire
variable is accessed in only one instruction. Such access is uninterruptable. You can
determine if a variable is being accessed atomically by looking at the assembly code
the compiler produces in the assembly list file, see Section 6.6 “Assembly List
Files”. If the variable is accessed in one instruction, it is atomic. Since the way vari-
ables are accessed can vary from statement to statement it is usually best to avoid
these issues entirely by disabling interrupts prior to the variable being accessed in
main-line code, then re-enable the interrupts afterwards, see Section 5.9.4 “Enabling
Interrupts”.
3.5.6
How Can I Prevent Misuse of My Code?
First, many devices with flash program memory allow all or part of this memory to be
write protected. The device configuration bits need to be set correctly for this to take
place, see Section 5.3.5 “Configuration Bit Access” and your device data sheet.
write protected. The device configuration bits need to be set correctly for this to take
place, see Section 5.3.5 “Configuration Bit Access” and your device data sheet.
Second, you can prevent third-party code being programmed at unused locations in the
program memory by filling these locations with a value rather than leaving them in their
default unprogrammed state. You can chose a fill value that corresponds to an instruc-
tion or set all the bits so as the values cannot be further modified. (Consider what will
happen if you program somehow reaches and starts executing from these filled values.
What instruction will be executed?)
program memory by filling these locations with a value rather than leaving them in their
default unprogrammed state. You can chose a fill value that corresponds to an instruc-
tion or set all the bits so as the values cannot be further modified. (Consider what will
happen if you program somehow reaches and starts executing from these filled values.
What instruction will be executed?)
The compiler’s HEXMATE utility (see Section 8.6 “HEXMATE”) has the capability to
fill unused locations and this operation can be requested using a command-line driver
option, see Section 4.8.30 “--FILL: Fill Unused Program Memory”. As HEXMATE
only works with HEX files, this feature is only available when producing HEX/COF file
outputs (as opposed to binary, for example), which is the default operation.
fill unused locations and this operation can be requested using a command-line driver
option, see Section 4.8.30 “--FILL: Fill Unused Program Memory”. As HEXMATE
only works with HEX files, this feature is only available when producing HEX/COF file
outputs (as opposed to binary, for example), which is the default operation.
And last, if you wish to make your library files or intermediate p-code files available to
others but do not want the original source code to be viewable, then you can obfuscate
the files using the --SHROUD option, see Section 4.8.54 “--SHROUD: Obfuscate
P-code Files”
others but do not want the original source code to be viewable, then you can obfuscate
the files using the --SHROUD option, see Section 4.8.54 “--SHROUD: Obfuscate
P-code Files”
3.5.7
How Do I Use Printf to Send Text to a Peripheral?
The printf function does two things: it formats text based on the format string and
placeholders you specify, and sends (prints) this formatted text to a destination (or
stream), see Appendix A. “Library Functions”. The printf function performs all the
formatting; then it calls a helper function, called putch, to send each byte of the for-
matted text. By customizing the putch function you can have printf send data to any
peripheral or location, see Section 5.11.1 “The printf Routine”. You may choose the
printf
placeholders you specify, and sends (prints) this formatted text to a destination (or
stream), see Appendix A. “Library Functions”. The printf function performs all the
formatting; then it calls a helper function, called putch, to send each byte of the for-
matted text. By customizing the putch function you can have printf send data to any
peripheral or location, see Section 5.11.1 “The printf Routine”. You may choose the
printf
output go to an LCD, SPI module or USART, for example.