2021-09-02 10:17 AM
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!
2021-09-02 11:06 AM
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.
2021-09-02 11:16 AM
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!
2021-09-02 11:45 AM
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();
2021-09-02 11:58 AM
Yep, HAL_FLASHEx_OB_DBankConfig is the right function to modify DBANK.
Should be able to recover with STM32CubeProgrammer, holding chip in reset.
2021-09-02 12:09 PM
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.
2021-09-02 12:16 PM
2021-09-02 12:59 PM
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.
2021-09-02 01:04 PM
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!
2023-06-01 08:32 AM
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!