Content originally posted in LPCWare by OldisGold on Fri Oct 02 16:56:26 MST 2015
This is the code in main.c
uint32_t *p;
uint8_t ret_code;
#define START_WRITE_ADDR 0x00018000
Board_LED_Set(1, TRUE);
__disable_irq();// Start by disabling interrupts, before changing interrupt vectors
//Chip_Clock_SetMainClockSource(SYSCTL_MAINCLKSRC_IRC); //switch to IRC
//Check Firmware Version
if(*new_firmware_version > *current_firmware_version)
{
//
//******************************Copy Extracted download firmware to location 0x18000*******************************************************//
/* IAP Flash programming */
// Prepare to write/erase the last sector
ret_code = Chip_IAP_PreSectorForReadWrite(3, 3);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute PrepareSectorForReadWrite. \r\n");} // Error checking
//Erase sector for Write
ret_code = Chip_IAP_EraseSector(3,3);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute. EraseSector \r\n");}
// Write to the last sector
// Prepare to write/erase the last sector
ret_code = Chip_IAP_PreSectorForReadWrite(3, 3);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute PrepareSectorForReadWrite. \r\n");} // Error checking
ret_code = Chip_IAP_CopyRamToFlash(START_WRITE_ADDR, src_iap_array_data00, 4096);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute array00. \r\n");}// Error checking
DEBUGSTR("Flash array00\r\n");
START_WRITE_ADDR = START_WRITE_ADDR+0x1000;
ret_code = Chip_IAP_PreSectorForReadWrite(3, 3);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute.PrepareSectorForReadWrite \r\n");} //Error checking
ret_code = Chip_IAP_CopyRamToFlash(START_WRITE_ADDR, src_iap_array_data01, 4096);
if (ret_code != IAP_CMD_SUCCESS) { DEBUGSTR("Command failed to execute array01. \r\n");}// Error checking
DEBUGSTR("Flash array01\r\n");
//*current_firmware_version = *new_firmware_version; //Set the Current Firmware Version to the new firmware after copying
/*******************To Check State of Sector 3**************************/
ret_code = Chip_IAP_BlankCheckSector(3,3);
if(ret_code == IAP_SECTOR_NOT_BLANK) {DEBUGSTR("Sector 3 Not Blank");}
/*************************************************************************/
}
DEBUGSTR("To test the SBL Code\n");
/* Set vector table offset*/
SCB->VTOR = user_start_sector_address;
p = (uint32_t *)user_start_sector_address;
Board_LED_Set(1, FALSE);
__enable_irq();//Enable interrupts before jumping to application
/*Set stack pointer to given address*/
__set_MSP(*p);
/*Set address for RESET*/
p++;
user_code_entry = (void (*)(void))(*p);
/*Jump to application*/
user_code_entry();