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.3.1: test-stage firmware (J4 TMC2209, HOME, limits, self-test), Stage tab and Calibrate in smi-dash (simulat…
c0c5c9b
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
// SPDX-License-Identifier: MIT
// Host test of the stage core (src/smi_stage.c) against a mock mechanism and a
// mock TMC2209 that checks CRCs, keeps registers and counts IFCNT.
// cc -O2 -Wall -Wextra -I../src test_stage.c ../src/smi_stage.c -o test_stage && ./test_stage
#include "smi_stage.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int fails, checks;
#define CHECK(c, ...) do { checks++; if (!(c)) { fails++; printf("FAIL %s:%d ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
typedef struct {
uint32_t now;
bool step, dir, en_n;
long mech; // true screw position in pulses; HOME switch opens at mech <= 0
long home_offset; // shifts the switch (simulates a bumped or broken switch)
bool home_stuck_high, home_never;
uint32_t regs[128];
uint8_t ifcnt;
bool tmc_dead, tmc_ifcnt_stuck;
uint8_t version;
long pulses;
uint32_t last_pulse_us, min_gap_us;
bool pulse_while_disabled;
bool write_while_enabled; // a UART register write with EN_N low
} mock_t;
static void put(void *c, uint8_t g, bool v) {
mock_t *m = c;
if (g == SMI_STAGE_STEP_GPIO) {
if (v && !m->step) {
if (m->en_n) m->pulse_while_disabled = true;
m->mech += m->dir ? 1 : -1;
m->pulses++;
if (m->last_pulse_us && m->now - m->last_pulse_us < m->min_gap_us) m->min_gap_us = m->now - m->last_pulse_us;
m->last_pulse_us = m->now;
}
m->step = v;
} else if (g == SMI_STAGE_DIR_GPIO) m->dir = v;
else if (g == SMI_STAGE_EN_N_GPIO) m->en_n = v;
}
static bool home_in(void *c) {
mock_t *m = c;
if (m->home_stuck_high) return true;
if (m->home_never) return false;
return m->mech + m->home_offset <= 0;
}
static void wait_us(void *c, uint32_t us) { ((mock_t *)c)->now += us; }
static bool uart(void *c, const uint8_t *tx, size_t ntx, uint8_t *rx, size_t nrx) {
mock_t *m = c;
if (m->tmc_dead) return false;
if (tx[0] != 0x05 || tx[ntx - 1] != smi_tmc_crc(tx, ntx - 1)) return false;
uint8_t reg = tx[2] & 0x7F;
if (ntx == 8 && (tx[2] & 0x80)) {
if (!m->en_n) m->write_while_enabled = true;
uint32_t v = ((uint32_t)tx[3] << 24) | ((uint32_t)tx[4] << 16) | ((uint32_t)tx[5] << 8) | tx[6];
if (reg == SMI_TMC_GSTAT) m->regs[reg] &= ~v; else m->regs[reg] = v;
if (!m->tmc_ifcnt_stuck) m->ifcnt++;
return nrx == 0;
}
if (ntx != 4 || nrx != 8) return false;
uint32_t v = reg == SMI_TMC_IFCNT ? m->ifcnt : reg == SMI_TMC_IOIN ? ((uint32_t)m->version << 24) | 0x40 : m->regs[reg];
rx[0] = 0x05; rx[1] = 0xFF; rx[2] = reg;
rx[3] = (uint8_t)(v >> 24); rx[4] = (uint8_t)(v >> 16); rx[5] = (uint8_t)(v >> 8); rx[6] = (uint8_t)v;
rx[7] = smi_tmc_crc(rx, 7);
return true;
}
static mock_t M;
static smi_stage_t S;
static void reset(long mech) {
memset(&M, 0, sizeof M);
M.mech = mech; M.version = SMI_TMC_VERSION; M.min_gap_us = 0xFFFFFFFFu; M.regs[SMI_TMC_GSTAT] = 0x1; M.now = 1000;
smi_stage_hal_t h = { .ctx = &M, .put = put, .home_in = home_in, .wait_us = wait_us, .uart = uart };
smi_stage_init(&S, &h);
}
static void run(uint32_t us) { // tick every 50 us
for (uint32_t t = 0; t < us; t += 50) { M.now += 50; smi_stage_tick(&S, M.now); }
}
static uint8_t cmd(uint8_t sub, int32_t arg, uint16_t seq) {
uint8_t p[6] = { SMI_OP_STAGE, sub, (uint8_t)arg, (uint8_t)(arg >> 8), (uint8_t)(arg >> 16), (uint8_t)(arg >> 24) };
uint8_t r[40];
smi_stage_command(&S, p, 6, seq, M.now, r);
return r[3];
}
static void run_until_idle(uint32_t max_us) {
for (uint32_t t = 0; t < max_us && (S.state == SMI_STAGE_HOMING || S.state == SMI_STAGE_MOVING || S.state == SMI_STAGE_SELFTEST); t += 50) {
M.now += 50; smi_stage_tick(&S, M.now);
}
}
int main(void) {
// CRC: the datasheet algorithm against a published datagram (read GCONF: 05 00 00 48).
{ uint8_t d[3] = { 0x05, 0x00, 0x00 }; CHECK(smi_tmc_crc(d, 3) == 0x48, "crc of 05 00 00 = %02X", smi_tmc_crc(d, 3)); }
// Boot: disabled, unhomed, moves refused.
reset(300);
CHECK(M.en_n, "EN_N high at boot");
CHECK(S.state == SMI_STAGE_OFF && !S.homed, "off at boot");
CHECK(cmd(SMI_STAGE_SUB_STEP, 10, 1) == SMI_STAGE_E_NOT_HOMED, "step before home refused");
CHECK(cmd(SMI_STAGE_SUB_SELFTEST, 0, 2) == SMI_STAGE_E_NOT_HOMED, "selftest before home refused");
CHECK(M.pulses == 0, "no pulses before home");
// Home from 300 pulses out: configures the driver, finds the opening edge.
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 3) == SMI_STAGE_OK, "home accepted");
CHECK(!M.en_n, "enabled after a verified configuration");
CHECK(M.regs[SMI_TMC_GCONF] == SMI_TMC_GCONF_VAL && M.regs[SMI_TMC_CHOPCONF] == SMI_TMC_CHOPCONF_VAL &&
M.regs[SMI_TMC_IHOLD_IRUN] == SMI_TMC_IHOLD_IRUN_VAL && M.regs[SMI_TMC_SLAVECONF] == SMI_TMC_SLAVECONF_VAL, "registers written");
run_until_idle(20000000);
CHECK(S.state == SMI_STAGE_IDLE && S.homed && S.pos == 0, "homed: state %s pos %ld", smi_stage_state_name(S.state), (long)S.pos);
CHECK(M.mech == 0, "zero on the opening edge (mech %ld)", M.mech);
CHECK(M.min_gap_us >= 1000000u / SMI_STAGE_RATE_HOME - 60, "homing rate at most 80/s (gap %u us)", M.min_gap_us);
CHECK(!M.pulse_while_disabled, "no pulses while disabled");
CHECK(!M.write_while_enabled, "every configuration write made with EN_N high");
// Moves: limits, rate, arrival.
CHECK(cmd(SMI_STAGE_SUB_STEP, -1, 4) == SMI_STAGE_E_LIMIT, "below 0 refused");
CHECK(cmd(SMI_STAGE_SUB_STEP, 1601, 5) == SMI_STAGE_E_LIMIT, "beyond 1600 refused");
CHECK(cmd(SMI_STAGE_SUB_SPEED, 641, 6) == SMI_STAGE_E_RATE, "speed above 640 refused");
CHECK(cmd(SMI_STAGE_SUB_SPEED, 0, 7) == SMI_STAGE_OK && S.rate == 160, "default speed 160");
M.min_gap_us = 0xFFFFFFFFu; M.last_pulse_us = 0;
CHECK(cmd(SMI_STAGE_SUB_STEP, 200, 8) == SMI_STAGE_OK, "step 200");
CHECK(cmd(SMI_STAGE_SUB_STEP, 5, 9) == SMI_STAGE_E_BUSY, "busy while moving");
run_until_idle(5000000);
CHECK(S.pos == 200 && M.mech == 200, "arrived at 200 (pos %ld mech %ld)", (long)S.pos, M.mech);
CHECK(M.min_gap_us >= 1000000u / 160 - 60, "rate 160/s (gap %u us)", M.min_gap_us);
CHECK(cmd(SMI_STAGE_SUB_SPEED, 640, 10) == SMI_STAGE_OK, "speed 640");
M.min_gap_us = 0xFFFFFFFFu; M.last_pulse_us = 0;
CHECK(cmd(SMI_STAGE_SUB_STEP, 1400, 11) == SMI_STAGE_OK, "step to 1600");
run_until_idle(5000000);
CHECK(S.pos == 1600, "at the 1600 limit");
CHECK(M.min_gap_us >= 1000000u / 640 - 60, "rate 640/s (gap %u us)", M.min_gap_us);
CHECK(cmd(SMI_STAGE_SUB_STEP, 1, 12) == SMI_STAGE_E_LIMIT, "one past the limit refused");
// Duplicate seq: cached answer, no new pulses.
long p0 = M.pulses;
CHECK(cmd(SMI_STAGE_SUB_MOVE_TO, 1000, 13) == SMI_STAGE_OK, "move to 1000");
run_until_idle(5000000);
long p1 = M.pulses;
CHECK(cmd(SMI_STAGE_SUB_MOVE_TO, 1000, 13) == SMI_STAGE_OK, "retry answered");
CHECK(cmd(SMI_STAGE_SUB_STEP, 50, 14) == SMI_STAGE_OK, "step 50");
run_until_idle(5000000);
long p2 = M.pulses;
CHECK(cmd(SMI_STAGE_SUB_STEP, 50, 14) == SMI_STAGE_OK, "retry of step 50 answered");
run(2000000);
CHECK(p1 - p0 == 600 && p2 - p1 == 50 && M.pulses == p2 && S.pos == 1050, "retry issued no pulses (pos %ld)", (long)S.pos);
// Stop mid-move keeps a valid position.
cmd(SMI_STAGE_SUB_SPEED, 160, 15);
cmd(SMI_STAGE_SUB_STEP, -500, 16);
run(500000);
CHECK(cmd(SMI_STAGE_SUB_STOP, 0, 17) == SMI_STAGE_OK && S.state == SMI_STAGE_IDLE && S.homed, "stop keeps homed");
CHECK(S.pos == M.mech, "position tracks the mechanism after stop (%ld vs %ld)", (long)S.pos, M.mech);
// Self-test: origin 128, +128 x4, -128 x4, 1 s dwells.
long visits[12]; int nv = 0; uint32_t t0 = M.now;
CHECK(cmd(SMI_STAGE_SUB_SELFTEST, 0, 18) == SMI_STAGE_OK, "selftest");
int32_t lastpos = S.pos; uint32_t still_since = M.now;
while (S.state == SMI_STAGE_SELFTEST && M.now - t0 < 60000000u) {
M.now += 50; smi_stage_tick(&S, M.now);
if (S.pos != lastpos) { lastpos = S.pos; still_since = M.now; }
else if (M.now - still_since == 900000u && nv < 12) visits[nv++] = S.pos;
}
long want[] = { 128, 256, 384, 512, 640, 512, 384, 256, 128 };
CHECK(nv == 9, "selftest plateaus: %d", nv);
for (int i = 0; i < nv && i < 9; i++) CHECK(visits[i] == want[i], "plateau %d at %ld (want %ld)", i, visits[i], want[i]);
CHECK(S.state == SMI_STAGE_IDLE && S.pos == 128, "selftest ends idle at 128");
// Unexpected HOME during a normal move: disable and invalidate.
M.home_offset = -400; // switch now reads high anywhere below 400
cmd(SMI_STAGE_SUB_MOVE_TO, 1000, 19); run_until_idle(10000000);
M.home_offset = -2000;
cmd(SMI_STAGE_SUB_STEP, -10, 20); run_until_idle(1000000);
CHECK(S.state == SMI_STAGE_FAULTED && S.fault == SMI_STAGE_FAULT_HOME_UNEXPECTED && M.en_n && !S.homed, "unexpected HOME faults (state %s)", smi_stage_state_name(S.state));
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 21) == SMI_STAGE_E_FAULT, "home refused while faulted");
CHECK(cmd(SMI_STAGE_SUB_OFF, 0, 22) == SMI_STAGE_OK && S.state == SMI_STAGE_OFF && S.fault == 0, "stage off clears the fault");
// Homing starting on the switch: release search first.
reset(-20);
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 1) == SMI_STAGE_OK, "home from on the switch");
run_until_idle(20000000);
CHECK(S.homed && M.mech == 0, "homed after release (mech %ld)", M.mech);
// Stuck HOME: release search fails after 128 pulses.
reset(500); M.home_stuck_high = true;
cmd(SMI_STAGE_SUB_HOME, 0, 1); run_until_idle(20000000);
CHECK(S.fault == SMI_STAGE_FAULT_HOME_STUCK && M.en_n && M.pulses <= SMI_STAGE_HOME_MAX_RELEASE, "stuck HOME faults after %ld pulses", M.pulses);
// Disconnected switch reads high: same as stuck. Missing switch edge: timeout.
reset(500); M.home_never = true;
cmd(SMI_STAGE_SUB_HOME, 0, 1); run_until_idle(60000000);
CHECK(S.fault == SMI_STAGE_FAULT_HOME_TIMEOUT && M.pulses <= SMI_STAGE_HOME_MAX_SEARCH, "search timeout after %ld pulses", M.pulses);
// Driver configuration failures leave the stage disabled.
reset(300); M.version = 0x20;
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 1) == SMI_STAGE_E_DRIVER && M.en_n && M.pulses == 0, "wrong driver version refused");
reset(300); M.tmc_dead = true;
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 1) == SMI_STAGE_E_DRIVER && M.en_n, "silent driver refused");
reset(300); M.tmc_ifcnt_stuck = true;
CHECK(cmd(SMI_STAGE_SUB_HOME, 0, 1) == SMI_STAGE_E_DRIVER && M.en_n, "IFCNT not counting refused");
// DRV_STATUS fault while holding, and link loss.
reset(100); cmd(SMI_STAGE_SUB_HOME, 0, 1); run_until_idle(20000000);
M.regs[SMI_TMC_DRV_STATUS] = 0x2; // ot
run(600000);
CHECK(S.fault == SMI_STAGE_FAULT_DRV_STATUS && M.en_n, "over-temperature disables");
reset(100); cmd(SMI_STAGE_SUB_HOME, 0, 1); run_until_idle(20000000);
smi_stage_link_lost(&S);
CHECK(S.fault == SMI_STAGE_FAULT_LINK && M.en_n && !S.homed, "link loss disables and invalidates");
// Text commands.
reset(100);
char r[200];
smi_stage_text(&S, "stage step 10", M.now, r, sizeof r);
CHECK(strstr(r, "not homed") != NULL, "text: %s", r);
smi_stage_text(&S, "stage home", M.now, r, sizeof r); run_until_idle(20000000);
smi_stage_text(&S, "stage step 64", M.now, r, sizeof r); run_until_idle(2000000);
smi_stage_text(&S, "stage status", M.now, r, sizeof r);
CHECK(strstr(r, "pos=64") != NULL && strstr(r, "homed=1") != NULL, "text status: %s", r);
smi_stage_text(&S, "stage speed 999", M.now, r, sizeof r);
CHECK(strstr(r, "1..640") != NULL, "text speed: %s", r);
smi_stage_text(&S, "stage current", M.now, r, sizeof r);
CHECK(strstr(r, "0.410 A") != NULL, "text current: %s", r);
printf("%d checks, %d failed\n", checks, fails);
return fails ? 1 : 0;
}