by John Lauer
Two-board Molecule interferometer with onboard RP2040, USB-C data, 15 V USB-C PD power and a JST stage interface.
main
John Lauer
software/ 1.2.1 for board 1.2.1: Pico firmware runs the board's controller contract (head DAC121C085 written and read b…
2e755c9
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
// SPDX-License-Identifier: MIT
// Host test of the board control core against the generated control table:
// boot defaults, the laser interlock, range limits, the gated head-DAC write
// sequence and its rate limit (or the ramp, for a static DAC), the guard band,
// readback checks, the heartbeat timeout, overcurrent trip, link loss and DAC failure.
// cc -O2 -Wall -Wextra -I../src test_ctl.c ../src/smi_ctl.c -o test_ctl && ./test_ctl
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "smi_ctl.h"
#include "smi_controls_gen.h"
static int fails, checks;
#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
typedef struct {
uint16_t dac[4][8];
int dac_writes;
bool gpio[64];
bool fail_exp;
bool fail_dac;
float max_step; // largest laser DAC change seen while ramping, in mA
float last_laser;
int gate_violations; // laser DAC writes made while the enable was asserted
int laser_writes;
int waits_ok; // disarm waits of at least disarm_us made with the enable dropped
int waits_bad;
int reads; // head DAC readbacks made by the core
bool fail_read;
} hw_t;
static hw_t hw;
static int LI;
static bool fake_dac(void *ctx, uint8_t dev, uint8_t ch, uint16_t code) {
hw_t *h = ctx;
if (h->fail_dac) return false;
if (dev == smi_controls[LI].dac && ch == smi_controls[LI].ch) {
h->laser_writes++;
if (smi_controls[LI].gate && h->gpio[SMI_LASER_EN_GPIO] == (SMI_LASER_EN_ACTIVE_HIGH ? 1 : 0)) h->gate_violations++;
float v = smi_ctl_value_of(&smi_controls[LI], code);
float d = fabsf(v - h->last_laser);
if (v > h->last_laser && d > h->max_step) h->max_step = d;
h->last_laser = v;
}
h->dac[dev][ch] = code;
h->dac_writes++;
return true;
}
static bool fake_gpio(void *ctx, uint8_t g, bool v) {
hw_t *h = ctx;
if (g >= SMI_EXP_BASE && h->fail_exp) return false;
h->gpio[g] = v;
return true;
}
static bool laser_pin_on(void) { return hw.gpio[SMI_LASER_EN_GPIO] == (SMI_LASER_EN_ACTIVE_HIGH ? 1 : 0); }
static void fake_wait(void *ctx, uint32_t us) {
hw_t *h = ctx;
bool on = h->gpio[SMI_LASER_EN_GPIO] == (SMI_LASER_EN_ACTIVE_HIGH ? 1 : 0);
if (!on && us >= smi_controls[LI].disarm_us) h->waits_ok++; else h->waits_bad++;
}
static bool fake_read(void *ctx, uint8_t dev, uint16_t *reg) {
hw_t *h = ctx;
h->reads++;
*reg = h->dac[dev][smi_controls[LI].ch];
return !h->fail_read;
}
// Armed: the firmware has the laser enabled. With a gated head DAC the pin itself
// is asserted only while a verified nonzero code is in the DAC.
static bool armed(const smi_ctl_t *c) { return c->laser_on; }
static uint8_t resp[160];
static uint16_t cseq;
static uint8_t cmd(smi_ctl_t *c, uint32_t now, const uint8_t *p, size_t n, size_t *len) {
size_t l = smi_ctl_command(c, p, n, ++cseq, now, resp);
if (len) *len = l;
CHECK(resp[0] == (uint8_t)cseq && resp[1] == (uint8_t)(cseq >> 8), "ack echoes seq");
return resp[3];
}
static uint8_t enable(smi_ctl_t *c, uint32_t now, int on, uint32_t key) {
uint8_t p[6] = {SMI_OP_LASER_ENABLE, (uint8_t)on, (uint8_t)key, (uint8_t)(key >> 8), (uint8_t)(key >> 16), (uint8_t)(key >> 24)};
return cmd(c, now, p, 6, NULL);
}
static uint8_t set(smi_ctl_t *c, uint32_t now, int idx, int mode, float v) {
uint8_t p[7] = {SMI_OP_SET, (uint8_t)idx, (uint8_t)mode};
memcpy(p + 3, &v, 4);
return cmd(c, now, p, 7, NULL);
}
static void hb(smi_ctl_t *c, uint32_t now) { uint8_t p[1] = {SMI_OP_HEARTBEAT}; cmd(c, now, p, 1, NULL); }
static float zero_code(const smi_ctl_t *c) { return smi_isense.zero_cal ? c->zero_raw : 0.0f; }
static int32_t raw_for(const smi_ctl_t *c, float ma) { return (int32_t)lroundf((ma - smi_isense.offset_ma) / smi_isense.ma_per_code + zero_code(c)); }
static smi_ctl_t boot(uint8_t reason) {
memset(&hw, 0, sizeof hw);
for (int i = 0; i < 64; i++) hw.gpio[i] = SMI_LASER_EN_ACTIVE_HIGH; // pessimistic: pins start "on"
smi_hal_t hal = {.ctx = &hw, .dac_write = fake_dac, .wait_us = fake_wait, .dac_read = fake_read, .gpio_put = fake_gpio};
static smi_ctl_t c;
smi_ctl_init(&c, smi_controls, SMI_NUM_CONTROLS, &smi_isense, SMI_MAP_HASH, SMI_MAP_VERSION, SMI_LASER_EN_GPIO, SMI_LASER_EN_ACTIVE_HIGH, &hal, reason, 0);
return c;
}
int main(void) {
LI = -1;
for (int i = 0; i < SMI_NUM_CONTROLS; i++) if (smi_controls[i].is_laser) LI = i;
CHECK(LI == 0, "laser control is index 0");
const smi_control_def_t *L = &smi_controls[LI];
float lim = L->max < L->rated ? L->max : L->rated;
uint32_t t = 0;
// Boot: laser off, every output at its default.
smi_ctl_t c = boot(SMI_OFF_BOOT);
CHECK(!laser_pin_on(), "laser enable pin off at boot");
CHECK(hw.dac[L->dac][L->ch] == smi_ctl_code_for(L, 0), "laser DAC at 0 mA at boot");
for (int i = 0; i < SMI_NUM_CONTROLS; i++) {
const smi_control_def_t *d = &smi_controls[i];
if (d->kind == SMI_KIND_DAC && !d->is_laser) CHECK(hw.dac[d->dac][d->ch] == smi_ctl_code_for(d, d->def), "control %d DAC default", i);
if (d->kind == SMI_KIND_SWITCH) {
int k = 0; for (int j = d->nset - 1; j >= 0; j--) if (d->set_value[j] == d->def) k = j;
for (int b = 0; b < d->ngpio; b++) CHECK(hw.gpio[d->gpio[b]] == ((d->set_code[k] >> b) & 1), "control %d switch default bit %d", i, b);
}
}
CHECK(c.off_reason == SMI_OFF_BOOT, "off reason boot");
smi_ctl_t w = boot(SMI_OFF_WATCHDOG);
CHECK(!laser_pin_on() && w.boot_reason == SMI_OFF_WATCHDOG, "watchdog boot: laser off, reason reported");
c = boot(SMI_OFF_BOOT);
// Startup rule: no enable until the current readback is validated at zero.
CHECK(enable(&c, t, 1, SMI_LASER_KEY) == (smi_isense.zero_cal ? SMI_E_NOT_READY : SMI_E_NOT_READY), "enable refused before the zero check");
for (int k = 0; k < 12; k++) { t += 100000u; hb(&c, t); smi_ctl_isense(&c, 37, t); }
CHECK(!smi_isense.zero_cal || (c.zero_n >= 8 && fabsf(c.zero_raw - 37) < 0.01f), "zero code calibrated (%.2f)", c.zero_raw);
CHECK(fabsf(c.isense_ma) < 0.01f, "reads 0 mA at the zero code");
// Interlock key.
CHECK(enable(&c, t, 1, 0x12345678) == SMI_E_KEY, "enable without the key is refused");
CHECK(!laser_pin_on(), "still off");
CHECK(enable(&c, t, 1, SMI_LASER_KEY) == SMI_OK && armed(&c), "enable with key");
CHECK(!L->gate || !laser_pin_on(), "gated: no enable carrier while the DAC holds code 0");
CHECK(c.laser_now == 0, "enable starts at 0 mA");
// Limits.
CHECK(set(&c, t, LI, 0, lim + 1.0f) == SMI_E_RANGE, "above the limit is refused");
CHECK(set(&c, t, LI, 0, L->rated + 10.0f) == SMI_E_RANGE, "above rated is refused");
CHECK(set(&c, t, LI, 0, -1.0f) == SMI_E_RANGE, "negative is refused");
CHECK(set(&c, t, LI, 0, NAN) == SMI_E_RANGE, "NaN is refused");
CHECK(set(&c, t, LI, 2, 1) == SMI_E_MODE, "index mode on a DAC is refused");
CHECK(set(&c, t, SMI_NUM_CONTROLS, 0, 1) == SMI_E_CONTROL, "unknown control");
if (L->gate) {
// Board 1.2.1 contract: no stepped ramp (every write resets the head's
// soft-start); one verified write per change, at most every min_write_us.
hw.laser_writes = 0; hw.gate_violations = 0; hw.waits_ok = 0; hw.waits_bad = 0; hw.reads = 0;
CHECK(L->max_code == 4095 - L->guard || !L->guard, "top DAC code is full scale minus the guard (%u)", L->max_code);
CHECK(fabsf(lim - smi_ctl_value_of(L, L->max_code)) < 1e-3f, "limit %.3f mA is the top code's current", lim);
CHECK(set(&c, t, LI, 0, lim) == SMI_OK, "set to the limit");
t += 1000; smi_ctl_tick(&c, t);
CHECK(hw.dac[L->dac][L->ch] == L->max_code, "written in one step to the top code (%u)", hw.dac[L->dac][L->ch]);
CHECK(laser_pin_on(), "enable asserted after the verified nonzero write");
CHECK(fabsf(c.laser_now - lim) < fabsf(L->per_code), "set-point at the target");
CHECK(hw.waits_ok >= 1 && hw.waits_bad == 0, "every write waited disarm_us with the enable dropped (%d ok, %d bad)", hw.waits_ok, hw.waits_bad);
CHECK(hw.reads >= 1, "read back once more right after arming");
CHECK(hw.gate_violations == 0, "no laser write with the enable asserted");
// Requests every 10 ms for 1 s: at most one write per min_write_us, the latest wins.
hw.laser_writes = 0;
float last_req = 0;
uint32_t t0 = t;
for (int k = 1; k <= 100; k++) {
t = t0 + (uint32_t)k * 10000u;
last_req = 20.0f + (float)(k % 7) * 3.0f;
set(&c, t, LI, 0, last_req);
smi_ctl_tick(&c, t);
}
for (int ms = 1; ms <= (int)(L->min_write_us / 1000) + 5; ms++) smi_ctl_tick(&c, t + (uint32_t)ms * 1000u);
t += L->min_write_us + 5000u; hb(&c, t);
int allowed = (int)(1e6f / (float)L->min_write_us) + 2;
CHECK(hw.laser_writes <= allowed, "write rate limited: %d writes in 1 s (allowed %d)", hw.laser_writes, allowed);
CHECK(hw.dac[L->dac][L->ch] == smi_ctl_code_for(L, last_req), "the latest request wins (%u)", hw.dac[L->dac][L->ch]);
CHECK(hw.gate_violations == 0 && hw.waits_bad == 0, "still every write disarmed first");
if (L->guard) {
// Command codes: 0..guard are disarmed; guard + 1 is DAC code 1.
t += L->min_write_us; hb(&c, t);
CHECK(set(&c, t, LI, 1, (float)L->guard) == SMI_OK, "command code %u accepted", L->guard);
t += L->min_write_us; smi_ctl_tick(&c, t);
CHECK(hw.dac[L->dac][L->ch] == 0 && !laser_pin_on() && armed(&c), "command code at the guard: DAC 0, enable off");
t += L->min_write_us; hb(&c, t);
CHECK(set(&c, t, LI, 1, (float)L->guard + 1) == SMI_OK, "command code guard + 1");
t += L->min_write_us; smi_ctl_tick(&c, t);
CHECK(hw.dac[L->dac][L->ch] == 1 && laser_pin_on(), "guard + 1: DAC code 1, enable on");
t += L->min_write_us; hb(&c, t);
CHECK(set(&c, t, LI, 1, 4095) == SMI_OK, "command code 4095");
t += L->min_write_us; smi_ctl_tick(&c, t);
CHECK(hw.dac[L->dac][L->ch] == 4095 - L->guard, "command 4095: DAC %u", hw.dac[L->dac][L->ch]);
}
// The readback right after arming fails: disarm, fault.
t += L->min_write_us; hb(&c, t);
set(&c, t, LI, 0, 30);
hw.fail_read = true;
t += L->min_write_us; smi_ctl_tick(&c, t);
CHECK(!armed(&c) && !laser_pin_on() && c.fault == SMI_FAULT_READBACK, "failed readback after arming: fault, off");
hw.fail_read = false;
uint8_t cf0[1] = {SMI_OP_CLEAR_FAULT};
cmd(&c, t, cf0, 1, NULL);
enable(&c, t, 1, SMI_LASER_KEY);
set(&c, t, LI, 0, lim / 2);
t += 1000; smi_ctl_tick(&c, t);
} else {
// Ramp: set to the limit, tick at 1 ms, the set-point moves at ramp_per_s.
hw.max_step = 0; hw.last_laser = 0; hw.laser_writes = 0;
CHECK(set(&c, t, LI, 0, lim) == SMI_OK, "set to the limit");
float expect_s = lim / L->ramp_per_s;
float half_t = 0;
uint32_t t0 = t;
for (int ms = 1; ms <= (int)(expect_s * 1000) + 50; ms++) {
t = t0 + (uint32_t)ms * 1000u;
smi_ctl_tick(&c, t);
if (ms % 1000 == 0) hb(&c, t);
if (half_t == 0 && c.laser_now >= lim / 2) half_t = ms / 1000.0f;
CHECK(c.laser_now <= L->ramp_per_s * ms / 1000.0f + 1e-3f, "ramp never ahead of the rate at %d ms", ms);
CHECK(c.laser_now <= lim + 1e-4f, "never above the limit");
CHECK(smi_ctl_value_of(L, c.code[LI]) <= c.laser_now + fabsf(L->per_code) * 0.51f, "DAC never ahead of the set-point at %d ms", ms);
}
CHECK(fabsf(c.laser_now - lim) <= 1e-3f, "reached the target (1 uA resolution)");
CHECK(fabsf(half_t - expect_s / 2) < 0.01f, "half way at %.3f s (expect %.3f)", half_t, expect_s / 2);
float wstep = L->ramp_per_s * (L->min_write_us > 1000 ? L->min_write_us : 1000) * 1e-6f;
CHECK(hw.max_step <= wstep + fabsf(L->per_code) * 1.01f, "largest DAC step %.4f mA (allowed %.4f)", hw.max_step, wstep);
CHECK(!L->gate || laser_pin_on(), "gated: enable asserted after a verified nonzero write");
CHECK(hw.gate_violations == 0, "gated: every laser DAC write made with the enable dropped (%d violations)", hw.gate_violations);
if (L->min_write_us) {
float max_rate = 1e6f / (float)L->min_write_us + 2;
CHECK(hw.laser_writes <= (int)(expect_s * max_rate) + 5, "ramp write rate limited: %d writes in %.2f s", hw.laser_writes, expect_s);
}
float maxv = smi_ctl_value_of(L, hw.dac[L->dac][L->ch]);
CHECK(maxv <= lim + fabsf(L->per_code) * 0.5f, "DAC code value %.3f within limit", maxv);
// A stalled loop (1 s between ticks) moves at most 10 ms worth.
set(&c, t, LI, 0, 0);
float before = c.laser_now;
t += 1000000u; hb(&c, t);
smi_ctl_tick(&c, t);
CHECK(before - c.laser_now <= L->ramp_per_s * 0.0101f, "stalled tick limited (%.3f mA)", before - c.laser_now);
}
// Heartbeat timeout: no command for longer than hb_timeout_ms turns it off.
set(&c, t, LI, 0, lim / 2);
uint32_t last = t;
for (int ms = 1; ms < c.hb_timeout_ms + 100; ms++) { t = last + (uint32_t)ms * 1000u; smi_ctl_tick(&c, t); }
CHECK(!laser_pin_on() && c.off_reason == SMI_OFF_HB_TIMEOUT, "heartbeat timeout turns the laser off");
CHECK(hw.dac[L->dac][L->ch] == smi_ctl_code_for(L, 0), "and the DAC to 0");
// Overcurrent: three readings over the trip latch a fault.
enable(&c, t, 1, SMI_LASER_KEY);
smi_ctl_isense(&c, raw_for(&c, smi_isense.trip_ma + 3), t);
smi_ctl_isense(&c, raw_for(&c, smi_isense.trip_ma + 3), t);
CHECK(armed(&c), "two readings over trip: still on (glitch filter)");
smi_ctl_isense(&c, raw_for(&c, smi_isense.trip_ma + 3), t);
CHECK(!laser_pin_on() && c.fault == SMI_FAULT_OVERCURRENT && c.off_reason == SMI_OFF_OVERCURRENT, "overcurrent trips");
CHECK(enable(&c, t, 1, SMI_LASER_KEY) == SMI_E_FAULT, "enable refused while the fault is latched");
uint8_t cf[1] = {SMI_OP_CLEAR_FAULT};
CHECK(cmd(&c, t, cf, 1, NULL) == SMI_OK && c.fault == 0, "clear fault");
smi_ctl_isense(&c, raw_for(&c, 0), t);
CHECK(enable(&c, t, 1, SMI_LASER_KEY) == SMI_OK, "enable after clear");
// Head ADC stops answering with the laser on: fault after three misses.
smi_ctl_isense_failed(&c, t);
smi_ctl_isense_failed(&c, t);
CHECK(armed(&c), "two missed current reads: still on");
smi_ctl_isense_failed(&c, t);
CHECK(!laser_pin_on() && c.fault == SMI_FAULT_ISENSE, "three missed current reads: fault, off");
cmd(&c, t, cf, 1, NULL);
smi_ctl_isense(&c, raw_for(&c, 0), t);
CHECK(enable(&c, t, 1, SMI_LASER_KEY) == SMI_OK, "enable after the readback returns");
// A negative zero code (signed ADC) calibrates too.
{
static smi_ctl_t z;
hw_t keep = hw;
smi_hal_t hal = {.ctx = &hw, .dac_write = fake_dac, .wait_us = fake_wait, .dac_read = fake_read, .gpio_put = fake_gpio};
smi_ctl_init(&z, smi_controls, SMI_NUM_CONTROLS, &smi_isense, SMI_MAP_HASH, SMI_MAP_VERSION, SMI_LASER_EN_GPIO, SMI_LASER_EN_ACTIVE_HIGH, &hal, SMI_OFF_BOOT, 0);
for (int k = 0; k < 12; k++) smi_ctl_isense(&z, -21, 400000u + k * 100000u);
CHECK(fabsf(z.zero_raw + 21) < 0.01f && fabsf(z.isense_ma) < 0.01f, "negative zero code %.2f", z.zero_raw);
uint8_t st2[160]; size_t l2 = smi_ctl_state(&z, 2000000u, st2);
int16_t zc = (int16_t)(st2[l2 - 2] | (st2[l2 - 1] << 8));
CHECK(zc == -21 * 16, "zero code in STATE (%d)", zc);
hw = keep;
}
// Measured current readback.
smi_ctl_isense(&c, raw_for(&c, 42.0f), t);
uint8_t ri[1] = {SMI_OP_READ_ISENSE};
size_t n;
CHECK(cmd(&c, t, ri, 1, &n) == SMI_OK && n == 12, "read isense");
float ma; memcpy(&ma, resp + 4, 4);
CHECK(fabsf(ma - 42.0f) < smi_isense.ma_per_code, "isense %.3f mA", ma);
// Link lost: off and the set-point back to 0.
set(&c, t, LI, 0, lim / 3);
smi_ctl_link_lost(&c, SMI_OFF_USB);
CHECK(!laser_pin_on() && c.off_reason == SMI_OFF_USB && c.target[LI] == 0, "USB loss: off, set-point 0");
// Controls the map marks "change with the laser disabled" are refused while it is on.
enable(&c, t, 1, SMI_LASER_KEY);
for (int i = 0; i < SMI_NUM_CONTROLS; i++)
if (smi_controls[i].needs_off) CHECK(set(&c, t, i, 2, 0) == SMI_E_LASER_ON, "control %d refused with the laser on", i);
enable(&c, t, 0, 0);
// Switches, by value, index and code; DAC controls in counts.
for (int i = 0; i < SMI_NUM_CONTROLS; i++) {
const smi_control_def_t *d = &smi_controls[i];
if (d->kind == SMI_KIND_SWITCH) {
int k = d->nset - 1;
for (int j = 0; j < d->nset; j++) if (d->set_value[j] == d->set_value[k]) { k = j; break; }
CHECK(set(&c, t, i, 0, d->set_value[k]) == SMI_OK && c.code[i] == d->set_code[k], "switch %d by value", i);
for (int b = 0; b < d->ngpio; b++) CHECK(hw.gpio[d->gpio[b]] == ((d->set_code[k] >> b) & 1), "switch %d bit %d", i, b);
CHECK(set(&c, t, i, 2, 0) == SMI_OK && c.code[i] == d->set_code[0], "switch %d by index", i);
CHECK(set(&c, t, i, 0, d->set_value[0] * 1.37f + 0.5f) == SMI_E_RANGE, "switch %d off-list value refused", i);
} else if (!d->is_laser) {
uint16_t code = smi_ctl_code_for(d, (d->min + d->max) / 2);
CHECK(set(&c, t, i, 1, code) == SMI_OK && hw.dac[d->dac][d->ch] == code, "DAC %d by counts", i);
CHECK(set(&c, t, i, 0, d->max + fabsf(d->per_code) * 2) == SMI_E_RANGE, "DAC %d above max refused", i);
}
}
// A value listed twice (bias LOW at codes 0 and 3): the first code is used.
for (int i = 0; i < SMI_NUM_CONTROLS; i++) {
const smi_control_def_t *d = &smi_controls[i];
for (int a = 0; a < d->nset; a++) for (int b2 = a + 1; b2 < d->nset; b2++)
if (d->kind == SMI_KIND_SWITCH && d->set_value[a] == d->set_value[b2])
CHECK(set(&c, t, i, 0, d->set_value[a]) == SMI_OK && c.code[i] == d->set_code[a], "control %d duplicate value takes code %d", i, d->set_code[a]);
}
// Head expander not answering at boot: a latched fault, the laser cannot be enabled.
{
hw_t keep = hw;
memset(&hw, 0, sizeof hw);
hw.fail_exp = true;
smi_hal_t hal = {.ctx = &hw, .dac_write = fake_dac, .wait_us = fake_wait, .dac_read = fake_read, .gpio_put = fake_gpio};
static smi_ctl_t e;
smi_ctl_init(&e, smi_controls, SMI_NUM_CONTROLS, &smi_isense, SMI_MAP_HASH, SMI_MAP_VERSION, SMI_LASER_EN_GPIO, SMI_LASER_EN_ACTIVE_HIGH, &hal, SMI_OFF_BOOT, 0);
if (SMI_EXP_PRESENT) {
CHECK(e.fault == SMI_FAULT_HW, "expander silent at boot: fault");
for (int k = 0; k < 12; k++) smi_ctl_isense(&e, 0, 400000u + k * 100000u);
CHECK(enable(&e, 1500000u, 1, SMI_LASER_KEY) == SMI_E_FAULT, "and enable is refused");
}
hw = keep;
}
// Readback polls (board: every 2 ms while armed). Matching register: counted;
// wrong code, a power-down mode or a failed read: fault and off.
cmd(&c, t, cf, 1, NULL);
enable(&c, t, 1, SMI_LASER_KEY);
set(&c, t, LI, 0, 20);
for (int ms = 1; ms < 1000; ms++) { smi_ctl_tick(&c, t + ms * 1000u); if (ms % 500 == 0) hb(&c, t + ms * 1000u); }
t += 1000000u;
uint16_t good = c.code[LI];
uint32_t rb0 = c.readbacks;
smi_ctl_laser_readback(&c, true, good, t);
CHECK(c.readbacks == rb0 + 1 && armed(&c), "matching readback counted");
smi_ctl_laser_readback(&c, true, (uint16_t)(good | 0x1000), t);
CHECK(!armed(&c) && !laser_pin_on() && c.fault == SMI_FAULT_READBACK, "power-down bits in the readback: fault, off");
cmd(&c, t, cf, 1, NULL); enable(&c, t, 1, SMI_LASER_KEY); set(&c, t, LI, 0, 20);
for (int ms = 1; ms < 1000; ms++) { smi_ctl_tick(&c, t + ms * 1000u); if (ms % 500 == 0) hb(&c, t + ms * 1000u); }
t += 1000000u;
smi_ctl_laser_readback(&c, true, (uint16_t)(c.code[LI] ^ 1), t);
CHECK(!armed(&c) && c.fault == SMI_FAULT_READBACK, "wrong code in the readback: fault, off");
cmd(&c, t, cf, 1, NULL); enable(&c, t, 1, SMI_LASER_KEY); set(&c, t, LI, 0, 20);
for (int ms = 1; ms < 1000; ms++) { smi_ctl_tick(&c, t + ms * 1000u); if (ms % 500 == 0) hb(&c, t + ms * 1000u); }
t += 1000000u;
smi_ctl_laser_readback(&c, false, 0, t);
CHECK(!armed(&c) && c.fault == SMI_FAULT_READBACK, "failed readback: fault, off");
smi_ctl_laser_readback(&c, false, 0, t);
CHECK(c.fault == SMI_FAULT_READBACK, "readback ignored while disarmed");
cmd(&c, t, cf, 1, NULL);
// DAC failure while on: fault, laser off.
enable(&c, t, 1, SMI_LASER_KEY);
set(&c, t, LI, 0, 10);
for (int ms = 1; ms < 400; ms++) smi_ctl_tick(&c, t + ms * 1000u);
t += 400000u; hb(&c, t);
set(&c, t, LI, 0, 12);
hw.fail_dac = true;
for (int ms = 1; ms < 50; ms++) smi_ctl_tick(&c, t + ms * 1000u);
// A gated head DAC verifies its own write, so a failed write reads as a readback fault.
CHECK(!laser_pin_on() && (c.fault == SMI_FAULT_HW || (L->gate && c.fault == SMI_FAULT_READBACK)), "DAC failure: fault %u, off", c.fault);
// State block layout.
uint8_t st[160];
size_t sl = smi_ctl_state(&c, t, st);
uint32_t hsh = st[0] | (st[1] << 8) | (st[2] << 16) | ((uint32_t)st[3] << 24);
CHECK(sl == 30u + 6u * SMI_NUM_CONTROLS && hsh == SMI_MAP_HASH && st[27] == SMI_NUM_CONTROLS, "state block");
printf("%s: %d checks, %d failed (map %s, %d controls, laser limit %.1f mA, ramp %.1f mA/s)\n", fails ? "FAIL" : "OK", checks, fails, SMI_MAP_VERSION, SMI_NUM_CONTROLS, lim, L->ramp_per_s);
return fails ? 1 : 0;
}