STM32F44 MCU: Read protection flag not removed
Hi
I try to implement the read protection on my STM32F44 board based on this example:
https://community.st.com/t5/stm32-mcus/how-to-change-the-read-out-protection-on-stm32f4/ta-p/49410
In principle, it works, but I have some strange behavior. My code:
if (level == 0x00 || level == 0x01) {
FLASH_OBProgramInitTypeDef pOBInit = { 0 };
HAL_FLASHEx_OBGetConfig(&pOBInit);
pOBInit.Banks = FLASH_BANK_1;
pOBInit.OptionType = OPTIONBYTE_RDP;
if (level == 0x00) {
new_level = OB_RDP_LEVEL_0;
}
else {
new_level = OB_RDP_LEVEL_1;
}
if (new_level != pOBInit.RDPLevel) {
do {
//Unlock flash
status = HAL_FLASH_Unlock();
//Unlock flash option sections
status = HAL_FLASH_OB_Unlock();
pOBInit.RDPLevel = new_level;
status = HAL_FLASHEx_OBProgram(&pOBInit);
//Triggers a reset
status = HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
printLogWarning(halToCANxError(status), "Writing flash read protection failed");
return 0;
} while (0);
//Something failed, flash maybe already unlocked
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}
}
Two points are not working as they should:
- When I enable the read protection: As far as I understood, HAL_FLASH_OB_Launch() should trigger a reset. This works when I'm using the debugger. Without debugger, the warning message after HAL_FLASH_OB_Launch() is sent, it seems that these lines are executed. But the read protection is enabled afterwards.
- When I remove the read protection (Level 0): The flash is erased as planned. However, when I try to reprogram the device, there is still a message that the device is read protected. It seems that the flag was not reset correctly.
Any idea what I can do to solve these issues, especially the second?
Thank you.
