2021-03-26 04:55 AM
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?