This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LPC1768 copy function code for flash to RAM

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) } }

Parents
  • As I said, I know very little about the NXP/LPC part, would have to research addresses and if core design permits execution from RAM, or requires remapping, so more generically

    #include <stdio.h>
    #include <string.h>
    
    typedef int (*pFunction)(void);
    
    void ExecuteFromRAM(void)
    {
        static const uint16_t CodeStart[] = { 0xF04F, 0x007B, 0x4770 }; // function, in ROM
        uint8_t ProgRamSpace[16]; // as big as required, here on stack
        pFunction fnRAM_code;
    
        memcpy(ProgRamSpace, CodeStart, sizeof(CodeStart)); // Copy code
    
        fnRAM_code = (pFunction)&ProgRamSpace[1]; // +1 for Thumb code
    
        printf("%d\n",fnRAM_code());
    }
    

Reply
  • As I said, I know very little about the NXP/LPC part, would have to research addresses and if core design permits execution from RAM, or requires remapping, so more generically

    #include <stdio.h>
    #include <string.h>
    
    typedef int (*pFunction)(void);
    
    void ExecuteFromRAM(void)
    {
        static const uint16_t CodeStart[] = { 0xF04F, 0x007B, 0x4770 }; // function, in ROM
        uint8_t ProgRamSpace[16]; // as big as required, here on stack
        pFunction fnRAM_code;
    
        memcpy(ProgRamSpace, CodeStart, sizeof(CodeStart)); // Copy code
    
        fnRAM_code = (pFunction)&ProgRamSpace[1]; // +1 for Thumb code
    
        printf("%d\n",fnRAM_code());
    }
    

Children