Set Read Out Protection (Level1) in STM32F446
Hi folks,
I am trying to set the RDP option bytes to have my code in flash read-protected. Instead of using ST-Link Utility, I want to set the read-out protection in the code itself (specifically, inside a self-designed bootloader). For this purpose, I use the flash functions provided by the FLASH HAL library. The problem is that I am facing a Hard Fault Exception, right after the option start bit is set (inside the function HAL_FLASH_OB_Launch). According to the reference manual, the sequence for setting the option bytes is the following:
- Unlock the OB
- Write the new OB value
- Set the option start bit
- Wait until the operation is completed
- Lock the OB
Still, at I mentioned before, at the step 4, an exception is triggered (MPU or Execute never XN default memory map access violation has occurred on an instruction fetch). It is important to say though that after a reset, the RDP is activated (by checking with ST-Link-Utility).
Probably I am missing something. Has anyone successfully set the RDP in code? Or has faced the same problem as me?
Many Thanks
Jorge
FLASH_OBProgramInitTypeDef obConfig;
HAL_FLASHEx_OBGetConfig(&obConfig);
if (obConfig.RDPLevel == OB_RDP_LEVEL_0) {
// this is first time we run mcu after flashing firmware
obConfig.RDPLevel = OB_RDP_LEVEL_1;
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
HAL_FLASHEx_OBProgram(&obConfig);
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
}