From 174a0b7631c36a6f8dde3699678c5c585a61c021 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 29 Apr 2026 05:53:18 -0500 Subject: [PATCH] [esp32] Use components/esp32/hal.cpp instead of core/hal/hal_esp32.cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous commit placed the out-of-line ESP32 HAL bodies under core/hal/hal_esp32.cpp and gated compilation via FILTER_SOURCE_FILES, mirroring the wake/wake_.cpp pattern. Switch to the simpler components/esp32/hal.cpp location instead: - A component-internal .cpp only compiles when USE_ is set, so no FILTER_SOURCE_FILES entry is needed (revert that addition). - The file can include the component's private crash_handler.h header directly without a layering inversion or forward decl, so arch_init() also moves into hal.cpp now (previously stuck in core.cpp because of that header dependency). - A future consolidation PR will move the existing core/wake/wake_*.cpp files into components//wake.cpp the same way and drop their FILTER_SOURCE_FILES entries. ci-custom's lint_namespace check is satisfied with an empty namespace esphome::esp32 {} block at the top of the file — the HAL functions themselves live in namespace esphome (root) since they are not part of the esp32 component's API. --- esphome/components/esp32/core.cpp | 27 ++----------------- .../esp32/hal.cpp} | 25 +++++++++++++++++ esphome/core/config.py | 10 ------- 3 files changed, 27 insertions(+), 35 deletions(-) rename esphome/{core/hal/hal_esp32.cpp => components/esp32/hal.cpp} (57%) diff --git a/esphome/components/esp32/core.cpp b/esphome/components/esp32/core.cpp index 267052ea2e..5249f4a59e 100644 --- a/esphome/components/esp32/core.cpp +++ b/esphome/components/esp32/core.cpp @@ -1,13 +1,8 @@ #ifdef USE_ESP32 -#include "esphome/core/defines.h" -#include "crash_handler.h" #include "esphome/core/application.h" -#include "esphome/core/hal.h" -#include "esphome/core/helpers.h" +#include "esphome/core/defines.h" #include "preferences.h" -#include -#include #include #include @@ -18,25 +13,7 @@ extern "C" __attribute__((weak)) void initArduino() {} namespace esphome { -// yield(), delay(), micros(), millis_64() inlined in hal.h. -// millis(), arch_restart(), arch_get_cpu_freq_hz() out-of-line in hal/hal_esp32.cpp. -// delayMicroseconds(), arch_feed_wdt(), arch_get_cpu_cycle_count() inlined in hal/hal_esp32.h. -void arch_init() { -#ifdef USE_ESP32_CRASH_HANDLER - // Read crash data from previous boot before anything else - esp32::crash_handler_read_and_clear(); -#endif - - // Enable the task watchdog only on the loop task (from which we're currently running) - esp_task_wdt_add(nullptr); - - // Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled, - // in which case safe_mode will mark it valid after confirming successful boot. -#ifndef USE_OTA_ROLLBACK - esp_ota_mark_app_valid_cancel_rollback(); -#endif -} - +// HAL functions live in hal.cpp. This file keeps only the loop task setup. TaskHandle_t loop_task_handle = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static StaticTask_t loop_task_tcb; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) static StackType_t diff --git a/esphome/core/hal/hal_esp32.cpp b/esphome/components/esp32/hal.cpp similarity index 57% rename from esphome/core/hal/hal_esp32.cpp rename to esphome/components/esp32/hal.cpp index a881953208..3a0836a507 100644 --- a/esphome/core/hal/hal_esp32.cpp +++ b/esphome/components/esp32/hal.cpp @@ -1,13 +1,22 @@ #ifdef USE_ESP32 +#include "crash_handler.h" +#include "esphome/core/defines.h" #include "esphome/core/hal.h" #include +#include #include +#include #include #include #include +// Empty esp32 namespace block to satisfy ci-custom's lint_namespace check. +// HAL functions live in namespace esphome (root) — they are not part of the +// esp32 component's API. +namespace esphome::esp32 {} // namespace esphome::esp32 + namespace esphome { // Use xTaskGetTickCount() when tick rate is 1 kHz (ESPHome's default via sdkconfig), @@ -33,6 +42,22 @@ void arch_restart() { } } +void arch_init() { +#ifdef USE_ESP32_CRASH_HANDLER + // Read crash data from previous boot before anything else + esp32::crash_handler_read_and_clear(); +#endif + + // Enable the task watchdog only on the loop task (from which we're currently running) + esp_task_wdt_add(nullptr); + + // Handle OTA rollback: mark partition valid immediately unless USE_OTA_ROLLBACK is enabled, + // in which case safe_mode will mark it valid after confirming successful boot. +#ifndef USE_OTA_ROLLBACK + esp_ota_mark_app_valid_cancel_rollback(); +#endif +} + uint32_t arch_get_cpu_freq_hz() { uint32_t freq = 0; esp_clk_tree_src_get_freq_hz(SOC_MOD_CLK_CPU, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &freq); diff --git a/esphome/core/config.py b/esphome/core/config.py index ae2a40d036..b4e81ce49f 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -815,16 +815,6 @@ FILTER_SOURCE_FILES = filter_source_files_from_platform( "wake/wake_zephyr.cpp": { PlatformFramework.NRF52_ZEPHYR, }, - # Per-platform HAL out-of-line implementations — hal.h dispatches to - # exactly one platform header based on USE_*, and the matching .cpp - # provides the few HAL functions that stay out-of-line. Other - # platforms still keep their out-of-line bodies in - # components//core.cpp until their per-platform follow-up - # PR moves them here. - "hal/hal_esp32.cpp": { - PlatformFramework.ESP32_ARDUINO, - PlatformFramework.ESP32_IDF, - }, # Note: lock_free_queue.h and event_pool.h are header files and don't need to be filtered # as they are only included when needed by the preprocessor }