cancel
Showing results for 
Search instead for 
Did you mean: 

Why do we need to explicitely set VTOR for dual bank if bank addresses are swapped?

themarcman
Associate III

My understanding is that when setting BFB2, Bank 2 is remapped to 0x0800 0000.

Why should we need to set VTOR on reset if thanks to remapping it should interact the same?

Meaning I expect the start of my vector table for app2 to be at the same address as app1 since I have not modified it.

I had to uncomment #define USER_VECT_TAB_ADDRESS so that 

SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;

would run during the SystemInit function at reset.

For context I am compiling two programs with little difference in code and with the same linker script.

 

/*!< Uncomment the following line if you need to relocate the vector table
     anywhere in Flash or Sram, else the vector table is kept at the automatic
     remap of boot address selected */
 #define USER_VECT_TAB_ADDRESS

#if defined(USER_VECT_TAB_ADDRESS)
/*!< Uncomment the following line if you need to relocate your vector Table
     in Sram else user remap will be done in Flash. */
/* #define VECT_TAB_SRAM */
#if defined(VECT_TAB_SRAM)
#define VECT_TAB_BASE_ADDRESS   SRAM_BASE       /*!< Vector Table base address field.
                                                     This value must be a multiple of 0x200. */
#else
#define VECT_TAB_BASE_ADDRESS   FLASH_BASE      /*!< Vector Table base address field.
                                                     This value must be a multiple of 0x200. */
#endif /* VECT_TAB_SRAM */
#if !defined(VECT_TAB_OFFSET)
#define VECT_TAB_OFFSET         0x00000000U     /*!< Vector Table offset field.
                                                     This value must be a multiple of 0x200. */

 

void SystemInit(void)
{
  /* FPU settings ------------------------------------------------------------*/
  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  #endif

#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  SystemInit_ExtMemCtl(); 
#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */

  /* Configure the Vector Table location -------------------------------------*/
#if defined(USER_VECT_TAB_ADDRESS)
  SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#endif /* USER_VECT_TAB_ADDRESS */
}

 

Thank you!

7 REPLIES 7
TDK
Super User

Setting VTOR explicitly has nothing to do with bank remapping. You need it to point to the right address. For some reason I have never understood, the default behavior of CubeMX changed a few years ago to generate code that does not set VTOR. Previously, it did. Uncommenting USER_VECT_TAB_ADDRESS fixed this and restores the old behavior.

 

If bank 2 is mapped at 0x08000000, and that's the code you want to run, VTOR needs to be set to 0x08000000.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for replying TDK,

Why does bank 1 boot correctly without explicit mapping then ?

When according to PM0214, VTOR = 0x0 at reset.

That would alias it to 0x0800 0000 so bank1 or bank2 depending on BFB2.
Could it be that VTOR is placed in system memory initially since apparently that's what we boot from setting BFB2?

Meaning 0x0 is aliased to system memory first (0x1FFF 0000) and then the system bootloader changes the alias to bank2 (remaped at 0x0800 0000) after?

Debug the code and examine what is happening.

You're running via the system bootloader now?

If you feel a post has answered your question, please click "Accept as Solution".

Yes I am.

Setting BFB2 will force a boot from system memory according to RM0090.

1758813445.png

It says that the system bootloader located there jumps to bank 2.

1758813368.png

When the device boots after setting BFB2, FB_MODE is also set (I checked via the debugger). Indicating Flash Bank 2 is mapped at 0x0800 0000.

1758813658.png

So what is the point of setting VTOR if its default value, 0x0000 0000, is the alias to Flash Bank 2.

Hopefully my question is clearer now.

Sorry if it was not before.

TDK
Super User

If I follow your logic, I come to the same conclusion you do--that remapping is not necessary.

However, something is "not working" so one of the best paths to figuring how why is to debug and investigate. What is VTOR set to that setting it to 0x08000000 fixes the issue?

You may find the problem is something else entirely and this is a red herring.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks TDK,

Sorry for not replying earlier, I somehow missed the notification.

VTOR is set to 0 when I inspect it using the debugger at the reset handler and it stays that way as far as I can see.

1759407754.png

The program reaches main and after jumping over MX_ETH_Init() I am brought back to the reset handler for some reason. I recall reading that the ethernet configuration messes with the same register as VTOR I think but I don't know if its related here.

The two seperate applications I run are identical with the only difference beeing the string they send over UART.

App 1 sends : "Running program bank1 ...\n"

App 2 sends : "Running program bank2 ...\n"

I programmed App 1 to 0x0800 0000 and App 2 to 0x08100 0000 using cubeprogrammer making sure the flash is not erased when programming.

Then I set BFB2 so that banks will be swapped and I boot from bank 2.

I then disable downloading of the flash application when running with the debugger like so:

1759409174.png

I do get this when debugging as well :

1759406748.png

Seems strange since the last compiled application is the one for bank 2 (the one I am running currently).

Thanks again for your patience,

I appreciate you taking the time to help.

TDK
Super User

I'm not sure why the behavior you describe is running counter to how the reference manual says the chip operates. I tend to side with the RM unless presented with a minimal working example which shows the fault. It could be you are missing something. Change your program so it only flashes an LED. Does it work correctly in that case?

I still think debugging the "not working" is your best bet to a solution.

If you feel a post has answered your question, please click "Accept as Solution".