4#ifdef USE_RP2_CRASH_HANDLER
11#include <hardware/regs/addressmap.h>
12#include <hardware/structs/watchdog.h>
13#include <hardware/watchdog.h>
24static constexpr uint32_t CRASH_MAGIC_SENTINEL = 0xDEAD0000;
25static constexpr uint32_t CRASH_DATA_VERSION = 1;
26static constexpr uint32_t CRASH_MAGIC_V1 = CRASH_MAGIC_SENTINEL | CRASH_DATA_VERSION;
42#if defined(PICO_RP2350)
43static constexpr uint32_t FLASH_SCAN_END = XIP_BASE + 0x400000;
45static constexpr uint32_t FLASH_SCAN_END = XIP_BASE + 0x200000;
50 return cleared >= XIP_BASE && cleared < FLASH_SCAN_END;
53static constexpr size_t MAX_BACKTRACE = 4;
60static struct CrashData {
66 uint8_t backtrace_count;
74static bool s_crash_data_read =
false;
78 return s_crash_data.valid;
82 if (s_crash_data_read)
84 s_crash_data_read =
true;
85 s_crash_data.valid =
false;
86 uint32_t magic = watchdog_hw->scratch[0];
87 if ((magic & 0xFFFF0000) == CRASH_MAGIC_SENTINEL && (magic & 0xFFFF) == CRASH_DATA_VERSION) {
88 s_crash_data.valid =
true;
89 s_crash_data.pc = watchdog_hw->scratch[1];
90 s_crash_data.lr = watchdog_hw->scratch[2];
91 s_crash_data.sp = watchdog_hw->scratch[3];
92 s_crash_data.backtrace_count = 0;
93 for (
size_t i = 0; i < MAX_BACKTRACE; i++) {
94 uint32_t addr = watchdog_hw->scratch[4 + i];
97 s_crash_data.backtrace[i] = addr;
98 s_crash_data.backtrace_count++;
102 for (
int i = 0; i < 8; i++) {
103 watchdog_hw->scratch[i] = 0;
116 ESP_LOGE(TAG,
"*** CRASH DETECTED ON PREVIOUS BOOT ***");
117 ESP_LOGE(TAG,
" PC: 0x%08" PRIX32
" (fault location)", s_crash_data.pc);
118 ESP_LOGE(TAG,
" LR: 0x%08" PRIX32
" (return address)", s_crash_data.lr);
119 ESP_LOGE(TAG,
" SP: 0x%08" PRIX32, s_crash_data.sp);
120 for (uint8_t i = 0; i < s_crash_data.backtrace_count; i++) {
121 ESP_LOGE(TAG,
" BT%d: 0x%08" PRIX32
" (stack backtrace)", i, s_crash_data.backtrace[i]);
125 int pos = snprintf(hint,
sizeof(hint),
"Use: addr2line -pfiaC -e firmware.elf 0x%08" PRIX32
" 0x%08" PRIX32,
126 s_crash_data.pc, s_crash_data.lr);
127 for (uint8_t i = 0; i < s_crash_data.backtrace_count &&
pos < (int)
sizeof(hint) - 12; i++) {
128 pos += snprintf(hint +
pos,
sizeof(hint) -
pos,
" 0x%08" PRIX32, s_crash_data.backtrace[i]);
130 ESP_LOGE(TAG,
"%s", hint);
137static_assert(MAX_BACKTRACE + 1 <= CRASH_REPORT_MAX_BACKTRACE,
"Crash report backtrace too small for the record");
139static bool s_report_read =
false;
148 out.pc = rp2::s_crash_data.pc;
149 auto &core =
out.cores[
out.core_count++];
150 core.addresses[core.count++] = rp2::s_crash_data.lr;
151 for (uint8_t i = 0; i < rp2::s_crash_data.backtrace_count; i++)
152 core.addresses[core.count++] = rp2::s_crash_data.backtrace[i];
171static inline bool is_valid_sram_ptr(
const uint32_t *ptr) {
172 auto addr =
reinterpret_cast<uintptr_t
>(
ptr);
175 return (addr % 4 == 0) && addr >= SRAM_BASE && (addr + 32) <= SRAM_END;
182 watchdog_reboot(0, 0, 10);
187 if (!is_valid_sram_ptr(frame)) {
188 watchdog_hw->scratch[0] = CRASH_MAGIC_V1;
189 watchdog_hw->scratch[1] = 0;
190 watchdog_hw->scratch[2] = 0;
191 watchdog_hw->scratch[3] =
reinterpret_cast<uintptr_t
>(frame);
192 for (
uint32_t i = 0; i < MAX_BACKTRACE; i++) {
193 watchdog_hw->scratch[4 + i] = 0;
196 __asm
volatile(
"nop");
204 static constexpr uint32_t EF_XPSR = 7;
210 watchdog_hw->scratch[0] = CRASH_MAGIC_V1;
211 watchdog_hw->scratch[1] = frame[EF_PC];
212 watchdog_hw->scratch[2] = frame[EF_LR];
229 if (is_code_addr(
val) &&
val != frame[EF_PC] &&
val != frame[EF_LR]) {
236 watchdog_hw->scratch[4 + i] = 0;
240 __asm
volatile(
"nop");
259 __asm
volatile(
"movs r0, #4 \n"
274 :
"i"(hard_fault_handler_c));
struct @67::@68 __attribute__
Wake the main loop task from an ISR. ISR-safe.
std::vector< uint8_t > & out
static void int int int int const uint8_t * ptr
bool crash_handler_has_data()
Returns true if crash data was found this boot, reading it first if needed.
void crash_handler_read_and_clear()
Read crash data from watchdog scratch registers and clear them.
ESPHOME_LOG_TAG(TAG, "rp2.crash")
void crash_handler_log()
Log crash data if a crash was detected on previous boot.
void crash_report_clear()
Mark the report read; it also no longer survives a reboot.
@ CRASH_REPORT_FLAG_BUILD_MATCHES
@ CRASH_REPORT_FLAG_PRESENT
bool crash_report_get(CrashReport &out)
The previous boot's crash; false when there is none or it was read. Implemented per platform.