2023-12-22 02:08 AM
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.
Solved! Go to Solution.
2023-12-22 02:59 AM
Hello @Junde
Referring to PM0068, you can check write protection definition (0 for activated WRP).
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.
2023-12-22 02:59 AM
Hello @Junde
Referring to PM0068, you can check write protection definition (0 for activated WRP).
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.
2023-12-24 06:53 PM
@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();
2024-10-17 06:45 PM - edited 2024-10-17 06:46 PM
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().