2016-07-13 06:17 AM
I can not handle the option byte as I would like.
I use a STM32F103ZE, so with only one memory bank.
I start from a state of write protection of all pages except the 60 and 61, then log FLASH_WRPR = 0x40000000.
Use the following code to get to have the write-protect pages from 0 to 59, but this does not happen. Where am I wrong? With the following code removes the write protection to the whole bank.
#define FLASH_PAGE_TO_BE_PROTECTED (OB_WRP_PAGES62TO255)
…
uint32_t FLASH_If_WriteProtectionConfig(uint32_t protectionstate)
{
uint32_t ProtectedPAGE = 0x0;
FLASH_OBProgramInitTypeDef config_new, config_old;
HAL_StatusTypeDef result = HAL_OK;
/* Get pages write protection status ****************************************/
HAL_FLASHEx_OBGetConfig(&config_old);
/* The parameter says whether we turn the protection on or off */
config_new.WRPState = (protectionstate == FLASHIF_WRP_ENABLE ? OB_WRPSTATE_ENABLE : OB_WRPSTATE_DISABLE);
/* We want to modify only the Write protection */
config_new.OptionType = OPTIONBYTE_WRP;
/* No read protection, keep BOR and reset settings */
config_new.RDPLevel = OB_RDP_LEVEL_0;
config_new.USERConfig = config_old.USERConfig;
/* Get pages already write protected ****************************************/
if(protectionstate == FLASHIF_WRP_DISABLE)
ProtectedPAGE = config_old.WRPPage | FLASH_PAGE_TO_BE_PROTECTED;
else if(protectionstate == FLASHIF_WRP_ENABLE)
ProtectedPAGE = ~OB_WRP_PAGES60TO61;
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Unlock the Options Bytes *************************************************/
HAL_FLASH_OB_Unlock();
/* Erase all the option Bytes ***********************************************/
result = HAL_FLASHEx_OBErase();
if (result == HAL_OK)
{
config_new.WRPPage = ProtectedPAGE;
result = HAL_FLASHEx_OBProgram(&config_new);
}
return (result == HAL_OK ? FLASHIF_OK: FLASHIF_PROTECTION_ERRROR);
}
2016-07-13 07:13 AM
It does not work, I write any value in the register ProtectedPAGE, after resetting the meeting FLASH_WRPR register to 0xFFFFFFFF, all pages without protection. I think there is something wrong in the function HAL_FLASHEx_OBProgram. It's possible?