Skip to content

Commit 670b098

Browse files
authored
Merge pull request #2917 from c1728p9/fix_InitTCs
Initialization steps in toolchains
2 parents 5693e7a + e867503 commit 670b098

File tree

23 files changed

+39
-54
lines changed

23 files changed

+39
-54
lines changed

platform/retarget.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,21 @@ static inline int openmode_to_posix(int openmode) {
149149
return posix;
150150
}
151151

152+
extern "C" WEAK void mbed_sdk_init(void);
153+
extern "C" WEAK void mbed_sdk_init(void) {
154+
}
155+
152156
extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) {
153157
#if defined(__MICROLIB) && (__ARMCC_VERSION>5030000)
154158
// Before version 5.03, we were using a patched version of microlib with proper names
155159
// This is the workaround that the microlib author suggested us
156160
static int n = 0;
161+
static int mbed_sdk_inited = 0;
162+
if (!mbed_sdk_inited) {
163+
mbed_sdk_inited = 1;
164+
mbed_sdk_init();
165+
}
157166
if (!std::strcmp(name, ":tt")) return n++;
158-
159167
#else
160168
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.
161169
*/
@@ -501,7 +509,7 @@ extern "C" void software_init_hook(void)
501509
mbed_die();
502510
}
503511
#endif/* FEATURE_UVISOR */
504-
512+
mbed_sdk_init();
505513
software_init_hook_rtos();
506514
}
507515
#endif
@@ -516,23 +524,22 @@ extern "C" WEAK void mbed_main(void);
516524
extern "C" WEAK void mbed_main(void) {
517525
}
518526

519-
extern "C" WEAK void mbed_sdk_init(void);
520-
extern "C" WEAK void mbed_sdk_init(void) {
521-
}
522-
523527
#if defined(TOOLCHAIN_ARM)
524528
extern "C" int $Super$$main(void);
525529

526530
extern "C" int $Sub$$main(void) {
527-
mbed_sdk_init();
528531
mbed_main();
529532
return $Super$$main();
530533
}
534+
535+
extern "C" void _platform_post_stackheap_init (void) {
536+
mbed_sdk_init();
537+
}
538+
531539
#elif defined(TOOLCHAIN_GCC)
532540
extern "C" int __real_main(void);
533541

534542
extern "C" int __wrap_main(void) {
535-
mbed_sdk_init();
536543
mbed_main();
537544
return __real_main();
538545
}

rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ void pre_main (void)
538538
__asm void __rt_entry (void) {
539539

540540
IMPORT __user_setup_stackheap
541+
IMPORT _platform_post_stackheap_init
541542
IMPORT os_thread_def_main
542543
IMPORT osKernelInitialize
543544
#ifdef __MBED_CMSIS_RTOS_CM
@@ -558,6 +559,7 @@ __asm void __rt_entry (void) {
558559
/* Ignore return value of __user_setup_stackheap since
559560
* this will be setup by set_stack_heap
560561
*/
562+
BL _platform_post_stackheap_init
561563
BL osKernelInitialize
562564
#ifdef __MBED_CMSIS_RTOS_CM
563565
BL set_stack_heap

targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
void mbed_sdk_init() {
3232
// Update the SystemCoreClock variable.
3333
SystemCoreClockUpdate();
34-
34+
#if !defined(TOOLCHAIN_GCC_ARM)
3535
// Need to restart HAL driver after the RAM is initialized
3636
HAL_Init();
37-
37+
#endif
3838
}

targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,6 @@ void pwmout_period_us(pwmout_t* obj, int us) {
162162

163163
__HAL_TIM_DISABLE(&TimHandle);
164164

165-
// Update the SystemCoreClock variable
166-
SystemCoreClockUpdate();
167-
168165
/* To make it simple, we use to possible prescaler values which lead to:
169166
* pwm unit = 1us, period/pulse can be from 1us to 65535us
170167
* or

targets/TARGET_STM/TARGET_STM32F0/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ static void init_uart(serial_t *obj)
9090
huart->Init.Mode = UART_MODE_TX_RX;
9191
}
9292

93-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
94-
/* and before HAL Init. SystemCoreClock init required here */
95-
SystemCoreClockUpdate();
96-
9793
if (HAL_UART_Init(huart) != HAL_OK) {
9894
error("Cannot initialize UART\n");
9995
}

targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void mbed_sdk_init()
3232
{
3333
// Update the SystemCoreClock variable.
3434
SystemCoreClockUpdate();
35+
#if !defined(TOOLCHAIN_GCC_ARM)
3536
// Need to restart HAL driver after the RAM is initialized
3637
HAL_Init();
38+
#endif
3739
}

targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,6 @@ void pwmout_period_us(pwmout_t* obj, int us)
152152

153153
__HAL_TIM_DISABLE(&TimHandle);
154154

155-
// Update the SystemCoreClock variable
156-
SystemCoreClockUpdate();
157-
158155
/* To make it simple, we use to possible prescaler values which lead to:
159156
* pwm unit = 1us, period/pulse can be from 1us to 65535us
160157
* or

targets/TARGET_STM/TARGET_STM32F1/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static void init_uart(serial_t *obj)
8282
huart->Init.Mode = UART_MODE_TX_RX;
8383
}
8484

85-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
86-
/* and before HAL Init. SystemCoreClock init required here */
87-
SystemCoreClockUpdate();
88-
8985
if (HAL_UART_Init(huart) != HAL_OK) {
9086
error("Cannot initialize UART\n");
9187
}

targets/TARGET_STM/TARGET_STM32F2/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static void init_uart(serial_t *obj)
8282
huart->Init.Mode = UART_MODE_TX_RX;
8383
}
8484

85-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
86-
/* and before HAL Init. SystemCoreClock init required here */
87-
SystemCoreClockUpdate();
88-
8985
if (HAL_UART_Init(huart) != HAL_OK) {
9086
error("Cannot initialize UART\n");
9187
}

targets/TARGET_STM/TARGET_STM32F3/i2c_api.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,6 @@ void i2c_frequency(i2c_t *obj, int hz)
113113
timeout = LONG_TIMEOUT;
114114
while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0));
115115

116-
// Update the SystemCoreClock variable.
117-
SystemCoreClockUpdate();
118-
119116
/*
120117
Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235)
121118
* Standard mode (up to 100 kHz)

targets/TARGET_STM/TARGET_STM32F3/mbed_overrides.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void mbed_sdk_init()
3232
{
3333
// Update the SystemCoreClock variable.
3434
SystemCoreClockUpdate();
35+
#if !defined(TOOLCHAIN_GCC_ARM)
3536
// Need to restart HAL driver after the RAM is initialized
3637
HAL_Init();
38+
#endif
3739
}

targets/TARGET_STM/TARGET_STM32F3/serial_api.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ static void init_uart(serial_t *obj)
8282
huart->Init.Mode = UART_MODE_TX_RX;
8383
}
8484

85-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
86-
/* and before HAL Init. SystemCoreClock init required here */
87-
SystemCoreClockUpdate();
8885

8986
if (HAL_UART_Init(huart) != HAL_OK) {
9087
error("Cannot initialize UART\n");

targets/TARGET_STM/TARGET_STM32F4/mbed_overrides.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ void mbed_sdk_init()
3333
{
3434
// Update the SystemCoreClock variable.
3535
SystemCoreClockUpdate();
36+
#if !defined(TOOLCHAIN_GCC_ARM)
3637
// Need to restart HAL driver after the RAM is initialized
3738
HAL_Init();
39+
#endif
3840
}
3941

4042
/**

targets/TARGET_STM/TARGET_STM32F4/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ static void init_uart(serial_t *obj)
8484
huart->Init.Mode = UART_MODE_TX_RX;
8585
}
8686

87-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
88-
/* and before HAL Init. SystemCoreClock init required here */
89-
SystemCoreClockUpdate();
90-
9187
if (HAL_UART_Init(huart) != HAL_OK) {
9288
error("Cannot initialize UART\n");
9389
}

targets/TARGET_STM/TARGET_STM32F7/mbed_overrides.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ void mbed_sdk_init()
3535
{
3636
// Update the SystemCoreClock variable.
3737
SystemCoreClockUpdate();
38+
#if !defined(TOOLCHAIN_GCC_ARM)
3839
// Need to restart HAL driver after the RAM is initialized
3940
HAL_Init();
41+
#endif
4042
}
4143

4244

targets/TARGET_STM/TARGET_STM32F7/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ static void init_uart(serial_t *obj)
8383
huart->Init.Mode = UART_MODE_TX_RX;
8484
}
8585

