Skip to content

Commit 47b78a2

Browse files
committed
Fix STM32 crashes on boot due to unset VTOR
Remove HAL_Init and related code from SystemInit and move it to mbed_sdk_init. The function SystemInit is called early in the boot sequence before RAM is initialized or the VTOR is setup, so it should not be used to perform the HAL initialization. This fixes crashes due the vector table being used before it has been relocated.
1 parent 35999be commit 47b78a2

File tree

65 files changed

+62
-723
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+62
-723
lines changed

targets/TARGET_STM/TARGET_STM32F0/TARGET_DISCO_F051R8/device/system_stm32f0xx.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,6 @@ void SystemInit(void)
234234

235235
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
236236
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
237-
238-
/* Configure the Cube driver */
239-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
240-
HAL_Init();
241-
242-
/* Configure the System clock source, PLL Multiplier and Divider factors,
243-
AHB/APBx prescalers and Flash settings */
244-
SetSysClock();
245237

246238
}
247239

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F030R8/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -234,18 +234,7 @@ void SystemInit(void)
234234

235235
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
236236
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
237-
238-
/* Configure the Cube driver */
239-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
240-
HAL_Init();
241-
242-
/* Configure the System clock source, PLL Multiplier and Divider factors,
243-
AHB/APBx prescalers and Flash settings */
244-
SetSysClock();
245237

246-
/* Reset the timer to avoid issues after the RAM initialization */
247-
TIM_MST_RESET_ON;
248-
TIM_MST_RESET_OFF;
249238
}
250239

251240
/**

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F031K6/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -234,18 +234,7 @@ void SystemInit(void)
234234

235235
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
236236
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
237-
238-
/* Configure the Cube driver */
239-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
240-
HAL_Init();
241-
242-
/* Configure the System clock source, PLL Multiplier and Divider factors,
243-
AHB/APBx prescalers and Flash settings */
244-
SetSysClock();
245237

246-
/* Reset the timer to avoid issues after the RAM initialization */
247-
TIM_MST_RESET_ON;
248-
TIM_MST_RESET_OFF;
249238
}
250239

251240
/**

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F042K6/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -234,18 +234,7 @@ void SystemInit(void)
234234

235235
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
236236
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
237-
238-
/* Configure the Cube driver */
239-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
240-
HAL_Init();
241-
242-
/* Configure the System clock source, PLL Multiplier and Divider factors,
243-
AHB/APBx prescalers and Flash settings */
244-
SetSysClock();
245237

246-
/* Reset the timer to avoid issues after the RAM initialization */
247-
TIM_MST_RESET_ON;
248-
TIM_MST_RESET_OFF;
249238
}
250239

251240
/**

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F070RB/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -234,18 +234,7 @@ void SystemInit(void)
234234

235235
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
236236
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
237-
238-
/* Configure the Cube driver */
239-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
240-
HAL_Init();
241-
242-
/* Configure the System clock source, PLL Multiplier and Divider factors,
243-
AHB/APBx prescalers and Flash settings */
244-
SetSysClock();
245237

246-
/* Reset the timer to avoid issues after the RAM initialization */
247-
TIM_MST_RESET_ON;
248-
TIM_MST_RESET_OFF;
249238
}
250239

251240
/**

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F072RB/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -233,18 +233,7 @@ void SystemInit(void)
233233

234234
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
235235
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
236-
237-
/* Configure the Cube driver */
238-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
239-
HAL_Init();
240-
241-
/* Configure the System clock source, PLL Multiplier and Divider factors,
242-
AHB/APBx prescalers and Flash settings */
243-
SetSysClock();
244236

245-
/* Reset the timer to avoid issues after the RAM initialization */
246-
TIM_MST_RESET_ON;
247-
TIM_MST_RESET_OFF;
248237
}
249238

250239
/**

targets/TARGET_STM/TARGET_STM32F0/TARGET_NUCLEO_F091RC/device/system_stm32f0xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
*/
8383

8484
#include "stm32f0xx.h"
85-
#include "hal_tick.h"
85+
8686
/**
8787
* @}
8888
*/
@@ -233,18 +233,7 @@ void SystemInit(void)
233233

234234
/* Enable SYSCFGENR in APB2EN, needed for 1st call of NVIC_SetVector, to copy vectors from flash to ram */
235235
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
236-
237-
/* Configure the Cube driver */
238-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
239-
HAL_Init();
240-
241-
/* Configure the System clock source, PLL Multiplier and Divider factors,
242-
AHB/APBx prescalers and Flash settings */
243-
SetSysClock();
244236

245-
/* Reset the timer to avoid issues after the RAM initialization */
246-
TIM_MST_RESET_ON;
247-
TIM_MST_RESET_OFF;
248237
}
249238

250239
/**

targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
void mbed_sdk_init() {
3232
// Update the SystemCoreClock variable.
3333
SystemCoreClockUpdate();
34-
// Need to restart HAL driver after the RAM is initialized
3534
HAL_Init();
35+
36+
/* Configure the System clock source, PLL Multiplier and Divider factors,
37+
AHB/APBx prescalers and Flash settings */
38+
SetSysClock();
39+
SystemCoreClockUpdate();
3640
}

targets/TARGET_STM/TARGET_STM32F1/TARGET_BLUEPILL_F103C8/device/system_stm32f1xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
*/
9393

9494
#include "stm32f1xx.h"
95-
#include "hal_tick.h"
95+
9696

