cancel
Showing results for 
Search instead for 
Did you mean: 

32F417 - the definitive way to set RDP2?

PHolt.1
Senior III

Is this the right way?

 

if (FLASH_OB_GetRDP() != OB_RDP_LEVEL_2)
{
   HAL_FLASH_Unlock();  // Unlock the flash memory
   HAL_FLASH_OB_Unlock(); // Unlock the option bytes
   *(__IO uint8_t*) OPTCR_BYTE1_ADDRESS = OB_RDP_LEVEL_2;  // Set RDP2
   HAL_FLASH_OB_Launch();  // Activate it
   HAL_FLASH_OB_Lock();
   HAL_FLASH_Lock();
}

 

You have to test if RDP2 is already set otherwise you get a hard fault - and a bricked unit unless you have some recovery mode.

The above uses existing ST-provided Cube IDE code. There are other examples which are more direct e.g.

HAL_FLASH_Lock() is written as FLASH->CR |= FLASH_CR_LOCK but it is the same code. Lots of people going around in circles e.g.

https://community.st.com/t5/stm32-mcus-embedded-software/what-causes-hal-flash-ob-unlock-to-call-hardfaulthandler-it/td-p/59977/page/3

Can anyone see a problem with my code? I've tested it and it appears to set RDP2 (the debugger stops working with error 19 or some such) but not yet with the IF statement around it, because each time I do it I blow away one board 🙂

2 REPLIES 2
Pavel A.
Evangelist III

> You have to test if RDP2 is already set otherwise you get a hard fault

Because setting RDP2 prevents change of option bytes. This is documented.

 

 

PHolt.1
Senior III

Sure. But do you think the above code is right? In particular, in the right order.

There is also a question mark regarding whether setting RDP2 resets the CPU. Some found it does. The above code does not do that for me, which is better.