Read protection on STM32L451CET6 stuck at level 1
I am using a stm32l451cet6 micro controller chip in my PCB. I want to make the flash memory read protected. For this I use the following code snippet -
while (HAL_FLASH_Unlock() != HAL_OK) {
;
}
while (HAL_FLASH_OB_Unlock() != HAL_OK) {
;
}
OptionsBytesStruct.OptionType = OPTIONBYTE_RDP; //Configure the RDP
OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_1;
while (HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
;
}
HAL_FLASH_OB_Launch();
Now this works perfectly and the board goes to level 1 protection. Now after a button press I want to change the read protection to level 0 from level 1. I use the following code snippet to change the RDP level back to 0.
void __attribute__((__section__(".RamFunc"))) RDP_Regression(void) {
__disable_irq(); // Disable interrupts
while (HAL_FLASH_Unlock() != HAL_OK) {
;
}
while (HAL_FLASH_OB_Unlock() != HAL_OK) {
;
}
OptionsBytesStruct.OptionType = OPTIONBYTE_RDP; //Configure the RDP
OptionsBytesStruct.RDPLevel = OB_RDP_LEVEL_0;
while (HAL_FLASHEx_OBProgram(&OptionsBytesStruct) != HAL_OK) {
;
}
HAL_FLASH_OB_Launch();
}
But this code does not bring back the RDP level back to 0. The board is sill in level 1 protection. I tried removing the SRAM attribute, but it still doesn't work. And once in level 1 protection, the board does not execute the LED blinking and buzzer operations. The code works fine without the read protection. What am I doing wrong? Please help!
