LPC4357 M4 code in RAM

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by pgeloso on Mon Jun 15 06:43:58 MST 2015
I'm trying to run a function from RAM in M4 core.
I'm using LPCXpresso 7.8.0 and an EA-LPC4357 eval board.

This function performs the erase of sector 0 Flash A.
If the code in RAM has no "links" with Flash A and there are no interrupts, it should run..but, actually, it crashes as soon as I try to erase sector 0 of Flash A.
...I don't understand why.

- I'm not using interrupts
- I left 32Bytes of stack offset. to use IAP
- I initialize IAP before using it
- I placed the routine routine into RAM

Here the simple code:

#include "board.h"

#include <cr_section_macros.h>

#include "iap_18xx_43xx.h"

//IAP: Settings .. ->.. stack offset=32
// Initialize IAP before using it
// Erase sector 0 from RAM

#define IAP_INIT_CMD 49

void Chip_IAP_Initialise(void)
uint32_t command[5], result[4];

command[0] = IAP_INIT_CMD;
iap_entry(command, result);

__RAMFUNC(RAM) void ram_fw_upgrade(void){

int i;
uint32_t n_sector=0;//we start writing data in this sector
uint32_t command[5], result[4];


// prepare sector 0
command[0] = IAP_PREWRRITE_CMD;
command[1] = n_sector;
command[2] = n_sector;
command[3] = 0;//flashBank A
iap_entry(command, result);

//erase sector 0
command[0] = IAP_ERSSECTOR_CMD;
command[1] = n_sector;
command[2] = n_sector;
command[3] = SystemCoreClock / 1000;
command[4] = 0;//flashBank A
iap_entry(command, result);           //---->crash!!

for(i=0;i<1000000;i++);//wait..do something in RAM



int main(void) {
int i=0;
void (*longfunc)(void) = &ram_fw_upgrade;

//   Board_Init();
//   Board_LED_Set(0, true);
      Board_UART_Init(0);//doing nothing, but it's needed, or it will not compile


    if(i!=0){ //just for safety..we enter here only manually, during debug..
    (*longfunc)();//  running from RAM
    while(1) {
    return 0 ;