3.14 __nearfunc Keyword
The IAR __nearfunc
keyword places specified function in first 128 KB of program
memory. When used with the definition of a function pointer, the pointer is made 16 bits
wide so that it can access functions any function in this range.
Suggested Replacement
The keyword can be removed.
Function pointers are always 16 bits wide, but there is no guarantee that the function will be placed in the first 128 KB of program memory. However, when using MPLAB XC8, the-mrelax
option automatically handles placement and indirect calls
for functions, regardless of where they are located in program memory. On devices with
more than 128 KB of program memory, function addresses point into a trampoline table
placed in near flash. The trampoline table entries then jump to the actual function
using a jmp
instruction.Caveats
When function addresses are obtained without a function symbol, for example when
casting an integer constant to a function pointer, this method might not worked as
expected. For
example:
int main(void)
{
((int(*)(void)) 0x20000)(); /* will not work */
}
might
fail. Instead, do the
following:int main(void)
{
extern int foo(void); /* Force creation of a symbol, with address set at link time */
foo();
}
and
associate an address with the symbol using the xc8-cc
driver option
-Wl,-defsym,foo=0x40000
, for example.Examples
Consider migrating IAR code such
as:
__nearfunc void incMode(void);
to MPLAB XC8 code similar
to:void incMode(void);
Further Information
See the Function Pointers section in the MPLAB XC8 C Compiler User's Guide for AVR MCUs for more information on how functions are handled.