cancel
Showing results for 
Search instead for 
Did you mean: 

How to enable/disable Flash Write Protection?

Junde
Senior II

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.

View solution in original post

2 REPLIES 2
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.

@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();