Hi,
I'm trying to run a m4 firmware upgrade code using LPC4357, but as soon as I erase sector 0, the code halts.
- I'm not using interrupts
- I've left 32Byte of stack offset. ... MCU Linker -> Target -> stack offest = 32
- I placed routines into RAM
- I copied the firmware that will upgrade the m4 into a free sector of Flash bank A then, to upgrade the firmware, I simply copy these sectors into sectors starting from 0
Am I missing something else?
Probably I'm not correctly using the instructions to place routines into RAM.
I'm attaching parts of the code:
could you please double check?
Any suggestions?
Thank you
Pietro
int main(void) {
uint8_t result8=0;
uint8_t (*longfunc)(void) = &ram_copy_fw_upgrade;
SystemCoreClockUpdate();
Board_UART_Init(0);//needed, or it will not compile
Chip_IAP_Initialise();//important! Or iap will not work!
result8=(*longfunc)();// routine that runs from RAM!
while(1) {
}
return 0 ;
}
#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) uint8_t ram_Chip_IAP_PreSectorForReadWrite(uint32_t strSector, uint32_t endSector, uint8_t flashBank)
{
uint32_t command[5], result[4];
command[0] = IAP_PREWRRITE_CMD;
command[1] = strSector;
command[2] = endSector;
command[3] = flashBank;
iap_entry(command, result);
return result[0];
}
__RAMFUNC(RAM) uint8_t ram_Chip_IAP_CopyRamToFlash(uint32_t dstAdd, uint32_t *srcAdd, uint32_t byteswrt)
{
uint32_t command[5], result[4];
command[0] = IAP_WRISECTOR_CMD;
command[1] = dstAdd;
command[2] = (uint32_t) srcAdd;
command[3] = byteswrt;
command[4] = SystemCoreClock / 1000;
iap_entry(command, result);
return result[0];
}
__RAMFUNC(RAM) uint8_t ram_Chip_IAP_EraseSector(uint32_t strSector, uint32_t endSector, uint8_t flashBank)
{
uint32_t command[5], result[4];
command[0] = IAP_ERSSECTOR_CMD;
command[1] = strSector;
command[2] = endSector;
command[3] = SystemCoreClock / 1000;
command[4] = flashBank;
iap_entry(command, result);
return result[0];
}
__RAMFUNC(RAM) uint8_t ram_copy_fw_upgrade(void){
//routine placed in ram that uses routines placed in ram
//In a previous run I copied blinky.h data into sector STARTING_SECTOR_N=8
//Now I copy flash sector 8 into sector 0
uint32_t fw_lenght,sector_address;
int i;
uint8_t result8;
uint32_t n_sector=0;//we start writing data in this sector
uint32_t *read_address=(uint32_t *)sector_banka_start_map[STARTING_SECTOR_N];//leggo da qui
/// pointers to call routines placed into RAM
uint8_t (*longfunc_prepare)(uint32_t strSector, uint32_t endSector, uint8_t flashBank) = &ram_Chip_IAP_PreSectorForReadWrite;
uint8_t (*longfunc_copy)(uint32_t dstAdd, uint32_t *srcAdd, uint32_t byteswrt) = &ram_Chip_IAP_CopyRamToFlash;
uint8_t (*longfunc_erase)(uint32_t strSector, uint32_t endSector, uint8_t flashBank) = &ram_Chip_IAP_EraseSector;
__disable_irq();
sector_address=sector_banka_start_map[n_sector];
result8=(*longfunc_prepare)(n_sector, n_sector, 0);//prepare..Flash Bank A
result8 +=(*longfunc_erase)(n_sector, n_sector, 0);//erase <<<<<<<<<<<<<<<< ---------- HERE THE PROGRAM HALTS -----------
fw_lenght=12988;//sizeof(new_firmware);
while(fw_lenght>0){
for(i=0;i<FLASH_DWORD_BLOCK;i++){ //copy sectors
flash_data=*read_address;
fw_lenght -=4;
read_address++;
if(!(fw_lenght>0)){//last_iteration?
for(i=i+1;i<FLASH_DWORD_BLOCK;i++) flash_data=0xFFFFFFFF;// fill with erased data
break;//fine del ciclo for
}
}
//storing data into flash
result8+=(*longfunc_prepare)(n_sector, n_sector, 0);//prepare
result8 +=(*longfunc_copy)(sector_address, flash_data, FLASH_BLOCK);//erase
//update flash address
sector_address +=FLASH_BLOCK;
if(!(sector_address<sector_banka_end_map[n_sector])){//if needed, change sector
n_sector++; //new sector
if(!(n_sector<MAX_FLASH_SECTOR)){
//fail !!
}
sector_address=sector_banka_start_map[n_sector];//new address
//nuovo settore:
result8+=(*longfunc_prepare)(n_sector, n_sector, 0);//prepare
result8 +=(*longfunc_erase)(n_sector, n_sector, 0);//erase
}
}
__enable_irq();
return result8;
}
|