[STM32F405] flash level 2 protection
Hi,
I was trying to test the feature of "Level 2 flash lock" on my custom board. I'm having an unexpected behaviour.
My total application consists of a bootloader responsable to load an application via CAN BUS through a custom communication protocol.
Now, If I load my application program via CAN (via bootloader), the MCU can not set itself to level2 but remains set to level 1. If I transfer the same application with JTAG, the MCU can set itself to level 2 protection and any attempt to connect to it is correctly disabled. (in level 1 I can still connect by erasing the whole chip content).
To give further informations, here is the code responsable to set level 2 protection implemented:
FM_ClearFlags();
FM_Unlock();
flashSts = FM_CheckProtectionStatus();
if(flashSts == FM_FLASH_NOT_PROTECTED) // check current protection status
{
FM_SetOBConfig();
flashSts = FM_CheckProtectionStatus();
}
FM_Lock();
void FM_ClearFlags(void)
{
FLASH->SR |= 0xE0; // clear the errors RDERR PGSERR PGPERR
}
void FM_Unlock(void)
{
FLASH_Unlock(); // unlock flash to deal with it
FLASH_OB_Unlock(); // unlock option byte to deal with it
}
void FLASH_Unlock(void)
{
if((FLASH->CR & FLASH_CR_LOCK) != RESET)
{
/* Authorize the FLASH Registers access */
FLASH->KEYR = FLASH_KEY1;
FLASH->KEYR = FLASH_KEY2;
}
}
void FLASH_Lock(void)
{
/* Set the LOCK Bit to lock the FLASH Registers access */
FLASH->CR |= FLASH_CR_LOCK;
}
void FM_SetOBConfig(void)
{
FLASH_OB_RDPConfig(OB_RDP_Level_2); // set to 0xCC to enable the level2 protection --> full chip protection. You cant go back to level 1 or 0. Set a number different (like 0x55) means protection of level 1
FLASH_OB_Launch(); // launch the option byte writing procedure
}
I think the code itself works, because it's working flashing via JTAG. So, why flashing through a custom bootloader does not work?
Thanks,
Marco.
