AnsweredAssumed Answered

I have a problem with Option Byte

Question asked by bruno.vincenzo on Jul 13, 2016
Latest reply on Jul 13, 2016 by bruno.vincenzo
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);
}

Outcomes