cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L5 - Single Bank Flash Mode

MBand.3
Associate III

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!

9 REPLIES 9
TDK
Guru

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
Associate III

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
Associate III

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
Guru

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
Associate III

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.

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".

Got it, thank you! I found an ST-Link and was able to change the option bits, but I'm still unable to write to flash, sadly.

Ah ha! (Sorry, I keep responding then happening upon a solution) I had changed my flash security settings when trying to change the DBANK bit, and it appears that I set all of the flash to non-secure, and therefore my secure code couldn't access flash. Resetting the "Secure Area" settings to the factory defaults (0x00/0x7F for both) made it so I could program again.

Thank you for all your help!

RArte.1
Associate II

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!