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) } }
When placing functions in RAM using the scatter file, you can get the linker to store the function code in the flash similar to initial data values. But the linker links the function will all references as if the code is in RAM. So any part of the program that wants to call the function will make jumps into RAM. And the startup code can keep track of how large the function is and perform the copy from flash to RAM.
Have you verified the individual assembler instructions to make sure that your memcpy() really will place a complete and correct function into RAM? The error indicates that you either land at the wrong address, or that there isn't correct code in the RAM. Or that you are copying to a RAM region that doesn't support program execution.
Hello, I understood your point. I was not right before. The INVSTATE bit is 1 not INVPC. Thank you George
Hello, Regarding the Hard Fault exception. This took place because of the LSP of PC was 0. I fixed this . And now the only issue that I have is with the permision writes. I went throught the datasheet of LPC1768 and I noticed that the region 0x10004000 has exec rights. The program counter has LSB 1. I attached the scatter file again LR_IROM1 0x00000000 0x00080000 { ; load region size_region ER_IROM1 0x00000000 0x00080000 { ; load address = execution address *.o(RESET, +First) *(InRoot$$Sections) *.o(+RO) } RW_IRAM1 0x10000000 0x00004000 { ; RW data .ANY (+RW +ZI) } RW_IRAM2 0x10004000 0x00002000 { ; RW data *(section)
}
Regarding the scatter file the following memory mapping is generated for the variable ProgramRamToSpace
Execution Region RW_IRAM2 (Base: 0x10004000, Size: 0x00000040, Max: 0x00002000, ABSOLUTE)
Base Addr Size Type Attr Idx E Section Name Object
0x10004000 0x00000040 Data RW 137 section copytoram.o.
This variable includes function instructions. But its type is Data. Can i mark in someway this variable as thumb code type as the functions that are allocated in ROM. I read carefully your previous post and I think that the corrects instructions are saved in the buffer and the initial address of the function in the RAM is correct.
Thank you for your time
Best regards George