From bb76ee76ec4762eeb0bdda321ff5b1fe74c86252 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 29 Apr 2026 05:59:38 -0500 Subject: [PATCH] [esp8266] Inline arch_get_cpu_cycle_count() and arch_get_cpu_freq_hz() Both bodies are one-liners: arch_get_cpu_cycle_count() -> esp_get_cycle_count() arch_get_cpu_freq_hz() -> F_CPU Move them inline in core/hal/hal_esp8266.h: - esp_get_cycle_count() comes from (now pulled into hal_esp8266.h, small ESP8266-only header). - F_CPU is a -DF_CPU=80000000L compiler flag from the ESP8266 Arduino board defs, available without any include. Drop the cross-platform arch_get_cpu_freq_hz() declaration from the dispatcher hal.h and add bare decls to hal_esp32.h / hal_libretiny.h / hal_rp2040.h / hal_host.h / hal_zephyr.h so subsequent per-platform follow-ups only touch their own platform header. --- esphome/components/esp8266/hal.cpp | 3 --- esphome/core/hal.h | 6 +++--- esphome/core/hal/hal_esp32.h | 1 + esphome/core/hal/hal_esp8266.h | 7 +++++-- esphome/core/hal/hal_host.h | 1 + esphome/core/hal/hal_libretiny.h | 1 + esphome/core/hal/hal_rp2040.h | 1 + esphome/core/hal/hal_zephyr.h | 1 + 8 files changed, 13 insertions(+), 8 deletions(-) diff --git a/esphome/components/esp8266/hal.cpp b/esphome/components/esp8266/hal.cpp index 12e4d7159d..ef93a95273 100644 --- a/esphome/components/esp8266/hal.cpp +++ b/esphome/components/esp8266/hal.cpp @@ -97,9 +97,6 @@ void arch_restart() { } } -uint32_t IRAM_ATTR HOT arch_get_cpu_cycle_count() { return esp_get_cycle_count(); } -uint32_t arch_get_cpu_freq_hz() { return F_CPU; } - } // namespace esphome // Linker wrap: redirect all ::millis() calls (Arduino libs, ISRs) to our accumulator. diff --git a/esphome/core/hal.h b/esphome/core/hal.h index 0498afe18c..a53296979c 100644 --- a/esphome/core/hal.h +++ b/esphome/core/hal.h @@ -31,10 +31,10 @@ namespace esphome { // Cross-platform declarations. delayMicroseconds(), arch_feed_wdt(), -// arch_get_cpu_cycle_count(), arch_init() vary per platform (some inline, -// some out-of-line) so they live in hal/hal_.h. +// arch_get_cpu_cycle_count(), arch_init(), arch_get_cpu_freq_hz() vary +// per platform (some inline, some out-of-line) so they live in +// hal/hal_.h. void __attribute__((noreturn)) arch_restart(); -uint32_t arch_get_cpu_freq_hz(); #ifndef USE_ESP8266 // All non-ESP8266 platforms: PROGMEM is a no-op, so these are direct dereferences. diff --git a/esphome/core/hal/hal_esp32.h b/esphome/core/hal/hal_esp32.h index 7bbd3406ee..d5d7752bf6 100644 --- a/esphome/core/hal/hal_esp32.h +++ b/esphome/core/hal/hal_esp32.h @@ -43,6 +43,7 @@ __attribute__((always_inline)) inline void arch_feed_wdt() { esp_task_wdt_reset( __attribute__((always_inline)) inline uint32_t arch_get_cpu_cycle_count() { return esp_cpu_get_cycle_count(); } void arch_init(); +uint32_t arch_get_cpu_freq_hz(); } // namespace esphome diff --git a/esphome/core/hal/hal_esp8266.h b/esphome/core/hal/hal_esp8266.h index 84b3f5cc68..0b8fca6aa5 100644 --- a/esphome/core/hal/hal_esp8266.h +++ b/esphome/core/hal/hal_esp8266.h @@ -3,6 +3,7 @@ #ifdef USE_ESP8266 #include +#include #include #include @@ -59,8 +60,10 @@ __attribute__((always_inline)) inline uint16_t progmem_read_uint16(const uint16_ __attribute__((always_inline)) inline void delayMicroseconds(uint32_t us) { delay_microseconds_safe(us); } __attribute__((always_inline)) inline void arch_feed_wdt() { system_soft_wdt_feed(); } __attribute__((always_inline)) inline void arch_init() {} - -uint32_t arch_get_cpu_cycle_count(); +// esp_get_cycle_count() declared in ; F_CPU is a +// compiler-driven macro from the ESP8266 Arduino board defs (-DF_CPU=...). +__attribute__((always_inline)) inline uint32_t arch_get_cpu_cycle_count() { return esp_get_cycle_count(); } +__attribute__((always_inline)) inline uint32_t arch_get_cpu_freq_hz() { return F_CPU; } } // namespace esphome diff --git a/esphome/core/hal/hal_host.h b/esphome/core/hal/hal_host.h index 5d378ac8a8..a8896fdf63 100644 --- a/esphome/core/hal/hal_host.h +++ b/esphome/core/hal/hal_host.h @@ -23,6 +23,7 @@ void delayMicroseconds(uint32_t us); // NOLINT(readability-identifier-naming) void arch_feed_wdt(); uint32_t arch_get_cpu_cycle_count(); void arch_init(); +uint32_t arch_get_cpu_freq_hz(); } // namespace esphome diff --git a/esphome/core/hal/hal_libretiny.h b/esphome/core/hal/hal_libretiny.h index af9f38da01..ecfe830fe3 100644 --- a/esphome/core/hal/hal_libretiny.h +++ b/esphome/core/hal/hal_libretiny.h @@ -92,6 +92,7 @@ void delayMicroseconds(uint32_t us); // NOLINT(readability-identifier-naming) void arch_feed_wdt(); uint32_t arch_get_cpu_cycle_count(); void arch_init(); +uint32_t arch_get_cpu_freq_hz(); } // namespace esphome diff --git a/esphome/core/hal/hal_rp2040.h b/esphome/core/hal/hal_rp2040.h index 7a2d21ae93..46f6e421cd 100644 --- a/esphome/core/hal/hal_rp2040.h +++ b/esphome/core/hal/hal_rp2040.h @@ -39,6 +39,7 @@ void delayMicroseconds(uint32_t us); // NOLINT(readability-identifier-naming) void arch_feed_wdt(); uint32_t arch_get_cpu_cycle_count(); void arch_init(); +uint32_t arch_get_cpu_freq_hz(); } // namespace esphome diff --git a/esphome/core/hal/hal_zephyr.h b/esphome/core/hal/hal_zephyr.h index 5f39d6d3af..d4b37b5eb6 100644 --- a/esphome/core/hal/hal_zephyr.h +++ b/esphome/core/hal/hal_zephyr.h @@ -23,6 +23,7 @@ void delayMicroseconds(uint32_t us); // NOLINT(readability-identifier-naming) void arch_feed_wdt(); uint32_t arch_get_cpu_cycle_count(); void arch_init(); +uint32_t arch_get_cpu_freq_hz(); } // namespace esphome