cancel
Showing results for 
Search instead for 
Did you mean: 

How to protect my application code read from MCU?

parth kothiya
Senior

I want to protect my code by writing OB using HAL

bool SetFlashReadProtection(bool state)
{
  FLASH_OBProgramInitTypeDef OptionsBytesStruct = {0};
  HAL_FLASHEx_OBGetConfig(&OptionsBytesStruct);
  
  if(state == true)
  {
    if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_0)
    {
      OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
      OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_1;
      
      HAL_FLASH_OB_Unlock();
      
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
      {
        HAL_FLASH_OB_Lock();
        
        return false;
      }
      
      HAL_FLASH_OB_Lock();
    }
  }
  else
  {
    if(OptionsBytesStruct.RDPLevel == OB_RDP_LEVEL_1)
    {
      OptionsBytesStruct.OptionType = OPTIONBYTE_RDP;
      OptionsBytesStruct.RDPLevel   = OB_RDP_LEVEL_0;
      
      HAL_FLASH_OB_Unlock();
      
      if(HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK)
      {
        HAL_FLASH_OB_Lock();
        
        return false;
      }
      
      HAL_FLASH_OB_Lock();
    }
  }
  
  return true;
}

and call this SetFlashReadProtection(true) function inside void main but still my code is redable so at which place i can call this function?

0 REPLIES 0