AnsweredAssumed Answered

problem erasing MK02FN128VLF10 flash with program calls to flash driver

Question asked by Ed McMurray on Oct 10, 2017
Hello,
I’m having a problem erasing MK02FN128VLF10 flash, using Flash driver calls from within our program .  Our board design is loosely based on the FRDM K22 board.  The part on our board is clocked with internal IRC48M clock.  The SDK used for the starting point of our software was built with NXP SDK builder configured for MK02FN128xxx10. 
The design works fine overall,  wejust ran into a problem when trying to use Flash to save “user setting”, between power downs.  I started the Flash access effort,  using the example in the SDK “flash_erase_program_verify”. (see attached C file)
The MK02 has 128M of Flash, in 1 block with 2K sectors.  Given the fact that Flash is one block, the section of code that sends flash command and waits for completion, has to be run from ram.  
I have verified that the correct section of Flash code is copied into the correct section of Ram.  Using the debugger, I am able to verify that the code runs as expected from Ram.  I can verify that the last sector in Flash is erased, and then that it is written with the correct 4 bytes. 
The problem is that I can only get the code to run successfully if I put a single breakpoint in the ram code, between the code to send the command and the code to wait for it to complete.  I place the breakpoint there and run the the erase command.  Then I remove it for all the other Read / Write commands.  It works fine and I can verify such in memory view.
When Running it without the break point there, and just letting it run, it doesn’t work.  I get a reset once the code is sent.  I’m not sure of the actual error that causes reset.
Any thoughts on what the problem may be or where I should look for hints?
Thanks,
Ed
Miscellaneous info:
a: flash_driver.c  see function call to erase flash sector
result = FLASH_Erase(&flashDriver, destAdrss, pflashSectorSize, kFLASH_apiEraseKey);
b: fsl_flash.c  see function call to run command in ram
callFlashRunCommand((FTFx_REG_ACCESS_TYPE)(&FTFx->FSTAT));
c: I have checked commnd registers, it looks like the flash commands are good.
d: Flash Code placed in Ram
 /* This function should be copied to RAM for execution to make sure that code works properly even flash cache is disabled.
    It is for flash-resident bootloader only, not technically required for ROM or flashloader (RAM-resident bootloader).  */
void flash_run_command(FTFx_REG_ACCESS_TYPE ftfx_fstat)
{
    volatile int i_ram; // mcmurray
    /* clear CCIF bit */
    *ftfx_fstat = FTFx_FSTAT_CCIF_MASK;
   // Breakpoint placed here
    /* Check CCIF bit of the flash status register, wait till it is set.  IP team indicates that this loop will always complete. */
   while (!((*ftfx_fstat) & FTFx_FSTAT_CCIF_MASK)){ }
}

 

Original Attachment has been moved to: flashTest.zip

Outcomes