Skip to main content
parth kothiya
Associate III
March 26, 2021
Question

How to protect my application code read from MCU?

  • March 26, 2021
  • 0 replies
  • 426 views

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?

    This topic has been closed for replies.