Skip to main content
Graduate II
November 14, 2024
Question

How to clear the specified flash page's WRP?

  • November 14, 2024
  • 1 reply
  • 899 views

Hi there,

I want to clear the specified flash page's WRP by program, but I can only do it by clearing all the flash page's WRP,

meanwhile, I also need to keep my data in option byte.

my code is as follows(the project at HERE :(

uint8_t data0 = HAL_FLASHEx_OBGetUserData(OB_DATA_ADDRESS_DATA0);
uint8_t data1 = HAL_FLASHEx_OBGetUserData(OB_DATA_ADDRESS_DATA1);
OBInit.OptionType |= OPTIONBYTE_WRP;
OBInit.WRPState = OB_WRPSTATE_DISABLE;
OBInit.WRPPage = ~OBInit.WRPPage & pages;
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
// hal_sta |= HAL_FLASHEx_OBErase();
hal_sta |= HAL_FLASHEx_OBProgram(&OBInit);
OBInit.OptionType = OPTIONBYTE_DATA;
OBInit.DATAAddress = OB_DATA_ADDRESS_DATA0;
OBInit.DATAData = data0;
HAL_FLASHEx_OBProgram(&OBInit);
OBInit.DATAAddress = OB_DATA_ADDRESS_DATA1;
OBInit.DATAData = data1;
HAL_FLASHEx_OBProgram(&OBInit);
if(hal_sta == HAL_OK) {
	HAL_FLASH_OB_Launch();
} else {
	LOG_ERR("clear Flash WRP err[%d]\n", hal_sta);
}

I don't know what I am wrong, please help me.

Thank you!

    This topic has been closed for replies.

    1 reply

    Technical Moderator
    November 15, 2024

    Hi @Junde ,

    Have a look to the example https://github.com/STMicroelectronics/STM32CubeF1/tree/master/Projects/STM32F103RB-Nucleo/Examples/FLASH/FLASH_WriteProtection.

    Check the Readme file to understand how it works. You can precise the pages to be erased with the define of FLASH_PAGE_TO_BE_PROTECTED in the main.c file.

    -Amel

    JundeAuthor
    Graduate II
    November 19, 2024

    Hi @Amel NASRI, thanks for your reply!

    I already referenced this example before I posted this question. Anyway, I reread it and wrote the test demo as follows:

    /**
     *	@brief	set Flash pages WRP state
     * 	@param	pages	@ref FLASHEx_OB_Write_Protection in stm32f1xx_hal_flash_ex.h
     *	@param	wrpSta	@ref FLASHEx_OB_WRP_State in stm32f1xx_hal_flash_ex.h
     *	@note	each bit in WRPPage means one page: 1 is mean this page's WRP is disable, and 0 is mean enable
     *	@note	can NOT clear the WRP of part pages, only clear ALL!
     */
    void set_flash_write_protection(uint32_t pages, uint32_t wrpSta)
    {
    	HAL_StatusTypeDef hal_sta = HAL_OK;
    	FLASH_OBProgramInitTypeDef OBInit = {0};
    
    	HAL_FLASHEx_OBGetConfig(&OBInit);
    	LOG_DBG("pagesSta[0x%08X](1=WRP-OFF), change to WRP-%s pages[0x%08X]\n", OBInit.WRPPage, wrpSta?"ON":"OFF", pages);
    	
    	if(((~OBInit.WRPPage & pages) != pages) && (wrpSta == OB_WRPSTATE_ENABLE)) {
    		HAL_FLASH_Unlock();
    		HAL_FLASH_OB_Unlock();
    		OBInit.OptionType = OPTIONBYTE_WRP;
    		OBInit.WRPState = OB_WRPSTATE_ENABLE;
    		OBInit.WRPPage = pages;
    		hal_sta = HAL_FLASHEx_OBProgram(&OBInit);
    		if(hal_sta == HAL_OK) {
    			HAL_FLASH_OB_Launch();
    		} else {
    			LOG_ERR("set Flash WRP err[%d]\n", hal_sta);
    		}
    	} else if(((OBInit.WRPPage & pages) != pages) && (wrpSta == OB_WRPSTATE_DISABLE)) {
    		HAL_FLASH_Unlock();
    		HAL_FLASH_OB_Unlock();
    		OBInit.OptionType = OPTIONBYTE_WRP;
    		OBInit.WRPState = OB_WRPSTATE_DISABLE;
    		OBInit.WRPPage = pages;
    		hal_sta = HAL_FLASHEx_OBProgram(&OBInit);
    		if(hal_sta == HAL_OK) {
    			HAL_FLASH_OB_Launch();
    		} else {
    			LOG_ERR("clear Flash WRP err[%d]\n", hal_sta);
    		}
    	}
    }

    The LOG as below:

    Junde_0-1732001283962.png

    My code is almost the same as the example, but why can't it work?

    Please help me, thank you.