Skip to content

Commit 242909c

Browse files
committed
Fix vector table bug when using bootloader on ST
The address of the vector table is hardcoded to the start of flash in many, if not all, ST targets. This causes a crash in applications that are using a bootloader. This patch updates the boards STM32F429xI, STM32F439xI and Odin so they properly handle updating the VTOR with a bootloader.
1 parent 7fc73e4 commit 242909c

File tree

10 files changed

+131
-7
lines changed

10 files changed

+131
-7
lines changed

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/TARGET_NUCLEO_F429ZI/system_stm32f4xx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
#include "stm32f4xx.h"
8282
#include "hal_tick.h"
83+
#include "nvic_addr.h"
8384

8485
#if !defined (HSE_VALUE)
8586
#define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -218,7 +219,7 @@ void SystemInit(void)
218219
#ifdef VECT_TAB_SRAM
219220
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
220221
#else
221-
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
222+
SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
222223
#endif
223224

224225
/* Configure the Cube driver */

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/cmsis_nvic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
*******************************************************************************
3030
*/
3131
#include "cmsis_nvic.h"
32+
#include "nvic_addr.h"
3233

3334
#define NVIC_RAM_VECTOR_ADDRESS (0x20000000) // Vectors positioned at start of RAM
34-
#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000) // Initial vector position in flash
3535

3636
void __NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
3737
uint32_t *vectors = (uint32_t *)SCB->VTOR;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef NVIC_ADDR_H
17+
#define NVIC_ADDR_H
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#if defined(__ICCARM__)
24+
#pragma section=".intvec"
25+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)__section_begin(".intvec"))
26+
#elif defined(__CC_ARM)
27+
extern uint32_t Load$$LR$$LR_IROM1$$Base[];
28+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)Load$$LR$$LR_IROM1$$Base)
29+
#elif defined(__GNUC__)
30+
extern uint32_t g_pfnVectors[];
31+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)g_pfnVectors)
32+
#else
33+
#error "Flash vector address not set for this toolchain"
34+
#endif
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F429xI/device/system_init_pre.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717
#include "stm32f4xx.h"
18+
#include "nvic_addr.h"
1819

1920
/*!< Uncomment the following line if you need to relocate your vector Table in
2021
Internal SRAM. */
@@ -37,6 +38,6 @@ void SystemInitPre(void)
3738
#ifdef VECT_TAB_SRAM
3839
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
3940
#else
40-
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
41+
SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
4142
#endif
4243
}

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/TARGET_NUCLEO_F439ZI/system_stm32f4xx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
#include "stm32f4xx.h"
8282
#include "hal_tick.h"
83+
#include "nvic_addr.h"
8384

8485
#if !defined (HSE_VALUE)
8586
#define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -218,7 +219,7 @@ void SystemInit(void)
218219
#ifdef VECT_TAB_SRAM
219220
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
220221
#else
221-
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
222+
SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
222223
#endif
223224

224225
/* Configure the Cube driver */

targets/TARGET_STM/TARGET_STM32F4/TARGET_STM32F439xI/device/cmsis_nvic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
*******************************************************************************
3030
*/
3131
#include "cmsis_nvic.h"
32+
#include "nvic_addr.h"
3233

3334
#define NVIC_RAM_VECTOR_ADDRESS (0x20000000) // Vectors positioned at start of RAM
34-
#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000) // Initial vector position in flash
3535

3636
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
3737
uint32_t *vectors = (uint32_t *)SCB->VTOR;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef NVIC_ADDR_H
17+
#define NVIC_ADDR_H
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#if defined(__ICCARM__)
24+
#pragma section=".intvec"
25+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)__section_begin(".intvec"))
26+
#elif defined(__CC_ARM)
27+
extern uint32_t Load$$LR$$LR_IROM1$$Base[];
28+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)Load$$LR$$LR_IROM1$$Base)
29+
#elif defined(__GNUC__)
30+
extern uint32_t g_pfnVectors[];
31+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)g_pfnVectors)
32+
#else
33+
#error "Flash vector address not set for this toolchain"
34+
#endif
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif

targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/cmsis_nvic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
*******************************************************************************
3030
*/
3131
#include "cmsis_nvic.h"
32+
#include "nvic_addr.h"
3233

3334
#define NVIC_RAM_VECTOR_ADDRESS (0x20000000) // Vectors positioned at start of RAM
34-
#define NVIC_FLASH_VECTOR_ADDRESS (0x08000000) // Initial vector position in flash
3535

3636
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
3737
uint32_t *vectors = (uint32_t *)SCB->VTOR;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef NVIC_ADDR_H
17+
#define NVIC_ADDR_H
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#if defined(__ICCARM__)
24+
#pragma section=".intvec"
25+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)__section_begin(".intvec"))
26+
#elif defined(__CC_ARM)
27+
extern uint32_t Load$$LR$$LR_IROM1$$Base[];
28+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)Load$$LR$$LR_IROM1$$Base)
29+
#elif defined(__GNUC__)
30+
extern uint32_t g_pfnVectors[];
31+
#define NVIC_FLASH_VECTOR_ADDRESS ((uint32_t)g_pfnVectors)
32+
#else
33+
#error "Flash vector address not set for this toolchain"
34+
#endif
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif

targets/TARGET_STM/TARGET_STM32F4/TARGET_UBLOX_EVK_ODIN_W2/device/system_stm32f4xx.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080

8181
#include "stm32f4xx.h"
8282
#include "hal_tick.h"
83+
#include "nvic_addr.h"
8384

8485
#if !defined (HSE_VALUE)
8586
#define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
@@ -218,7 +219,7 @@ void SystemInit(void)
218219
#ifdef VECT_TAB_SRAM
219220
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
220221
#else
221-
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
222+
SCB->VTOR = NVIC_FLASH_VECTOR_ADDRESS; /* Vector Table Relocation in Internal FLASH */
222223
#endif
223224

224225
/* Configure the Cube driver */

0 commit comments

Comments
 (0)