I am using KSKD 3.0, with SDK 2.0 to run a FREERtos project on a standalone board with a MK22FN256VLL12 on it.
During the initialization, before the RTOS is started with vTaskStartScheduler(), I am able to erase and write to the last couple of sectors of Flash with the following code:
uint32_t try_flash_erase_in_rtos( void )
{
uint32_t destAdrss = SETTINGS_0_START;
status_t result;
if (!nv_initialized) {
NVInit();
nv_initialized = 1;
}
// try disabling interrupts
// taskENTER_CRITICAL();
// taskDISABLE_INTERRUPTS();
__disable_irq();
// erase sector before trying to write (can only write 0 bits, not 1)
result = FLASH_Erase(&flashDriver, destAdrss, FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE, kFLASH_apiEraseKey);
if (kStatus_FLASH_Success != result)
{
return FALSE;
}
// taskENABLE_INTERRUPTS();
// portENABLE_INTERRUPTS();
// taskEXIT_CRITICAL();
__enable_irq();
__disable_irq();
flash_security_state_t securityStatus;
result = FLASH_GetSecurityState(&flashDriver, &securityStatus);
// now write the struct to the flash
result = FLASH_Program(&flashDriver, destAdrss, (uint32_t *)&SystemSettings, sizeof(SystemSettings));
uint32_t failAddr, failDat;
result = FLASH_VerifyProgram(&flashDriver, destAdrss, sizeof(SystemSettings), (uint32_t *)&SystemSettings, kFLASH_marginValueUser, &failAddr, &failDat);
__enable_irq();
if (kStatus_FLASH_Success != result) {
return FALSE;
}
return TRUE;
}
However, if I try to execute that code from inside a running task, The call to FLASH_Erase() causes a core lockup and restarts my program. If I manually erase the whole chip first, and comment out the call to FLASH_Erase(), the call to FLASH_Write() works fine with no reset, and actually does program the flash.
From tracing through the SDK code, it appears that the flash-erasing code is copied to RAM, so I don't think it's an issue of writing to flash while running code from it. I'm also disabling interrupts, so I don't think I'm getting any unwanted execution of ISR code from Flash. I did see a reference to resetting the flash cache, but there is at least some mention of the cache in the call to the RAM function, so I'm assuming it's addressed...
much thanks for any suggestions. Chris
BTW, the clock configuration is taken directly from the SDK flash demo code; core clock is 80 MHz, flash clock is 20 MHz, running in PEE mode.