Hi Jing, thanks for answering.
* All the flash protection registers are 0xFF
* The flash configuration is set to 0xFFFFFFFE

The only unusual thing here is the F9 in the FOPT to disable EzPort. Unrelated to this.
* Watchdog is disabled
* Interrupts are disabled
Watchdog was always disabled. Now I tried also disabling interrupts, but the result was the same.
* The code is running in RAM
fsl_flash.c takes care of it for me. It copies the following function to the Lower SRAM area:
void flash_run_command(FTFx_REG_ACCESS_TYPE ftfx_fstat)
{
/* clear CCIF bit */
*ftfx_fstat = FTFx_FSTAT_CCIF_MASK;
/* 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))
{
}
}
Investigating the memory everything seemed to be in order, but the MGSTAT0 flag is active after this command runs.
* fails whether or not debugger is attached
Would mind to illustrate which MCU you use?
I first noticed it was failing when the release version of the application was running on the device, with the bootloader on it too. Now I've been testing it with the debugger to try to find out what the problem could be. So, answering your question, it fails whether i'm debugging or not.
I'm using a MK64FN1 MCU in a custom board.