2019-11-06 06:03 AM
I am working with the STM32F446 microcontroller and I want embed the option bytes into my source code so I wrote the following and call it first thing in main():
static FLASH_OBProgramInitTypeDef __attribute__((aligned(4))) CORRECT_OPTION_BYTES = {
0, // OptionType
OB_WRPSTATE_DISABLE, // WRPState
0xFF, // WRPSector
FLASH_BANK_1, // Banks
OB_RDP_LEVEL_0, // RDPLevel
OB_BOR_LEVEL3, // BORLevel
OB_IWDG_SW | OB_STOP_NO_RST | OB_STDBY_NO_RST // USERConfig
};
void updateOptionBytesIfNecessary() {
FLASH_OBProgramInitTypeDef __attribute__((aligned(4))) currentOptionBytes;
HAL_FLASHEx_OBGetConfig( ¤tOptionBytes);
CORRECT_OPTION_BYTES.OptionType = 0;
if ((currentOptionBytes.WRPState != CORRECT_OPTION_BYTES.WRPState) ||
(currentOptionBytes.WRPSector != CORRECT_OPTION_BYTES.WRPSector)) {
CORRECT_OPTION_BYTES.OptionType |= OPTIONBYTE_WRP;
}
if (currentOptionBytes.RDPLevel != CORRECT_OPTION_BYTES.RDPLevel) {
CORRECT_OPTION_BYTES.OptionType |= OPTIONBYTE_RDP;
logDebugNullTermString(0, 0, " RDP ");
}
if (currentOptionBytes.USERConfig != CORRECT_OPTION_BYTES.USERConfig) {
CORRECT_OPTION_BYTES.OptionType |= OPTIONBYTE_USER;
}
if (currentOptionBytes.BORLevel != CORRECT_OPTION_BYTES.BORLevel) {
CORRECT_OPTION_BYTES.OptionType |= OPTIONBYTE_BOR;
}
if (CORRECT_OPTION_BYTES.OptionType != 0) {
if (HAL_FLASH_OB_Unlock() != HAL_OK) {
// Error!
}
if (HAL_FLASHEx_OBProgram(&CORRECT_OPTION_BYTES) != HAL_OK) {
// Error!
}
HAL_FLASH_OB_Launch();
HAL_FLASH_OB_Lock();
}
}
When I use STM32 ST-Link Utility to fiddle with the option bytes, I find that the code above works great for everything except if I change the Read Out Protection to level 1 or 2. Whenever I change the Read Out Protection to level 1 or 2, the microcontroller doesn't seem to execute properly afterwards (I don't get a welcome message over my UART) but I can still connect with STM32 ST-Link Utility to find out that the Read Out Protection hasn't changed. Does anybody know what's wrong with my code?