简体   繁体   中英

How to change SCB->VTOR in app (stm32f4 HAL)?

I build the bootloader for STM32F4 and build user app with the changes the VECT_TAB_OFFSET value manually for SCB->VTOR in system_stm32f4xx.c file but If I do it in the application. It's not working properly.

I called the SCB->VTOR = 0x8040000 at the beginning of the main() but it didn't work. BOOTLOADER USER APPLICATION

The STM32 startup code calls SystemInit() before main() . SystemInit() sets the SCB->VTOR value (among other things). SystemInit() is implemented in the vendor provided file system_stm32f4xx.c . You can customize the value that gets assigned to SCB-VTOR by editing the value of VECT_TAB_OFFSET , which is also defined in system_stm32f4xx.c . (There should be a copy of system_stm32f4xx.c in your project folder that you can customize for your project.)

If you need different values of VECT_TAB_OFFSET for your boot and application programs then you can use a preprocessor statement like this. (This allows the boot and application programs to use the same copy of system_stm32fxx.c . Alternatively you could use different copies of the file for each program.)

#ifdef BOOT
#define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.
                                   This value must be a multiple of 0x200. */
#else
#define VECT_TAB_OFFSET  0x80000
#endif

Just add this code in main, first line before any other initialization, before HAL_Init or between /* USER CODE BEGIN 1 / / USER CODE END 1 */

__disable_irq();
SCB->VTOR = 0x8008000;
__DSB();
__enable_irq();

Don't forgot to preserve bootloader memory window in ld file:

FLASH    (rx)    : ORIGIN = 0x8008000,   LENGTH = 2048K - 0x8000

That's all, even debugger will run this code w/o bootloader in Stm32CubeIDE at offset 0x8008000, but w/o debugger you must have bootloader at start of flash.

Delete the VTOR set in the startup file. It should not be there.

Set the VTOR in the bootloader before passing the control to the application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM