Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix(pro): a hardware watchdog reset leaves each core's last PC and SP (#2210)

harness-for-autonomous[bot]10491c3b0323
diff --git a/devices/harness-pro/firmware/bootloader_components/wdt_pc/CMakeLists.txt b/devices/harness-pro/firmware/bootloader_components/wdt_pc/CMakeLists.txt
new file mode 100644
index 000000000..763da9bbb
--- /dev/null
+++ b/devices/harness-pro/firmware/bootloader_components/wdt_pc/CMakeLists.txt
@@ -0,0 +1,5 @@
+# The bootloader half of wdt_pc.h: save each core's last PC and SP after a watchdog reset, before the bootloader
+# re-arms the recorder. IDF links bootloader_components/ into the bootloader build by itself.
+idf_component_register(SRCS "wdt_pc.c"
+ PRIV_INCLUDE_DIRS "../../main/sys"
+ PRIV_REQUIRES hal soc esp_rom)
diff --git a/devices/harness-pro/firmware/bootloader_components/wdt_pc/wdt_pc.c b/devices/harness-pro/firmware/bootloader_components/wdt_pc/wdt_pc.c
new file mode 100644
index 000000000..cf8258cc0
--- /dev/null
+++ b/devices/harness-pro/firmware/bootloader_components/wdt_pc/wdt_pc.c
@@ -0,0 +1,30 @@
+// See main/sys/wdt_pc.h. Runs first thing in the bootloader: no BSS, no flash, no heap yet, only registers.
+#include <stdint.h>
+
+#include "esp_rom_sys.h"
+#include "hal/assist_debug_ll.h"
+#include "soc/assist_debug_reg.h"
+#include "soc/lp_system_reg.h"
+#include "soc/soc.h"
+#include "wdt_pc.h"
+
+// The hooks are weak in the bootloader; this symbol makes the linker keep the file.
+void bootloader_hooks_include(void) {}
+
+void bootloader_before_init(void)
+{
+ if (!wdt_pc_is_watchdog((unsigned)esp_rom_get_reset_reason(0))
+ && !wdt_pc_is_watchdog((unsigned)esp_rom_get_reset_reason(1))) return;
+ _assist_debug_ll_enable_bus_clock(true);
+ uint32_t w[WDT_PC_WORDS] = {
+ REG_READ(ASSIST_DEBUG_CORE_0_RCD_PDEBUGPC_REG), REG_READ(ASSIST_DEBUG_CORE_0_RCD_PDEBUGSP_REG),
+ REG_READ(ASSIST_DEBUG_CORE_1_RCD_PDEBUGPC_REG), REG_READ(ASSIST_DEBUG_CORE_1_RCD_PDEBUGSP_REG),
+ 0,
+ };
+ wdt_pc_seal(w);
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE11_REG, w[WDT_PC_PC0]);
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE12_REG, w[WDT_PC_SP0]);
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE13_REG, w[WDT_PC_PC1]);
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE14_REG, w[WDT_PC_SP1]);
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE15_REG, w[WDT_PC_CHECK]);
+}
diff --git a/devices/harness-pro/firmware/main/sys/crash_record.c b/devices/harness-pro/firmware/main/sys/crash_record.c
index f90021319..54a3075b1 100644
--- a/devices/harness-pro/firmware/main/sys/crash_record.c
+++ b/devices/harness-pro/firmware/main/sys/crash_record.c
@@ -47,3 +47,46 @@ size_t cr_format(const crash_record_t *r, char *out, size_t cap)
#undef ADD
return n;
}
+
+// The P4's raw reset codes (soc/reset_reasons.h), in words. The reset level matters: a core reset restarts the
+// CPUs and the digital core, a CPU reset only the CPUs.
+typedef struct { unsigned code; const char *name; const char *level; } cause_t;
+static const cause_t CAUSES[] = {
+ { 0x01, "power-on", NULL }, { 0x03, "software", "core reset" },
+ { 0x05, "power-down", NULL }, { 0x07, "the HP watchdog", "core reset" },
+ { 0x09, "the LP watchdog", "core reset" }, { 0x0B, "the HP watchdog", "CPU reset" },
+ { 0x0C, "software", "CPU reset" }, { 0x0D, "the LP watchdog", "CPU reset" },
+ { 0x0F, "brownout", NULL }, { 0x10, "the LP watchdog", "system reset" },
+ { 0x12, "the super watchdog", NULL }, { 0x13, "a power glitch", NULL },
+ { 0x14, "an eFuse CRC error", NULL }, { 0x16, "USB JTAG", NULL },
+ { 0x17, "USB serial", NULL }, { 0x18, "JTAG", NULL },
+ { 0x1A, "a CPU lockup", NULL }, { 0x1B, "a supply glitch", NULL },
+};
+
+static const cause_t *cause_of(unsigned code)
+{
+ for (size_t i = 0; i < sizeof CAUSES / sizeof CAUSES[0]; i++) if (CAUSES[i].code == code) return &CAUSES[i];
+ return NULL;
+}
+
+size_t cr_format_hw(const cr_hw_t *hw, char *out, size_t cap)
+{
+ if (!cap) return 0;
+ size_t n = 0;
+ out[0] = '\0';
+#define ADD(...) do { if (n < cap) { int w = snprintf(out + n, cap - n, __VA_ARGS__); if (w > 0) n = n + (size_t)w < cap ? n + (size_t)w : cap - 1; } } while (0)
+ ADD("crumbs: hardware reset:");
+ for (unsigned i = 0; i < 2; i++) {
+ const cause_t *c = cause_of(hw->cause[i]);
+ if (!c) ADD("%s core %u by reset code 0x%02x", i ? "," : "", i, hw->cause[i]);
+ else if (c->level) ADD("%s core %u by %s (%s, 0x%02x)", i ? "," : "", i, c->name, c->level, hw->cause[i]);
+ else ADD("%s core %u by %s (0x%02x)", i ? "," : "", i, c->name, hw->cause[i]);
+ }
+ ADD("\n");
+ if (hw->have_pc) {
+ ADD("crumbs: hardware record (addr2line against this build's ELF): core 0 pc 0x%08x sp 0x%08x, core 1 pc 0x%08x sp 0x%08x\n",
+ (unsigned)hw->pc[0], (unsigned)hw->sp[0], (unsigned)hw->pc[1], (unsigned)hw->sp[1]);
+ }
+#undef ADD
+ return n;
+}
diff --git a/devices/harness-pro/firmware/main/sys/crash_record.h b/devices/harness-pro/firmware/main/sys/crash_record.h
index 9d448a097..288cb0595 100644
--- a/devices/harness-pro/firmware/main/sys/crash_record.h
+++ b/devices/harness-pro/firmware/main/sys/crash_record.h
@@ -33,3 +33,15 @@ bool cr_valid(const crash_record_t *r);
void cr_phase(crash_record_t *r, unsigned slot, const char *phase, uint32_t now_ms);
// Text for the daemon's log, one '\n'-separated line per fact. Returns the length (always NUL-terminated).
size_t cr_format(const crash_record_t *r, char *out, size_t cap);
+
+// What the hardware kept across the reset (wdt_pc.h): each core's raw reset code and, when the bootloader
+// saved them, each core's last PC and SP.
+typedef struct {
+ unsigned cause[2];
+ bool have_pc;
+ uint32_t pc[2];
+ uint32_t sp[2];
+} cr_hw_t;
+
+// Same contract as cr_format.
+size_t cr_format_hw(const cr_hw_t *hw, char *out, size_t cap);
diff --git a/devices/harness-pro/firmware/main/sys/last_words.c b/devices/harness-pro/firmware/main/sys/last_words.c
index 33fc76ee1..8f34b2ed0 100644
--- a/devices/harness-pro/firmware/main/sys/last_words.c
+++ b/devices/harness-pro/firmware/main/sys/last_words.c
@@ -7,9 +7,13 @@
#include "esp_heap_caps.h"
#include "esp_log.h"
#include "esp_private/panic_internal.h"
+#include "esp_rom_sys.h"
#include "esp_timer.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
+#include "soc/lp_system_reg.h"
+#include "soc/soc.h"
+#include "wdt_pc.h"
static const char *TAG = "boot";
@@ -34,6 +38,9 @@ static RTC_NOINIT_ATTR ring_t s_ring;
static RTC_NOINIT_ATTR crash_record_t s_rec;
static crash_record_t s_prev_rec;
static bool s_have_rec;
+// What the hardware kept (wdt_pc.h): read at boot, reported with the reset reason.
+static cr_hw_t s_hw;
+static bool s_have_hw;
static portMUX_TYPE s_mux = portMUX_INITIALIZER_UNLOCKED;
static esp_reset_reason_t s_reason;
@@ -67,6 +74,7 @@ static const char *reason_name(esp_reset_reason_t r)
case ESP_RST_SDIO: return "sdio";
case ESP_RST_USB: return "usb";
case ESP_RST_JTAG: return "jtag";
+ case ESP_RST_CPU_LOCKUP: return "CPU LOCKUP";
default: return "unknown";
}
}
@@ -75,7 +83,25 @@ void last_words_boot(void)
{
s_reason = esp_reset_reason();
s_abnormal = s_reason == ESP_RST_PANIC || s_reason == ESP_RST_INT_WDT || s_reason == ESP_RST_TASK_WDT
- || s_reason == ESP_RST_WDT || s_reason == ESP_RST_BROWNOUT || s_reason == ESP_RST_UNKNOWN;
+ || s_reason == ESP_RST_WDT || s_reason == ESP_RST_BROWNOUT || s_reason == ESP_RST_CPU_LOCKUP
+ || s_reason == ESP_RST_UNKNOWN;
+ if (s_abnormal) {
+ // Which watchdog, and at what level: ESP_RST_WDT folds the P4's six watchdog resets into one.
+ s_hw.cause[0] = (unsigned)esp_rom_get_reset_reason(0);
+ s_hw.cause[1] = (unsigned)esp_rom_get_reset_reason(1);
+ uint32_t w[WDT_PC_WORDS] = {
+ REG_READ(LP_SYSTEM_REG_LP_STORE11_REG), REG_READ(LP_SYSTEM_REG_LP_STORE12_REG),
+ REG_READ(LP_SYSTEM_REG_LP_STORE13_REG), REG_READ(LP_SYSTEM_REG_LP_STORE14_REG),
+ REG_READ(LP_SYSTEM_REG_LP_STORE15_REG),
+ };
+ // Sealed by the bootloader hook only after a watchdog or lockup reset; an older bootloader never seals it.
+ s_hw.have_pc = wdt_pc_valid(w);
+ s_hw.pc[0] = w[WDT_PC_PC0]; s_hw.sp[0] = w[WDT_PC_SP0];
+ s_hw.pc[1] = w[WDT_PC_PC1]; s_hw.sp[1] = w[WDT_PC_SP1];
+ s_have_hw = true;
+ }
+ // Read once: a later clean reset must not replay this one's record.
+ REG_WRITE(LP_SYSTEM_REG_LP_STORE15_REG, 0);
if (s_abnormal && ring_valid() && s_ring.len) {
// Unroll the ring into reading order: oldest byte first.
uint32_t start = (s_ring.head + RING_BYTES - s_ring.len) % RING_BYTES;
@@ -139,19 +165,30 @@ void last_words_add(const char *line, size_t len)
portEXIT_CRITICAL(&s_mux);
}
+// One warning per '\n'-separated line of a crash_record text.
+static void log_lines(char *text)
+{
+ for (char *p = text, *nl; p && *p; p = nl ? nl + 1 : NULL) {
+ if ((nl = strchr(p, '\n'))) *nl = '\0';
+ ESP_LOGW(TAG, "%s", p);
+ }
+}
+
void last_words_report(void)
{
if (s_reported) return;
s_reported = true;
if (s_abnormal) ESP_LOGW(TAG, "reset reason: %s", reason_name(s_reason));
else ESP_LOGI(TAG, "reset reason: %s", reason_name(s_reason));
+ static char text[512];
+ if (s_have_hw) {
+ cr_format_hw(&s_hw, text, sizeof text);
+ log_lines(text);
+ s_have_hw = false;
+ }
if (s_have_rec) {
- static char text[512];
cr_format(&s_prev_rec, text, sizeof text);
- for (char *p = text, *nl; p && *p; p = nl ? nl + 1 : NULL) {
- if ((nl = strchr(p, '\n'))) *nl = '\0';
- ESP_LOGW(TAG, "%s", p);
- }
+ log_lines(text);
s_have_rec = false;
}
if (!s_previous_len) return;
diff --git a/devices/harness-pro/firmware/main/sys/wdt_pc.h b/devices/harness-pro/firmware/main/sys/wdt_pc.h
new file mode 100644
index 000000000..ab7633702
--- /dev/null
+++ b/devices/harness-pro/firmware/main/sys/wdt_pc.h
@@ -0,0 +1,45 @@
+// What a hardware watchdog leaves behind: each core's last PC and SP.
+//
+// A hang the panic handler never sees (both cores stalled on a bus, interrupts off on both) ends in a hardware
+// watchdog reset with no backtrace. The P4's assist_debug block records each core's PC and SP in real time and
+// keeps them across that reset, but the bootloader turns core 0's recording back on before the app runs, which
+// overwrites it. So the bootloader hook (bootloader_components/wdt_pc) copies both cores' values into LP store
+// registers first, and last_words.c reads them at boot. Shared by the two; pure, so it is host-tested.
+//
+// LP_SYSTEM_REG_LP_STORE11..15: free in IDF 5.5 on the P4 (esp32p4/rom/rtc.h assigns 0..10). They hold across
+// every reset but a power cut, where they read 0 and the check fails.
+#pragma once
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#define WDT_PC_MAGIC 0x57445043u // "WDPC"
+
+enum { WDT_PC_PC0, WDT_PC_SP0, WDT_PC_PC1, WDT_PC_SP1, WDT_PC_CHECK, WDT_PC_WORDS };
+
+static inline uint32_t wdt_pc_sum(const uint32_t w[WDT_PC_WORDS])
+{
+ return WDT_PC_MAGIC ^ w[WDT_PC_PC0] ^ w[WDT_PC_SP0] ^ w[WDT_PC_PC1] ^ w[WDT_PC_SP1];
+}
+
+static inline void wdt_pc_seal(uint32_t w[WDT_PC_WORDS]) { w[WDT_PC_CHECK] = wdt_pc_sum(w); }
+
+static inline bool wdt_pc_valid(const uint32_t w[WDT_PC_WORDS]) { return w[WDT_PC_CHECK] == wdt_pc_sum(w); }
+
+// The P4's raw reset codes (soc/reset_reasons.h) that mean a watchdog or a CPU lockup: the resets the panic
+// handler did not get to write a backtrace for.
+static inline bool wdt_pc_is_watchdog(unsigned code)
+{
+ switch (code) {
+ case 0x07: // RESET_REASON_CORE_MWDT
+ case 0x09: // RESET_REASON_CORE_RWDT
+ case 0x0B: // RESET_REASON_CPU_MWDT
+ case 0x0D: // RESET_REASON_CPU_RWDT
+ case 0x10: // RESET_REASON_SYS_RWDT
+ case 0x12: // RESET_REASON_SYS_SUPER_WDT
+ case 0x1A: // RESET_REASON_CPU_LOCKUP
+ return true;
+ default:
+ return false;
+ }
+}
diff --git a/devices/harness-pro/firmware/sdkconfig.defaults b/devices/harness-pro/firmware/sdkconfig.defaults
index b24cb2596..5d94b8e36 100644
--- a/devices/harness-pro/firmware/sdkconfig.defaults
+++ b/devices/harness-pro/firmware/sdkconfig.defaults
@@ -102,3 +102,6 @@ CONFIG_ESP_TASK_WDT_EN=y
CONFIG_ESP_TASK_WDT_INIT=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
+# A hang the panic handler never sees ends in a hardware watchdog reset. The CPUs' PC recorder (assist_debug) is
+# what bootloader_components/wdt_pc saves for that case; off, the app gates its clock and the record goes stale.
+CONFIG_ESP_SYSTEM_HW_PC_RECORD=y
diff --git a/devices/harness-pro/firmware/test/host_esp/esp_rom_sys.h b/devices/harness-pro/firmware/test/host_esp/esp_rom_sys.h
new file mode 100644
index 000000000..18ee09aac
--- /dev/null
+++ b/devices/harness-pro/firmware/test/host_esp/esp_rom_sys.h
@@ -0,0 +1,3 @@
+// Host-test stand-in for esp_rom_sys.h: the test supplies the reset reason per core.
+#pragma once
+unsigned esp_rom_get_reset_reason(int cpu_no);
diff --git a/devices/harness-pro/firmware/test/host_esp/hal/assist_debug_ll.h b/devices/harness-pro/firmware/test/host_esp/hal/assist_debug_ll.h
new file mode 100644
index 000000000..7e33aa193
--- /dev/null
+++ b/devices/harness-pro/firmware/test/host_esp/hal/assist_debug_ll.h
@@ -0,0 +1,4 @@
+// Host-test stand-in for hal/assist_debug_ll.h: the test records that the bus clock was turned on.
+#pragma once
+#include <stdbool.h>
+void _assist_debug_ll_enable_bus_clock(bool enable);
diff --git a/devices/harness-pro/firmware/test/host_esp/soc/assist_debug_reg.h b/devices/harness-pro/firmware/test/host_esp/soc/assist_debug_reg.h
new file mode 100644
index 000000000..680ca2592
--- /dev/null
+++ b/devices/harness-pro/firmware/test/host_esp/soc/assist_debug_reg.h
@@ -0,0 +1,7 @@
+// Host-test stand-in: the P4's assist_debug PC and SP record registers (offsets as in IDF, base made up).
+#pragma once
+#define DR_REG_ASSIST_DEBUG_BASE 0x1000u
+#define ASSIST_DEBUG_CORE_0_RCD_PDEBUGPC_REG (DR_REG_ASSIST_DEBUG_BASE + 0x48)
+#define ASSIST_DEBUG_CORE_0_RCD_PDEBUGSP_REG (DR_REG_ASSIST_DEBUG_BASE + 0x4c)
+#define ASSIST_DEBUG_CORE_1_RCD_PDEBUGPC_REG (DR_REG_ASSIST_DEBUG_BASE + 0xc8)
+#define ASSIST_DEBUG_CORE_1_RCD_PDEBUGSP_REG (DR_REG_ASSIST_DEBUG_BASE + 0xcc)
diff --git a/devices/harness-pro/firmware/test/host_esp/soc/lp_system_reg.h b/devices/harness-pro/firmware/test/host_esp/soc/lp_system_reg.h
new file mode 100644
index 000000000..014b26a16
--- /dev/null
+++ b/devices/harness-pro/firmware/test/host_esp/soc/lp_system_reg.h
@@ -0,0 +1,8 @@
+// Host-test stand-in: the P4's LP store registers (offsets as in IDF, base made up).
+#pragma once
+#define DR_REG_LP_SYS_BASE 0x2000u
+#define LP_SYSTEM_REG_LP_STORE11_REG (DR_REG_LP_SYS_BASE + 0x58)
+#define LP_SYSTEM_REG_LP_STORE12_REG (DR_REG_LP_SYS_BASE + 0x5c)
+#define LP_SYSTEM_REG_LP_STORE13_REG (DR_REG_LP_SYS_BASE + 0x60)
+#define LP_SYSTEM_REG_LP_STORE14_REG (DR_REG_LP_SYS_BASE + 0x64)
+#define LP_SYSTEM_REG_LP_STORE15_REG (DR_REG_LP_SYS_BASE + 0x68)
diff --git a/devices/harness-pro/firmware/test/host_esp/soc/soc.h b/devices/harness-pro/firmware/test/host_esp/soc/soc.h
new file mode 100644
index 000000000..30fdb4a3f
--- /dev/null
+++ b/devices/harness-pro/firmware/test/host_esp/soc/soc.h
@@ -0,0 +1,7 @@
+// Host-test stand-in for soc/soc.h: register access goes to the test's register file.
+#pragma once
+#include <stdint.h>
+uint32_t host_reg_read(uint32_t addr);
+void host_reg_write(uint32_t addr, uint32_t value);
+#define REG_READ(addr) host_reg_read((uint32_t)(addr))
+#define REG_WRITE(addr, value) host_reg_write((uint32_t)(addr), (uint32_t)(value))
diff --git a/devices/harness-pro/firmware/test/run.sh b/devices/harness-pro/firmware/test/run.sh
index 840c24924..be1e7b9aa 100755
--- a/devices/harness-pro/firmware/test/run.sh
+++ b/devices/harness-pro/firmware/test/run.sh
@@ -127,6 +127,9 @@ cc "${cc_flags[@]}" -I"$fw/main/sys" -o "$out/test_fw_update" "$here/test_fw_upd
cc "${cc_flags[@]}" -Wframe-larger-than=1024 -c -o "$out/crash_record.o" "$fw/main/sys/crash_record.c"
cc "${cc_flags[@]}" -I"$fw/main/sys" -o "$out/test_crash_record" "$here/test_crash_record.c" "$out/crash_record.o"
"$out/test_crash_record"
+# wdt_pc: the bootloader hook that saves each core's PC and SP after a watchdog reset, on a fake register file.
+cc "${cc_flags[@]}" -I"$fw/main/sys" -o "$out/test_wdt_pc_hook" "$here/test_wdt_pc_hook.c" "$fw/bootloader_components/wdt_pc/wdt_pc.c"
+"$out/test_wdt_pc_hook"
# The app layer: pro_app.c, pod_glue.c and pro_touch.c with the cable, the recorder and the clock as host stubs,
# replayed. It builds its own fonts and objects.
diff --git a/devices/harness-pro/firmware/test/test_crash_record.c b/devices/harness-pro/firmware/test/test_crash_record.c
index 1351f8f32..b2ab1bf85 100644
--- a/devices/harness-pro/firmware/test/test_crash_record.c
+++ b/devices/harness-pro/firmware/test/test_crash_record.c
@@ -4,6 +4,7 @@
#include <string.h>
#include "crash_record.h"
+#include "wdt_pc.h"
int main(void)
{
@@ -45,6 +46,33 @@ int main(void)
size_t n = cr_format(&r, tiny, sizeof tiny);
assert(n < sizeof tiny && tiny[n] == '\0');
assert(cr_format(&r, tiny, 0) == 0);
+
+ // The hardware record: a hang that never reached the panic handler (a hardware watchdog, a lockup) leaves
+ // only what the CPU kept: each core's last PC and SP, which the bootloader copies into LP store registers.
+ assert(wdt_pc_is_watchdog(0x07) && wdt_pc_is_watchdog(0x09) && wdt_pc_is_watchdog(0x0B));
+ assert(wdt_pc_is_watchdog(0x0D) && wdt_pc_is_watchdog(0x10) && wdt_pc_is_watchdog(0x12) && wdt_pc_is_watchdog(0x1A));
+ assert(!wdt_pc_is_watchdog(0x01) && !wdt_pc_is_watchdog(0x03) && !wdt_pc_is_watchdog(0x0C)); // power-on, software
+ assert(!wdt_pc_is_watchdog(0x0F) && !wdt_pc_is_watchdog(0x17) && !wdt_pc_is_watchdog(0)); // brownout, USB, none
+ uint32_t st[WDT_PC_WORDS] = { 0 };
+ assert(!wdt_pc_valid(st)); // power-on: the LP store registers read 0
+ st[WDT_PC_PC0] = 0x4ff0abcdu; st[WDT_PC_SP0] = 0x4ff7f000u; st[WDT_PC_PC1] = 0x40001234u; st[WDT_PC_SP1] = 0x4ff6e000u;
+ wdt_pc_seal(st);
+ assert(wdt_pc_valid(st));
+ st[WDT_PC_PC1] ^= 4; // one word changed after the seal: not a record
+ assert(!wdt_pc_valid(st));
+ st[WDT_PC_PC1] ^= 4;
+
+ cr_hw_t hw = { .cause = { 0x07, 0x0B }, .have_pc = true,
+ .pc = { 0x4ff0abcdu, 0x40001234u }, .sp = { 0x4ff7f000u, 0x4ff6e000u } };
+ cr_format_hw(&hw, out, sizeof out);
+ assert(!strcmp(out, "crumbs: hardware reset: core 0 by the HP watchdog (core reset, 0x07), core 1 by the HP watchdog (CPU reset, 0x0b)\n"
+ "crumbs: hardware record (addr2line against this build's ELF): core 0 pc 0x4ff0abcd sp 0x4ff7f000, core 1 pc 0x40001234 sp 0x4ff6e000\n"));
+ hw.have_pc = false; hw.cause[0] = 0x12; hw.cause[1] = 0x55; // an old bootloader: the cause alone; an unknown code by number
+ cr_format_hw(&hw, out, sizeof out);
+ assert(!strcmp(out, "crumbs: hardware reset: core 0 by the super watchdog (0x12), core 1 by reset code 0x55\n"));
+ memset(tiny, 'Z', sizeof tiny);
+ n = cr_format_hw(&hw, tiny, sizeof tiny);
+ assert(n < sizeof tiny && tiny[n] == '\0');
puts("test_crash_record: ok");
return 0;
}
diff --git a/devices/harness-pro/firmware/test/test_wdt_pc_hook.c b/devices/harness-pro/firmware/test/test_wdt_pc_hook.c
new file mode 100644
index 000000000..217f734d1
--- /dev/null
+++ b/devices/harness-pro/firmware/test/test_wdt_pc_hook.c
@@ -0,0 +1,77 @@
+// bootloader_components/wdt_pc: the bootloader hook against a fake register file. A bad bootloader can leave a
+// unit needing a USB reflash, so the hook's whole behaviour is checked here before any device runs it.
+#include <assert.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "soc/assist_debug_reg.h"
+#include "soc/lp_system_reg.h"
+#include "soc/soc.h"
+#include "wdt_pc.h"
+
+void bootloader_before_init(void);
+
+#define REGS 0x3000u
+static uint32_t s_reg[REGS / 4];
+static unsigned s_writes;
+static unsigned s_cause[2];
+static bool s_clock;
+
+uint32_t host_reg_read(uint32_t addr) { assert(addr < REGS && addr % 4 == 0); return s_reg[addr / 4]; }
+void host_reg_write(uint32_t addr, uint32_t value) { assert(addr < REGS && addr % 4 == 0); s_reg[addr / 4] = value; s_writes++; }
+unsigned esp_rom_get_reset_reason(int cpu_no) { assert(cpu_no == 0 || cpu_no == 1); return s_cause[cpu_no]; }
+void _assist_debug_ll_enable_bus_clock(bool enable) { s_clock = enable; }
+
+static void boot(unsigned cause0, unsigned cause1)
+{
+ memset(s_reg, 0, sizeof s_reg);
+ s_writes = 0; s_clock = false;
+ s_cause[0] = cause0; s_cause[1] = cause1;
+ s_reg[ASSIST_DEBUG_CORE_0_RCD_PDEBUGPC_REG / 4] = 0x4ff0abcdu;
+ s_reg[ASSIST_DEBUG_CORE_0_RCD_PDEBUGSP_REG / 4] = 0x4ff7f000u;
+ s_reg[ASSIST_DEBUG_CORE_1_RCD_PDEBUGPC_REG / 4] = 0x40001234u;
+ s_reg[ASSIST_DEBUG_CORE_1_RCD_PDEBUGSP_REG / 4] = 0x4ff6e000u;
+ bootloader_before_init();
+}
+
+static void stored(uint32_t w[WDT_PC_WORDS])
+{
+ w[WDT_PC_PC0] = s_reg[LP_SYSTEM_REG_LP_STORE11_REG / 4];
+ w[WDT_PC_SP0] = s_reg[LP_SYSTEM_REG_LP_STORE12_REG / 4];
+ w[WDT_PC_PC1] = s_reg[LP_SYSTEM_REG_LP_STORE13_REG / 4];
+ w[WDT_PC_SP1] = s_reg[LP_SYSTEM_REG_LP_STORE14_REG / 4];
+ w[WDT_PC_CHECK] = s_reg[LP_SYSTEM_REG_LP_STORE15_REG / 4];
+}
+
+int main(void)
+{
+ uint32_t w[WDT_PC_WORDS];
+
+ // A clean boot writes nothing at all: not a register, not the clock.
+ boot(0x01, 0x01); // power-on
+ assert(s_writes == 0 && !s_clock);
+ boot(0x0C, 0x0C); // esp_restart, an OTA reboot
+ assert(s_writes == 0 && !s_clock);
+ boot(0x17, 0x17); // reset over USB serial (a flash)
+ assert(s_writes == 0 && !s_clock);
+
+ // A core reset by the HP watchdog: both cores' PC and SP land in the LP store, sealed.
+ boot(0x07, 0x07);
+ stored(w);
+ assert(s_clock && wdt_pc_valid(w));
+ assert(w[WDT_PC_PC0] == 0x4ff0abcdu && w[WDT_PC_SP0] == 0x4ff7f000u);
+ assert(w[WDT_PC_PC1] == 0x40001234u && w[WDT_PC_SP1] == 0x4ff6e000u);
+ assert(s_writes == 5); // the five LP store registers and nothing else
+
+ // Only core 1 reset by the watchdog: still saved. A CPU lockup on core 0: saved.
+ boot(0x0C, 0x0B);
+ stored(w);
+ assert(wdt_pc_valid(w) && w[WDT_PC_PC1] == 0x40001234u);
+ boot(0x1A, 0x0C);
+ stored(w);
+ assert(wdt_pc_valid(w) && w[WDT_PC_PC0] == 0x4ff0abcdu);
+ puts("test_wdt_pc_hook: ok");
+ return 0;
+}