Skip to main content
Visitor II
September 19, 2021
Solved

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

  • September 19, 2021
  • 2 replies
  • 822 views

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.

    This topic has been closed for replies.
    Best answer by JKova.3

    Thankyou, that certainly did the trick!

    2 replies

    Super User
    September 20, 2021

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

    JKova.3AuthorAnswer
    Visitor II
    September 20, 2021

    Thankyou, that certainly did the trick!