Copy FLASH to RAM and running the code from RAM

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

Copy FLASH to RAM and running the code from RAM

Jump to solution
2,026 Views
kinetis_user
Contributor II

Using CW10.1 and K10.

Apparently I can't write or erase sectors from the same block that my code is running from (different sectors from the sectors where the code exist of course)

I want to copy a portion of my code from flash to RAM and run sections of the code from RAM and other part of the code from FLASH.

How can I do that ?

 

Thank you.

 

 

0 Kudos
1 Solution
1,126 Views
mjbcswitzerland
Specialist V

Hi

 

Here's one method for calling the Flash routine from RAM

 

// This routine runs from SRAM - the reason why the pointer is passed is to avoid the routine taking it from a const value in FLASH, which is then not code location dependent//static void fnFlashRoutine(volatile unsigned char *ptrFTFL_BLOCK){    *ptrFTFL_BLOCK = FTFL_STAT_CCIF;                                 // launch the command - this clears the FTFL_STAT_CCIF flag (register is FTFL_FSTAT)    while (!(*ptrFTFL_BLOCK & FTFL_STAT_CCIF)) {}                    // wait for the command to terminate}static void fnConfigFlashRam(void){    static void (*fnRAM_code)(volatile unsigned char *) = 0;    #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];               // 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);}

 

call fnFlashRoutine() using;

fnRAM_code((volatile unsigned char *)FTFL_BLOCK);                    // execute the command from SRAM

 

Also block interrupts when the routine is called if interrupt handlers are in FLASH.

 

Regards

 

Mark

 

View solution in original post

0 Kudos
2 Replies
1,127 Views
mjbcswitzerland
Specialist V

Hi

 

Here's one method for calling the Flash routine from RAM

 

// This routine runs from SRAM - the reason why the pointer is passed is to avoid the routine taking it from a const value in FLASH, which is then not code location dependent//static void fnFlashRoutine(volatile unsigned char *ptrFTFL_BLOCK){    *ptrFTFL_BLOCK = FTFL_STAT_CCIF;                                 // launch the command - this clears the FTFL_STAT_CCIF flag (register is FTFL_FSTAT)    while (!(*ptrFTFL_BLOCK & FTFL_STAT_CCIF)) {}                    // wait for the command to terminate}static void fnConfigFlashRam(void){    static void (*fnRAM_code)(volatile unsigned char *) = 0;    #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];               // 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);}

 

call fnFlashRoutine() using;

fnRAM_code((volatile unsigned char *)FTFL_BLOCK);                    // execute the command from SRAM

 

Also block interrupts when the routine is called if interrupt handlers are in FLASH.

 

Regards

 

Mark

 

0 Kudos
1,126 Views
kinetis_user
Contributor II

Great.

Thank you.

Tried it and it works perfect for me.

 

 

0 Kudos