2024-08-23 11:06 AM
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.
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 🙂
2024-08-23 02:02 PM
> 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.
2024-08-23 02:40 PM
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.