main
John Lauer Publish 1.4.0 892200b

Main-board RP2040 firmware

The onboard RP2040 samples the two photodiode channels, controls the laser head and drives the external stage. GPIO24 senses DATA VBUS. The PD port supplies all operating power. The application builds for the smi_v14 board target.

Pin Use
GPIO26 / ADC0, pin 31 Channel 1, laser monitor photodiode after the analog chain
GPIO27 / ADC1, pin 32 Channel 2, external photodiode
GPIO15, pin 20 SYNC from the motion stage (the board's SN74LVC1G17 buffer, non-inverting; internal pulls off)
ADC_VREF, pin 35 Filtered on the board (C150), not driven by the firmware
USB CDC serial: the data stream and the command channel
GPIO4 / GPIO5 I2C0 SDA / SCL, 100 kHz, 750 us per transfer: the laser head's DAC121C085 (0x09, laser set-point), MCP23008 (0x20) and ADS1115 (0x49, differential AIN0-AIN1); the main board's DAC7574 (0x4C, offsets)
GPIO6 Laser enable as a 10 kHz carrier (PWM), only after a verified nonzero set-point
GPIO7, GPIO8, GPIO9 Monitor second-stage gain, external second-stage gain, external TIA gain
GPIO28 / ADC2, pin 34 Main 5.3 V analog rail through a 1:2 divider, read once a second
GPIO10, GPIO11, GPIO12 Stage (J4) STEP, DIR, EN_N (active low; driven high at boot before anything else touches the port)
GPIO13 Stage TMC2209 PDN_UART, one wire, 115200 8N1, bit-banged half duplex (driven only while sending, released after the stop bit)
GPIO14 Stage HOME, normally closed to ground with the board's 10 k pull-up (high = open or disconnected)

The board's pins and devices come from its control map: controls/gen_controls.py writes them into src/smi_controls_gen.h. Rebuild after changing the map.

Flashing

  1. Hold the BOOTSEL button and plug the Pico into USB. A drive called RPI-RP2 appears.
  2. Copy smi_pico.uf2 (from software/dist/, or your own build) onto it. The Pico reboots and enumerates as a USB serial device, "SMI Interferometer Digitizer", serial number = the board's flash ID.
  3. With picotool instead: picotool load -x smi_pico.uf2 while the Pico is in BOOTSEL mode.

What it does

  • The ADC runs free at 48 MHz / 96 = 500 ksps in round robin over ADC0 and ADC1, so each channel gets 250 ksps and channel 2 is sampled 2 us after channel 1. Even samples are always ADC0 because acquisition restarts at input 0.
  • Two DMA channels, chained ping-pong, fill a ring of 16 buffers of 1024 pairs (4.1 ms each, 65 ms of slack for USB stalls). If the ring overruns, the buffer is counted as dropped and the host sees the gap in the sample index.
  • A GPIO interrupt on both edges of GPIO15 reads the live DMA position, so every SYNC edge carries the index of the sample pair it landed on (resolution one pair, 4 us, plus interrupt latency) and a time_us_32 timestamp.
  • The main loop packs each finished buffer into one DATA frame and writes it to USB CDC. A STATUS frame every 0.5 s reports the pair count, dropped buffers and lost SYNC events.
  • Streaming starts when the host opens the port (DTR) and stops when it closes it. Text commands, one per line: start, stop, info, and the board contract's laser CODE, laser off, laser current, head MASK, gain MASK, offset B CODE, offset C CODE (plus hb MS), in src/smi_text.c.
  • Controls (board 1.2 and 1.3): binary CMD frames on the same link run the control core in src/smi_ctl.c (ops, ACK and STATE layouts in src/smi_ctl.h). The laser is off at power-up, after a watchdog reset, when the port closes or USB goes away, and after 3 s without a command; each set-point write follows the board's controller contract (enable dropped, 2 ms wait, write, readback, carrier only on a verified nonzero code, one more readback; at most four writes a second; command codes pass a 45-count guard band) and is refused above the limit; while armed the head DAC is read back every 2 ms (which also feeds the head's I2C watchdog) and the bus carries no other writes; the head's ADS1115 is read 50 times a second for the overcurrent trip, with its configuration written only while disarmed. A STATE frame goes out four times a second.
  • Test stage (board 1.3, src/smi_stage.c, rules in src/smi_stage.h): disabled at boot; the TMC2209 is configured over its UART with EN_N high, every write checked by IFCNT, before it is enabled; homing as test-stage/design/stage-map.json describes; moves only inside 0 to 1600 pulses from HOME after homing; 160 pulses/s by default, at most 640; disabled on a fault, stage off, USB loss, or (after the first binary command) 3 s without a host command. Binary op 0x09 (status, home, step, speed, stop, off, current, selftest, move) with sequence-number duplicate suppression; text stage ... lines. A STAGE frame every 50 ms while enabled, else every 500 ms. DIR_POS_LEVEL in smi_stage.c sets the DIR polarity for commissioning. Board 1.4 keeps the same GPIO numbers.
  • The analog rail on ADC2 is read once a second by pausing the round robin after a complete ch0/ch1 pair for about 14 us; pair indices stay contiguous and the pause count is in STATUS.