9797
/**
9898
* @}
@@ -254,17 +254,6 @@ void SystemInit (void)
254254
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
255255
#endif
256256

257-
/* Configure the Cube driver */
258-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
259-
HAL_Init();
260-
261-
/* Configure the System clock source, PLL Multiplier and Divider factors,
262-
AHB/APBx prescalers and Flash settings */
263-
SetSysClock();
264-
265-
/* Reset the timer to avoid issues after the RAM initialization */
266-
TIM_MST_RESET_ON;
267-
TIM_MST_RESET_OFF;
268257
}
269258

270259
/**

targets/TARGET_STM/TARGET_STM32F1/TARGET_DISCO_F100RB/device/system_stm32f1xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
*/
9191

9292
#include "stm32f1xx.h"
93-
#include "hal_tick.h"
93+
9494

9595
/**
9696
* @}
@@ -252,17 +252,6 @@ void SystemInit (void)
252252
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
253253
#endif
254254

255-
/* Configure the Cube driver */
256-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
257-
HAL_Init();
258-
259-
/* Configure the System clock source, PLL Multiplier and Divider factors,
260-
AHB/APBx prescalers and Flash settings */
261-
SetSysClock();
262-
263-
/* Reset the timer to avoid issues after the RAM initialization */
264-
TIM_MST_RESET_ON;
265-
TIM_MST_RESET_OFF;
266255
}
267256

268257
/**

targets/TARGET_STM/TARGET_STM32F1/TARGET_NUCLEO_F103RB/device/system_stm32f1xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
*/
9393

9494
#include "stm32f1xx.h"
95-
#include "hal_tick.h"
95+
9696

9797
/**
9898
* @}
@@ -254,17 +254,6 @@ void SystemInit (void)
254254
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */
255255
#endif
256256

257-
/* Configure the Cube driver */
258-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
259-
HAL_Init();
260-
261-
/* Configure the System clock source, PLL Multiplier and Divider factors,
262-
AHB/APBx prescalers and Flash settings */
263-
SetSysClock();
264-
265-
/* Reset the timer to avoid issues after the RAM initialization */
266-
TIM_MST_RESET_ON;
267-
TIM_MST_RESET_OFF;
268257
}
269258

270259
/**

targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ void mbed_sdk_init()
3232
{
3333
// Update the SystemCoreClock variable.
3434
SystemCoreClockUpdate();
35-
// Need to restart HAL driver after the RAM is initialized
3635
HAL_Init();
36+
37+
/* Configure the System clock source, PLL Multiplier and Divider factors,
38+
AHB/APBx prescalers and Flash settings */
39+
SetSysClock();
40+
SystemCoreClockUpdate();
3741
}

targets/TARGET_STM/TARGET_STM32F2/TARGET_NUCLEO_F207ZG/device/system_stm32f2xx.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
*/
8080

8181
#include "stm32f2xx.h"
82-
#include "hal_tick.h"
8382

8483
/**
8584
* @}
@@ -203,20 +202,6 @@ void SystemInit(void)
203202
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
204203
#endif
205204

206-
#if (USE_PLL_HSE_XTAL != 0) || (USE_PLL_HSE_EXTC != 0)
207-
SystemCoreClock = 120000000;
208-
#else
209-
SystemCoreClock = 96000000;
210-
#endif
211-
HAL_Init();
212-
213-
/* Configure the System clock source, PLL Multiplier and Divider factors,
214-
AHB/APBx prescalers and Flash settings */
215-
SetSysClock();
216-
217-
/* Reset the timer to avoid issues after the RAM initialization */
218-
TIM_MST_RESET_ON;
219-
TIM_MST_RESET_OFF;
220205
}
221206

222207
/**

targets/TARGET_STM/TARGET_STM32F2/mbed_overrides.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ void mbed_sdk_init()
3333
{
3434
// Update the SystemCoreClock variable.
3535
SystemCoreClockUpdate();
36-
// Need to restart HAL driver after the RAM is initialized
3736
HAL_Init();
37+
38+
/* Configure the System clock source, PLL Multiplier and Divider factors,
39+
AHB/APBx prescalers and Flash settings */
40+
SetSysClock();
41+
SystemCoreClockUpdate();
3842
}
3943

4044
/**

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F302x8/TARGET_NUCLEO_F302R8/system_stm32f3xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
*/
8585

8686
#include "stm32f3xx.h"
87-
#include "hal_tick.h"
87+
8888

8989
/**
9090
* @}
@@ -213,17 +213,6 @@ void SystemInit(void)
213213
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
214214
#endif
215215

216-
/* Configure the Cube driver */
217-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
218-
HAL_Init();
219-
220-
/* Configure the System clock source, PLL Multiplier and Divider factors,
221-
AHB/APBx prescalers and Flash settings */
222-
SetSysClock();
223-
224-
/* Reset the timer to avoid issues after the RAM initialization */
225-
TIM_MST_RESET_ON;
226-
TIM_MST_RESET_OFF;
227216
}
228217

229218
/**

targets/TARGET_STM/TARGET_STM32F3/TARGET_STM32F303x8/device/system_stm32f3xx.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
*/
8585

8686
#include "stm32f3xx.h"
87-
#include "hal_tick.h"
87+
8888

8989
/**
9090
* @}
@@ -213,17 +213,6 @@ void SystemInit(void)
213213
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
214214
#endif
215215

216-
/* Configure the Cube driver */
217-
SystemCoreClock = 8000000; // At this stage the HSI is used as system clock
218-
HAL_Init();
219-
220-
/* Configure the System clock source, PLL Multiplier and Divider factors,
221-
AHB/APBx prescalers and Flash settings */
222-
SetSysClock();
223-
224-
/* Reset the timer to avoid issues after the RAM initialization */
225-
TIM_MST_RESET_ON;
226-
TIM_MST_RESET_OFF;
227216
}
228217

229218
/**

0 commit comments

Comments
 (0)