We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
I use the LPC1768 board and I want to copy a function from flash to RAM in runtime. I use the following code
void f() { int x=0; printf("%d\n",x); } void g() { int x=5; printf("Hello: g"); printf("%d\n",x); } int main(void) { char c; static void (*fnRAM_code)(void) = &f; int offset= (int)((long)g-(long)f); int i=0; int z=0; int *x; extern void uart0_init(void); unsigned char *pCodeStart = (unsigned char *)f;
uart0_init();
//(*fnRAM_code)();
//thump2 addresss /* Thumb 2 address */ pCodeStart = (unsigned char *)(((unsigned int)pCodeStart) & ~0x1); while (i < PROG_SIZE) { ProgRamSpace[i] = pCodeStart[i]; i++;
} (*fnRAM_code)(); fnRAM_code = (void(*)(void))(ProgRamSpace); (*fnRAM_code)(); }
But when I access the new address of the function I get the following error access violation at 0x100000018: no execute /read permision. Can anyone provide a solution for this issue Best regards, George
The scatter file is the following ; ************************************************************* ; *** Scatter-Loading Description File generated by uVision *** ; *************************************************************
LR_IROM1 0x00000000 0x00080000 { ; load region size_region ER_IROM1 0x00000000 0x00080000 { ; load address = execution address *.o (RESET, +First) *(InRoot$$Sections) .ANY (+RO) } RW_IRAM1 0x10000000 0x00008000 { ; RW data .ANY (+RW +ZI) } }
Why not let the linker place the function in RAM? Then the startup code will perform the copy of the code from flash into RAM and all references to that function will make use of the RAM address.
The datasheet for the processor contains information which memory regions that supports code execution, DMA transfers etc.
Clearly there are many solutions to the problem, but the problem is multifaceted and one really has to understand the requirements and ramification very precisely.
If you start calling functions, or libraries from RAM based code you need to make sure everything in the call tree is brought there too, if the goal is to modify or erase flash, or other things that would preclude running from flash, or would cause significant stalling of execution.
IAR has a directive like 'ramfunc' in Keil I think you need to use attributes, sections and scatter files, I've made that work in the past too, but don't have my example to hand.
If reprogramming a flash, then it's often best to create a self-contained piece of assembler code to run in RAM to make really sure there aren't any hidden helper functions that gets referenced from flash.
But if the goal is to use RAM for additional speed, like critical ISR or evaluation code, then it works well to just make use of the scatter file.
Here I put a whole object file in RAM, but you can do so on a section or function basis also. http://www.keil.com/forum/24821/
Hello, Thanks for the sample. It is very useful George