Skip to main content
Associate II
January 24, 2025
Question

Read protection on STM32L451CET6 stuck at level 1

  • January 24, 2025
  • 2 replies
  • 771 views

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

ST Employee
January 24, 2025

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

Associate II
January 28, 2025

I referred to this article only and it didn't solve my issue. The MSI frequency in my project is 4Mhz.

TDK
Super User
January 24, 2025

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""."
Associate II
January 25, 2025

Yes, I have no issue in chip erase. I just dont want to use Cube Programmer or ST link utility to do this. I want to do this through code.