Skip to main content
MBand.3
Associate III
September 2, 2021
Question

STM32L5 - Single Bank Flash Mode

  • September 2, 2021
  • 5 replies
  • 3141 views

I'm attempting to switch to single-bank flash mode on an STM32L562ZET6Q (512k Flash), but so far I've been unable to get the Flash OPTR to operate as the reference manual claims.

I used the normal process for modifying the OPTR:

 HAL_FLASH_Unlock();
 HAL_FLASH_OB_Unlock();
 HAL_FLASHEx_OBGetConfig(&flash_ob);
 flash_ob.OptionType = OPTIONBYTE_USER;
 flash_ob.USERType = OB_USER_DBANK;
 flash_ob.USERConfig = OB_DBANK_128_BITS;
 HAL_FLASHEx_OBProgram(&flash_ob);
 HAL_FLASH_OB_Launch();
 HAL_FLASH_OB_Lock();
 HAL_FLASH_Lock();

This, however, did not change the Flash OPTR on the next boot. I looked into it a bit more and discovered that the FLASH_OB_UserConfig function in stm32l5xx_hal_flash_ex.c doesn't have any code to handle the DBANK bit. I searched the entire stm32l5 HAL codebase and couldn't find anywhere OB_USER_DBANK is actually referenced (except for where it's defined).

Is there another way I'm supposed to be setting the DBANK bit?

Thanks!

This topic has been closed for replies.

5 replies

TDK
September 2, 2021

It appears to be an omission in HAL. You can simply set it manually. Perhaps we're missing something as to why it's not there.

FLASH->OPTR |= OB_USER_DBANK;

They do implement OB_USER_DUALBANK which is confusingly not the same as OB_USER_DBANK.

https://github.com/STMicroelectronics/STM32CubeL5/blob/1a04696d5732626c678dea940eda2f33b320ab15/Drivers/STM32L5xx_HAL_Driver/Src/stm32l5xx_hal_flash_ex.c#L1055

"If you feel a post has answered your question, please click ""Accept as Solution""."
MBand.3
MBand.3Author
Associate III
September 2, 2021

I've also found HAL_FLASHEx_OB_DBankConfig in the flash ramfuncs (link), which seems like it may do what I want, but I haven't managed to get it to work yet :) I'll update back here if/when I figure it out.

Thank you!

MBand.3
MBand.3Author
Associate III
September 2, 2021

Good news and bad news: I got the HAL_FLASHEx_OB_DBankConfig function to execute, but (1) the DBANK bit is still 1 and (2) I can no longer run anything on the board.

This is the code that you probably shouldn't run unless you're ok using the ST HAL to brick your board: :)

HAL_FLASH_Unlock();
 HAL_FLASH_OB_Unlock();
 
 HAL_FLASHEx_OBGetConfig(&flash_ob);
 flash_ob.OptionType = OPTIONBYTE_WMSEC;
 flash_ob.WMSecConfig = OB_WMSEC_AREA1 | OB_WMSEC_SECURE_AREA_CONFIG;
 flash_ob.WMSecStartPage = 0x7F;
 flash_ob.WMSecEndPage = 0x00;
 flash_ob.WMHDPEndPage = 0x00;
 HAL_FLASHEx_OBProgram(&flash_ob);
 flash_ob.WMSecConfig = OB_WMSEC_AREA2 | OB_WMSEC_SECURE_AREA_CONFIG;
 HAL_FLASHEx_OBProgram(&flash_ob);
 
 HAL_FLASHEx_OB_DBankConfig(OB_DBANK_128_BITS);
 
 HAL_FLASH_OB_Launch();
 HAL_FLASH_OB_Lock();
 HAL_FLASH_Lock();

TDK
September 2, 2021

Yep, HAL_FLASHEx_OB_DBankConfig is the right function to modify DBANK.

Should be able to recover with STM32CubeProgrammer, holding chip in reset.

"If you feel a post has answered your question, please click ""Accept as Solution""."
MBand.3
MBand.3Author
Associate III
September 2, 2021

Do you know what doc I should be looking for recovery instructions in? I'm working with IAR and an I-Jet, so I'm guessing I'll need something ST-specific if I have any hope of unbricking.

TDK
September 2, 2021
Not sure with your setup. With ST-Link and STM32CubeProgrammer, you select connect under reset, then connect, then modify option bytes or erase the chip.
"If you feel a post has answered your question, please click ""Accept as Solution""."
RArte.1
Associate
June 1, 2023

Hi all,

How did you modify DBANK bit through HAL_FLASHEx_OB_DBankConfig()?

I have tried without expected results.

I follow steps from HAL documentation, but the condition never match the condition below.

if(reg > FLASH->PCROP2ER)

First I change RDP level from 0 to 1. Then I change RDP level from 1 to 0. And, finally I executed function that change from DUAL BANK to SINGLE BANK.

Below you can find the code.

 // Set level 1 RDP and check it
 op_init.RDPLevel = OB_RDP_LEVEL_1;
 HAL_FLASHEx_OBProgram(&op_init);
 HAL_FLASHEx_OBGetConfig(&op_init);
 if (op_init.RDPLevel != OB_RDP_LEVEL_1) return E_FLASH_RES_ERROR;
 
 // Set level 0 RDP and check it
 op_init.RDPLevel = OB_RDP_LEVEL_0;
 HAL_FLASHEx_OBProgram(&op_init);
 HAL_FLASHEx_OBGetConfig(&op_init);
 if (op_init.RDPLevel != OB_RDP_LEVEL_0) return E_FLASH_RES_ERROR;
 
 // Perform Single Bank configuration
 HAL_FLASHEx_OB_DBankConfig(OB_DBANK_128_BITS);

Team, thanks in advance!