ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hw_health.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file hw_health.h
6 * @brief Hardware-health diagnostics: rail droop, SPI CRC backoff, GPIO short, cap leakage
7 * (PROTOCORE_ENABLE_HW_HEALTH).
8 *
9 * Four pure decision cores an app feeds with samples it reads from the hardware (ADC millivolts, a SPI
10 * CRC pass/fail, a driven-vs-readback GPIO level, a capacitor decay time). Each turns raw measurements
11 * into an actionable verdict for a "/health" panel or a fail-safe hook, without touching a peripheral
12 * itself:
13 *
14 * - **Power-rail voltage-drop logger**: track a rail's worst droop and count sag / brownout crossings.
15 * - **SPI-bus CRC audit + clock backoff**: a hysteretic state machine that halves the SPI clock after a
16 * run of CRC failures and steps it back up after a run of good transfers.
17 * - **GPIO short-circuit test**: compare a driven level to its readback to spot a short to ground / Vcc.
18 * - **Capacitor-leakage diag**: compare a measured RC decay time to the expected one to spot a leaky
19 * cap (too fast) or a high-ESR / open path (too slow).
20 *
21 * Pure, zero heap, no stdlib, host-testable.
22 */
23
24#ifndef PROTOCORE_HW_HEALTH_H
25#define PROTOCORE_HW_HEALTH_H
26
27#include "protocore_config.h" // the entry point: protocore_types.h for the widths
28
29#if PROTOCORE_ENABLE_HW_HEALTH
30
31EMBED_BEGIN_DECLS
32
33/** @brief Rail sample verdict (the sole return of protocore_hwhealth_rail_sample). */
34EMBED_ENUM_BYTE(HwRailVerdict,
35 HW_RAIL_OK = 0, ///< at or above the warn threshold.
36 HW_RAIL_SAG = 1, ///< below warn, at or above crit.
37 HW_RAIL_BROWNOUT = 2 ///< below the crit threshold.
38);
39
40/** @brief GPIO short-circuit verdict (the sole return of protocore_hwhealth_gpio_short). */
41EMBED_ENUM_BYTE(HwGpioVerdict,
42 HW_GPIO_OK = 0, ///< readback matches the driven level.
43 HW_GPIO_SHORT_GND = 1, ///< drove high, read low: shorted to ground.
44 HW_GPIO_SHORT_VCC = 2 ///< drove low, read high: shorted to Vcc.
45);
46
47/** @brief Capacitor-leakage verdict (the sole return of protocore_hwhealth_cap_leak). */
48EMBED_ENUM_BYTE(HwCapVerdict,
49 HW_CAP_OK = 0, ///< decay time within tolerance of expected.
50 HW_CAP_LEAK = 1, ///< decays too fast: leaky capacitor.
51 HW_CAP_HIGH_ESR = 2 ///< decays too slow: high-ESR / open charge path.
52);
53
54/** @brief Rolling monitor for one power rail (in millivolts). */
55typedef struct
56{
57 uint32_t nominal_mv;
58 uint32_t warn_mv; ///< below this -> SAG.
59 uint32_t crit_mv; ///< below this -> BROWNOUT.
60 uint32_t min_mv; ///< lowest sample seen (worst droop).
61 uint32_t sag_events;
62 uint32_t brownout_events;
63} HwRailMonitor;
64
65/** @brief Hysteretic SPI clock backoff state. */
66typedef struct
67{
68 uint32_t hz; ///< current clock.
69 uint32_t min_hz; ///< floor.
70 uint32_t max_hz; ///< ceiling.
71 uint16_t fail_streak;
72 uint16_t ok_streak;
73 uint16_t fail_trip; ///< consecutive failures that halve the clock.
74 uint16_t ok_trip; ///< consecutive successes that double the clock.
75} HwSpiBackoff;
76
77/** @brief The rail a call watches, and the reading it just took. */
78typedef struct
79{
80 HwRailMonitor *m; ///< the monitor a call acts on
81 const HwRailMonitor *m_ro; ///< the same monitor, where a call only reads it
82 uint32_t nominal_mv; ///< the rail's nominal level
83 uint32_t warn_mv; ///< below this a reading is a sag
84 uint32_t crit_mv; ///< below this it is a brownout
85 uint32_t mv; ///< the reading just taken
86} HwRailArgs;
87
88/** @brief The bus a backoff governs, and how it just fared. */
89typedef struct
90{
91 HwSpiBackoff *s; ///< the backoff state a call acts on
92 uint32_t start_hz; ///< the clock it starts at
93 uint32_t min_hz; ///< its floor
94 uint32_t max_hz; ///< its ceiling
95 uint16_t fail_trip; ///< consecutive failures before it halves
96 uint16_t ok_trip; ///< consecutive successes before it doubles
97 embed_bool crc_ok; ///< the transfer just completed checked out
98} HwSpiArgs;
99
100/** @brief A pin driven against what it read back, and a discharge against what was expected. */
101typedef struct
102{
103 embed_bool driven_high; ///< the level the pin was driven to
104 embed_bool read_high; ///< the level it read back
105 uint32_t measured_ms; ///< the discharge just timed
106 uint32_t expected_ms; ///< what it should have been
107 uint8_t tol_pct; ///< the tolerance band around that, as a percentage
108} HwProbeArgs;
109
110/** @brief Where a report is written. */
111typedef struct
112{
113 char *out; ///< where the JSON lands
114 size_t cap; ///< how much room it has
115} HwOutArgs;
116
117/**
118 * @brief The hardware health checks over caller-owned monitors.
119 *
120 * A caller sets the members a call takes, invokes it through ::HwHealth, and reads the outcome off
121 * the same handle. Every monitor is the caller's.
122 *
123 * @var HwHealthNs::rail the rail a call watches, and the reading it just took
124 * @var HwHealthNs::spi the bus a backoff governs, and how it just fared
125 * @var HwHealthNs::probe a pin driven against what it read back, and a timed discharge
126 * @var HwHealthNs::out_args where a report is written
127 * @var HwHealthNs::rail_verdict what a rail sample decided
128 * @var HwHealthNs::gpio_verdict what a pin probe decided
129 * @var HwHealthNs::cap_verdict what a discharge probe decided
130 * @var HwHealthNs::hz the clock a backoff settled on
131 * @var HwHealthNs::n bytes a report wrote, or 0 when it did not fit
132 * @var HwHealthNs::rail_init arm a rail monitor at its thresholds
133 * @var HwHealthNs::rail_sample judge one reading and tally what it was
134 * @var HwHealthNs::rail_json report the rail's tallies
135 * @var HwHealthNs::spi_init arm a bus backoff between its floor and ceiling
136 * @var HwHealthNs::spi_result feed one transfer's outcome in and take the new clock
137 * @var HwHealthNs::gpio_short a pin that does not read back what it was driven to
138 * @var HwHealthNs::cap_leak a discharge outside its tolerance band
139 *
140 * No storage member: every call works in the caller's monitor.
141 */
142typedef struct
143{
144 HwRailArgs rail;
145 HwSpiArgs spi;
146 HwProbeArgs probe;
147 HwOutArgs out_args;
148 HwRailVerdict rail_verdict;
149 HwGpioVerdict gpio_verdict;
150 HwCapVerdict cap_verdict;
151 uint32_t hz;
152 size_t n;
153} HwHealthVars;
154
155/** @brief The operands and the outcome. */
156extern HwHealthVars HwHealthV;
157
158/** @brief The entries. */
159typedef struct
160{
161 void (*const rail_init)(const uint8_t *work);
162 void (*const rail_sample)(const uint8_t *work);
163 void (*const rail_json)(const uint8_t *work);
164 void (*const spi_init)(const uint8_t *work);
165 void (*const spi_result)(const uint8_t *work);
166 void (*const gpio_short)(const uint8_t *work);
167 void (*const cap_leak)(const uint8_t *work);
168} HwHealthNs;
169
170// What the table binds, defined once in the .c and taking one parameter each: everything
171// else an entry needs is an operand in HwHealthV or a region of the borrow at a fixed offset.
172void protocore_hw_health_rail_init(const uint8_t *work);
173void protocore_hw_health_rail_sample(const uint8_t *work);
174void protocore_hw_health_rail_json(const uint8_t *work);
175void protocore_hw_health_spi_init(const uint8_t *work);
176void protocore_hw_health_spi_result(const uint8_t *work);
177void protocore_hw_health_gpio_short(const uint8_t *work);
178void protocore_hw_health_cap_leak(const uint8_t *work);
179
180// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
181// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
182// `HwHealth.rail_init(work)` resolves to a named function and becomes a DIRECT call. An extern table
183// leaves the call indirect and the symbol live at every level, -O2 -flto included.
184static const HwHealthNs HwHealth __attribute__((unused)) = {
185 .rail_init = protocore_hw_health_rail_init,
186 .rail_sample = protocore_hw_health_rail_sample,
187 .rail_json = protocore_hw_health_rail_json,
188 .spi_init = protocore_hw_health_spi_init,
189 .spi_result = protocore_hw_health_spi_result,
190 .gpio_short = protocore_hw_health_gpio_short,
191 .cap_leak = protocore_hw_health_cap_leak,
192};
193
194EMBED_END_DECLS
195
196#endif // PROTOCORE_ENABLE_HW_HEALTH
197
198#endif // PROTOCORE_HW_HEALTH_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.