cancel
Showing results for 
Search instead for 
Did you mean: 

Option Byte Programming Fail -> HAL_FLASH_OB_Launch() returns HAL Error

AAgar.2
Associate III

I am trying to flash options bytes on a STM32H7 via the code below. Both the code snippets return HAL_ERROR from the HAL_FLASH_OB_Launch(). What am I doing wrong?

I am referring to the following STM32Cube example code: https://github.com/STMicroelectronics/STM32CubeH7/blob/master/Projects/STM32H743I-EVAL/Applications/IAP/IAP_Main/Src/flash_if.c#L301

This is the one to setup RDP:

        if ( HAL_FLASH_Unlock() != HAL_OK ) { goto flash_lock; }
	if ( HAL_FLASH_OB_Unlock() != HAL_OK ) { goto ob_lock; }
 
	FLASH_OBProgramInitTypeDef ob_struct = {0};
 
	// RDP on Flash bank 2
	ob_struct.Banks = FLASH_BANK_2;
	ob_struct.OptionType = OPTIONBYTE_RDP;
	ob_struct.RDPLevel = OB_RDP_LEVEL_1;
 
	// Program options bytes
	HAL_FLASHEx_OBProgram(&ob_struct);
	if ( HAL_FLASH_OB_Launch() != HAL_OK) { goto ob_lock; }
 
	// Lock memory
	HAL_FLASH_OB_Lock();
	HAL_FLASH_Lock();
 
	return OK;
ob_lock:
	HAL_FLASH_OB_Lock();
flash_lock:
	HAL_FLASH_Lock();
	return ERR_PROTECT;

 This is the one to setup a secure area:

        if ( HAL_FLASH_Unlock() != HAL_OK ) { goto flash_lock; }
	if ( HAL_FLASH_OB_Unlock() != HAL_OK ) { goto ob_lock; }
 
	FLASH_OBProgramInitTypeDef ob_struct = {0};
 
	// Secure Area on bank 1 without erase when RDP reset
	ob_struct.Banks = FLASH_BANK_1;
	ob_struct.OptionType = OPTIONBYTE_SECURE_AREA;
	ob_struct.USERType = OB_USER_SECURITY;
	ob_struct.USERConfig = OB_SECURITY_ENABLE;
	ob_struct.SecureAreaConfig = OB_SECURE_RDP_NOT_ERASE;
 
	// First 3 sectors
	ob_struct.SecureAreaStartAddr = ADDR_FLASH_BANK_1_SECTOR_0_START;//0x000;
	ob_struct.SecureAreaEndAddr = ADDR_FLASH_BANK_1_SECTOR_3_START;//0x5DB;
 
	// Program options bytes
	HAL_FLASHEx_OBProgram(&ob_struct);
	if ( HAL_FLASH_OB_Launch() != HAL_OK) { goto ob_lock; }
 
	// Lock memory
	HAL_FLASH_OB_Lock();
	HAL_FLASH_Lock();
 
	return OK;
ob_lock:
	HAL_FLASH_OB_Lock();
flash_lock:
	HAL_FLASH_Lock();
	return ERR_PROTECT;

2 REPLIES 2
TDK
Guru

Be aware that RDP is a global flag and not a per-bank setting.

Look at the return value of HAL_FLASHEx_OBProgram to make sure it's successful.

If you feel a post has answered your question, please click "Accept as Solution".
AAgar.2
Associate III

Thanks for the response.

1) To confirm I do not need to set ob_struct.Banks = FLASH_BANK_1?

2) I have confirmed in both code samples the HAL_FLASHEx_OBProgram returns HAL_OK...

3) Do you have any other ideas for debugging this?

Appreciate your support.