I'm playing with RDP Levels on a STM32H743 (Nucleo board, STM32CubeIDE).
Here is the code I've tested:
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
OBInit.Banks = FLASH_BANK_1;
HAL_FLASHEx_OBGetConfig(&OBInit);
RdpLevel = OBInit.RDPLevel;
BSP_PB_Init(BUTTON_USER,BUTTON_MODE_GPIO);
BSP_LED_Init(LED1);
if ( RdpLevel == OB_RDP_LEVEL_0 )
{
// toggle LED1 to show RDP level
BSP_LED_Toggle(LED1);
/* Wait for User push-button press before starting the Communication */
while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET);
// program OB
HAL_FLASH_OB_Unlock();
HAL_FLASH_Unlock();
OBInit.OptionType = OPTIONBYTE_RDP;
OBInit.RDPLevel = OB_RDP_LEVEL_1;
HAL_FLASHEx_OBProgram(&OBInit);
/* Start the Option Bytes programming process */
if (HAL_FLASH_OB_Launch() != HAL_OK)
{
/* User can add here some code to deal with this error */
while (1)
{
BSP_LED_Toggle(LED1);
HAL_Delay(200);
}
}
}
else
{
BSP_LED_Toggle(LED2);
/* Wait for User push-button press before starting the Communication */
while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET);
// program OB
HAL_FLASH_OB_Unlock();
HAL_FLASH_Unlock();
OBInit.OptionType = OPTIONBYTE_RDP;
OBInit.RDPLevel = OB_RDP_LEVEL_0;
HAL_FLASHEx_OBProgram(&OBInit);
/* Start the Option Bytes programming process */
if (HAL_FLASH_OB_Launch() != HAL_OK)
{
/* User can add here some code to deal with this error */
while (1)
{
BSP_LED_Toggle(LED2);
HAL_Delay(200);
}
}
}
while (1)
{
}
}
The switch to Level 1 works fine. I see that the Level is taken into account (at next reset the debugger is not working any more). The LED shows also the level.
But when I try to come back to Level 0 I'm not sure what's going on, since I cannot debug: I know a full erase of the flash occurs. But I expect that after that I'm able to connect through the debugger. but it's not the case and the software doesn't seem to run anymore.
Is there something wrong in this piece of code ?
EDIT: i tried to power off the board after the regression