FastLED 3.9.15
Loading...
Searching...
No Matches
watchdog.h
Go to the documentation of this file.
1#pragma once
2
32
33#include "fl/stl/stdint.h"
34#include "fl/stl/noexcept.h"
35#include "fl/stl/compiler_control.h" // FL_NORETURN
36#include "fl/stl/span.h"
37#include "fl/stl/function.h"
38#include "fl/stl/string_view.h" // for resetCauseName() return type
39
40namespace fl {
41
54
62 switch (c) {
63 case ResetCause::POWER_ON: return fl::string_view("POWER_ON");
64 case ResetCause::BROWNOUT: return fl::string_view("BROWNOUT");
65 case ResetCause::EXTERNAL_PIN: return fl::string_view("EXTERNAL_PIN");
66 case ResetCause::WATCHDOG: return fl::string_view("WATCHDOG");
67 case ResetCause::SOFTWARE: return fl::string_view("SOFTWARE");
68 case ResetCause::LOCKUP: return fl::string_view("LOCKUP");
69 case ResetCause::DEBUGGER: return fl::string_view("DEBUGGER");
70 case ResetCause::PANIC: return fl::string_view("PANIC");
72 default: return fl::string_view("UNKNOWN");
73 }
74}
75
82struct ResetInfo {
85 fl::u32 rawRegister;
86
89
94
103 fl::size describe(fl::span<char> out, bool verbose = false) const FL_NOEXCEPT;
104};
105
107using WatchdogTimeoutCallback = void(*)(void* user_data);
108
111 bool valid;
112 fl::u32 pc;
113 fl::u32 lr;
114 fl::u32 psr;
117 const char* fault_type;
118};
119
123class Watchdog {
124public:
125 // Singleton accessor — DECLARATION ONLY. The function-local `static
126 // Watchdog` must live in a `.cpp.hpp` / `.impl.hpp` TU (one definition,
127 // one guard variable) so the Teensy 3.x `__cxa_guard` ABI conflict does
128 // not pull a guarded singleton path into every TU that includes
129 // FastLED.h. See `agents/docs/cpp-standards.md` → "Header rules:
130 // function-local statics with non-trivial constructors are forbidden in
131 // `src/**/*.h`."
133
134 // ========== Tier 0 — universal ==========
135
136 void begin(fl::u32 timeout_ms) FL_NOEXCEPT;
139
142
148
149 fl::u8 persistRead(fl::size idx) const FL_NOEXCEPT;
150 void persistWrite(fl::size idx, fl::u8 v) FL_NOEXCEPT;
151
156 void setSafeModeThreshold(fl::u16 threshold) FL_NOEXCEPT;
157
159
160 // ========== Tier 1 — most platforms ==========
161
162 bool onTimeout(WatchdogTimeoutCallback cb, void* user_data = nullptr) FL_NOEXCEPT;
163 bool onTimeout(fl::function<void()> cb) FL_NOEXCEPT;
164 bool setPauseOnDebug(bool pause) FL_NOEXCEPT;
165 bool writeCrashLog(fl::span<const fl::u8> payload) FL_NOEXCEPT;
166 fl::size readCrashLog(fl::span<fl::u8> out) const FL_NOEXCEPT;
168
169 // ========== Tier 2 — advanced platforms ==========
170
171 bool setWindow(fl::u32 window_min_ms, fl::u32 window_max_ms) FL_NOEXCEPT;
175
176private:
178 ~Watchdog() FL_NOEXCEPT = default;
179 Watchdog(const Watchdog&) FL_NOEXCEPT = delete;
180 Watchdog& operator=(const Watchdog&) FL_NOEXCEPT = delete;
181
183};
184
206public:
209
212 explicit ScopedWatchdog(fl::u32 timeout_ms) FL_NOEXCEPT;
213
217
221 static int activeScopeCount() FL_NOEXCEPT;
222
224 ScopedWatchdog& operator=(const ScopedWatchdog&) FL_NOEXCEPT = delete;
225};
226
227} // namespace fl
228
229// ============================================================================
230// FL_WATCHDOG_AUTO(...) — the canonical zero-setup loop()-top guard.
231//
232// Variadic so the same macro covers both the no-argument default (15 000 ms)
233// and the explicit-timeout form:
234//
235// void loop() {
236// FL_WATCHDOG_AUTO(); // 15 000 ms default
237// doWork();
238// }
239//
240// void loop() {
241// FL_WATCHDOG_AUTO(5000); // 5-second timeout
242// doWork();
243// }
244//
245// "AUTO" because this macro does more than just feed: it lazily arms the WDT
246// on first call, prints the prior-boot reset/crash diagnostic, pauses 3 s on
247// a crash so the developer can read the message, and feeds the WDT on both
248// construction AND destruction. Single source-line use; the macro stamps the
249// local variable name from `__LINE__` so two `FL_WATCHDOG_AUTO()` on the same
250// line are a compile error. Two on different lines but simultaneously alive
251// (nested scopes) trigger a one-shot runtime warning — see ScopedWatchdog.
252//
253// Future variants stay grouped under the `FL_WATCHDOG_*` prefix:
254// FL_WATCHDOG_AUTO_QUIET / FL_WATCHDOG_AUTO_VERBOSE — log-level variants
255// FL_WATCHDOG_MANUAL — non-RAII setup helper
256// ============================================================================
257
258#define FL_WATCHDOG__CONCAT_INNER(a, b) a##b
259#define FL_WATCHDOG__CONCAT(a, b) FL_WATCHDOG__CONCAT_INNER(a, b)
260#define FL_WATCHDOG_AUTO(...) \
261 ::fl::ScopedWatchdog FL_WATCHDOG__CONCAT(_fl_wdt_, __LINE__) { __VA_ARGS__ }
262
263// ============================================================================
264// FL_WATCHDOG_* capability macros — defined by the per-platform impl.
265// The noop fallback defines NONE of the boolean flags and defaults numerics to 0.
266// User code may #ifdef on the boolean flags or static_assert on the numerics.
267// ============================================================================
268//
269// #define FL_WATCHDOG_HAS_HARDWARE // 1 wherever a real or emulated WDT runs
270// #define FL_WATCHDOG_PERSIST_BYTES N // small persistent buffer size, >= 8
271// #define FL_WATCHDOG_HAS_PRE_TIMEOUT_IRQ // pre-reset callback honored at HW level
272// #define FL_WATCHDOG_HAS_WINDOW_MODE // setWindow() honored
273// #define FL_WATCHDOG_HAS_CRASH_REPORT // hasCrashReport()/readCrashReport() honored
274// #define FL_WATCHDOG_HAS_BOOTLOADER_REBOOT // rebootIntoBootloader() honored
275// #define FL_WATCHDOG_MAX_TIMEOUT_MS N // upper bound, in ms
276//
277// The Tier 0 surface is always callable; Tier 1/2 methods return false on
278// platforms whose impl does not honor them, so user code that checks the
279// return value works portably.
ScopedWatchdog() FL_NOEXCEPT
Default construct with the library default timeout (15 000 ms).
Definition watchdog.h:208
RAII watchdog guard for the canonical loop()-top use case.
Definition watchdog.h:205
fl::u16 mSafeModeThreshold
Definition watchdog.h:182
static Watchdog & instance() FL_NOEXCEPT
void disable() FL_NOEXCEPT
fl::size readCrashLog(fl::span< fl::u8 > out) const FL_NOEXCEPT
void markCleanShutdown() FL_NOEXCEPT
void begin(fl::u32 timeout_ms) FL_NOEXCEPT
void feed() FL_NOEXCEPT
bool setPauseOnDebug(bool pause) FL_NOEXCEPT
fl::u8 persistRead(fl::size idx) const FL_NOEXCEPT
bool rebootIntoBootloader() FL_NOEXCEPT
fl::u16 safeModeThreshold() const FL_NOEXCEPT
bool hasCrashReport() const FL_NOEXCEPT
void persistWrite(fl::size idx, fl::u8 v) FL_NOEXCEPT
ResetCause lastResetCause() const FL_NOEXCEPT
bool onTimeout(WatchdogTimeoutCallback cb, void *user_data=nullptr) FL_NOEXCEPT
WatchdogCrashReport readCrashReport() const FL_NOEXCEPT
bool setWindow(fl::u32 window_min_ms, fl::u32 window_max_ms) FL_NOEXCEPT
Watchdog() FL_NOEXCEPT=default
bool writeCrashLog(fl::span< const fl::u8 > payload) FL_NOEXCEPT
fl::u16 consecutiveCrashCount() const FL_NOEXCEPT
void setSafeModeThreshold(fl::u16 threshold) FL_NOEXCEPT
bool lastResetWasWatchdog() const FL_NOEXCEPT
ResetInfo lastResetInfo() const FL_NOEXCEPT
Detailed reset information including platform raw register + subcause id.
FL_NORETURN void reboot() FL_NOEXCEPT
void clearCrashReport() FL_NOEXCEPT
bool isInSafeMode() const FL_NOEXCEPT
unsigned char u8
Definition s16x16x4.h:132
unsigned char u8
Definition stdint.h:131
ResetCause
Cause of the previous boot. Call once early in setup().
Definition watchdog.h:43
void(*)(void *user_data) WatchdogTimeoutCallback
ISR-safe C function pointer callback for onTimeout().
Definition watchdog.h:107
@ UNKNOWN
Unknown or unspecified error.
Definition expected.h:49
fl::string_view resetCauseName(ResetCause c) FL_NOEXCEPT
Return a static-lifetime name for c — zero-cost, no allocation.
Definition watchdog.h:61
Base definition for an LED controller.
Definition crgb.hpp:179
const char * fault_type
Definition watchdog.h:117
Decoded crash report (Tier 2).
Definition watchdog.h:110
#define FL_NORETURN
#define FL_NOEXCEPT
ResetCause cause
Normalized cross-platform enum.
Definition watchdog.h:83
fl::u32 rawRegister
Raw value of the platform's reset-cause register.
Definition watchdog.h:85
fl::string_view causeName() const FL_NOEXCEPT
Cause name (zero-cost, static string_view).
Definition watchdog.h:88
fl::string_view subcauseName() const FL_NOEXCEPT
Platform-specific subcause name (zero-cost, static string_view).
fl::size describe(fl::span< char > out, bool verbose=false) const FL_NOEXCEPT
Write a single-line human-readable description into the caller's buffer.
fl::u8 subcauseId
Platform-specific subcause id (0 = none)
Definition watchdog.h:84
Detailed reset information bundling the normalized cause with a platform-specific subcause id and the...
Definition watchdog.h:82