ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
crash_handler.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
4#ifdef USE_LIBRETINY_CRASH_HANDLER
5
6#include "crash_handler.h"
7
8#if !defined(USE_BK72XX) && !defined(USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH)
9#error "The LibreTiny crash handler has no fault capture for this chip family"
10#endif
11
12#include "core.h"
13#include <FreeRTOS.h>
15#include "esphome/core/log.h"
17
18#include <cinttypes>
19#include <cstddef>
20#include <cstdint>
21#include <cstdio>
22
23#ifdef USE_BK72XX
24extern "C" {
25#include <arm_mcu_pub.h> // MCU_REG_BACKUP_SP_*
26#include <intc.h> // struct arm_registers
27}
28
29// On a fault the SDK (boot_handlers.S, PUSH_SVC_REG) builds a struct arm_registers frame and branches
30// to bk_trap_udef/pabt/dabt, which record the crash type, print a dump sdk_silent hides and spin for
31// the watchdog. The wrappers (-Wl,--wrap) first save the fault location and a stack scan for the
32// next boot. pc is the exception LR; PUSH_SVC_REG stores pc and spsr at these word offsets.
33static_assert(sizeof(arm_registers) == 18 * sizeof(uint32_t), "SDK exception frame changed size");
34static_assert(offsetof(arm_registers, pc) == 15 * 4 && offsetof(arm_registers, spsr) == 16 * 4,
35 "SDK exception frame layout changed");
36static_assert(LT_HW_WATCHDOG, "the crash reboot relies on the hardware watchdog");
37
38// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
39extern "C" {
40void __real_bk_trap_udef(arm_registers *regs);
41void __real_bk_trap_pabt(arm_registers *regs);
42void __real_bk_trap_dabt(arm_registers *regs);
43// Bounds of the app's flash image and its ITCM code (linker symbols).
44extern char _vector_start[];
45extern char _data_flash_begin[];
46extern char _itcmcode_ram_begin[];
47extern char _itcmcode_ram_end[];
48}
49// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
50#endif // USE_BK72XX
51
52#ifdef USE_LIBRETINY_VARIANT_RTL8720C
53// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
54extern "C" {
55// Bounds of the code in flash (XIP) and in RAM (linker symbols, rtl8710c_ram.ld).
56extern char __xip_code_text_start__[];
57extern char __xip_code_text_end__[];
58extern char __ram_entry_text_start__[];
59extern char __ram_code_text_end__[];
60}
61// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
62#endif // USE_LIBRETINY_VARIANT_RTL8720C
63
64#ifdef USE_LIBRETINY_VARIANT_RTL8710B
65// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
66extern "C" {
67// Bounds of the code in flash (XIP) and of the IRAM_ATTR code (linker symbols; the latter from patch_linker.py).
68extern char __flash_text_start__[];
69extern char __flash_text_end__[];
70extern char __esphome_sram_text_start[];
71extern char __esphome_sram_text_end[];
72}
73// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
74#endif // USE_LIBRETINY_VARIANT_RTL8710B
75
76#ifdef USE_LN882X
77// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
78extern "C" {
79// Bounds of the code in flash and of the IRAM_ATTR code (linker symbols; the latter from patch_linker.py).
80extern char _stext[];
81extern char _etext[];
84}
85// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
86#endif // USE_LN882X
87
88namespace esphome::libretiny {
89
90ESPHOME_LOG_TAG(TAG, "libretiny.crash");
91
92// 0xDEAD sentinel, then the record layout version.
93static constexpr uint32_t CRASH_MAGIC_V2 = 0xDEAD0002;
94
95// BK72xx: the ARM9 exception taken.
96static constexpr uint32_t CRASH_REASON_UNDEFINED = 1;
97static constexpr uint32_t CRASH_REASON_PREFETCH_ABORT = 2;
98static constexpr uint32_t CRASH_REASON_DATA_ABORT = 3;
99// Cortex-M families: the exception number (3 to 7) plus this offset.
100static constexpr uint32_t CRASH_REASON_CORTEX_M_OFFSET = 1;
101// After the Cortex-M ones (4 to 8).
102static constexpr uint32_t CRASH_REASON_ABORT = 9;
103PROGMEM_STRING_TABLE(CrashReasonStrings, "Unknown", "Undefined instruction", "Prefetch abort", "Data abort",
104 "Hard fault", "MemManage fault", "Bus fault", "Usage fault", "Secure fault", "Abort");
105
106static constexpr size_t MAX_BACKTRACE = 8;
107// Words scanned above sp for return addresses.
108static constexpr size_t STACK_SCAN_WORDS = 64;
109
110struct CrashRecord {
111 uint32_t magic;
112 uint32_t reason;
113 uint32_t pc;
114 uint32_t lr;
115 uint32_t sp;
116 uint32_t fault_status; // Cortex-M CFSR, 0 on BK72xx
117 uint32_t fault_address; // Cortex-M BFAR or MMFAR when valid, else 0
118 uint32_t backtrace[MAX_BACKTRACE];
119};
120
121#ifdef USE_BK72XX
122// Kept in ITCM, see patch_noinit.py.script.
123#define CRASH_RECORD_SECTION ".noinit"
124#elif defined(USE_LN882X)
125// ln882hki_bsp.template.ld puts .noinit in RETENTION RAM, which startup does not clear.
126#define CRASH_RECORD_SECTION ".noinit"
127#elif defined(USE_LIBRETINY_VARIANT_RTL8710B)
128// The AmebaZ linker script puts .bfsram.data* (the FreeRTOS heap) after the .bss that startup zeroes, and the
129// image the bootloader loads ends before .bss.
130#define CRASH_RECORD_SECTION ".bfsram.data.esphome_crash"
131#else
132// rtl8710c_ram.ld keeps .ram.bss.noinit* out of the startup zeroing.
133#define CRASH_RECORD_SECTION ".ram.bss.noinit.esphome_crash"
134#endif
135// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
136__attribute__((section(CRASH_RECORD_SECTION))) static CrashRecord s_record;
137
138static bool s_record_read = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
139static bool s_record_valid = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
140
142 if (!s_record_read) {
143 s_record_read = true;
144 s_record_valid = s_record.magic == CRASH_MAGIC_V2;
145 // Clear the magic so the record is reported once.
146 s_record.magic = 0;
147 }
148 return s_record_valid;
149}
150
151// One ESP_LOGE per line: the CLI decodes per line and partial output still shows.
154 return;
155
156 ESP_LOGE(TAG, "*** CRASH DETECTED ON PREVIOUS BOOT ***");
157 ESP_LOGE(TAG, " Reason: %s", LOG_STR_ARG(CrashReasonStrings::get_log_str(s_record.reason, 0)));
158 if (s_record.fault_status != 0) {
159 ESP_LOGE(TAG, " CFSR: 0x%08" PRIX32 " Address: 0x%08" PRIX32, s_record.fault_status, s_record.fault_address);
160 }
161 ESP_LOGE(TAG, " PC: 0x%08" PRIX32 " (fault location)", s_record.pc);
162 // 0 for abort(), and for a Cortex-M frame outside SRAM
163 if (s_record.lr != 0) {
164 ESP_LOGE(TAG, " LR: 0x%08" PRIX32 " (return address)", s_record.lr);
165 }
166 ESP_LOGE(TAG, " SP: 0x%08" PRIX32, s_record.sp);
167 uint8_t count = 0;
168 for (; count < MAX_BACKTRACE && s_record.backtrace[count] != 0; count++) {
169 ESP_LOGE(TAG, " BT%d: 0x%08" PRIX32 " (stack backtrace)", count, s_record.backtrace[count]);
170 }
171 // addr2line hint with every address
172 char hint[160];
173 int pos = snprintf(hint, sizeof(hint), "Use: addr2line -pfiaC -e raw_firmware.elf 0x%08" PRIX32, s_record.pc);
174 if (s_record.lr != 0)
175 pos += snprintf(hint + pos, sizeof(hint) - pos, " 0x%08" PRIX32, s_record.lr);
176 for (uint8_t i = 0; i < count && pos < (int) sizeof(hint) - 12; i++) {
177 pos += snprintf(hint + pos, sizeof(hint) - pos, " 0x%08" PRIX32, s_record.backtrace[i]);
178 }
179 ESP_LOGE(TAG, "%s", hint);
180}
181
182// --- Crash time: exception mode, a few dozen bytes of stack: no printing, no large locals.
183
184static inline bool is_code_addr(uint32_t val) {
185 uint32_t addr = val & ~1u; // clear the Thumb bit of a return address
186#ifdef USE_BK72XX
187 return (addr >= (uint32_t) _vector_start && addr < (uint32_t) _data_flash_begin) ||
189#elif defined(USE_LIBRETINY_VARIANT_RTL8710B)
190 return (addr >= (uint32_t) __flash_text_start__ && addr < (uint32_t) __flash_text_end__) ||
192#elif defined(USE_LN882X)
193 return (addr >= (uint32_t) _stext && addr < (uint32_t) _etext) ||
195#else
196 return (addr >= (uint32_t) __xip_code_text_start__ && addr < (uint32_t) __xip_code_text_end__) ||
198#endif
199}
200
201static void fill_record(uint32_t reason, uint32_t pc, uint32_t lr, uint32_t sp, uint32_t fault_status,
202 uint32_t fault_address) {
203 s_record.reason = reason;
204 s_record.pc = pc;
205 s_record.lr = lr;
206 s_record.sp = sp;
207 s_record.fault_status = fault_status;
208 s_record.fault_address = fault_address;
209}
210
211// Return addresses above sp give the backtrace; then the magic marks the record complete.
212static void finish_record(uint32_t sp, uint32_t sram_start, uint32_t sram_end) {
213 uint32_t count = 0;
214 if (sp >= sram_start && sp < sram_end && (sp & 3) == 0) {
215 auto *scan = (const uint32_t *) sp; // NOLINT(performance-no-int-to-ptr)
216 auto *end = (const uint32_t *) sram_end; // NOLINT(performance-no-int-to-ptr)
217 for (size_t i = 0; i < STACK_SCAN_WORDS && scan < end && count < MAX_BACKTRACE; i++, scan++) {
218 uint32_t val = *scan;
219 if (is_code_addr(val) && val != s_record.lr && (val & ~1u) != s_record.pc)
220 s_record.backtrace[count++] = val;
221 }
222 }
223 if (count < MAX_BACKTRACE)
224 s_record.backtrace[count] = 0; // terminator, the reader stops at the first 0
225 s_record.magic = CRASH_MAGIC_V2;
226}
227
228#ifdef USE_BK72XX
229
230// With Wi-Fi up the boot-time watchdog no longer fires: any interrupts-off spin hangs until power is
231// cycled (BK7238 and BK7231N; cause not in the open SDK). Re-armed here, long enough for the SDK's
232// register dump when sdk_silent is off.
233static constexpr uint32_t CRASH_REBOOT_WDT_MS = 500;
234
235// SRAM: 192 KB on the BK7231N, 256 KB on the BK7238.
236static constexpr uint32_t SRAM_START = 0x00400000;
237#ifdef USE_LIBRETINY_VARIANT_BK7231N
238static constexpr uint32_t SRAM_END = 0x00430000;
239#else
240static constexpr uint32_t SRAM_END = 0x00440000;
241#endif
242
243static constexpr uint32_t CPSR_MODE_MASK = 0x1F;
244static constexpr uint32_t CPSR_MODE_USR = 0x10;
245static constexpr uint32_t CPSR_MODE_FIQ = 0x11;
246static constexpr uint32_t CPSR_MODE_IRQ = 0x12;
247static constexpr uint32_t CPSR_MODE_SVC = 0x13;
248static constexpr uint32_t CPSR_MODE_ABT = 0x17;
249static constexpr uint32_t CPSR_MODE_UND = 0x1B;
250static constexpr uint32_t CPSR_MODE_SYS = 0x1F;
251static constexpr uint32_t CPSR_THUMB = 1u << 5;
252
253// PUSH_ALL_ARM_REG (boot_handlers.S) saves every mode's registers at the arm_mcu_pub.h addresses
254// before bk_trap_*; lr (r14) follows sp (r13).
255struct SavedBank {
256 uint32_t mode;
257 uint32_t sp_addr;
258};
259static constexpr SavedBank SAVED_BANKS[] = {
260 {CPSR_MODE_SYS, MCU_REG_BACKUP_SP_SYS}, {CPSR_MODE_IRQ, MCU_REG_BACKUP_SP_IRQ},
261 {CPSR_MODE_FIQ, MCU_REG_BACKUP_SP_FIQ}, {CPSR_MODE_ABT, MCU_REG_BACKUP_SP_ABT},
262 {CPSR_MODE_UND, MCU_REG_BACKUP_SP_UND}, {CPSR_MODE_SVC, MCU_REG_BACKUP_SP_SVC},
263};
264
265// sp and lr stay 0 for an unknown mode (corrupt spsr).
266static void read_saved_sp_lr(uint32_t mode, uint32_t *sp, uint32_t *lr) {
267 for (const auto &bank : SAVED_BANKS) {
268 if (bank.mode != mode)
269 continue;
270 const auto *regs = (const volatile uint32_t *) bank.sp_addr; // NOLINT(performance-no-int-to-ptr)
271 *sp = regs[0];
272 *lr = regs[1];
273 return;
274 }
275}
276
277static void record_crash(arm_registers *regs, uint32_t reason, uint32_t pc_offset) {
278 uint32_t mode = regs->spsr & CPSR_MODE_MASK;
279 if (mode == CPSR_MODE_USR)
280 mode = CPSR_MODE_SYS; // same register bank
281 uint32_t sp = 0;
282 uint32_t lr = 0;
283 read_saved_sp_lr(mode, &sp, &lr);
284
285 fill_record(reason, regs->pc - pc_offset, lr, sp, 0, 0);
286 finish_record(sp, SRAM_START, SRAM_END);
287
288 lt_wdt_enable(CRASH_REBOOT_WDT_MS);
289}
290
291// The exception LR is past the fault: one instruction (undefined), 4 bytes (prefetch), 8 (data abort).
292// NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
293extern "C" void __wrap_bk_trap_udef(arm_registers *regs) {
294 record_crash(regs, CRASH_REASON_UNDEFINED, (regs->spsr & CPSR_THUMB) ? 2 : 4);
296}
297
298extern "C" void __wrap_bk_trap_pabt(arm_registers *regs) {
299 record_crash(regs, CRASH_REASON_PREFETCH_ABORT, 4);
301}
302
303extern "C" void __wrap_bk_trap_dabt(arm_registers *regs) {
304 record_crash(regs, CRASH_REASON_DATA_ABORT, 8);
306}
307// NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
308
309#endif // USE_BK72XX
310
311#ifdef USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH
312
313// The SDK's RAM vector table (VTOR) points the fault vectors at a handler that prints a register dump and then
314// spins: a ROM handler on the RTL8720C and RTL8710B, CmBacktrace (hal_interrupt.c) on the LN882H.
315// crash_handler_install() puts esphome_fault_entry() in front of it, as the RTL8720C SDK's own CONFIG_MIIO patch
316// (app_start.c) does.
317static_assert(LT_HW_WATCHDOG, "the crash reboot relies on the hardware watchdog");
318// The RTL8720C ROM dump takes about 0.4 s at 115200 baud; without the re-arm the reset waits for the 10 s
319// watchdog from arch_init().
320static constexpr uint32_t CRASH_REBOOT_WDT_MS = 1000;
321static constexpr uint32_t FIRST_FAULT_VECTOR = 3; // HardFault, then MemManage, BusFault, UsageFault
322#ifdef USE_LIBRETINY_VARIANT_RTL8720C
323static constexpr uint32_t FAULT_VECTOR_COUNT = 5; // and SecureFault (ARMv8-M)
324// DTCM, where the app's stacks and heap live (rtl8710c_ram.ld).
325static constexpr uint32_t SRAM_START = 0x10000000;
326static constexpr uint32_t SRAM_END = 0x1003FA00;
327#elif defined(USE_LIBRETINY_VARIANT_RTL8710B)
328static constexpr uint32_t FAULT_VECTOR_COUNT = 4;
329// All of SRAM: the heap regions span it (heap_5.c) and MSP starts at 0x1003EFFC.
330static constexpr uint32_t SRAM_START = 0x10000000;
331static constexpr uint32_t SRAM_END = 0x10040000;
332#else
333static constexpr uint32_t FAULT_VECTOR_COUNT = 4;
334// RAM0, where the stacks and heap live (ln882hki_bsp.template.ld).
335static constexpr uint32_t SRAM_START = 0x20000000;
336static constexpr uint32_t SRAM_END = 0x20049C00;
337#endif
338
339// Architectural System Control Block registers (ARMv7-M and ARMv8-M).
340static constexpr uintptr_t SCB_VTOR = 0xE000ED08;
341static constexpr uintptr_t SCB_CFSR = 0xE000ED28;
342static constexpr uintptr_t SCB_MMFAR = 0xE000ED34;
343static constexpr uintptr_t SCB_BFAR = 0xE000ED38;
344static constexpr uint32_t CFSR_MMARVALID = 1u << 7;
345static constexpr uint32_t CFSR_BFARVALID = 1u << 15;
346
347// Exception frame: r0-r3, r12, lr, pc, xPSR, then the FPU registers when EXC_RETURN says so (FPU chips).
348static constexpr uint32_t FRAME_LR = 5;
349static constexpr uint32_t FRAME_PC = 6;
350static constexpr uint32_t FRAME_XPSR = 7;
351static constexpr uint32_t FRAME_WORDS = 8;
352static constexpr uint32_t EXTENDED_FRAME_WORDS = 26;
353static constexpr uint32_t EXC_RETURN_STANDARD_FRAME = 1u << 4;
354static constexpr uint32_t XPSR_STACK_ALIGNED = 1u << 9;
355
356// The SDK handlers fault_entry() continues into, by vector.
357static uint32_t s_sdk_fault_vectors[FAULT_VECTOR_COUNT]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
358static bool s_fault_recorded = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
359
360static inline uint32_t read_reg(uintptr_t addr) {
361 return *(const volatile uint32_t *) addr; // NOLINT(performance-no-int-to-ptr)
362}
363
364// Returns the SDK handler to continue into. pc, lr and sp stay 0 when the frame is outside SRAM.
365extern "C" __attribute__((used)) uint32_t esphome_record_fault(const uint32_t *frame, uint32_t vector,
367 // A later fault, in the recording below or in the SDK handler (CmBacktrace on the LN882H walks past the end
368 // of RAM), keeps the first record and waits for the watchdog instead of starting the dump again.
369 if (s_fault_recorded) {
370 while (true) {
371 }
372 }
373 s_fault_recorded = true;
374 lt_wdt_enable(CRASH_REBOOT_WDT_MS);
375 uint32_t cfsr = read_reg(SCB_CFSR);
377 if (cfsr & CFSR_BFARVALID) {
378 address = read_reg(SCB_BFAR);
379 } else if (cfsr & CFSR_MMARVALID) {
380 address = read_reg(SCB_MMFAR);
381 }
382 auto frame_addr = (uint32_t) frame;
383 uint32_t frame_words = (exc_return & EXC_RETURN_STANDARD_FRAME) ? FRAME_WORDS : EXTENDED_FRAME_WORDS;
387 if (frame_addr >= SRAM_START && frame_addr <= SRAM_END - frame_words * 4 && (frame_addr & 3) == 0) {
388 pc = frame[FRAME_PC];
389 lr = frame[FRAME_LR];
390 sp = frame_addr + frame_words * 4 + ((frame[FRAME_XPSR] & XPSR_STACK_ALIGNED) ? 4 : 0);
391 }
392 fill_record(vector + CRASH_REASON_CORTEX_M_OFFSET, pc, lr, sp, cfsr, address);
393 finish_record(sp, SRAM_START, SRAM_END);
394 return s_sdk_fault_vectors[vector - FIRST_FAULT_VECTOR];
395}
396
397// Fault vector: calls esphome_record_fault(frame, exception number, EXC_RETURN), then jumps to the SDK handler it
398// returns with r0-r3 and lr restored. Asm because C would push onto MSP before the frame is found, and the SDK
399// handler reads EXC_RETURN from lr.
400extern "C" __attribute__((naked, used)) void esphome_fault_entry() {
401 __asm volatile("push {r0-r3, r4, lr} \n" // r4 only keeps the stack 8-byte aligned
402 "tst lr, #4 \n"
403 "ite eq \n"
404 "mrseq r0, msp \n"
405 "mrsne r0, psp \n"
406 "it eq \n"
407 "addeq r0, r0, #24 \n" // MSP: skip the six words pushed above
408 "mrs r1, ipsr \n"
409 "ubfx r1, r1, #0, #9 \n" // exception number, 3 to 7
410 "mov r2, lr \n"
411 "bl esphome_record_fault \n"
412 "mov r12, r0 \n" // SDK handler address
413 "pop {r0-r3, r4, lr} \n"
414 "bx r12 \n");
415}
416
418 auto *vectors = (uint32_t *) read_reg(SCB_VTOR); // NOLINT(performance-no-int-to-ptr)
419 for (uint32_t i = 0; i < FAULT_VECTOR_COUNT; i++) {
420 s_sdk_fault_vectors[i] = vectors[FIRST_FAULT_VECTOR + i];
421 vectors[FIRST_FAULT_VECTOR + i] = (uint32_t) &esphome_fault_entry;
422 }
423}
424
425#endif // USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH
426
427// newlib's abort() spins in _exit() with no record and, on the BK7238, no watchdog reset. Interrupts go off first
428// because every task's delay() feeds the watchdog.
429// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp,readability-identifier-naming)
430extern "C" [[noreturn]] void __wrap_abort() {
431 portDISABLE_INTERRUPTS();
432 lt_wdt_enable(CRASH_REBOOT_WDT_MS);
433 uint32_t sp;
434 __asm volatile("mov %0, sp" : "=r"(sp));
435 // Inside the bl that called abort(): a noreturn call can be the caller's last instruction, so the return
436 // address may already be the next function. Bit 0 is the Thumb bit.
437 fill_record(CRASH_REASON_ABORT, ((uint32_t) __builtin_return_address(0) & ~1u) - 2, 0, sp, 0, 0);
438 // Valid without a backtrace, so a fault in the stack scan still leaves this record; every entry is cleared, so
439 // a scan cut short leaves none from an earlier crash
440 for (auto &addr : s_record.backtrace)
441 addr = 0;
442 s_record.magic = CRASH_MAGIC_V2;
443 // Keeps those writes ahead of the scan's reads
444 __asm volatile("" ::: "memory");
445#ifdef USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH
446 // A fault in the scan then spins for the watchdog instead of replacing this record or running the SDK dump
447 s_fault_recorded = true;
448#endif
449 finish_record(sp, SRAM_START, SRAM_END);
450 while (true) {
451 }
452}
453
454} // namespace esphome::libretiny
455
456namespace esphome {
457
458static_assert(libretiny::MAX_BACKTRACE + 1 <= CRASH_REPORT_MAX_BACKTRACE,
459 "Crash report backtrace too small for the record");
460
461static bool s_report_read = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
462
463bool crash_report_get(CrashReport &out) {
464 if (s_report_read || !libretiny::crash_handler_has_data())
465 return false;
466 const auto &record = libretiny::s_record;
467 out = {};
468 // The record has no build stamp; like ESP8266 and RP2, assume the running firmware wrote it
470 if (record.reason == libretiny::CRASH_REASON_ABORT) {
472 } else {
474 }
475 out.pc = record.pc;
476#ifdef USE_LIBRETINY_CRASH_HANDLER_VECTOR_PATCH
477 // Cortex-M CFSR, and the BFAR or MMFAR its valid bits mark (0 is a real address for a null dereference);
478 // BK72xx records neither
479 if (record.fault_status != 0) {
481 out.cause = record.fault_status;
482 }
483 if (record.fault_status & (libretiny::CFSR_BFARVALID | libretiny::CFSR_MMARVALID)) {
485 out.fault_address = record.fault_address;
486 }
487#endif
488 auto &core = out.cores[out.core_count++];
489 // 0 for abort(), and for a Cortex-M frame outside SRAM
490 if (record.lr != 0)
491 core.addresses[core.count++] = record.lr;
492 for (size_t i = 0; i < libretiny::MAX_BACKTRACE && record.backtrace[i] != 0; i++)
493 core.addresses[core.count++] = record.backtrace[i];
494 return true;
495}
496
497void crash_report_clear() { s_report_read = true; }
498
499} // namespace esphome
500
501#endif // USE_LIBRETINY_CRASH_HANDLER
502#endif // USE_LIBRETINY
BedjetMode mode
BedJet operating mode.
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
std::vector< uint8_t > & out
Definition gif.cpp:49
unsigned count
Definition gif.cpp:51
char _stext[]
char __ram_entry_text_start__[]
void __real_bk_trap_udef(arm_registers *regs)
char _data_flash_begin[]
char _itcmcode_ram_end[]
void __real_bk_trap_dabt(arm_registers *regs)
char _etext[]
char __xip_code_text_end__[]
char _itcmcode_ram_begin[]
void __real_bk_trap_pabt(arm_registers *regs)
char _vector_start[]
char __flash_text_start__[]
char __ram_code_text_end__[]
char __esphome_sram_text_start[]
char __esphome_sram_text_end[]
char __xip_code_text_start__[]
char __flash_text_end__[]
mopeka_std_values val[3]
PROGMEM_STRING_TABLE(CrashReasonStrings, "Unknown", "Undefined instruction", "Prefetch abort", "Data abort", "Hard fault", "MemManage fault", "Bus fault", "Usage fault", "Secure fault", "Abort")
void __wrap_bk_trap_udef(arm_registers *regs)
lt_wdt_enable(CRASH_REBOOT_WDT_MS)
void crash_handler_install()
Put the crash recorder in front of the SDK's fault handlers.
void __wrap_bk_trap_dabt(arm_registers *regs)
void __wrap_bk_trap_pabt(arm_registers *regs)
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
ESPHOME_LOG_TAG(TAG, "libretiny.crash")
bool crash_handler_has_data()
Returns true if crash data was found this boot, reading it first if needed.
uint32_t uint32_t exc_return
void crash_report_clear()
Mark the report read; it also no longer survives a reboot.
size_t size_t pos
Definition helpers.h:1493
@ CRASH_REPORT_FLAG_BUILD_MATCHES
@ CRASH_REPORT_FLAG_PRESENT
@ CRASH_REPORT_FLAG_CAUSE
@ CRASH_REPORT_FLAG_FAULT_ADDRESS
bool crash_report_get(CrashReport &out)
The previous boot's crash; false when there is none or it was read. Implemented per platform.
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17