AN4329 Copy function to RAM and execute from RAM (frdm-k64 example project)

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

AN4329 Copy function to RAM and execute from RAM (frdm-k64 example project)

1,099 Views
jacobjennings
Contributor III

Hello,

 

Based on AN-4329, I assembled a test project (attached) for the FRDM-K64F which attempts to copy a function to RAM and run it, but I am getting a fault when attempting to call the function. The Source and Destiny variables seem to be in the correct ranges in copyToMyRam, but the call fails.

 

AN4329 - NXP

Note changes in the ProcessorExpert.ld file, and main.c

 

Any advice is appreciated! It seems very close.

 

Thanks,

Jake

Original Attachment has been moved to: test-run-from-memory-frdm-k64f.zip

Labels (1)
Tags (3)
0 Kudos
2 Replies

574 Views
mjbcswitzerland
Specialist V

Hi

Make sure that you are creating a thumb-comform copy of the code in SRAM since if it i not correctly aligned it will fail.

Below is an example where fnFlashRoutine() is a small subroutine to be copied to SRAM and later executed by calling fnRAM_code()

Regards

Mark

    #define PROG_WORD_SIZE 30                                    // adequate space for the small program
    int i = 0;
    unsigned char *ptrThumb2 = (unsigned char *)fnFlashRoutine;
    static unsigned short usProgSpace[PROG_WORD_SIZE] = {0};     // make space for the routine on stack (this will have an even boundary)

    ptrThumb2 =  (unsigned char *)(((CAST_POINTER_ARITHMETIC)ptrThumb2) & ~0x1); // thumb 2 address
    while (i < PROG_WORD_SIZE) {                                 // copy program to SRAM
        usProgSpace[i++] = *(unsigned short *)ptrThumb2;
        ptrThumb2 += sizeof (unsigned short);
    }
    ptrThumb2 = (unsigned char *)usProgSpace;
    ptrThumb2++;                                                 // create a thumb 2 call
    fnRAM_code = (void(*)(volatile unsigned char *))(ptrThumb2);
0 Kudos