cancel
Showing results for 
Search instead for 
Did you mean: 

Using HAL LL to set BFB2 bit on STM32L4xx, help needed!!

Christophe MARIN
Associate II

Hi community,

I'm currently trying to set/reset the BFB2 bit from application program, under MBed, with a STM32L476RG.

Though setting/reseting BFB2 can be done properly thru ST-LINK, I did not manage in modifying the option bytes, using the HAL/LL drivers.

The code I've tried looks like:

void OptionByteSet(bool bfb2)
{
	int retCode;
	
	FLASH_OBProgramInitTypeDef optionBytes;	// Structure that holds the option bytes to be programmed
	
	printf("\r\n*********************** SETTING BFB2 ****************************");
	if(HAL_FLASH_Unlock()==HAL_OK) // Unlock the flash controls registers	
		printf("\r\n\tHAL_FLASH_Unlock -> OK");
	else
		printf("\r\n\tHAL_FLASH_Unlock -> Failure");
	
					
	if (HAL_FLASH_OB_Unlock()==HAL_OK)		// Unlock the option bytes controls registers
		printf("\r\n\tHAL_FLASH_OB_Unlock -> OK");
	else
		printf("\r\n\tHAL_FLASH_OB_Unlock -> Failure");
 
	HAL_FLASHEx_OBGetConfig(&optionBytes);	// Get current optionbytes configuration
	
	printf("\r\n-------------------Before modifications");
	OptionByteDisplay(&optionBytes);
	
	optionBytes.OptionType = OPTIONBYTE_USER;	
	optionBytes.USERType = OB_USER_BFB2;
	
	if (bfb2)								// Set the bfb2 option bit as needed
		optionBytes.USERConfig |= OB_BFB2_ENABLE;
	else
		optionBytes.USERConfig &= ~OB_BFB2_ENABLE;
	
	
	printf("\r\n-------------------Projected modifications");
	OptionByteDisplay(&optionBytes);
	
	if ((retCode=HAL_FLASHEx_OBProgram(&optionBytes))==HAL_OK)
		printf("\r\n\r\n BFB2 Configuration OK");		
	else
		printf("\r\n\r\n BFB2 Configuration Failure, code=%02X",retCode);
	
	HAL_FLASH_OB_Lock();
	HAL_FLASH_Lock();
	retCode = HAL_FLASH_OB_Launch();
	printf("\r\n\r\n BFB2 Validation, code=%02X",retCode);	
			
	printf("\r\n-------------------After modifications");
	HAL_FLASHEx_OBGetConfig(&optionBytes);	// Get current optionbytes configuration
	OptionByteDisplay(&optionBytes);
}

The output I get is the following:

*********************** SETTING BFB2 ****************************

       HAL_FLASH_Unlock -> OK

       HAL_FLASH_OB_Unlock -> OK

-------------------Before modifications

OPTION BYTES

 OptionType =          00000007

 WRPArea =             00000000

 WRPStartOffset =      000000FF

 WRPEndOffset =        00000000

 RDPLevel =            000000AA

 USERType =            20000014

 USERConfig =          FFAFF800

 PCROPConfig =         5C613118

 PCROPStartAddr =      00000000

 PCROPEndAddr =        00000000

-------------------Projected modifications

OPTION BYTES

 OptionType =          00000004

 WRPArea =             00000000

 WRPStartOffset =      000000FF

 WRPEndOffset =        00000000

 RDPLevel =            000000AA

 USERType =            00000080

 USERConfig =          FFBFF800

 PCROPConfig =         5C613118

 PCROPStartAddr =      00000000

 PCROPEndAddr =        00000000

 BFB2 Configuration Failure, code=01

 BFB2 Validation, code=00

-------------------After modifications

OPTION BYTES

 OptionType =          00000007

 WRPArea =             00000000

 WRPStartOffset =      000000FF

 WRPEndOffset =        00000000

 RDPLevel =            000000AA

 USERType =            00000080

 USERConfig =          FFAFF800

 PCROPConfig =         5C613118

 PCROPStartAddr =      00000000

 PCROPEndAddr =        00000000

What am I missing there ? I'm stuck at this point, and cannot either point out why nor think about anything else to try, since two weeks now.

1 REPLY 1
Theophile Alary
Associate

Hello,

I've had some problem to modify the BFB2 bit using examples found on the internet.

Finally, I managed to get it to work by clearing the FlashOB_ProgramInitTypedef structure in order to make sure only the BFB2 bit will be written.

Below is the working code which makes a toggle of the BFB2 :

FLASH_OBProgramInitTypeDef  optionBytes;

uint8_t setOrReset;

__disable_irq();

HAL_FLASH_Unlock();

HAL_FLASH_OB_Unlock();

HAL_FLASHEx_OBGetConfig(&optionBytes); // Get current optionbytes configuration

// If BFB2 set, we will reset it

if ( optionBytes.USERConfig & OB_BFB2_ENABLE ){

setOrReset = 0;

}else{

setOrReset = 1;

}

// Clean structure before using it for option bytes writing :

memset((void*)&optionBytes,0,sizeof(optionBytes));

optionBytes.OptionType = OPTIONBYTE_USER;

optionBytes.USERType = OB_USER_BFB2;

if (setOrReset // Set the bfb2 option bit as needed

optionBytes.USERConfig = OB_BFB2_ENABLE;

else

optionBytes.USERConfig = OB_BFB2_DISABLE;

if (HAL_FLASHEx_OBProgram(&optionBytes)==HAL_OK)

{

HAL_FLASH_OB_Launch();

}

HAL_FLASH_OB_Lock();

HAL_FLASH_Lock();