86-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
87-
/* and before HAL Init. SystemCoreClock init required here */
88-
SystemCoreClockUpdate();
89-
9086
if (HAL_UART_Init(huart) != HAL_OK) {
9187
error("Cannot initialize UART\n");
9288
}

targets/TARGET_STM/TARGET_STM32L0/mbed_overrides.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ void mbed_sdk_init()
3232
{
3333
// Update the SystemCoreClock variable.
3434
SystemCoreClockUpdate();
35+
#if !defined(TOOLCHAIN_GCC_ARM)
36+
// Need to restart HAL driver after the RAM is initialized
37+
HAL_Init();
38+
#endif
3539
}

targets/TARGET_STM/TARGET_STM32L1/mbed_overrides.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ void mbed_sdk_init()
3535
{
3636
// Update the SystemCoreClock variable.
3737
SystemCoreClockUpdate();
38+
#if !defined(TOOLCHAIN_GCC_ARM)
39+
// Need to restart HAL driver after the RAM is initialized
40+
HAL_Init();
41+
#endif
3842

3943
#if defined(TARGET_XDOT_L151CC)
4044
if (PWR->CSR & PWR_CSR_SBF) {

targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ void pwmout_period_us(pwmout_t* obj, int us)
166166

167167
__HAL_TIM_DISABLE(&TimHandle);
168168

169-
SystemCoreClockUpdate();
170-
171169
/* To make it simple, we use to possible prescaler values which lead to:
172170
* pwm unit = 1us, period/pulse can be from 1us to 65535us
173171
* or

targets/TARGET_STM/TARGET_STM32L1/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static void init_uart(serial_t *obj)
8282
huart->Init.Mode = UART_MODE_TX_RX;
8383
}
8484

85-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
86-
/* and before HAL Init. SystemCoreClock init required here */
87-
SystemCoreClockUpdate();
88-
8985
if (HAL_UART_Init(huart) != HAL_OK) {
9086
error("Cannot initialize UART\n");
9187
}

targets/TARGET_STM/TARGET_STM32L4/mbed_overrides.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ void mbed_sdk_init()
3232
{
3333
// Update the SystemCoreClock variable.
3434
SystemCoreClockUpdate();
35+
#if !defined(TOOLCHAIN_GCC_ARM)
36+
// Need to restart HAL driver after the RAM is initialized
37+
HAL_Init();
38+
#endif
3539
}

targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ void pwmout_period_us(pwmout_t* obj, int us)
167167

168168
__HAL_TIM_DISABLE(&TimHandle);
169169

170-
SystemCoreClockUpdate();
171-
172170
/* To make it simple, we use to possible prescaler values which lead to:
173171
* pwm unit = 1us, period/pulse can be from 1us to 65535us
174172
* or

targets/TARGET_STM/TARGET_STM32L4/serial_api.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ static void init_uart(serial_t *obj)
8282
huart->Init.Mode = UART_MODE_TX_RX;
8383
}
8484

85-
/* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */
86-
/* and before HAL Init. SystemCoreClock init required here */
87-
SystemCoreClockUpdate();
88-
8985
if (HAL_UART_Init(huart) != HAL_OK) {
9086
error("Cannot initialize UART\n");
9187
}

0 commit comments

Comments
 (0)