3.4 __farflash Keyword
The IAR __farflash
keyword places objects in program memory, and uses 24-bit
addresses to read such objects either directly or indirectly via any pointer qualified
with this same keyword. Arithmetic on 24-bit wide pointers is only performed on the
lower 16 bits, except for comparisons, which are always performed on the entire
address.
Suggested Replacement
This keyword can be removed when using a memory-placement feature of MPLAB XC8, or alternatively, there are several MPLAB XC8 qualifiers that performs a similar tasks to this keyword, but there are some differences in their effect.
When enabled (the default state), the MPLAB XC8's const-data-in-program-memory
feature places const
objects into program memory. In this case, the
IAR keyword is redundant and should be removed.
When this feature is disabled, using the -mno-const-data-in-progmem
option, use the const
qualifier provided by MPLAB XC8 and either
the __memx
qualifier or one of the
__flashn
qualifiers, where
n
is the 64 KB flash segment in which to
place and access the object. When using the __memx
qualifier, both
access and address arithmetic is on a full 24 bit address. With any of the flash
qualifiers, pointers and address arithmetic is 16-bit, and the compiler sets RAMPZ
to the appropriate 64 KB segment number on each access.
Caveats
The const
qualifier must be used with either __memx
or any of the flash qualifiers when using MPLAB XC8, whereas this is only preferable
when using __farflash
with IAR.
If the const-in-program-memory feature is disabled, alternate versions of the string
functions normally provided by <string.h>
must be used to
access string objects located in program memory.
Examples
__farflash int x = 200; /* Place and access x somewhere in flash */
volatile int y;
int main() {
y = x;
}
to
MPLAB XC8 code similar
to:const volatile __flash1 int x = 200; /* Place and access x in the 64K-128K segment of flash */
volatile int y;
int main(void) {
y = x;
}
Further Information
See the Special Type Qualifiers and Options Specific to AVR Devices sections in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on these qualifiers and option.