Stream format

All fields little-endian. Every frame:

Bytes Field
2 sync word A5 5A
1 type: 1 DATA, 2 SYNC, 3 STATUS, 4 INFO, 0x10 CMD (host to Pico), 0x11 ACK, 0x12 STATE, 0x13 STAGE
1 flags, 0
2 payload length
2 sequence number, wraps
n payload
2 CRC-16/CCITT-FALSE (poly 0x1021, init 0xFFFF) over type through the end of the payload

DATA payload: u64 first_pair, u32 t_us (time the DMA buffer completed), u8 groups (16), then for each group of 64 pairs a channel 1 block and a channel 2 block. A block is u16 head (bits 0 to 11 the first sample, bits 12 to 15 a width code w) followed by 63 values packed LSB first and padded to a byte: for w from 0 to 12 they are zigzag-encoded deltas of w bits, for w = 15 they are raw 12-bit samples (used when the deltas would need 13 bits).

SYNC payload: u64 pair, u32 t_us, u8 level. STATUS payload: u64 pairs, u32 dropped_buffers, u32 sync_lost, u32 total_sps, u32 t_us, then (1.2.0) u32 adc2_pauses, u16 rail_mV. INFO payload: ASCII text with the version and rates. STAGE payload (1.3.1): u8 state (0 off, 1 idle, 2 homing, 3 moving, 4 selftest, 5 faulted), u8 flags (1 enabled, 2 homed, 4 HOME high, 8 moving), u8 fault, u8 home_phase, i32 pos, i32 target, u16 rate, u8 selftest_rung, u8 ifcnt, u32 steps_total, u32 drv_status, then u64 pair (the sample-pair count when sent).

On realistic fringe signals a block takes about 4.6 bits per sample including all headers, about 290 kB/s for both channels; the worst case (pure noise) is 12 bits per sample, 750 kB/s, still inside USB full speed.

Building

export PICO_SDK_PATH=/path/to/pico-sdk     # tested with 2.1.1 and its lib/tinyusb submodule
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j                     # build/smi_pico.uf2

Toolchain used for the published file: arm-none-eabi-gcc 13.2 on Ubuntu 24.04. The first configure downloads and builds picotool, which the SDK uses to make the .uf2.

Tests

test/test_ctl.c runs the control core against the generated control table (293 checks; cc -O2 -Isrc test/test_ctl.c src/smi_ctl.c -lm). test/test_stage.c runs the stage core against a mock TMC2209 and screw (62 checks; cc -O2 -Isrc test/test_stage.c src/smi_stage.c). test/ctl_pty.c serves the control, stage, text-command and frame code behind a pseudo-terminal (cc -O2 -Isrc test/ctl_pty.c src/smi_ctl.c src/smi_text.c src/smi_frame.c src/smi_stage.c -lm), so the dashboard's serial command path and the text commands (test/text_cmd_test.py, 20 checks including the stage) can be tested without a Pico.

test/test_pack.c round-trips 40,000 blocks through the packer and checks the CRC against its standard check value, and writes test/vectors.bin, which the dashboard's Rust decoder must decode exactly. test/stream_gen.c packs raw sample pairs from stdin into a full stream, for replaying recorded or simulated data into the dashboard over a serial port.

gcc -O2 -Isrc -o test_pack test/test_pack.c src/smi_frame.c -lm && ./test_pack test/vectors.bin