2019-12-09 05:34 AM
Hello,
I'm trying to test RDP levels configuration on STM32H753.
Here is the code I've tested (on a Nucleo 743 with STM32CubeIDE):
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 )
{
/* Wait for User push-button press before starting the Communication */
while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET)
{
}
// toggle LED1 to show RDP level
BSP_LED_Toggle(LED1);
// 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);
}
}
}
else
{
/* Wait for User push-button press before starting the Communication */
while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET)
{
}
BSP_LED_Toggle(LED2);
// 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);
}
}
}
while (1)
{
}
}
The switch to Level 1 works fine. I can check that I am in Level 1 for example because the debugger doesn't work anymore and the LED 2 switches on. However the regression to Level 0 doesn't seem to work. I know the Flash is supposed to be erased but I cannot do anything after the "regression": cannot connect to a debugger or to STM32CubeProgrammer, no LED switches, etc...
Do you see something wrong in the code ?
2019-12-09 02:52 PM
Can you please check with CubeProgrammer with the option 'connect under reset', possibly after a power cycle of the board, what's the status of Option Bytes?
2019-12-09 03:12 PM
Pretty sure you'd need to power cycle. Reset doesn't clear option latching, protects against hacking attacks.
2019-12-09 11:53 PM
I had tried a power cycle, even if I didn't write it. But perhaps I didn't wait long enough for the Flash to be erased. What happens in case of tearing during the mass erase ? I have been working on this kind of issues and I know it can be tricky.
In the meantime I unlocked the chip through the USB bootloader and the BOOT0 pin. This time I cannot switch from level 0 to Level 1. I wonder if there isn't something wrong with my code. Step by step, I can see the register FLASH_OPTSR_PROG is correctly written, I set OPTSTART, wait for the operation to be done, I get no error message or timeout but the option byte is not written to OPTSR_CUR.
2019-12-10 01:32 AM
More clarifications on my previous message. I double checked at register level my code to switch from Level 0 to Level 1. After busy bit is cleared, I get no error message and when I read back OPTSR_CUR it still contains the old level 0 value.
BUT when I power off the chip and plug it to STCubeProgrammer, I can see that the chip is indeed in Level 1.
It looks like the RDP modification was taken into account in NVM but not reloaded to OPTSR_CUR. Does it make sense ?
2019-12-10 06:09 AM
I think I found out the problem. Looks it was due to the problem described in errata sheet: "Option byte loading can be done with the user wait-state configuration"
I changed the WS from 4 to 7 and it "fixed" the issue: I can switch from Level 0 to level 1 and then from Level 1 to Level 0 (with a power off in between).
There is still something strange: the change of RDP level seems to be taken into account immediatly when the option byte is programmed: the debug session is immediatly closed (on Keil through STLink). I thought the RDP level was active at the next POR or reset.