AnsweredAssumed Answered

Swap function sometimes causes a device reset

Question asked by Tamer Mash on Aug 28, 2015
Latest reply on Sep 28, 2015 by Tamer Mash

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,&currentSwapMode, \

                  &currentSwapBlock, &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, &currentSwapMode, \

                           &currentSwapBlock, &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);

  }

}

Outcomes