Skip to main content
Visitor II
September 11, 2025
Question

OB programming issue on the custom bootloader

  • September 11, 2025
  • 1 reply
  • 368 views

Hello,

To protect the device's software, I am updating the RDP values in my own bootloader software, and to protect it in the event of a sudden power outage, I am also updating the BOR values. I am performing these updates only using HAL APIs. Additionally, I am writing the option bytes one by one. During development and testing, this worked flawlessly. However, the device has now entered serial production, and we are programming in large quantities. So far, 200 MCUs have been programmed, and approximately 50 of them ended up with an RDP value of 0xFF and the BOR value not set. I urgently need assistance regarding this issue. Production has been halted and is waiting for this problem to be resolved. Please help me.

I am adding the code block where I perform this operation below.

 

 /* USER CODE BEGIN 2 */

#ifndef DEBUG
 FLASH_OBProgramInitTypeDef ob_init;

 // Unlock Flash and Option Bytes
 HAL_FLASH_Unlock();
 HAL_FLASH_OB_Unlock();

 // Get current configuration
 HAL_FLASHEx_OBGetConfig(&ob_init);

 // Check current RDP level
 uint32_t curr_rdp_level = ob_init.RDPLevel;

 // Only proceed if either BOR or RDP needs to be changed
 if (OB_RDP_LEVEL_1 != curr_rdp_level)
 {
 if ((ob_init.USERConfig & OB_BOR_LEVEL_2) != OB_BOR_LEVEL_2)
 {
 	 ob_init.OptionType |= OPTIONBYTE_USER;
 	 ob_init.USERType = OB_USER_BOR_LEV;
 	 ob_init.USERConfig &= ~(FLASH_OPTR_BOR_LEV);
 	 ob_init.USERConfig |= OB_BOR_LEVEL_2;
 if (HAL_FLASHEx_OBProgram(&ob_init) != HAL_OK)
 {
 Error_Handler();
 }
 }

 ob_init.OptionType = OPTIONBYTE_RDP;
 // Configure RDP Level 1
 ob_init.RDPLevel = OB_RDP_LEVEL_1;

 // Program the option bytes
 if (HAL_FLASHEx_OBProgram(&ob_init) != HAL_OK)
 {
 Error_Handler();
 }

 if (HAL_FLASH_OB_Launch() != HAL_OK)
 {
 	 HAL_FLASH_OB_Lock();
 	 HAL_FLASH_Lock();
 	 Error_Handler();
 }

	 HAL_FLASH_OB_Lock();
	 HAL_FLASH_Lock();
	 HAL_Delay(100);
	 HAL_NVIC_SystemReset();
 }
 else
 {
	 HAL_FLASH_OB_Lock();
	 HAL_FLASH_Lock();
 }
#endif

 

pi_issue_2.png

    This topic has been closed for replies.

    1 reply

    Super User
    September 11, 2025

    RDP value of 0xFF indicates the option byte process was interrupted (potentially due to loss of power) and perhaps the option bytes contain invalid data. The complementary option bytes must be consistent.

    I recommend initializing ob_init with exactly and only what you want to modify. Take a look at HAL_FLASHEx_OBProgram to see what it needs to modify those two values.

    stm32g4xx-hal-driver/Src/stm32g4xx_hal_flash_ex.c at ab70f83eb0c372e388cc760284c66debf3d5fe5f · STMicroelectronics/stm32g4xx-hal-driver