Skip to content

Initialization steps in toolchains #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions hal/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is mbed_sdk_init only being called on _open for microlib? Also, here mbed_sdk_inited is checked, but it is not checked below so this might get called twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c1728p9: it just happen that _open "seems" to be called on every init and that we are looking for a path that is called after RAM init and before C++ object initialization. If you happen to know another alternative, that would be welcome.

Here is what I wrote in commit message :

In uARM, the library's hook _platform_post_stackheap_init does not seem to
exist and I couldnot find a documentation describing the initialisation
flow. All we know is that _open is called after RAM init and before the
C++ init, so this is a working placeholder.

This is maybe not acceptable so a uARM lib expert may propose a better
hook to fullfil the requirement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@c1728p9 About being called twice, I think that various calls are under different #defines - the one in _open is only for uARM toolchain, the other calls are in case of GCC or ARM toolchain. But I may be wrong, which line to you suspect to be the 2nd call ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. I didn't notice the define wrapping it before.

}
if (!std::strcmp(name, ":tt")) return n++;

#else
/* Use the posix convention that stdin,out,err are filehandles 0,1,2.
*/
Expand Down Expand Up @@ -502,7 +510,7 @@ extern "C" void software_init_hook(void)
mbed_die();
}
#endif/* FEATURE_UVISOR */

mbed_sdk_init();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is before the RTOS has been initialized which might case problems on other devices. I'll trigger testing to check this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, with this patch, mbed_sdk_init is called before RTOS initialization. Good to have tests on other platforms - thanks !

software_init_hook_rtos();
}
#endif
Expand All @@ -517,23 +525,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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mbed_sdk_init for ARMCC is moved to before the RTOS is initialized, but is still called in GCC after the RTOS has been initialized. This behavior should be unified across the toolchains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I changed the place where mbed_sdk_init is called in commit:
INIT:GCC with rtos: Align pre-main initialization steps between TCs

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();
}
Expand Down
4 changes: 2 additions & 2 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F0/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F0/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F1/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F1/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F3/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F3/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,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
Expand Down
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F3/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F4/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down
4 changes: 0 additions & 4 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F4/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F7/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand Down
3 changes: 0 additions & 3 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F7/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ static void init_uart(serial_t *obj)
UartHandle.Init.Mode = UART_MODE_TX_RX;
}

// Fix because HAL_RCC_GetHCLKFreq() don't update anymore SystemCoreClock
SystemCoreClockUpdate();

if (HAL_UART_Init(&UartHandle) != HAL_OK) {
error("Cannot initialize UART");
}
Expand Down
4 changes: 4 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L0/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L1/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L1/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L1/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
4 changes: 4 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L4/mbed_overrides.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 0 additions & 2 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L4/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions hal/targets/hal/TARGET_STM/TARGET_STM32L4/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
2 changes: 2 additions & 0 deletions rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,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
Expand All @@ -766,6 +767,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
Expand Down