From ca623ace1a7d8c829edadd6aa9f8234e1bfd6393 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 5 Apr 2026 12:43:18 -1000 Subject: [PATCH] Remove s_crash_valid and crash_handler_read_and_clear MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit resetInfo is a global that persists until next reset — no need to cache the crash validity in a separate bool. Check resetInfo.reason directly in crash_handler_has_data() and crash_handler_log(). This eliminates the only RAM byte from the crash handler and removes the now-empty crash_handler_read_and_clear() function and its call from arch_init(). --- esphome/components/esp8266/core.cpp | 9 +-------- esphome/components/esp8266/crash_handler.cpp | 15 +++++---------- esphome/components/esp8266/crash_handler.h | 3 --- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/esphome/components/esp8266/core.cpp b/esphome/components/esp8266/core.cpp index 5db5b064d4..159ec20e77 100644 --- a/esphome/components/esp8266/core.cpp +++ b/esphome/components/esp8266/core.cpp @@ -2,9 +2,6 @@ #include "core.h" #include "esphome/core/defines.h" -#ifdef USE_ESP8266_CRASH_HANDLER -#include "crash_handler.h" -#endif #include "esphome/core/hal.h" #include "esphome/core/time_64.h" #include "esphome/core/helpers.h" @@ -31,11 +28,7 @@ void arch_restart() { yield(); } } -void arch_init() { -#ifdef USE_ESP8266_CRASH_HANDLER - esp8266::crash_handler_read_and_clear(); -#endif -} +void arch_init() {} void HOT arch_feed_wdt() { system_soft_wdt_feed(); } uint8_t progmem_read_byte(const uint8_t *addr) { diff --git a/esphome/components/esp8266/crash_handler.cpp b/esphome/components/esp8266/crash_handler.cpp index abda1923bb..4651291e0a 100644 --- a/esphome/components/esp8266/crash_handler.cpp +++ b/esphome/components/esp8266/crash_handler.cpp @@ -87,17 +87,12 @@ namespace esphome::esp8266 { static const char *const TAG = "esp8266.crash"; -// Whether the previous boot was a crash. Set once in crash_handler_read_and_clear(). -// resetInfo and RTC backtrace data persist until the next reset, so no caching needed. -static bool s_crash_valid = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) - -bool crash_handler_has_data() { return s_crash_valid; } - -void crash_handler_read_and_clear() { - uint32_t reason = resetInfo.reason; - s_crash_valid = (reason == REASON_WDT_RST || reason == REASON_EXCEPTION_RST || reason == REASON_SOFT_WDT_RST); +static inline bool is_crash_reason(uint32_t reason) { + return reason == REASON_WDT_RST || reason == REASON_EXCEPTION_RST || reason == REASON_SOFT_WDT_RST; } +bool crash_handler_has_data() { return is_crash_reason(resetInfo.reason); } + // Xtensa exception cause names (shared with ESP32, same ISA). // Keep in sync with Xtensa ISA reference manual Table 4-64. // Uses if-else with LOG_STR instead of switch to avoid CSWTCH jump tables @@ -195,7 +190,7 @@ static uint8_t read_rtc_backtrace(uint32_t *backtrace, size_t max_entries) { // crashes again during boot, and allowing the CLI's process_stacktrace to match // and decode each address individually. void crash_handler_log() { - if (!s_crash_valid) + if (!is_crash_reason(resetInfo.reason)) return; // Read and filter backtrace from RTC into stack-local buffer (no persistent RAM cost). diff --git a/esphome/components/esp8266/crash_handler.h b/esphome/components/esp8266/crash_handler.h index def94fbbe9..2d42d07a7e 100644 --- a/esphome/components/esp8266/crash_handler.h +++ b/esphome/components/esp8266/crash_handler.h @@ -8,9 +8,6 @@ namespace esphome::esp8266 { -/// Check if previous boot was a crash and set validity flag. -void crash_handler_read_and_clear(); - /// Log crash data if a crash was detected on previous boot. void crash_handler_log();