2021-10-11 08:22 AM
In file generated by CubeMx when creating a new project, the SystemInit function does not set the register of the interrupt vector table (SCB-> VTOR) !!!
The default value is zero, which causes a crash on the first interrupt trigger, i.e. the code loops to an address 0x1FFFxxxx (system memory)
/**
* @brief Setup the microcontroller system.
* @retval None
*/
void SystemInit(void)
{
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#endif
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << 20U)|(3UL << 22U)); /* set CP10 and CP11 Full Access */
#endif
/* Reset the RCC clock configuration to the default reset state ------------*/
...
code of the previous version of the package
/**
* @brief Setup the microcontroller system.
* @param None
* @retval None
*/
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
/* Reset the RCC clock configuration to the default reset state ------------*/
/* Set MSION bit */
RCC->CR |= RCC_CR_MSION;
/* Reset CFGR register */
RCC->CFGR = 0x00000000U;
/* Reset HSEON, CSSON , HSION, and PLLON bits */
RCC->CR &= 0xEAF6FFFFU;
/* Reset PLLCFGR register */
RCC->PLLCFGR = 0x00001000U;
/* Reset HSEBYP bit */
RCC->CR &= 0xFFFBFFFFU;
/* Disable all interrupts */
RCC->CIER = 0x00000000U;
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
}
why this register is no longer set with the default flash memory address ?
Thanks for your help
Solved! Go to Solution.
2021-10-14 04:38 AM
Hello @MFred.2 ,
There is a change request which is deployed on all STM32 series:
the purpose is to set SCB->VTOR only when requested by user application. This is for protect Vector table modification following SRAM or FLASH preprocessor directive by a generic preprocessor directive : USER_VECT_TAB_ADDRESS
So, the following lines from the previous version of the package:
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
are updated as follow:
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#endif
If user want to force SCB-> VTOR configuration at start-up level, he shall just uncomment this line (Line 127) in system_stm32l4xx.c file:
/*!< 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 */
Note that the system_stm32l4xx.c available in CMSIS file is provided as template file and user shall copy/past this file in his application and customize it as he wants.
I hope that is clear now :).
Thanks for your contribution and do not hesitate to raise any issue/ feedback.
If you issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)
Imen
2021-10-11 10:15 AM
Trying to understand why, and why this wasn't done with symbols the linker could fix-up, will give anyone migraines..
Little appears to get tested, or scrutinized by QA
2021-10-11 10:43 AM
Your code have other issue, because this
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#endif
isnt used, and VTOR is initialized ok by HW. I have project with same SystemInit and works without problems.
But yes this defines seems as bug migraines
2021-10-12 12:58 AM
Indeed, without setting VTOR, the code sometimes works depending on the configuration of the peripherals in CubeMx. I have encountered this same problem with the default NUCLEO L4XX board projects. Really very strange.
I do not understand the purpose of this modification !!!
2021-10-14 04:38 AM
Hello @MFred.2 ,
There is a change request which is deployed on all STM32 series:
the purpose is to set SCB->VTOR only when requested by user application. This is for protect Vector table modification following SRAM or FLASH preprocessor directive by a generic preprocessor directive : USER_VECT_TAB_ADDRESS
So, the following lines from the previous version of the package:
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
are updated as follow:
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#endif
If user want to force SCB-> VTOR configuration at start-up level, he shall just uncomment this line (Line 127) in system_stm32l4xx.c file:
/*!< 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 */
Note that the system_stm32l4xx.c available in CMSIS file is provided as template file and user shall copy/past this file in his application and customize it as he wants.
I hope that is clear now :).
Thanks for your contribution and do not hesitate to raise any issue/ feedback.
If you issue is solved, please close this post by clicking the "Select as Best" button. This will help other members of the community find this response more quickly :)
Imen
2021-10-14 06:03 AM
Thank you for this feedback
I understood this possibility of customizing the vector table, but I am not convinced by this new code. It breaks compatibility with all previous versions. In 98% of cases, the vector table is defined at the beginning of flash memory.
The interest of CubeMX is to generate a code template for people who are not necessarily micro-controller experts and which works without asking too much questions.
I have been using the Cube environment since the acquisition of Attolic Truestudio, and I was tricked by this code change. And it is not the innocuous sentence in the "CMSIS release note" file (Update VTOR configuration to be modified by user) which helped me.
Otherwise, I think the code below would have been better
#if defined(USER_VECT_TAB_ADDRESS)
/* Configure the Vector Table location -------------------------------------*/
SCB->VTOR = VECT_TAB_BASE_ADDRESS | VECT_TAB_OFFSET;
#else
/* Vector Table Relocation in Internal FLASH */
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET;
#endif
best regards
Frederic
2021-10-14 06:08 AM
Maybe it would be good to show this vector table configuration in the Project Manager tab of CubeMx?
best regards
Frederic
2021-10-14 06:16 AM
Thanks Frederic for taking the time to post your feedback. I will escalate this internally to the appropriate team.
Hi @Khouloud OTHMAN , @Khouloud ZEMMELI ,
Can you please take Frederic's request regarding the CubeMX part ?
Thanks
Imen
2023-10-24 02:29 AM
I just spent 2 weeks of nightmare because of this. I have 2 old projects with a STM32L476 that were written with AC6 System Workbench. I needed a little modification on one of these and converted it to use CubeIDE and ... the bank switching didn't work anymore...I checked compiler options and ioc file many times, created a new simple blink led project that didn't work.
If there had been something in CubeMX I wouldn't have lost that much time figuring out what went wrong. I understand this change, but pleeeaase try to preserve backward compatibility by default when introducing such things.
2023-11-28 04:41 AM
This is still an issue.
I spent 2 hours trying to get a "blink led" working on an STM32G0 due to this.
The default behaviour shouldn't wind up with "HAL_Delay" causing a jump to random program space and a locked up MCU. Needing to find this thread then edit some random file before "blink LED" code works is not a useful pattern. If people are doing the fancy bits by all means allow them to do so, but the whole point of the pointy clicky cubemx setup is to make the basics work for most people most of the time.