diff --git a/platform/retarget.cpp b/platform/retarget.cpp index 17af0798b31..3a1d3aef9ec 100644 --- a/platform/retarget.cpp +++ b/platform/retarget.cpp @@ -149,13 +149,21 @@ static inline int openmode_to_posix(int openmode) { return posix; } +extern "C" WEAK void mbed_sdk_init(void); +extern "C" WEAK void mbed_sdk_init(void) { +} + extern "C" FILEHANDLE PREFIX(_open)(const char* name, int openmode) { #if defined(__MICROLIB) && (__ARMCC_VERSION>5030000) // Before version 5.03, we were using a patched version of microlib with proper names // This is the workaround that the microlib author suggested us static int n = 0; + static int mbed_sdk_inited = 0; + if (!mbed_sdk_inited) { + mbed_sdk_inited = 1; + mbed_sdk_init(); + } if (!std::strcmp(name, ":tt")) return n++; - #else /* Use the posix convention that stdin,out,err are filehandles 0,1,2. */ @@ -501,7 +509,7 @@ extern "C" void software_init_hook(void) mbed_die(); } #endif/* FEATURE_UVISOR */ - + mbed_sdk_init(); software_init_hook_rtos(); } #endif @@ -516,23 +524,22 @@ extern "C" WEAK void mbed_main(void); extern "C" WEAK void mbed_main(void) { } -extern "C" WEAK void mbed_sdk_init(void); -extern "C" WEAK void mbed_sdk_init(void) { -} - #if defined(TOOLCHAIN_ARM) extern "C" int $Super$$main(void); extern "C" int $Sub$$main(void) { - mbed_sdk_init(); mbed_main(); return $Super$$main(); } + +extern "C" void _platform_post_stackheap_init (void) { + mbed_sdk_init(); +} + #elif defined(TOOLCHAIN_GCC) extern "C" int __real_main(void); extern "C" int __wrap_main(void) { - mbed_sdk_init(); mbed_main(); return __real_main(); } diff --git a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h index ec59ccb7607..267cec0d926 100644 --- a/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h +++ b/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h @@ -538,6 +538,7 @@ void pre_main (void) __asm void __rt_entry (void) { IMPORT __user_setup_stackheap + IMPORT _platform_post_stackheap_init IMPORT os_thread_def_main IMPORT osKernelInitialize #ifdef __MBED_CMSIS_RTOS_CM @@ -558,6 +559,7 @@ __asm void __rt_entry (void) { /* Ignore return value of __user_setup_stackheap since * this will be setup by set_stack_heap */ + BL _platform_post_stackheap_init BL osKernelInitialize #ifdef __MBED_CMSIS_RTOS_CM BL set_stack_heap diff --git a/targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c index cf25b0328c9..a14c2980e12 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32F0/mbed_overrides.c @@ -31,8 +31,8 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); - +#if !defined(TOOLCHAIN_GCC_ARM) // Need to restart HAL driver after the RAM is initialized HAL_Init(); - +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c index 6cea9f9b18d..63368cc4c18 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/pwmout_api.c @@ -162,9 +162,6 @@ void pwmout_period_us(pwmout_t* obj, int us) { __HAL_TIM_DISABLE(&TimHandle); - // Update the SystemCoreClock variable - SystemCoreClockUpdate(); - /* To make it simple, we use to possible prescaler values which lead to: * pwm unit = 1us, period/pulse can be from 1us to 65535us * or diff --git a/targets/TARGET_STM/TARGET_STM32F0/serial_api.c b/targets/TARGET_STM/TARGET_STM32F0/serial_api.c index a5b49e1ef87..d956c5489e1 100644 --- a/targets/TARGET_STM/TARGET_STM32F0/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F0/serial_api.c @@ -90,10 +90,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c index 9783dd90a53..2252f1c8249 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32F1/mbed_overrides.c @@ -32,6 +32,8 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) // Need to restart HAL driver after the RAM is initialized HAL_Init(); +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c index cb24045c9e0..4eb3a28c4f3 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32F1/pwmout_api.c @@ -152,9 +152,6 @@ void pwmout_period_us(pwmout_t* obj, int us) __HAL_TIM_DISABLE(&TimHandle); - // Update the SystemCoreClock variable - SystemCoreClockUpdate(); - /* To make it simple, we use to possible prescaler values which lead to: * pwm unit = 1us, period/pulse can be from 1us to 65535us * or diff --git a/targets/TARGET_STM/TARGET_STM32F1/serial_api.c b/targets/TARGET_STM/TARGET_STM32F1/serial_api.c index e88f9885b85..8d7940508f4 100644 --- a/targets/TARGET_STM/TARGET_STM32F1/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F1/serial_api.c @@ -82,10 +82,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32F2/serial_api.c b/targets/TARGET_STM/TARGET_STM32F2/serial_api.c index e3a90c285ad..5c9a12bc88d 100644 --- a/targets/TARGET_STM/TARGET_STM32F2/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F2/serial_api.c @@ -82,10 +82,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32F3/i2c_api.c b/targets/TARGET_STM/TARGET_STM32F3/i2c_api.c index fae23e0501e..5ecd267446d 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/i2c_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/i2c_api.c @@ -113,9 +113,6 @@ void i2c_frequency(i2c_t *obj, int hz) timeout = LONG_TIMEOUT; while ((__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY)) && (timeout-- != 0)); - // Update the SystemCoreClock variable. - SystemCoreClockUpdate(); - /* Values calculated with I2C_Timing_Configuration_V1.0.1.xls file (see AN4235) * Standard mode (up to 100 kHz) diff --git a/targets/TARGET_STM/TARGET_STM32F3/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32F3/mbed_overrides.c index 9783dd90a53..2252f1c8249 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32F3/mbed_overrides.c @@ -32,6 +32,8 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) // Need to restart HAL driver after the RAM is initialized HAL_Init(); +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32F3/serial_api.c b/targets/TARGET_STM/TARGET_STM32F3/serial_api.c index 7624baa6f44..97b5f9e8eb0 100644 --- a/targets/TARGET_STM/TARGET_STM32F3/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F3/serial_api.c @@ -82,9 +82,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); diff --git a/targets/TARGET_STM/TARGET_STM32F4/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32F4/mbed_overrides.c index a21a749f800..93e4da780b3 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32F4/mbed_overrides.c @@ -33,8 +33,10 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) // Need to restart HAL driver after the RAM is initialized HAL_Init(); +#endif } /** diff --git a/targets/TARGET_STM/TARGET_STM32F4/serial_api.c b/targets/TARGET_STM/TARGET_STM32F4/serial_api.c index e77c92497f3..7ac2dc95012 100644 --- a/targets/TARGET_STM/TARGET_STM32F4/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F4/serial_api.c @@ -84,10 +84,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32F7/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32F7/mbed_overrides.c index 496c4adc66d..6b174b89801 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32F7/mbed_overrides.c @@ -35,8 +35,10 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) // Need to restart HAL driver after the RAM is initialized HAL_Init(); +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32F7/serial_api.c b/targets/TARGET_STM/TARGET_STM32F7/serial_api.c index 832e07bd886..32f890e8703 100644 --- a/targets/TARGET_STM/TARGET_STM32F7/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32F7/serial_api.c @@ -83,10 +83,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32L0/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32L0/mbed_overrides.c index 509fed21988..4025d0aaa0c 100644 --- a/targets/TARGET_STM/TARGET_STM32L0/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32L0/mbed_overrides.c @@ -32,4 +32,8 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) + // Need to restart HAL driver after the RAM is initialized + HAL_Init(); +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32L1/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32L1/mbed_overrides.c index 2d91ef23c10..9b9c85031fd 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32L1/mbed_overrides.c @@ -35,6 +35,10 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) + // Need to restart HAL driver after the RAM is initialized + HAL_Init(); +#endif #if defined(TARGET_XDOT_L151CC) if (PWR->CSR & PWR_CSR_SBF) { diff --git a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c index bbbcbb3567f..6e4e85e835c 100644 --- a/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/pwmout_api.c @@ -166,8 +166,6 @@ void pwmout_period_us(pwmout_t* obj, int us) __HAL_TIM_DISABLE(&TimHandle); - SystemCoreClockUpdate(); - /* To make it simple, we use to possible prescaler values which lead to: * pwm unit = 1us, period/pulse can be from 1us to 65535us * or diff --git a/targets/TARGET_STM/TARGET_STM32L1/serial_api.c b/targets/TARGET_STM/TARGET_STM32L1/serial_api.c index 2dce257a065..9f804f521e6 100755 --- a/targets/TARGET_STM/TARGET_STM32L1/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32L1/serial_api.c @@ -82,10 +82,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); } diff --git a/targets/TARGET_STM/TARGET_STM32L4/mbed_overrides.c b/targets/TARGET_STM/TARGET_STM32L4/mbed_overrides.c index 509fed21988..4025d0aaa0c 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/mbed_overrides.c +++ b/targets/TARGET_STM/TARGET_STM32L4/mbed_overrides.c @@ -32,4 +32,8 @@ void mbed_sdk_init() { // Update the SystemCoreClock variable. SystemCoreClockUpdate(); +#if !defined(TOOLCHAIN_GCC_ARM) + // Need to restart HAL driver after the RAM is initialized + HAL_Init(); +#endif } diff --git a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c b/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c index 8d121414006..8b8cfe90684 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/pwmout_api.c @@ -167,8 +167,6 @@ void pwmout_period_us(pwmout_t* obj, int us) __HAL_TIM_DISABLE(&TimHandle); - SystemCoreClockUpdate(); - /* To make it simple, we use to possible prescaler values which lead to: * pwm unit = 1us, period/pulse can be from 1us to 65535us * or diff --git a/targets/TARGET_STM/TARGET_STM32L4/serial_api.c b/targets/TARGET_STM/TARGET_STM32L4/serial_api.c index 0664d1fdc90..3458a8bf393 100644 --- a/targets/TARGET_STM/TARGET_STM32L4/serial_api.c +++ b/targets/TARGET_STM/TARGET_STM32L4/serial_api.c @@ -82,10 +82,6 @@ static void init_uart(serial_t *obj) huart->Init.Mode = UART_MODE_TX_RX; } - /* uAMR & ARM: Call to UART init is done between reset of pre-initialized variables */ - /* and before HAL Init. SystemCoreClock init required here */ - SystemCoreClockUpdate(); - if (HAL_UART_Init(huart) != HAL_OK) { error("Cannot initialize UART\n"); }