Skip to main content
JSaez
Associate II
August 16, 2019
Question

Set Read Out Protection (Level1) in STM32F446

  • August 16, 2019
  • 4 replies
  • 3014 views

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:

  1. Unlock the OB
  2. Write the new OB value
  3. Set the option start bit
  4. Wait until the operation is completed
  5. 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();
 }

This topic has been closed for replies.

4 replies

Uwe Bonnes
Chief
August 16, 2019

You probably have to init obConfig with sensible values...

JSaez
JSaezAuthor
Associate II
August 16, 2019

Thanks for your reply. What do you mean with sensible values?

Uwe Bonnes
Chief
August 16, 2019

obConfig.RDPLevel = OB_RDP_LEVEL_1;

seems like a sensible default.

Did you check your code against  FLASH_If_WriteProtectionConfig() e.g. in

Projects/STM32446E_EVAL/Applications/IAP/IAP_Main/Src/flash_if.c

?