cancel
Showing results for 
Search instead for 
Did you mean: 

Set Read Out Protection (Level1) in STM32F446

JSaez
Associate II

Hi folks,

I am trying to set the RDP option bytes to have my code in flash read-protected. Instead of using ST-Link Utility, I want to set the read-out protection in the code itself (specifically, inside a self-designed bootloader). For this purpose, I use the flash functions provided by the FLASH HAL library. The problem is that I am facing a Hard Fault Exception, right after the option start bit is set (inside the function HAL_FLASH_OB_Launch). According to the reference manual, the sequence for setting the option bytes is the following:

  1. Unlock the OB
  2. Write the new OB value
  3. Set the option start bit
  4. Wait until the operation is completed
  5. Lock the OB

Still, at I mentioned before, at the step 4, an exception is triggered (MPU or Execute never XN default memory map access violation has occurred on an instruction fetch). It is important to say though that after a reset, the RDP is activated (by checking with ST-Link-Utility).

Probably I am missing something. Has anyone successfully set the RDP in code? Or has faced the same problem as me?

Many Thanks

Jorge

    FLASH_OBProgramInitTypeDef obConfig;
 
    HAL_FLASHEx_OBGetConfig(&obConfig);
 
    if (obConfig.RDPLevel == OB_RDP_LEVEL_0) {
        // this is first time we run mcu after flashing firmware
        obConfig.RDPLevel = OB_RDP_LEVEL_1;
        HAL_FLASH_Unlock();
        HAL_FLASH_OB_Unlock();
        HAL_FLASHEx_OBProgram(&obConfig);
        HAL_FLASH_OB_Launch();
        HAL_FLASH_OB_Lock();
        HAL_FLASH_Lock();
    }

4 REPLIES 4
Uwe Bonnes
Principal II

You probably have to init obConfig with sensible values...

Thanks for your reply. What do you mean with sensible values?

obConfig.RDPLevel = OB_RDP_LEVEL_1;

seems like a sensible default.

Did you check your code against  FLASH_If_WriteProtectionConfig() e.g. in

Projects/STM32446E_EVAL/Applications/IAP/IAP_Main/Src/flash_if.c

?

Well, the only difference I see is at this function the old OB config is read first (besides, it is only about changing the write protection, not read or BOR). I set then the OptionType to OPTIONBYTE_RDP but the result is sadly the same ( Hard Fault exception).

Actually, I have already tried to modify the write protection and it worked perfectly. The problem comes when I want to increase the read out protection from level 0 to level 1. With ST-Link works it can be changed without any issue though.

HAL_StatusTypeDef FLASH_If_WriteProtectionConfig(uint32_t modifier)
{
  uint32_t ProtectedSECTOR = 0xFFF;
  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 = modifier;
 
  /* 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 ****************************************/
  ProtectedSECTOR = config_old.WRPSector | FLASH_SECTOR_TO_BE_PROTECTED;
 
  /* Unlock the Flash to enable the flash control register access *************/ 
  HAL_FLASH_Unlock();
 
  /* Unlock the Options Bytes *************************************************/
  HAL_FLASH_OB_Unlock();
  
  config_new.WRPSector    = ProtectedSECTOR;
  result = HAL_FLASHEx_OBProgram(&config_new);
  
  return result;
}