hi FU grace,
The following example show how to load code in flash to ram and run it.
/************************************************************************
* NAME: ftfl_command_inram
*
* DESCRIPTION: Launch the command. It must be in RAM.
************************************************************************/
void ftfl_command_inram(volatile unsigned char *ftfe_fstat)
{
*ftfe_fstat = MK_FTFL_FSTAT_CCIF_MASK;
while (!(*ftfe_fstat & MK_FTFL_FSTAT_CCIF_MASK))
{};
}
static void ftfl_command_32bit( unsigned char command, unsigned long *address, char *data )
{
char *pdata = data;
static void (*fnRAM_code)(volatile unsigned char *) = 0;
/* Adequate space for the small program */
#define PROG_SIZE (50)
if(!fnRAM_code)
{
int i = 0;
unsigned char *pCodeStart = (unsigned char *)ftfl_command_inram;
/* Make space for the routine on stack (this will have an even boundary) */
#if defined(__IAR_SYSTEMS_ICC__) || defined(__IASMARM__)
#pragma data_alignment=2
volatile static unsigned char ProgRamSpace[PROG_SIZE];
#else
volatile static unsigned char ProgRamSpace[PROG_SIZE] __attribute__ ((aligned (2)));
#endif
/* Thumb 2 address */
pCodeStart = (unsigned char *)(((unsigned int)pCodeStart) & ~0x1);
/* Copy program to SRAM */
while (i < PROG_SIZE)
{
ProgRamSpace[i] = pCodeStart[i];
i++;
}
/* Create a thumb 2 call */
fnRAM_code = (void(*)(volatile unsigned char *))(ProgRamSpace + 1);
}
DisableInterrupts;
/* The CCIF flag must read 1 to verify that any previous command has
* completed. If CCIF is zero, the previous command execution is still active, a new
* command write sequence cannot be started, and all writes to the FCCOB registers are
* ignored.
*/
while (!(MK_FTFL_FSTAT & MK_FTFL_FSTAT_CCIF_MASK))
{};
/* Ensure that the ACCERR and FPVIOL bits in the FSTAT register are cleared prior to
* starting the command write sequence.
*/
MK_FTFL_FSTAT = (MK_FTFL_FSTAT_ACCERR_MASK | MK_FTFL_FSTAT_FPVIOL_MASK);
/* The FCCOB register group uses a big endian addressing convention. */
/* Write a valid command to the FCCOB0 register. */
MK_FTFL_FCCOB0 = command;
/* Flash address.*/
MK_FTFL_FCCOB1 = (uint8)(((uint32)address) >> 16); /* Flash address [23:16] */
MK_FTFL_FCCOB2 = (uint8)(((uint32)address) >> 8); /* Flash address [15:8] */
MK_FTFL_FCCOB3 = (uint8)((uint32)address); /* Flash address [7:0] */
/* Data.*/
MK_FTFL_FCCOB4 = (uint8)(pdata[3]); /* Data Byte 0.*/
MK_FTFL_FCCOB5 = (uint8)(pdata[2]); /* Data Byte 1.*/
MK_FTFL_FCCOB6 = (uint8)(pdata[1]); /* Data Byte 2.*/
MK_FTFL_FCCOB7 = (uint8)(pdata[0]); /* Data Byte 3.*/
fnRAM_code(&MK_FTFL_FSTAT);
EnableInterrupts;
}