feat(system_init): introduce pre-scheduler and pre-app main handler registrations

- Added macros for registering handlers to run before the scheduler and app main.
- Updated various components to utilize the new registration system for initialization.
- Refactored app startup logic to streamline initialization sequence.
This commit is contained in:
Konstantin Kondrashov
2026-09-02 16:58:44 +03:00
parent 40f25003b4
commit 754ac1dd45
21 changed files with 348 additions and 129 deletions
@@ -0,0 +1,47 @@
/*
* SPDX-FileCopyrightText: 2026 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#pragma once
#include "esp_cpu.h"
#include "esp_private/esp_sys_event.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Register a handler to run on core 0 immediately before the scheduler starts
*/
#define ESP_PRE_SCHEDULER_HANDLER_REGISTER(fn, priority) \
static esp_err_t fn(void *user_arg, void *ctx); \
_ESP_SYS_EVENT_REGISTER(PRE_SCHEDULER, priority, __esp_pre_scheduler_##fn) \
{ \
if (esp_cpu_get_core_id() != 0) { \
return ESP_OK; \
} \
return fn(user_arg, ctx); \
} \
static esp_err_t fn(void *user_arg, void *ctx)
/**
* @brief Register a handler to run on every core immediately before its scheduler starts
*/
#define ESP_PRE_SCHEDULER_HANDLER_REGISTER_PER_CPU(fn, priority) \
_ESP_SYS_EVENT_REGISTER(PRE_SCHEDULER, priority, fn)
/**
* @brief Register a handler to run in the main task immediately before app_main
*
* Handlers run in ascending priority order after all CPUs have completed
* FreeRTOS initialization. The scheduler and heap allocator are available.
*/
#define ESP_PRE_APP_MAIN_HANDLER_REGISTER(fn, priority) \
_ESP_SYS_EVENT_REGISTER(PRE_APP_MAIN, priority, fn)
#ifdef __cplusplus
}
#endif
@@ -34,6 +34,8 @@
#define _ESP_SYS_EVENT_SECTION_SHUTDOWN esysev_shdn
#define _ESP_SYS_EVENT_SECTION_SYSTEM_INIT_CORE esysev_initc
#define _ESP_SYS_EVENT_SECTION_SYSTEM_INIT_SECONDARY esysev_inits
#define _ESP_SYS_EVENT_SECTION_PRE_SCHEDULER esysev_presc
#define _ESP_SYS_EVENT_SECTION_PRE_APP_MAIN esysev_pream
#define _ESP_SYS_EVENT_SECTION_PANIC esysev_panic
#define _ESP_SYS_EVENT_SECTION_PANIC_EARLY_BREAK esysev_paneb
#endif
@@ -42,5 +44,7 @@
ESP_SYS_EVENT_DEF(SHUTDOWN)
ESP_SYS_EVENT_DEF(SYSTEM_INIT_CORE)
ESP_SYS_EVENT_DEF(SYSTEM_INIT_SECONDARY)
ESP_SYS_EVENT_DEF(PRE_SCHEDULER)
ESP_SYS_EVENT_DEF(PRE_APP_MAIN)
ESP_SYS_EVENT_DEF(PANIC)
ESP_SYS_EVENT_DEF(PANIC_EARLY_BREAK)