[esp32] Use components/esp32/hal.cpp instead of core/hal/hal_esp32.cpp

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_<platform>.cpp pattern. Switch to the simpler
components/esp32/hal.cpp location instead:

- A component-internal .cpp only compiles when USE_<PLATFORM> 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/<platform>/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.
This commit is contained in:
J. Nick Koston
2026-04-29 05:53:18 -05:00
parent 5be4f746ba
commit 174a0b7631
3 changed files with 27 additions and 35 deletions
+2 -25
View File
@@ -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 <esp_ota_ops.h>
#include <esp_task_wdt.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
@@ -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
@@ -1,13 +1,22 @@
#ifdef USE_ESP32
#include "crash_handler.h"
#include "esphome/core/defines.h"
#include "esphome/core/hal.h"
#include <esp_clk_tree.h>
#include <esp_ota_ops.h>
#include <esp_system.h>
#include <esp_task_wdt.h>
#include <esp_timer.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
// 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);
-10
View File
@@ -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/<platform>/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
}