Skip to main content
Visitor II
January 9, 2024
Solved

Setting RDP option byte in STM32L452

  • January 9, 2024
  • 2 replies
  • 1932 views

I have a problem programmatically setting RDP to level 1.

My code is:

OBInit.RDPLevel = OB_RDP_LEVEL_1;
OBInit.OptionType = OPTIONBYTE_RDP;
ret = HAL_FLASH_Unlock();
ret = HAL_FLASH_OB_Unlock();
/* Clear SR register */
CLEAR_BIT(FLASH->SR, FLASH_SR_BSY);
ret = HAL_FLASHEx_OBProgram(&OBInit);
ret = HAL_FLASH_OB_Lock();
ret = HAL_FLASH_Lock();
HAL_Delay(2000);
ret = HAL_FLASH_OB_Launch();
// shouldn't get to this point, as processor will reset

I had to put in the clear pf FLASH->SR, otherwise the "FLASH_WaitForLastOperation" would fail inside "FLASH_OB_RDPConfig". I've no idea why the flash should be busy at this stage.

Whether or not I run this under the debugger, the RDP byte never changes. The last HAL_Delay is to give the flash chance to be written properly before the launch resets the processor.

    This topic has been closed for replies.
    Best answer by TDK

    The BSY bit is read-only. Wait for BSY to be low, you can't set it to a value.

    You must call HAL_FLASH_OB_Launch before locking option bytes. Locking them prevents access.

    Several examples out there for this. Here is one:

    https://github.com/STMicroelectronics/STM32CubeL4/blob/93f2cde30d17996651d7b31f7091ab3dfe2f99bb/Projects/NUCLEO-L4R5ZI/Examples/FLASH/FLASH_WriteProtection/Src/main.c#L299

     

    2 replies

    TDKAnswer
    Super User
    January 9, 2024

    The BSY bit is read-only. Wait for BSY to be low, you can't set it to a value.

    You must call HAL_FLASH_OB_Launch before locking option bytes. Locking them prevents access.

    Several examples out there for this. Here is one:

    https://github.com/STMicroelectronics/STM32CubeL4/blob/93f2cde30d17996651d7b31f7091ab3dfe2f99bb/Projects/NUCLEO-L4R5ZI/Examples/FLASH/FLASH_WriteProtection/Src/main.c#L299

     

    ST Employee
    January 9, 2024

    Hello @SLevi.1, and @TDK,

    I add this article named How to program STM32 Option Bytes with the HAL API.

    Enjoy your reading ! :)

    Pierre