cancel
Showing results for 
Search instead for 
Did you mean: 

How to enable/disable Flash Write Protection?

Junde
Senior III

Hi guys,

I am read the example "STM32Cube_FW_F1_V1.8.4\Projects\STM32F103RB-Nucleo\Examples\FLASH\FLASH_WriteProtection", and want to rewrite it on my board.

my code is as follows:

 

  HAL_FLASH_Unlock();
  HAL_FLASH_OB_Unlock();
  
  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  LOG_DBG("current config: WRPState[%d]\n", OptionsBytesStruct.WRPState);
  LOG_DBG("current config: WRPPage[0x%08x]\n", OptionsBytesStruct.WRPPage);
  if((OptionsBytesStruct.WRPPage & FLASH_PAGE_TO_BE_PROTECTED) == FLASH_PAGE_TO_BE_PROTECTED) { // if WRP enable
      LOG_DBG("Flash is WriteProtection\n");
      OptionsBytesStruct.OptionType = OPTIONBYTE_WRP;
      OptionsBytesStruct.WRPState = OB_WRPSTATE_DISABLE;
      OptionsBytesStruct.WRPPage = FLASH_PAGE_TO_BE_PROTECTED;
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
          LOG_ERR("Flash OB program err when turn WRP off\n");
      }
      HAL_Delay(10000);
      LOG_DBG("system reset\n");
      HAL_Delay(1000);
      HAL_FLASH_OB_Launch();
} else {
      LOG_DBG("Flash is NOT WriteProtection\n");
      OptionsBytesStruct.OptionType = OPTIONBYTE_WRP;
      OptionsBytesStruct.WRPState = OB_WRPSTATE_ENABLE;
      OptionsBytesStruct.WRPPage = FLASH_PAGE_TO_BE_PROTECTED;
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
          LOG_ERR("Flash OB program err when turn WRP on\n");
      }
      HAL_Delay(10000);
      LOG_DBG("system reset\n");
      HAL_Delay(1000);
      HAL_FLASH_OB_Launch();
  }

  HAL_FLASH_OB_Lock();
  HAL_FLASH_Lock();

 

The output is as this:

 

Flash Write Protection demo...
current config: WRPState[0]
current config: WRPPage[0xFFFFFFFF]
Flash is WriteProtection
system reset

Flash Write Protection demo...
current config: WRPState[0]
current config: WRPPage[0xFFFFFFFF]
Flash is WriteProtection
system reset

Flash Write Protection demo...
current config: WRPState[0]
current config: WRPPage[0xFFFFFFFF]
Flash is WriteProtection
system reset

 

What I expected of the output should be like "WRP_EN --> WRP_DIS -->WRP_EN --> WRP_DIS ......".

I have no idea what I was wrong, help me, please. 

Thank you.

 

BTW, I am confused about the result of "HAL_FLASHEx_OBGetConfig(&OBStruct);", if the result as below:

 

  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  LOG_DBG("current config: WRPState[%d]\n", OptionsBytesStruct.WRPState);   // 0
  LOG_DBG("current config: WRPPage[0x%08x]\n", OptionsBytesStruct.WRPPage); // 0xFFFFFFFF

 

Then, does that mean all the flash is WRP, or all the flash is NOT in WRP?

Thanks again for your help in advance.

 

1 ACCEPTED SOLUTION

Accepted Solutions
FBL
ST Employee

Hello @Junde 

Referring to PM0068, you can check write protection definition (0 for activated WRP). 

FBelaid_0-1703240458006.png

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.


I'm out of offce with limited access to my emails.
Happy New Year!

View solution in original post

3 REPLIES 3
FBL
ST Employee

Hello @Junde 

Referring to PM0068, you can check write protection definition (0 for activated WRP). 

FBelaid_0-1703240458006.png

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.


I'm out of offce with limited access to my emails.
Happy New Year!

@FBL Thanks for your help.

I was wrong about the meaning of the FLASH_WRPR bit.(0 is for active, NOT 1)

So my code should be as this:

  HAL_FLASH_Unlock();
  HAL_FLASH_OB_Unlock();
  
  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  LOG_DBG("current config: WRPState[%d]\n", OptionsBytesStruct.WRPState);
  LOG_DBG("current config: WRPPage[0x%08x]\n", OptionsBytesStruct.WRPPage);
  if((~OptionsBytesStruct.WRPPage & FLASH_PAGE_TO_BE_PROTECTED) == FLASH_PAGE_TO_BE_PROTECTED) { // if WRP enable
      LOG_DBG("Flash is WriteProtection\n");
      OptionsBytesStruct.OptionType = OPTIONBYTE_WRP;
      OptionsBytesStruct.WRPState = OB_WRPSTATE_DISABLE;
      OptionsBytesStruct.WRPPage = FLASH_PAGE_TO_BE_PROTECTED;
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
          LOG_ERR("Flash OB program err when turn WRP off\n");
      }
      HAL_Delay(10000);
      LOG_DBG("system reset\n");
      HAL_Delay(1000);
      HAL_FLASH_OB_Launch();
} else {
      LOG_DBG("Flash is NOT WriteProtection\n");
      OptionsBytesStruct.OptionType = OPTIONBYTE_WRP;
      OptionsBytesStruct.WRPState = OB_WRPSTATE_ENABLE;
      OptionsBytesStruct.WRPPage = FLASH_PAGE_TO_BE_PROTECTED;
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
          LOG_ERR("Flash OB program err when turn WRP on\n");
      }
      HAL_Delay(10000);
      LOG_DBG("system reset\n");
      HAL_Delay(1000);
      HAL_FLASH_OB_Launch();
  }

  HAL_FLASH_OB_Lock();
  HAL_FLASH_Lock();

 

Checking WRPState is useless, because this field is never set in HAL_FLASHEx_OBGetConfig() function:

 

void HAL_FLASHEx_OBGetConfig(FLASH_OBProgramInitTypeDef *pOBInit)
{
  pOBInit->OptionType = OPTIONBYTE_WRP | OPTIONBYTE_RDP | OPTIONBYTE_USER;

  /*Get WRP*/
  pOBInit->WRPPage = FLASH_OB_GetWRP();

  /*Get RDP Level*/
  pOBInit->RDPLevel = FLASH_OB_GetRDP();

  /*Get USER*/
  pOBInit->USERConfig = FLASH_OB_GetUser();

  /* WRPState was never set! */
}

 

You can only rely on WRPPage, RDPLevel and USERConfig fields.

WRPState is only useful when you set OBInit.WRPState to OB_WRPSTATE_EBABLE or OB_WRPSTATE_DISABLE and then feed OBInit to the HAL_FLASHEx_OBProgram().

Disappointed with crowdfunding projects? Make a lasting, meaningful impact as a Tech Sponsor instead: Visit TechSponsor.io to Start Your Journey!