I have the swap function working on my K60 with 512MB flash dev kit; however, one in every 5 or so swaps, the microcontroller resets after erasing the upper block. This happens when the flash is in the READY state where I put it into the UPDATE state and then erase the upper block so it moves to the UPDATE_ERASED state and I can re-program the flash. Adding a delay between putting the flash in the UPDATE state and erasing the upper block seems to help, but doesn't fix it. Below is the code that changes the state:
bool erase_upper_block()
{
LOG_DEBUG("Erasing upper block...");
uint32_t destination = P_FLASH_BASE + (P_FLASH_SIZE/2);
uint32_t ret = FlashEraseBlock(&flash_ssd_config, destination, flash_launch_command);
if(ret != FTFx_OK)
{
LOG_ERROR("failed (error: %d)\r\n", ret);
}
else
{
LOG_DEBUG("success\r\n");
}
return (FTFx_OK == ret);
}
void swap_flash_for_ready_state()
{
LOG_DEBUG("Flash in READY state, so put in UPDATE state\r\n");
delay(100);
// move to UPDATE state
ret = PFlashSwapCtl(&flash_ssd_config, PSWAP_INDICATOR_ADDR, FTFx_SWAP_SET_IN_PREPARE,¤tSwapMode, \
¤tSwapBlock, &nextSwapBlock , flash_launch_command);
if(ret == FTFx_OK)
{
// verify it's in update state
ret = PFlashSwapCtl(&flash_ssd_config, PSWAP_INDICATOR_ADDR, FTFx_SWAP_REPORT_STATUS, ¤tSwapMode, \
¤tSwapBlock, &nextSwapBlock, flash_launch_command);
if(ret == FTFx_OK)
{
if(currentSwapMode == FTFx_SWAP_UPDATE)
{
LOG_DEBUG("Flash is now in UPDATE state, so erase upper block to move to UPDATE_ERASED state\r\n");
// need to sleep for a bit before erasing; otherwise the MC sometimes resets
delay(500); // this helps but the MC still sometimes resets
// now erase to move to UPDATE_ERASED move
if(!erase_upper_block())
{
ret = FTFx_ERR_MGSTAT0;
}
}
else
{
LOG_ERROR("Failed to put the swap system into UPDATE mode\r\n");
ret = FTFx_ERR_MGSTAT0;
}
}
}
else
{
LOG_ERROR("Failed to change swap status to UPDATE mode. Error: 0x%x\r\n", ret);
}
}
Hello Tamer Mash:
Regarding this swap mechanism issue:
- Are you able to debug and see in what part of that code the MCU is resetting?- You can check the RCM_SRSx registers to find the cause of the last reset.
Looking forward to your reply.
Regards!
Jorge Gonzalez
Hi Jorge,
Sorry for the delay in reply. If I put a breakpoint and step through the code, I don't see the problem. I added a few 10ms delays in key places and the problem stopped happening.
Best regards,
Tamer