Cable receiver: a Pico 2 W and an MCP4728
This page is firmware/pico/README.md from the repository.
A browser page (web/serial.html) sends voltergeist frames over a USB cable to a Raspberry Pi Pico 2 W (RP2350). The
Pico decodes them with c/voltergeist.h, plays them out on its own clock a fixed delay later, and writes four 12 bit
codes to an MCP4728 over I2C. Gates from EVENTS go to four GPIOs. No Pi, no network, nothing allocated.
Written and built on 2026-12-06 before the Pico or the board was on the desk: everything below the line "What needs the real Pico" was measured in a container and in headless Chrome, not on hardware.
Files
| file | what |
|---|---|
vgrx.h, vgrx.c | the receiver with no platform in it: byte stream in, Multi-Write, gates, PONG, REPORT and status out |
main.c | the Pico: TinyUSB CDC, i2c0, the 1 kHz repeating timer, the LED |
usb_descriptors.c, tusb_config.h | one CDC serial port, 2E8A:0009, named "voltergeist receiver" |
CMakeLists.txt | pico-sdk 2.x project, PICO_BOARD=pico2_w |
host-test.c | vgrx.c on Linux with a fake clock, fake I2C, fake serial and fake gate pins |
make-vector.mjs, vector.h | the stream the host test feeds, made by the page's own code (web/serial.mjs), and the DAC writes src/mcp4728.mjs makes for it |
check-page.mjs | web/serial.html in headless Chrome, with a fake serial port |
build.sh, build-inside.sh | the container build: host test, sabotage, firmware |
vgrx.h includes ../../c/voltergeist.h by a relative path; nothing is copied.
What the firmware does
- USB in. A CDC ACM port. Bytes go through
vg_reader(SPEC.md 1.1, a u16 little endian length before each frame), thenvg_decode. A frame over 2048 bytes is skipped and counted (oversize). A length under 16 stops the byte stream, as the spec says; the page opening the port again (DTR raised) starts a new one. - MAP, SAMPLES, EVENTS, CLOCK. Up to four streams at once. A MAP gives the rate, the format and each channel's
minandmax. Map id 0 frames are played raw (SPEC.md 4.1), a regular one timed from two frames. CLOCK sets the offset (stream 0 for all streams); until one arrives the smallest transit seen is used (SPEC.md 6). - Playout.
vg_playoutper samples stream (512 slots, 512 ms at 1 kHz), read every 1 ms by a repeating timer atT = now - offset - D, D = 20 ms (VGRX_DELAY_US). Hold on loss, linear between consecutive samples. - The DAC. Channels 0 to 3 of the first samples stream heard in the last 500 ms go to DAC A to D. A value maps
through its channel's
minandmaxto 0..4095 (dacCodeinsrc/mcp4728.mjs); with no range, or map id 0, the range is -1..1. Integer formats are scaled first (vg_to_unit). When any code changed, one Multi-Write of every routed channel goes to 0x60 on i2c0, VDD reference, UDAC 0 (no LDAC needed): 13 bytes, about 0.3 ms at 400 kHz. A failed write is counted and retried on the next tick. - Gates (stretch, done). EVENTS channels 0 to 3 drive GP16 to GP19: gate on and off by value, note on high,
note off low, trigger high for 5 ms. An event up to 10 ms late still fires (
late fired); later is dropped (late). - PING and PONG. Every PING gets a PONG on the same line, t1 the arrival, t2 the send, so the page can set the offset (SPEC.md 5.3).
- Once a second a REPORT per stream (lost, late, late fired, recovered, jitter, highest seq; SPEC.md 5.5) and a
HELLO whose name is the status:
vgrx 1 i2c=ok writes=600 i2cerr=0 frames=67 rejected=0 skipped=0 stale=0 oversize=0 link=ok delay=20. - LED (on the wireless chip, so
pico_cyw43_arch_none): a short blink each second while waiting, on while frames arrive, a fast blink after an I2C error or a stopped byte stream.
Concurrency: the main loop runs TinyUSB and feeds bytes; the timer IRQ plays out and writes I2C. A hardware spin lock guards the streams; the I2C write happens outside it.
Where it differs from src/receiver.mjs
- A frame whose map does not match is dropped and counted
stale, not held for up to 64 frames: on a serial line the page sends the MAP first and nothing is reordered. - Repeats are recognised over the last 1024 seqs per stream (JS: 65536).
- A samples stream wider than 8 channels is not played (
unsupported); four streams at most, a slot idle 5 s is reused. - The page adds its own delay on top of the fixed 20 ms by adding it to the CLOCK offset (below).
The page
web/serial.html (Chrome or Edge on a desktop; it says so elsewhere). Connect asks for the port (filtered to
2E8A:0009; ?any lists every port), drops and raises DTR, then:
- test signal: four channels at 1 kHz, k 10 (ramp, sine, square in 0..1 and a ramp in -5..5 V, from
web/serial.mjs), a gate at 2 Hz on channel 0 of a second stream; - relay room: joins a relay room as
receive.htmldoes, answers the sender's PINGs, and forwards its MAP, SAMPLES and EVENTS frames to the Pico unchanged, each with the length prefix. The sender's CLOCK for a stream (sender to page) is added to the page's own offset (page to Pico) and sent on as a CLOCK for that stream, the two legs of SPEC.md 5.4.
It pings the Pico 4 times a second, sends CLOCK every second, and shows the round trip, the Pico's status and its REPORTs (lost, late, late fired, recovered, jitter). Delay (default 40 ms, 80 for a relay) is what the page asks for; the part above the firmware's 20 ms is added to the offset in each CLOCK.
Build (in a container, never on the Mac)
sh firmware/pico/build.sh # host test, sabotage, firmware
sh firmware/pico/build.sh host # or one of host, sabotage, pico
node firmware/pico/make-vector.mjs # after changing web/serial.mjs's test source (--check says whether it is stale)
node firmware/pico/check-page.mjs # the page in headless Chrome (--sabotage must fail)
build.sh runs a throwaway alpine container, installs the toolchain (gcc, cmake, arm-none-eabi-gcc 16.2,
newlib, linux-headers for picotool) into a 6 GB RAM disk and runs it there through chroot, so the toolchain never
touches a disk. Two reasons: this Mac kills binaries compiled on it, and on 2026-12-06 the OrbStack VM had 148 MB
free and the Mac 2 GB, too little for a Debian image with the toolchain installed. The only things written to the
repository's disk are the pico-sdk 2.3.1 checkout (shallow, tinyusb and cyw43-driver only) and the picotool build,
90 MB together in firmware/.cache (git-ignored), and the output in firmware/pico/build.
Measured 2026-12-06, OrbStack on an M-series Mac, arm64 container:
| result | |
|---|---|
| firmware | build/voltergeist-rx.uf2, 600,064 bytes (1,172 UF2 blocks, family RP2350 ARM-S plus the SDK's E10 workaround block); text 303,808 bytes (most of it the CYW43 firmware the LED needs), bss 135,628 bytes of the 520 KB RAM |
| compiler warnings | 0, with -Wall -Wextra on the target |
| host test | 30 of 30 |
| sabotage | 5 of 5 caught |
| page check | 24 of 24; with --sabotage 2 fail as they must |
The host test
host-test.c feeds vector.h through vgrx_feed in random chunks of 1 to 64 bytes, ticks every 1 ms, and checks:
- all 600 Multi-Writes are byte for byte what
src/mcp4728.mjsproduces for the same samples, at address 0x60, each at its sample's clock + offset + 20 ms to the microsecond; - the gate edges on GP16 at 0, 250 and 500 ms (+ 20);
- the PING gets one PONG with t0 copied and t1, t2 the Pico's clock;
- REPORT: highest 599, none lost, none late; then with one frame dropped and one 40 ms late, lost 10 and late 10;
- a version 2 frame, a NaN value and a frame one byte short are each rejected and counted, an unknown type is
skipped, a 3000 byte frame is skipped as oversize, and the next good frame plays; a length under 16 stops the
stream until
vgrx_reset_link; - a failing I2C bus is counted and the codes go out on the first tick after it answers;
- map id 0 raw values -1, 0, 1, 0.5 become codes 0, 2048, 4095, 3071, from a stream that starts after another ended.
build.sh sabotage breaks one thing at a time in a copy of vgrx.c and requires the test to fail: the Multi-Write
command byte (2 checks fail), half the delay (5), PONG's t0 and t1 swapped (2), late samples not counted (1), the DAC
staying on a stream that went quiet (1).
The page check
check-page.mjs serves the repository with web/serve.mjs, opens web/serial.html in headless Chrome (muted, temp
profile, both removed at the end) and checks: no exception; the page's own code makes the 11,678 bytes of vector.h
exactly; without Web Serial it says what is needed and Connect is off; with a fake port and a fake Pico answering PONG,
REPORT and status: DTR dropped and raised, both MAPs before the first SAMPLES, SAMPLES at 99.9 frames a second with no
seq gap, gates, PINGs, a CLOCK within 0.05 ms of the fake Pico's offset + 20 ms, the status and REPORT shown, a clean
disconnect; in relay mode through the local relay, send.html's frames forwarded with a per-stream CLOCK of both legs
(0.25 ms from the expected value) and none for stream 0.
What needs the real Pico and board
Nothing below has run yet:
- that the .uf2 boots on a Pico 2 W, enumerates as
2E8A:0009, and Chrome opens it; - the TinyUSB direct path (no
pico_stdio_usb), the DTR callback, and whether a PONG and REPORT written from the main loop ever drop (serial_dropped, not shown yet); - the MCP4728 answering at 0x60 on GP4 and GP5 at 400 kHz, the Multi-Write landing as the datasheet says, the voltages;
- the 1 kHz timer's jitter with a 0.3 ms blocking I2C write inside the IRQ, and USB staying healthy meanwhile;
- the LED through
cyw43_arch; - the real round trip and the delay a browser's 4 ms timers and USB polling need; 20 ms for the firmware and 40 for the page is a guess from the test signal's batching (9 ms) plus timer and USB slack.
Flashing
- Hold BOOTSEL on the Pico 2 W and plug it into USB; it appears as a drive called
RP2350. - Copy
firmware/pico/build/voltergeist-rx.uf2onto it. The Pico reboots into the firmware and the drive goes away. - The LED gives a short blink once a second: it is waiting for frames.
Wiring the Electrokit board (EKM016) to the Pico
| Pico 2 W pin | signal | board pin |
|---|---|---|
| 36, 3V3 OUT | 3.3 V supply, and the full scale with the VDD reference | VCC |
| 38 (or any GND) | ground | GND |
| 6, GP4 | I2C0 SDA | SDA |
| 7, GP5 | I2C0 SCL | SCL |
| 21, 22, 24, 25: GP16 to GP19 | gates for EVENTS channels 0 to 3, 3.3 V logic | (not on the board) |
LDAC and RDY stay unconnected (UDAC 0, no EEPROM writes). Stay on 3V3: on 5 V the board's pull-ups would put 5 V on the Pico's pins. The outputs give 0 to 3.3 V; Eurorack levels need an op-amp stage (hardware/README.md).
In the browser
- Chrome or Edge on a desktop. Serve the repository (
node web/serve.mjs) and openhttp://127.0.0.1:8091/web/serial.html. - Press Connect Pico and pick "voltergeist receiver". The Pico panel shows
I2C idlewithin a second. - Leave Source on test signal and press Start. Within a second the round trip shows, the Pico panel shows
I2C okand DAC writes climbing, the Reports table shows a row with lost 0 and late 0, and the LED stays on.I2C errormeans the board is not answering at 0x60: check the four wires. - For a stream from elsewhere, pick relay room, give the relay and room the sender uses, press Start, and start
send.html(ortools/send.mjs) into that room. - Keep the tab in front: a background tab's timers slow to about once a second and every frame arrives late.
Measured on a real Pico 2 W (2026-12-06)
Plugged into the Raspberry Pi 4 (positron's board) by USB, no MCP4728 attached yet. The stock program it held answered a
1200 baud open by rebooting into BOOTSEL (stty -F /dev/ttyACM0 1200), so it was flashed with no button: mount the
RP2350 drive, copy voltergeist-rx.uf2. It enumerated as 2e8a:0009 voltergeist voltergeist receiver on
/dev/ttyACM0. node firmware/pico/pi-test.mjs on the Pi, 15 s of the 4-channel test signal at 1 kHz plus a gate:
| samples sent | 14,992 |
| lost, late (its REPORT) | 0, 0 |
| jitter (its REPORT) | 0.4 ms |
| round trip over USB serial, 29 pings | 0.34 ms min, 0.51 ms median |
| frames handled, rejected, skipped, stale | 5,276, 0, 0, 0 |
| i2c | error on every write: no board yet, as expected |
The firmware now also reboots into BOOTSEL on a 1200 baud open (tud_cdc_line_coding_cb), so after this build is on the
Pico it can be reflashed from the host with no button. Done and checked the same day: one BOOTSEL press put this build on,
then stty -F /dev/ttyACM0 1200 dropped it into BOOTSEL and it was reflashed over ssh with nobody touching it; the test above
read the same again (0 lost, 0.33 ms min round trip).
Measured from Chrome on the studio Mac (2026-12-06)
The Pico on the M1 through a USB hub, https://voltergeist.positron.studio/tools/serial in Chrome, test signal: ThreatLocker
on that Mac let Chrome open the port (2e8a:9). 887 frames out, 166 kB; round trip 0.59 ms; the Pico handled 947 frames,
0 rejected, skipped, stale or oversize; its REPORTs on both streams 0 lost, 0 late, jitter 1.67 ms (samples) and 1.23 ms
(gate); i2c=error, no board yet. The page's "Pico clock offset" cell shows the raw offset between the browser's epoch
clock and the Pico's boot clock (about -1.8e9 s), which is right but unreadable.