Execution in RAM
The __ramfunc keyword makes a function execute in RAM. In other words it places the function in a section that has read/write attributes. The function is copied from ROM to RAM at system startup just like any initialized variable, see System startup and termination.
The keyword is specified before the return type:
__ramfunc void foo(void);
If a function declared __ramfunc tries to access ROM, the compiler will issue a warning.
If the whole memory area used for code and constants is disabled—for example, when the whole flash memory is being erased—only functions and data stored in RAM may be used. Interrupts must be disabled unless the interrupt vector and the interrupt service routines are also stored in RAM.
String literals and other constants can be avoided by using initialized variables. For example, the following lines:
__ramfunc void test()
{
/* myc: initializer in ROM */
const int myc[] = { 10, 20 };
/* string literal in ROM */
msg("Hello");
}can be rewritten to:
__ramfunc void test()
{
/* myc: initialized by cstartup */
static int myc[] = { 10, 20 };
/* hello: initialized by cstartup */
static char hello[] = "Hello";
msg(hello);
}For more information, see Initializing code—copying ROM to RAM.