2022-05-17 3:36 PM
Hello!
I try to use dual bank option on the stm32l476 (256kB) for implementing a custom bootloader. I want just read new program (bin) by custom protocol and write it to another bank. After that I want to switch the banks, reset mcu and start the new program (from another bank).
Sounds easy!
My actions:
I built the program for bank1, load it to the address 0x08000000.
Then I loaded the same program to the bank 2 by address 0x08020000. (either with the st-link utility or just by writing to the flash directly from my program, tried both of variants).
After that I set option byte bfb2 from 0 to 1 (also either with st-link or just from program). But my mcu does not work at all.
I`ve compared the program in both banks and original .bin file with the help of the st-link utility and found that there is no difference between it (no corruption during program).
I`ve even tried to build program for bank 2 (with offset), but it also did not help.
I`ve noted that when I change the bfb2 bit, the value in the 0x4 address (entry point) also changes to strange address 0x1FFF6145 instead of 0x08005DE9 (my real entry point). I understand that mcu reads this address as an entry point and traps. But why does this address change to something strange one?
From the bank1 (bfb2=0) program works well.
Please, can anybody say what I do wrong?
Configurations: Boot0 connected to the ground. DaulBank=1, nBOOT1=1, FB_MODE=0
Best regards, Klynkin Kirill.
2022-05-17 5:36 PM
> strange address 0x1FFF6145
This looks like the internal ROM bootloader
2022-05-18 12:22 AM
Yes, it seem to be true.
But it`s not obviously why? What I did wrong?
2022-05-23 2:28 PM
UPD:
The problem was with the SCB->VTOR register.
It`s important to write the actual address of your interrupt table. (default value is 0)
If you don`t fill the SCB->VTOR with the corresponding value and leave it by default, you will receive the following troubles:
When you boot from the bank 1 (bfb2 bit is set to 0) the flash memory is mapped to the 0x0 address. And the vector table which is stored in your flash (0x08000000) is mapped to the 0x0. That`s why it works fine for booting form bank1
But when you boot from bank 2 (bfb 2 is set to 1), the system memory is mapped to the 0x0 address. And mcu can`t find the right reset vector table.
To avoid it, write the right offset for the vector table to SCB->VTOR register (not from 0x0 address where different memory might be mapped, but from the real FLASH)
For me it was just one code line SCB->VTOR = 0x08000000
2024-02-25 9:40 PM
I also encountered the same problem but assume we don't have any firmware loaded into bank1. then what address should we use for the VTOR reg? I want to run my firmware only from bank2 and i have nothing loaded in bank1 and when i set the VTOR to point to bank2 start (0x08018000 for STM32L073RZ) it doesn't work.
2025-12-04 11:32 AM - edited 2025-12-04 11:34 AM
Apologies for the thread exhumation - I ran into the same problem with BFB2 and VTOR on L496 and figured I'd post this in case other unfortunate folks encounter it in future.
SCB->VTOR is not set by default in the CubeMX v 1.19 generated system_stm32l4xx.c (!!)
The key paragraph in RM0351 is in 2.6.1: (esp. the bit in bold).
To select boot from Flash memory bank 2, set the BFB2 bit in the user option bytes.
When this bit is set and the boot pins are in the “boot from main Flash memory” configuration,
the device boots from system memory, and the boot loader jumps to execute the user
application programmed in Flash memory bank 2. The system memory remains aliased to
the boot memory space (0x0000 0000).
This means that if VTOR is left at 0x00000000 the code will execute fine from bank 1, but when running from bank 2 the interrupt vectors addresses will be based on the system memory (ROM bootloader) - so it crashes and restarts on the first interrupt.
I had to uncomment #define USER_VECT_TAB_ADDRESS in system_stm32l4xx.c so that SCB->VTOR is set to 0x08000000 in SystemInit().