From bbc31d70f4217057ca7a597a58532e5daa240c57 Mon Sep 17 00:00:00 2001 From: xuxingliang Date: Sat, 6 Jul 2024 19:04:04 +0800 Subject: [PATCH] drivers: add API for drivers early initialization Signed-off-by: xuxingliang --- drivers/drivers_initialize.c | 20 +++++++++++ drivers/note/note_initialize.c | 60 ++++++++++++++++++++++---------- include/nuttx/drivers/drivers.h | 11 ++++++ include/nuttx/note/note_driver.h | 22 ++++++++++-- sched/init/nx_start.c | 10 +++--- 5 files changed, 98 insertions(+), 25 deletions(-) diff --git a/drivers/drivers_initialize.c b/drivers/drivers_initialize.c index 70b10f257c..8a52b47c3d 100644 --- a/drivers/drivers_initialize.c +++ b/drivers/drivers_initialize.c @@ -53,6 +53,26 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: drivers_early_initialize + * + * Description: + * drivers_early_initialize will be called once before OS initialization + * when no system resource is ready to use. + * + * drivers_early_initialize serves the purpose of bringing up drivers as + * early as possible, so they can be used even during OS initialization. + * It must not rely on any system resources, such as heap memory. + * + ****************************************************************************/ + +void drivers_early_initialize(void) +{ +#ifdef CONFIG_DRIVERS_NOTE + note_early_initialize(); +#endif +} + /**************************************************************************** * Name: drivers_initialize * diff --git a/drivers/note/note_initialize.c b/drivers/note/note_initialize.c index bcece5019f..5d50728925 100644 --- a/drivers/note/note_initialize.c +++ b/drivers/note/note_initialize.c @@ -35,6 +35,46 @@ * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: note_early_initialize + * + * Description: + * Registers note drivers early, without depending on system features + * such as heap memory. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success. A negated errno value is returned on a failure. + * + ****************************************************************************/ + +int note_early_initialize(void) +{ + int ret = 0; + +#ifdef CONFIG_SEGGER_SYSVIEW + ret = note_sysview_initialize(); + if (ret < 0) + { + serr("note_sysview_initialize failed %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_DRIVERS_NOTESNAP + ret = notesnap_register(); + if (ret < 0) + { + serr("notesnap_register failed %d\n", ret); + return ret; + } +#endif + + return ret; +} + /**************************************************************************** * Name: note_initialize * @@ -46,7 +86,7 @@ * None. * * Returned Value: - * Zero on succress. A negated errno value is returned on a failure. + * Zero on success. A negated errno value is returned on a failure. * ****************************************************************************/ @@ -81,23 +121,5 @@ int note_initialize(void) } #endif -#ifdef CONFIG_SEGGER_SYSVIEW - ret = note_sysview_initialize(); - if (ret < 0) - { - serr("note_sysview_initialize failed %d\n", ret); - return ret; - } -#endif - -#ifdef CONFIG_DRIVERS_NOTESNAP - ret = notesnap_register(); - if (ret < 0) - { - serr("notesnap_register failed %d\n", ret); - return ret; - } -#endif - return ret; } diff --git a/include/nuttx/drivers/drivers.h b/include/nuttx/drivers/drivers.h index de025d7d00..3dec180225 100644 --- a/include/nuttx/drivers/drivers.h +++ b/include/nuttx/drivers/drivers.h @@ -43,6 +43,17 @@ extern "C" #define EXTERN extern #endif +/**************************************************************************** + * Name: drivers_early_initialize + * + * Description: + * Performs one-time, early driver initialization that doesn't rely on OS + * resources being ready. + * + ****************************************************************************/ + +void drivers_early_initialize(void); + /**************************************************************************** * Name: drivers_initialize * diff --git a/include/nuttx/note/note_driver.h b/include/nuttx/note/note_driver.h index 8e1de97037..ddea033743 100644 --- a/include/nuttx/note/note_driver.h +++ b/include/nuttx/note/note_driver.h @@ -112,6 +112,25 @@ struct note_driver_s #if defined(__KERNEL__) || defined(CONFIG_BUILD_FLAT) +#ifdef CONFIG_DRIVERS_NOTE + +/**************************************************************************** + * Name: note_early_initialize + * + * Description: + * Early register sched note related drivers that do not rely on system + * features like mm. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success. A negative errno value is returned on a failure. + * + ****************************************************************************/ + +int note_early_initialize(void); + /**************************************************************************** * Name: note_initialize * @@ -123,11 +142,10 @@ struct note_driver_s * None. * * Returned Value: - * Zero on succress. A negated errno value is returned on a failure. + * Zero on success. A negative errno value is returned on a failure. * ****************************************************************************/ -#ifdef CONFIG_DRIVERS_NOTE int note_initialize(void); #endif diff --git a/sched/init/nx_start.c b/sched/init/nx_start.c index a74b5ea22a..997612739c 100644 --- a/sched/init/nx_start.c +++ b/sched/init/nx_start.c @@ -518,10 +518,6 @@ void nx_start(void) g_nx_initstate = OSINIT_BOOT; - /* Initialize RTOS Data ***************************************************/ - - sched_trace_begin(); - /* Initialize task list table *********************************************/ tasklist_initialize(); @@ -534,6 +530,12 @@ void nx_start(void) g_nx_initstate = OSINIT_TASKLISTS; + /* Initialize RTOS Data ***************************************************/ + + drivers_early_initialize(); + + sched_trace_begin(); + /* Initialize RTOS facilities *********************************************/ /* Initialize the semaphore facility. This has to be done very early