diff --git a/esphome/components/esp32/core.cpp b/esphome/components/esp32/core.cpp index 267052ea2e3..5249f4a59e0 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 a881953208c..3a0836a5073 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 ae2a40d0363..b4e81ce49fa 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 }