Hello,
I'm working on developing a bootloader for the FRDM-K64F board and encountered an issue. When trying to erase a specific area of flash memory starting from the APP_START_ADDRESS, we run into Flash Protection issues. The functions we have tried to disable this protection are failing.
When attempting to erase the flash memory, we receive a Flash Protection Violation error. Below, I've outlined the steps we're following. However, even though we are using functions like FLASH_PflashSetProtection and FLASH_SecurityBypass, we're unable to bypass the protection and erase the specified APP_START_ADDRESS.
void EraseFlashMemory(uint32_t startAddress, uint32_t eraseSize)
{
flash_status = FLASH_Init(&s_flashDriver);
if (flash_status != kStatus_FLASH_Success)
{
UART_Send("Flash initialization failed.\r\n");
return;
}
uint32_t protectStatus = 0x00000000;
flash_status = FLASH_PflashSetProtection(&s_flashDriver, protectStatus);
if (flash_status != kStatus_FLASH_Success)
{
UART_Send("Failed to remove flash protection.\r\n");
return;
}
flash_status = FLASH_Erase(&s_flashDriver, startAddress, eraseSize, kFTFx_ApiEraseKey);
if (flash_status == kStatus_FLASH_Success)
{
UART_Send("Flash erase successful.\r\n");
}
else
{
UART_Send("Flash erase failed.\r\n");
char error_msg[64];
sprintf(error_msg, "Error code: 0x%x\r\n", flash_status);
UART_Send(error_msg);
return;
}
I don't know what causes the flash protection I mentioned.
Hi,
I suppose that you can use two methods to set the protection.
1)set the word3 as 0xFFFFFFFF, all the program flash will be unprotected.
2)call the
status_t FLASH_PflashSetProtection(flash_config_t *config, pflash_prot_status_t *protectStatus)
The FRDM-K64 use MK64FN1M0VLL12, it has 1MB flash, ranging from 0x00000000 to 0x10_0000
so you can write the code as
uint32_t protectStatus;
FLASH_PflashGetProtection(&s_flashDriver, &protectStatus);
protectStatus|=1<<5; //unprotect 0x2_0000 address
FLASH_PflashSetProtection&s_flashDriver, &protectStatus);
I hope you use the first method.
Hope it can help you
BR
XiangJun Rong