cancel
Showing results for 
Search instead for 
Did you mean: 

Managing dual bank boot on STM32L476

Christophe MARIN
Associate II

Hi community,

I'm currently struggling hard with the dual bank boot on the stm32L476:

  • I managed in setting BFB2 as needed, thru an utility such as ST-LINK (ok, for debug only, not for production...)
  • I cannot set it using DFU-USB read/write operations (this was my option A), the chip always denies access to Flash control registers and I could not point out why, so far. I tried using my own Java application thru libusbk driver, and dfu-util, with the same results.
  • I'm not sure on how to set it thru LL driver interface, at run-time of my application of my Stm32L476 (this is my option B)

Did anybody here managed in achieving my option B, if yes, where to find the small bit of code that would achieve that feature ?

1 REPLY 1
Christophe MARIN
Associate II

Well, I've been struggling hard again with ST's HAL documentation, and I'm now stucked there: as soon as I call the HAL_FLASHEx_OBProgram, I got an HAL_ERROR code in return (and option bytes are not writen, of course). Here's the code I'm trying:

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");
		HAL_FLASH_OB_Lock();
		HAL_FLASH_Lock();
		retCode = HAL_FLASH_OB_Launch();
		printf("\r\n\r\n BFB2 Validation, code=%02X",retCode);				
	}
	else
	{
		printf("\r\n\r\n BFB2 Configuration Failure, code=%02X",retCode);
	}
	
	printf("\r\n-------------------After modifications");
	HAL_FLASHEx_OBGetConfig(&optionBytes);	// Get current optionbytes configuration
	OptionByteDisplay(&optionBytes);   
}

Here's the ouput I'm getting:

*********************** 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

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

OPTION BYTES

 OptionType =          00000007

 WRPArea =             00000000

 WRPStartOffset =      000000FF

 WRPEndOffset =        00000000

 RDPLevel =            000000AA

 USERType =            00000080

 USERConfig =          FFAFF800

 PCROPConfig =         5C613118

 PCROPStartAddr =      00000000

 PCROPEndAddr =        00000000

Does anyone here have an idea on what I'm currently missing ?