STM32WLE5CC MCU brick when trying to set Brownout Reset level
- January 31, 2022
- 4 replies
- 7021 views
Greetings
I have been trying to set the BOR option byte via software in a STM32WLE5CC (as part of a RAK3172 evaluation board), but even following various guides i have not been successful in this endeavor, often resulting in the MCU being bricked and requiring intervention via CubeProgrammer to reset the corrupted option bytes followed by reflashing of the firware via CubeIDE.
The code is as follows:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
MX_GPIO_Init();
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
SystemApp_Init();
/* USER CODE BEGIN 2 */
HAL_StatusTypeDef status;
FLASH_OBProgramInitTypeDef FLASH_Handle;
if ( (FLASH->OPTR & FLASH_OPTR_BOR_LEV_Msk) != OB_BOR_LEVEL_4){
HAL_FLASHEx_OBGetConfig(&FLASH_Handle);
FLASH_Handle.OptionType = OPTIONBYTE_USER;
FLASH_Handle.UserConfig = FLASH_Handle.UserConfig | (uint32_t)OB_BOR_LEVEL_4;
FLASH_Handle.UserType = OB_USER_BOR_LEV;
status= HAL_FLASH_Unlock();
status= HAL_FLASH_OB_Unlock();
status=HAL_FLASHEx_OBProgram(&FLASH_Handle);
if (status != HAL_OK)
{
APP_LOG(TS_OFF, VLEVEL_M, "Error during programming %x\r\n", (uint32_t)status);
return;
}
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
status=HAL_FLASH_OB_Launch();
if(status==HAL_ERROR){
APP_LOG(TS_OFF, VLEVEL_M, "Error during launch\r\n");
}
}
APP_LOG(TS_OFF, VLEVEL_M, "FLASH->OPTR is = %X\r\n", (uint32_t)READ_REG(FLASH->OPTR));
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
//APP_LOG(TS_OFF, VLEVEL_M, "Loop\r\n");
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}In this configuration, HAL_FLASH_OB_Launch returns HAL_ERROR rather than causing a reset of the MCU to load the new option bytes, thus resulting in the option bytes not being effectively written to the register, successive power cycling (with or without a debugger attached) show that the option bits were not overwrote
When removing both unlock functions, the MCU hangs/bricks with power cycling failing to resolve the issue, the RDP field is corrupted to FF and some other parts of the flash seems corrupted as well thus preventing reflashing via CubeIDE
Only intervention through CubeProgrammer to reset the option bytes followed by reflashing via CubeIDE has shown any effectiveness at recovering from said state.
Any suggestion in how to fix this issue? Thanks in advance.
Edit: just in case they might be needed, i attach images of the Option Bytes read from the ST-LINK
Edit #2: i have noticed that during the various write operations (es: in FLASH_OB_OptrConfig), the register value does not appear to be updated, whether this is a limitation of the debugger or of the printout via APP_LOG erroneously returning a old value i do not know