cancel
Showing results for 
Search instead for 
Did you mean: 

Setting RDP option byte in STM32L452

SLevi.1
Associate III

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.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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

 

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

View solution in original post

2 REPLIES 2
TDK
Guru

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

 

If you feel a post has answered your question, please click "Accept as Solution".
Pierre_Paris
ST Employee

Hello @SLevi.1, and @TDK,

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

Enjoy your reading ! :)

Pierre

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.