Files
hardware/firmware
Claude 1bb455684a hardware: W variant netlist and schematic (RM2 Bluetooth)
build_netlist.py and build_schematic.py take --variant w: the RM2 wired
as on Pico W (GP23-25, GP29), fitted LEDs (two on the RM2's GPIOs), VBUS
sense on RM2 GPIO0 and a pairing button on GP28. Adds the RM2 symbol and
a footprint generated from the datasheet's Figure 6, and the W pin header
for the firmware. The design date is now fixed, so regenerating on a
later day leaves the files unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PRsUCpxazyipPn5UGGkF8
2026-10-18 10:39:15 +00:00
..

pico-serial firmware

RP2040 firmware for the DB25 hood board (SPEC.md §5): a USB CDC serial port bridged to the DB25. It works out by itself whether a computer (DCE role) or a modem (DTE role) is attached, and switches on the matching RS-232 chip. As DCE it also learns the computer's baud rate from its typing.

Build

Needs CMake 3.13+, Ninja or Make, and the Arm GNU toolchain (arm-none-eabi-gcc).

cmake -S firmware -B firmware/build -G Ninja      # -DPS_BOARD=hood (default) or pico
ninja -C firmware/build
# -> firmware/build/pico-serial-hood.uf2
  • Dependencies: CMake fetches Pico SDK 2.3.1 and TinyUSB 0.21.0 from GitHub. To use local copies instead, pass -DPICO_SDK_PATH=… and -DPICO_TINYUSB_PATH=….
  • Why a newer TinyUSB: the SDK bundles an older TinyUSB, and only 0.19+ can send CDC serial-state notifications (DCD/DSR/RI to the host).
  • Boards:
    • hood: the real board, with 16 MB flash and the slow-crystal start-up margin.
    • pico: a bench rig built on a Raspberry Pi Pico wired to the two transceivers as SPEC.md §4.5. Same GPIOs. The LEDs move off GP23/24, which belong to the Pico.

Flash

  • First time: hold BOOTSEL, plug in USB, copy the .uf2 to the RPI-RP2 drive.
  • After that: run picotool reboot -u -f, then copy. The board has Raspberry Pi's reset interface, so no button press is needed.
  • No 1200-baud reboot: the "set 1200 baud to reboot" trick is off, because 1200 baud is a real modem speed.

What it does

USB Composite device: CDC serial port, the pico-serial control vendor interface (PROTOCOL.md), and the picotool reset interface
Role auto (default): samples chip A's STATUS (a computer drives pins 2/4/20) and chip B's INVALID (a modem drives pins 3/5/6/8/22) every 50 ms with both chips off. Three matching samples in a row switch on that chip. The peer missing for 1 s switches it off again. A computer wins if both look present. dce/dte can also be forced; the setting is saved in flash
DCE lines Host DTR → computer's DCD and DSR; host RTS → computer's CTS, also held off while the to-host buffer is ¾ full. RI comes only from SET_OUTPUTS
DTE lines Host DTR → modem's DTR; host RTS → modem's RTS (same buffer hold-off)
To the host CDC SERIAL_STATE. As DCE: DSR = the computer's DTR, DCD = a computer is attached. As DTE: the modem's DCD, DSR, RI. Framing, parity, overrun and break events also go up
Flow control auto: obeys the far end's flow line only once it has asserted it, so 3-wire cables still work. on and off can be forced
Baud rate auto (default): as DCE, learned from the computer's characters; as DTE, the host's. host and learn (both roles) can be set; saved in flash. See below
Line coding 5–8 data bits, none/odd/even parity, 1 or 2 stop bits. Mark/space parity and 1.5 stop bits are mapped to the nearest the UART can do, and flagged in GET_INFO, as are speeds above the transceivers' 250 kbit/s
Break CDC SEND_BREAK, timed or until cleared
LEDs PWR, TR, CD, SD, RD, RI, as on a modem's front panel (optional on the board)
Safety Both transceivers start off (as do the board's pull-downs) and are never on together. Watchdog 1 s
USB IDs 1209:0001, pid.codes' shared test PID. Get a real PID before giving boards to others (src/rp2040/board_config.h)

Automatic baud rate

As DCE, the computer types at whatever speed its terminal is set to, and the board works out which. The host's SET_LINE_CODING speed is ignored, but its data bits, parity and stop bits still apply. As DTE the host's speed is used, since a modem follows the speed it's spoken to at; learn mode turns learning on there too (for a device that talks first, like a GPS).

Measuring. A PIO state machine (src/rp2040/autobaud.pio) watches the RX pin while the UART keeps it: PIO reads every pin's input whatever the pin is assigned to. From the first falling edge it times each low and high stretch, in units of 2 system clocks (16 ns at 125 MHz), and an interrupt passes them to the core.

Deciding (src/core/autobaud.c). For each standard rate, 110 to 230 400 baud, it decodes the timings as a UART would, with the host's format. A rate fits if:

  • a bit is no longer than the shortest stretch;
  • the level changes inside each character fall on bit boundaries;
  • at most 1 in 8 characters has a framing or parity error.

Of the rates that fit, it takes the slowest whose changes sit about as close to bit boundaries as the best one's. That rules out a rate too fast that happens to decode, and a slower one that fits characters with no 1-bit run. It decides once 2 characters have decoded at that rate. They are counted only at the chosen rate, because a rate too fast can split one character into two.

Then:

  • it waits for the line to go idle for a character's time, so the switch never lands in the middle of one;
  • the characters it learned from are decoded from the timings and sent to the host, so "AT" isn't lost;
  • the UART switches to the new speed, and GET_INFO reports it.

Learning again: after 3 framing errors within 2 s (the computer changed speed), on the AUTOBAUD_RESTART request, or when a new peer is detected. Until the new speed is known, the board keeps sending at the old one and holds back what it receives.

Limits:

  • Two characters are enough: "AT", or Enter as CR LF, or Enter pressed twice on a terminal that sends only CR. One character on its own isn't. With so few characters, very noisy lines can rarely fool it (see the tests below); a wrong guess shows up as framing errors and is re-learned.
  • Standard rates only. A sender up to about 3% off a standard rate is fine.
  • Not the format. 7E1 and 8N1 characters are the same length, so the speed is right either way, but the bytes are only right with the host's format.
  • Lost characters: while it re-learns, characters that came at the old speed are lost; so is anything after more than ~40 characters without a decision (it then starts over from the newest).

Layout

src/core/       portable: role detection, line mapping, flow control, baud-rate
                learning, settings (no SDK)
src/rp2040/     HAL (GPIO, UART + IRQ, PIO line timing, flash), USB descriptors
                and callbacks, main
src/generated/  board_pins.h, written by hardware/netlist/build_netlist.py
boards/         SDK board header for the hood board
test/           host tests for the core (fake HAL) and the emulator test

The GPIO numbers come from the hardware netlist script. Change the board there, and the pin header follows.

Tests

make -C firmware/test run                                   # core and baud detector, sanitizers on
firmware/test/emu/run.sh firmware/build/pico-serial-hood.bin   # whole firmware in an emulator
  • Host tests: the core's logic against a fake board (27 tests). The tests were checked by breaking the core on purpose: removing DCE detection, the buffer hysteresis, the DSR mapping, the one-shot error events, holding back data while learning, the re-learn on errors. Each break is caught.
  • Baud detector tests (test_autobaud.c): waveforms made from text and random bytes at every standard rate, in 8N1, 7E1, 7O1, 8N2 and 8E1, with the sender up to 2.5% off and edges moved by up to 10% of a bit. Also: glitches, a capture starting mid-character, and characters with no single-bit run.
  • Stress run (not in CI): 20 000 random messages per setting, with edges moved by up to 8% of a bit and the sender up to 3% off.
    • Printable text, 2 to 12 characters: no wrong rate.
    • Random bytes: no wrong rate from 5 characters up. With only 2 characters, 2 in 20 000 were wrong.
    • CR LF, and CR CR: no wrong rate, all decoded.
    • One character alone: waits, except 5–7 in 20 000 at 5% jitter (none without jitter).
  • Emulator test: boots the real .bin in rp2040js, which plays the USB host, the transceivers' detect lines and the far end's modem lines. It checks:
    • enumeration;
    • DCE detection and the chip-enable pins;
    • the DCD/DSR/CTS mapping;
    • data both ways through the UART;
    • SERIAL_STATE notifications;
    • GET_INFO and GET_INPUTS;
    • forcing DTE mode, and its line mapping;
    • getting through a settings save without a hang or watchdog reset;
    • learning the baud rate: "AT\r" driven onto GP1 at 9600 baud (1% fast) and later "ATI3\r" at 115200 (2% slow) reach the host, the UART switches, and GET_INFO reports it. rp2040js runs PIO on its own timer, so the test steps PIO once per CPU cycle to keep its timing true.

Not yet verified

  • On real hardware: nothing has run on one yet. The first board or bench rig is the real test.
  • Flash write: the emulator doesn't model flash programming, so a saved mode surviving a power cycle is unchecked.
  • UART pin routing: the emulator doesn't model the UART-to-GPIO routing either, so the swap between chip A's and chip B's UART pins is unchecked.
  • Baud learning on real lines: real transceivers make low and high stretches slightly unequal (slew). The detector allows for 5/16 of a bit, which should cover this at 230 400 baud, but it needs checking on the bench.
  • Real host drivers: Windows' usbser, macOS and Linux cdc_acm, and Android's usb-serial-for-android haven't been tried.
  • Not built yet:
    • the Bluetooth (W) board's BLE/SPP links, SPEC-W.md §4–5;
    • Microsoft OS descriptors, so Windows binds WinUSB to the control interface without Zadig.