Help with bootloader. Execute code in RAM, why?

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

Help with bootloader. Execute code in RAM, why?

Jump to solution
2,840 Views
markosiponen
Contributor III

I'm about to write a bootloader. I have found some example code (AN2295SW).

There is a function that is copied to RAM:

void FTFL_Initialization(void)

{

  Word i;

  //  initialize pointer to ram function

  ExecuteOnStack = (void(*)(void))&buffer[1];

  //  copy function from ROM to RAM

  for(i=0;i<(128+1);i++)

      buffer[i] = ((Byte*)ExecuteOnStackStart)[i-1];

  //  inititalization of flash clock module

  FTFL_INIT_FLASH_CLOCK;

}

static void ExecuteOnStackStart(void)

{

  //  launch a command

  FTFL_FSTAT |= FTFL_FSTAT_CCIF_MASK;

  //  waiting for the finishing of the command

  while((FTFL_FSTAT&FTFL_FSTAT_CCIF_MASK) != FTFL_FSTAT_CCIF_MASK){};

}

Why is there a need to copy it to RAM?

Is it not safe to execute code in flash during a flash command?

What happens if an interrupt runs from flash during a flash command? Or should I disable all interrupts before any flash command like this?

__disable_irq();

// launch a command

FTFL_FSTAT |= FTFL_FSTAT_CCIF_MASK;

//  waiting for the finishing of the command

while((FTFL_FSTAT&FTFL_FSTAT_CCIF_MASK) != FTFL_FSTAT_CCIF_MASK){};

__enable_irq();

0 Kudos
1 Solution
1,083 Views
carlos_neri
NXP Employee
NXP Employee

Usually the flash memory are just allowed to do one thing at a time, either read or write . That is why you need to copy the function to execute the command to RAM, to avoid the "Read-While-Write" violations. Here is an app note that explains why this should be done and addresses the questions you have on ISR handling :smileyhappy:

http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf?fpsp=1&WT_TYPE=Application%20Notes&WT...

View solution in original post

0 Kudos
2 Replies
1,084 Views
carlos_neri
NXP Employee
NXP Employee

Usually the flash memory are just allowed to do one thing at a time, either read or write . That is why you need to copy the function to execute the command to RAM, to avoid the "Read-While-Write" violations. Here is an app note that explains why this should be done and addresses the questions you have on ISR handling :smileyhappy:

http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf?fpsp=1&WT_TYPE=Application%20Notes&WT...

0 Kudos
1,083 Views
egoodii
Senior Contributor III

The things you say are true -- you cannot let anything attempt a 'read' of a flash bank while it is writing.  For two-bank parts you can just insure nothing 'runs' from the 'inactive' bank, but it is always just 'safer' to let all flash operations run from RAM.  I let my whole bootloader be copied out to RAM, vector table and all, by the IAR linker setup.