cancel
Showing results for 
Search instead for 
Did you mean: 

Read protection on STM32L451CET6 stuck at level 1

Akshat-hu-vro
Visitor

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!

2 REPLIES 2
STea
ST Employee

Hello @Akshat-hu-vro ,

Could you please check this article note that the function for RDP regression is as follows:

void __attribute__((__section__(".RamFunc"))) RDP_Regression(void) {
      __disable_irq(); // Disable interrupts
      printf("Mass Erase Start\r\n");

      HAL_FLASH_Unlock();  // Unlock the flash memory
      HAL_FLASH_OB_Unlock(); // Unlock the option bytes

      *(__IO uint8_t*) OPTCR_BYTE1_ADDRESS = OB_RDP_LEVEL_0;

      HAL_FLASH_OB_Launch();
}

could you also check the MSI frequency value as the Option byte loading can fail if the MSI frequency is greater than 8 MHz see ERRTA section 2.2.21 .

 Regards

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
TDK
Guru

RDP 1 -> RDP 0 requires a chip erase, so your program will not run after that. Is that acceptable?

If you feel a post has answered your question, please click "Accept as Solution".