cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with enabling flash readback protection (Level 1) on STM32F4 using HAL interface.

JKova.3
Associate II

Hi,

I am trying to enable flash readback protection (level 1) on a STM32F411VET6 using the following HAL code:

 FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
   HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  
   if (OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
   {
      OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
      OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_1;
      
      HAL_FLASH_OB_Unlock();
      
      if (HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
      {
        HAL_FLASH_OB_Lock();
      }
      
      HAL_FLASH_OB_Lock();
   }

Whenever the code is executed (even after power off -> power on (checked multiple times)) the RDP level is readback as level 0 so it proceeds to set it to level 1. I can then connect my ST Link application to it and still readback the flash to confirm its not working.

Can you possibly shed any light on what might be going wrong? I've tried stepping through the HAL calls with the debugger and everything looks ok.

I can also confirm that the readback protection will work if done manually through the ST Link GUI, however I need it done in code for my application.

1 ACCEPTED SOLUTION

Accepted Solutions
JKova.3
Associate II

Thankyou, that certainly did the trick!

View solution in original post

2 REPLIES 2
TDK
Guru

You need to launch the option bytes before they will take effect. HAL_FLASH_OB_Launch(). This will also reset the chip.

If you feel a post has answered your question, please click "Accept as Solution".
JKova.3
Associate II

Thankyou, that certainly did the trick!