Skip to main content
Associate
June 19, 2023
Solved

Flash RDP activate

  • June 19, 2023
  • 9 replies
  • 6064 views

Hello everyone, 

I have board and I used STM32G071 MCU on this board. I want to activate RDP Level 1. There is my code: 

 
 
void SysCodeProtection( void )
{
FLASH_OBProgramInitTypeDef pOBInit = {0};
 
HAL_FLASHEx_OBGetConfig( &pOBInit );
 
if ( pOBInit.RDPLevel != OB_RDP_LEVEL_1 )
{
HAL_FLASH_Unlock();
 
HAL_FLASH_OB_Unlock();
 
pOBInit.OptionType = OPTIONBYTE_RDP;
 
pOBInit.RDPLevel = OB_RDP_LEVEL_1;
 
HAL_FLASHEx_OBProgram( &pOBInit );
 
HAL_FLASH_OB_Launch();
 
HAL_FLASH_OB_Lock();
 
HAL_FLASH_Lock();
 
}
 
}
 
I'm programming my MCU on ST-LINK with ST-LINK Utility. But sometimes after I programmed my MCU It's locking or crushing. My power LED is not working on my board and I can't connect my board. I lost many boards after activate RDP If I disable RDP I didn't see any problems. Can anyone help me about it ?
This topic has been closed for replies.
Best answer by Diane POMABIA

Hello @RE11 

Try to disable Prefech before programming your OB.

Please refer to my test code :

FLASH_OBProgramInitTypeDef OptionsBytesInit= {0};

HAL_RCC_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
__disable_irq();
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
/* Unlock Flash Control register and Option Bytes */
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
HAL_FLASHEx_OBGetConfig(&OptionsBytesInit);


if(OptionsBytesInit.RDPLevel!=OB_RDP_LEVEL_1){
/* First step: Choose option byte type.*/
OptionsBytesInit.OptionType =OPTIONBYTE_RDP;
OptionsBytesInit.RDPLevel = OB_RDP_LEVEL_1;
/* Program Option Bytes */
HAL_FLASHEx_OBProgram(&OptionsBytesInit);
/* Launch Option Bytes Loading */
HAL_FLASH_OB_Launch();

Regards

DianeP

9 replies

Diane POMABIABest answer
ST Employee
June 20, 2023

Hello @RE11 

Try to disable Prefech before programming your OB.

Please refer to my test code :

FLASH_OBProgramInitTypeDef OptionsBytesInit= {0};

HAL_RCC_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
__disable_irq();
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
/* Unlock Flash Control register and Option Bytes */
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
HAL_FLASHEx_OBGetConfig(&OptionsBytesInit);


if(OptionsBytesInit.RDPLevel!=OB_RDP_LEVEL_1){
/* First step: Choose option byte type.*/
OptionsBytesInit.OptionType =OPTIONBYTE_RDP;
OptionsBytesInit.RDPLevel = OB_RDP_LEVEL_1;
/* Program Option Bytes */
HAL_FLASHEx_OBProgram(&OptionsBytesInit);
/* Launch Option Bytes Loading */
HAL_FLASH_OB_Launch();

Regards

DianeP

RE11Author
Associate
June 20, 2023

Hello @Diane POMABIA 

Thanks for reply and your help. I will try it for my boards. I want to ask one more question. My some boards crushed after I upload my code. I can not connect them. There is any way to connect them again ? Can I save my boards ? 

ST Employee
June 20, 2023

@RE11  At this point, you have lost your board, we suspect that your last code have created OB missmatch that have activated your bootlock.

Regards

Diane

waclawek.jan
Super User
June 20, 2023

Hi @Diane POMABIA ,

can you please point me to the section of RM where this is documented?

Thanks,

JW

ST Employee
June 21, 2023

Hi @waclawek.jan 

We are still analysed the issue. An updated will be done on product document (RM or Errata) when we will finish.

Regards

Diane

RE11Author
Associate
June 22, 2023

Hello again @Diane POMABIA 

 

FLASH_OBProgramInitTypeDef OptionsBytesInit = {0};
 
HAL_RCC_DeInit();
 
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
 
__disable_irq();
 
__HAL_FLASH_PREFETCH_BUFFER_DISABLE();
 
/* Unlock Flash Control register and Option Bytes */
HAL_FLASH_Unlock();
 
HAL_FLASH_OB_Unlock();
 
HAL_FLASHEx_OBGetConfig(&OptionsBytesInit);
 
if( OptionsBytesInit.RDPLevel != OB_RDP_LEVEL_1 )
  {
/* First step: Choose option byte type.*/
OptionsBytesInit.OptionType = OPTIONBYTE_RDP;
 
OptionsBytesInit.RDPLevel = OB_RDP_LEVEL_1;
/* Program Option Bytes */
HAL_FLASHEx_OBProgram(&OptionsBytesInit);
 
/* Launch Option Bytes Loading */
HAL_FLASH_OB_Launch();
 
}
 
  HAL_FLASH_OB_Lock();
 
  HAL_FLASH_Lock();
 
  __enable_irq();
 
I tried my board with this code. But my boards still locking I can't connect them again. There is any wrong to lock my boards ? To enable irq or To lock flash can affect my boards ? 
ST Employee
June 23, 2023

Hi  @RE11 

Is it the same board or new board ?

I tested your code on a part and I do not encounter the same problem. Can you confirm me that you are not using the same sample ? 

Regards

Diane

RE11Author
Associate
June 23, 2023

@Diane POMABIA 

I'm using same boards but this not happening always. I have many boards and they are same. My some boards working well but some of them when I update my code They are not working again. Also I was having F series board too. I didn't experienced it with F series board. But this time I'm using STM32G071 microcontroller and after I enable RDP Level 1 bootlocking this started to happen. After I tried your solution code this didn't fix. Still my some boards not working again. As i said It is not happening always.

waclawek.jan
Super User
June 23, 2023

Maybe you may try to check state of BOOT_LOCK before trying to set RDP1.

waclawekjan_2-1687523417047.png

 

 

I don't know why would BOOT_LOCK be set, but maybe this is the mechanism through which the chips are "lost".

JW

ST Employee
July 6, 2023

Internal ticket number:156398 (This is an internal tracking number and is not accessible or usable by customers).

Explorer
September 11, 2023

Hi,

I encounter the similar problem at STM32L422CB. When I set OB_RDP_LEVEL_1 and then call HAL_FLASH_OB_Launch the MCU does not boot anymore. When setting OB_RDP_LEVEL_0 there is no problem. I have not tried OB_RDP_LEVEL_2.
Surprisingly, when I set the OB_RDP_LEVEL_1 without calling HAL_FLASH_OB_Launch but doing a manual power off-on, there is no problem at all and everything works as it should.
I guess the problem is the HAL_FLASH_OB_Launch procedure.
I hope it may be helpful.

Regards.