As DCE the board now works out the computer's baud rate from what it
types, like a Hayes modem, so the host's speed setting doesn't have to
match the terminal's.
- PIO (autobaud.pio) times each low/high stretch on the RX pin while the
UART keeps the pin.
- src/core/autobaud.c decodes those timings at every standard rate
(110-230400) and picks the one they fit: bit length vs the shortest
stretch, edges on bit boundaries, framing/parity, then the slowest rate
whose edges score near the best.
- The characters it learned from ("AT") are decoded and passed on; the
UART switches only once the line is idle.
- Re-learns on 3 framing errors in 2 s, AUTOBAUD_RESTART, or a new peer.
- Baud mode setting (saved, settings byte 7): auto (learn as DCE, host's
as DTE), host, learn. Vendor requests 8 SET_BAUD_MODE and
9 AUTOBAUD_RESTART; GET_INFO grows to 14 bytes with flags
BAUD_LOCKED/BAUD_LEARNING and the baud mode.
- Tests: detector suite (every rate, 5 formats, jitter, rate error,
glitches, mid-character starts), 5 new core tests, and the emulator
test drives "AT\r" at 9600 and "ATI3\r" at 115200 onto GP1 with PIO
stepped per CPU cycle.
- Docs: firmware README, PROTOCOL.md, SPEC.md 5.7 and 6.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013PRsUCpxazyipPn5UGGkF8
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
.uf2to 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 least 2 characters decode, and at most 1 in 8 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.
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"). With only two, 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.
- Emulator test: boots the real
.binin 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.