Add the site, with a protocol reference made from the headers (#10)

firmware/tools/protocol_ref.py writes docs/protocol-reference.md from
protocol.h, line_proto.h and wb_proto.h: every request, constant and
enum, and each wire struct's fields with their offsets and sizes (checked
against the headers' _Static_asserts). The firmware workflow and the new
Pages workflow fail if the page is out of date.

The site is docs/, the README, the specs and the firmware, hardware,
enclosure and net list pages (extra.site_include), on brand's theme.


Claude-Session: https://claude.ai/code/session_01B82eRSqjp9Pn2KBr9N9LAn

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
NeoandClaude authored and GitHub committed 2026-11-05 18:01:00 -08:00
1 parent 968bf76d28
commit dc1c24e3f4
8 files changed
+698 -1

No files matched your search

+3
View File
@@ -31,6 +31,9 @@ jobs:
- name: Every function is documented
run: python3 firmware/tools/check_docs.py
- name: Protocol reference matches the headers
run: python3 firmware/tools/protocol_ref.py --check
- name: Core tests (host)
run: make -C firmware/test run
+23
View File
@@ -0,0 +1,23 @@
# The hardware's site, https://d-modem.github.io/hardware/: docs/, the README
# and the specs, firmware and hardware pages, with brand's shared theme and
# workflow (brand's docs/sites.md). A pull request builds it, main deploys
# it. It fails if docs/protocol-reference.md isn't what the protocol
# headers make now (python3 firmware/tools/protocol_ref.py).
name: Pages
on:
push:
branches: [main]
pull_request:
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: pages-${{ github.ref }}
cancel-in-progress: true
jobs:
site:
uses: D-Modem/brand/.github/workflows/site.yml@main
with:
prepare: python3 firmware/tools/protocol_ref.py --check
+2
View File
@@ -10,3 +10,5 @@ hardware/kicad/*.kicad_prl
hardware/kicad/*-backups/
hardware/kicad/route/
hardware/kicad/fp-info-cache
.site/
site/
+1
View File
@@ -11,6 +11,7 @@
- [firmware/README.md](../firmware/README.md): build, flash, what it does, Bluetooth, automatic baud rate, tests, the Line and WB sticks
- [firmware/PROTOCOL.md](../firmware/PROTOCOL.md): the control protocol for host apps
- [Protocol reference](protocol-reference.md): every request, constant and wire struct in the protocol headers, made from them (`firmware/tools/protocol_ref.py`)
## Hardware
+380
View File
@@ -0,0 +1,380 @@
<!-- Made by firmware/tools/protocol_ref.py from the protocol headers: change the headers (or the script), not this page. -->
# Protocol reference
What the firmware's protocol headers define, value by value: the requests, constants and wire structs a host app shares with the boards. [PROTOCOL.md](../firmware/PROTOCOL.md) says how to use them. Every struct is packed and little-endian.
## pico-serial control
The DB25 and W boards' control interface: [`protocol.h`](../firmware/src/core/protocol.h).
pico-serial vendor protocol: control requests on the "pico-serial control" interface. Shared by the firmware and any host app; see firmware/PROTOCOL.md.
| name | value | |
|---|---|---|
| `PS_PROTOCOL_VERSION` | `1` | |
USB: vendor interface, class 0xFF, subclass/protocol below; requests are vendor-type, recipient interface, wIndex = that interface's number.
| name | value | |
|---|---|---|
| `PS_ITF_SUBCLASS` | `80` (`0x50`) | 'P' |
| `PS_ITF_PROTOCOL` | `1` | |
### `enum ps_request`
| name | value | |
|---|---|---|
| `PS_REQ_GET_INFO` | `1` | IN ps_info_t |
| `PS_REQ_SET_MODE` | `2` | OUT wValue = ps_mode (saved) |
| `PS_REQ_SET_OUTPUTS` | `3` | OUT wValue = mask &lt;&lt; 8 \| value (PS_LINE_*), not saved |
| `PS_REQ_GET_INPUTS` | `4` | IN ps_inputs_t |
| `PS_REQ_SET_FLOW` | `5` | OUT wValue = ps_flow (saved) |
| `PS_REQ_GET_STATS` | `6` | IN ps_stats_t |
| `PS_REQ_CLEAR_STATS` | `7` | OUT |
| `PS_REQ_SET_BAUD_MODE` | `8` | OUT wValue = ps_baud (saved) |
| `PS_REQ_AUTOBAUD_RESTART` | `9` | OUT: learn the baud rate again now |
| | | *W board only (stall elsewhere):* |
| `PS_REQ_BT_PAIRING` | `10` | OUT wValue = seconds to allow pairing (0 = close the window) |
| `PS_REQ_BT_FORGET` | `11` | OUT: forget every bonded phone |
### `enum ps_mode`
| name | value | |
|---|---|---|
| `PS_MODE_AUTO` | `0` | |
| `PS_MODE_DCE` | `1` | |
| `PS_MODE_DTE` | `2` | |
### `enum ps_role`
| name | value | |
|---|---|---|
| `PS_ROLE_NONE` | `0` | |
| `PS_ROLE_DCE` | `1` | |
| `PS_ROLE_DTE` | `2` | |
### `enum ps_flow`
| name | value | |
|---|---|---|
| `PS_FLOW_AUTO` | `0` | |
| `PS_FLOW_ON` | `1` | |
| `PS_FLOW_OFF` | `2` | |
### `enum ps_baud`
Where the UART's baud rate comes from. AUTO: learned from the far end as DCE (a computer typing at its own speed), the host's as DTE (a modem follows the speed it's spoken to at). HOST: always the host's (SET_LINE_CODING). LEARN: always learned. Data bits, parity and stop bits are always the host's.
| name | value | |
|---|---|---|
| `PS_BAUD_AUTO` | `0` | |
| `PS_BAUD_HOST` | `1` | |
| `PS_BAUD_LEARN` | `2` | |
Modem lines, 1 = asserted (ON). Which are outputs and which inputs depends on the role: as DCE the board drives DCD DSR RI CTS and reads DTR RTS; as DTE it drives DTR RTS and reads DCD DSR RI CTS.
| name | value | |
|---|---|---|
| `PS_LINE_DTR` | `1` | |
| `PS_LINE_RTS` | `2` | |
| `PS_LINE_DCD` | `4` | |
| `PS_LINE_DSR` | `8` | |
| `PS_LINE_RI` | `16` (`0x10`) | |
| `PS_LINE_CTS` | `32` (`0x20`) | |
ps_info_t.flags.
| name | value | |
|---|---|---|
| `PS_INFO_CODING_ADJUSTED` | `1` | last SET_LINE_CODING asked for something the UART can't do |
| `PS_INFO_BAUD_OVER_RATING` | `2` | baud above the transceivers' 250 kbit/s rating |
| `PS_INFO_FLOW_ACTIVE` | `4` | hardware CTS flow control currently obeyed |
| `PS_INFO_BAUD_LOCKED` | `8` | the baud rate was learned from the far end |
| `PS_INFO_BAUD_LEARNING` | `16` (`0x10`) | learning it now (received data waits until it's known) |
### `ps_info_t`
14 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `protocol` | PS_PROTOCOL_VERSION |
| 1 | 1 | `uint8_t` | `fw_major` | |
| 2 | 1 | `uint8_t` | `fw_minor` | |
| 3 | 1 | `uint8_t` | `fw_patch` | |
| 4 | 1 | `uint8_t` | `board` | 0 = DB25 hood board, 1 = W (wireless), 2 = Pico bench rig |
| 5 | 1 | `uint8_t` | `mode` | ps_mode setting |
| 6 | 1 | `uint8_t` | `role` | ps_role now |
| 7 | 1 | `uint8_t` | `flow` | ps_flow setting |
| 8 | 1 | `uint8_t` | `flags` | PS_INFO_* |
| 9 | 4 | `uint32_t` | `baud` | UART baud now (learned, or the host's) |
| 13 | 1 | `uint8_t` | `baud_mode` | ps_baud setting |
ps_inputs_t.detect.
| name | value | |
|---|---|---|
| `PS_DETECT_A_STATUS` | `1` | chip A sees valid RS-232 levels on pins 2/4/20 (a computer) |
| `PS_DETECT_B_INVALID` | `2` | chip B sees valid RS-232 levels on pins 3/5/6/8/22 (a modem) |
| `PS_DETECT_A_R1OUTB` | `4` | chip A's always-on receiver on pin 2 (non-inverting) |
### `ps_inputs_t`
5 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `role` | |
| 1 | 1 | `uint8_t` | `inputs` | PS_LINE_* read from the far end in this role |
| 2 | 1 | `uint8_t` | `outputs` | PS_LINE_* the board is driving now |
| 3 | 1 | `uint8_t` | `detect` | PS_DETECT_* |
| 4 | 1 | `uint8_t` | `host` | PS_LINE_DTR/RTS as the host last set them |
### `ps_stats_t`
32 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 4 | `uint32_t` | `to_rs232` | bytes sent out of the DB25 |
| 4 | 4 | `uint32_t` | `from_rs232` | bytes received on the DB25 |
| 8 | 4 | `uint32_t` | `framing` | |
| 12 | 4 | `uint32_t` | `parity` | |
| 16 | 4 | `uint32_t` | `overrun` | |
| 20 | 4 | `uint32_t` | `breaks` | |
| 24 | 4 | `uint32_t` | `dropped` | received bytes lost because the host wasn't reading |
| 28 | 4 | `uint32_t` | `role_changes` | |
## Line stick
pico-serial Line, the phone-line stick: [`line_proto.h`](../firmware/src/core/line_proto.h).
The phone-line stick's USB protocol (SPEC-L.md §5, firmware/PROTOCOL.md "Line stick"): what the host's driver (D-Modem's dmodemd) and the firmware share. Little-endian, packed.
| name | value | |
|---|---|---|
| `LP_VERSION` | `1` | |
Interface 0: vendor class, this subclass and protocol. Alternate setting 0 has no endpoints (no bandwidth); alternate setting 1 has the three below, and selecting it starts the samples both ways.
| name | value | |
|---|---|---|
| `LP_ITF_SUBCLASS` | `76` (`0x4C`) | 'L' |
| `LP_ITF_PROTOCOL` | `1` | |
| `LP_EP_OUT` | `1` | isochronous, 1 ms: the modem's samples to the line |
| `LP_EP_IN` | `130` (`0x82`) | isochronous, 1 ms: the line's samples |
| `LP_EP_EVENT` | `131` (`0x83`) | interrupt, 1 ms: lp_event_t when something changes |
| `LP_MAX_SAMPLES` | `20` (`0x14`) | per packet: 16 kHz (16 a ms) with room to catch up |
| `LP_PACKET_BYTES` | `(2 * LP_MAX_SAMPLES)` | |
### `LP_REQ_*`
Vendor requests (recipient: the interface or the device, wIndex 0)
| name | value | |
|---|---|---|
| `LP_REQ_GET_INFO` | `1` | IN: lp_info_t |
| `LP_REQ_GET_STATUS` | `2` | IN: lp_status_t |
| `LP_REQ_SET_RATE` | `16` (`0x10`) | OUT: wValue = sample rate in Hz (lp_rate_code) |
| `LP_REQ_SET_HOOK` | `17` (`0x11`) | OUT: wValue 1 = off hook, 0 = on hook |
| `LP_REQ_HOOK_FLASH` | `18` (`0x12`) | OUT: wValue = ms on hook (1..2000), then off hook again |
| `LP_REQ_SET_MONITOR` | `19` (`0x13`) | OUT: wValue 1 = hear the line on hook (caller ID), 0 = not |
| `LP_REQ_DAA_READ` | `32` (`0x20`) | IN: wValue = register (0..127); 1 byte |
| `LP_REQ_DAA_WRITE` | `33` (`0x21`) | OUT: wValue = register &lt;&lt; 8 \| value |
| `LP_REQ_RESET` | `48` (`0x30`) | OUT: the DAA reset and brought up again, on hook |
| `LP_REQ_REBOOT_UF2` | `49` (`0x31`) | OUT: into the RP2040's UF2 loader |
| `LP_REQ_MS_OS_20` | `77` (`0x4D`) | IN, wIndex 7: the Microsoft OS 2.0 descriptor set (Windows) |
State bits (lp_info_t.state, lp_status_t.state, lp_event_t.state)
| name | value | |
|---|---|---|
| `LP_ST_LINK` | `1` | the Si3056 and the line side talk (FDT) |
| `LP_ST_OFF_HOOK` | `2` | |
| `LP_ST_MONITOR` | `4` | on-hook line monitor on |
| `LP_ST_STREAMING` | `8` | alternate setting 1 |
| `LP_ST_RINGING` | `16` (`0x10`) | the ring detector is active now |
| `LP_ST_FLASH` | `32` (`0x20`) | a hook flash is in progress |
### `lp_info_t`
12 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `version` | LP_VERSION |
| 1 | 1 | `uint8_t` | `fw_major` | |
| 2 | 1 | `uint8_t` | `fw_minor` | |
| 3 | 1 | `uint8_t` | `fw_patch` | |
| 4 | 2 | `uint16_t` | `rate` | samples a second |
| 6 | 1 | `uint8_t` | `max_samples` | LP_MAX_SAMPLES |
| 7 | 1 | `uint8_t` | `state` | LP_ST_* |
| 8 | 1 | `uint8_t` | `reg11` | Si3056 register 11: line-side ID (bits 7:4) and system-side revision |
| 9 | 1 | `uint8_t` | `reg13` | register 13: line-side revision (bits 5:2) |
| 10 | 2 | `uint8_t[2]` | `reserved` | |
### `lp_status_t`
20 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `reg12` | FDT (bit 6), loop current LCS[4:0] |
| 1 | 1 | `uint8_t` | `reg28` | loop current LCS2 |
| 2 | 1 | `uint8_t` | `reg29` | line voltage LVS |
| 3 | 1 | `uint8_t` | `reg5` | DAA control 1: RDTN, RDTP, ONHM, RDT, OHE, OH |
| 4 | 1 | `uint8_t` | `int_src` | register 4 bits seen since the last event or status |
| 5 | 1 | `uint8_t` | `state` | LP_ST_* |
| 6 | 2 | `uint16_t` | `underruns` | OUT samples the line wanted and the host hadn't sent |
| 8 | 2 | `uint16_t` | `overruns` | samples dropped: OUT too far ahead, or IN not taken |
| 10 | 4 | `uint32_t` | `samples_in` | the line's samples handed to the host |
| 14 | 4 | `uint32_t` | `samples_out` | the host's samples played |
| 18 | 1 | `uint8_t` | `ring_edges` | ring detector changes, mod 256 |
| 19 | 1 | `uint8_t` | `reserved` | |
### `lp_event_t`
8 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `state` | LP_ST_* |
| 1 | 1 | `uint8_t` | `ring_edges` | ring detector changes, mod 256: each one is a pulse for the modem's ring() |
| 2 | 1 | `uint8_t` | `int_src` | register 4 bits since the last event (ring, overload, polarity...) |
| 3 | 1 | `uint8_t` | `seq` | +1 each event |
| 4 | 2 | `uint16_t` | `underruns` | |
| 6 | 2 | `uint16_t` | `overruns` | |
## WB stick
pico-serial WB, the wideband stick: [`wb_proto.h`](../firmware/src/core/wb_proto.h).
The wideband stick's USB protocol (SPEC-WB.md §5, firmware/PROTOCOL.md "WB stick"): what the host's driver (D-Modem's dmodemd) and the firmware share. Shaped as the Line stick's (line_proto.h): the same interface layout and endpoints, other requests. Little-endian, packed.
| name | value | |
|---|---|---|
| `WP_VERSION` | `1` | |
| `WP_BOARD` | `4` | wp_info_t.board: the WB stick (board_config.h PS_BOARD_ID) |
Interface 0: vendor class, this subclass and protocol. Alternate setting 0 has no endpoints (no bandwidth); alternate setting 1 has the three below, and selecting it starts the samples both ways.
| name | value | |
|---|---|---|
| `WP_ITF_SUBCLASS` | `87` (`0x57`) | 'W' |
| `WP_ITF_PROTOCOL` | `1` | |
| `WP_EP_OUT` | `1` | isochronous, 1 ms: the modem's samples to the line |
| `WP_EP_IN` | `130` (`0x82`) | isochronous, 1 ms: the line's samples |
| `WP_EP_EVENT` | `131` (`0x83`) | interrupt, 4 ms: wp_event_t when something changes |
| `WP_MAX_SAMPLES` | `240` (`0xF0`) | per packet: 192 kHz (192 a ms) with room to catch up |
| `WP_PACKET_BYTES` | `(2 * WP_MAX_SAMPLES)` | |
### `WP_REQ_*`
Vendor requests (recipient: the interface or the device; wIndex's low byte the interface, 0)
| name | value | |
|---|---|---|
| `WP_REQ_GET_INFO` | `1` | IN: wp_info_t |
| `WP_REQ_GET_STATUS` | `2` | IN: wp_status_t (the peaks and the codec's flags since the last one) |
| `WP_REQ_SET_RATE` | `16` (`0x10`) | OUT: wValue = the sample rate in kHz: 48, 96 or 192 |
| `WP_REQ_SET_CARRIER` | `17` (`0x11`) | OUT: wValue = WP_CARRIER_* (the LINK LED) |
| `WP_REQ_SET_TX_GAIN` | `18` (`0x12`) | OUT: wValue = int16, 0.5 dB steps, -127..48: the DAC's volume |
| `WP_REQ_SET_RX_GAIN` | `19` (`0x13`) | OUT: wValue = 0..95, 0.5 dB steps: the hybrid's MicPGA |
| `WP_REQ_SET_INPUT` | `20` (`0x14`) | OUT: wValue = WP_INPUT_*: which ADC channel the host gets |
| `WP_REQ_SET_LOOPBACK` | `21` (`0x15`) | OUT: wValue = WP_LOOP_* |
| `WP_REQ_CODEC_READ` | `32` (`0x20`) | IN: wValue = register (1..127), wIndex = page &lt;&lt; 8; 1 byte |
| `WP_REQ_CODEC_WRITE` | `33` (`0x21`) | OUT: wValue = register &lt;&lt; 8 \| value, wIndex = page &lt;&lt; 8 |
| `WP_REQ_RESET` | `48` (`0x30`) | OUT: the codec reset and brought up again (rate and settings kept) |
| `WP_REQ_REBOOT_UF2` | `49` (`0x31`) | OUT: into the RP2040's UF2 loader |
| `WP_REQ_MS_OS_20` | `77` (`0x4D`) | IN, wIndex 7: the Microsoft OS 2.0 descriptor set (Windows) |
### `WP_CARRIER_*`
AUTO (the default): lit while streaming.
| name | value | |
|---|---|---|
| `WP_CARRIER_OFF` | `0` | |
| `WP_CARRIER_ON` | `1` | |
| `WP_CARRIER_AUTO` | `2` | |
### `WP_INPUT_*`
Left ADC (default), right ADC.
| name | value | |
|---|---|---|
| `WP_INPUT_HYBRID` | `0` | |
| `WP_INPUT_LINE` | `1` | |
### `WP_LOOP_*`
| name | value | |
|---|---|---|
| `WP_LOOP_OFF` | `0` | |
| `WP_LOOP_FIRMWARE` | `1` | |
| `WP_LOOP_CODEC` | `2` | |
State bits (wp_info_t.state, wp_status_t.state, wp_event_t.state)
| name | value | |
|---|---|---|
| `WP_ST_CODEC` | `1` | the codec came up and its clocks run |
| `WP_ST_CARRIER` | `2` | the LINK LED is lit |
| `WP_ST_LOOPBACK` | `4` | a loopback is on |
| `WP_ST_STREAMING` | `8` | alternate setting 1 |
### `wp_info_t`
16 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `version` | WP_VERSION |
| 1 | 1 | `uint8_t` | `fw_major` | |
| 2 | 1 | `uint8_t` | `fw_minor` | |
| 3 | 1 | `uint8_t` | `fw_patch` | |
| 4 | 4 | `uint32_t` | `rate` | samples a second |
| 8 | 2 | `uint16_t` | `max_samples` | WP_MAX_SAMPLES |
| 10 | 1 | `uint8_t` | `state` | WP_ST_* |
| 11 | 1 | `uint8_t` | `board` | WP_BOARD |
| 12 | 1 | `uint8_t` | `input` | WP_INPUT_* |
| 13 | 1 | `uint8_t` | `loopback` | WP_LOOP_* |
| 14 | 1 | `int8_t` | `tx_gain` | the DAC's volume, 0.5 dB steps |
| 15 | 1 | `uint8_t` | `rx_gain` | the hybrid MicPGA's gain, 0.5 dB steps |
### `wp_status_t`
28 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 2 | `uint16_t` | `peak_hybrid` | the largest \|sample\| since the last status: left ADC (32768 = full scale) |
| 2 | 2 | `uint16_t` | `peak_line` | ... right ADC |
| 4 | 2 | `uint16_t` | `peak_tx` | ... the host's samples played |
| 6 | 1 | `uint8_t` | `ovf` | codec page 0 register 42 bits since the last status: DAC/ADC overflows |
| 7 | 1 | `uint8_t` | `oc` | register 44's over-current bits (HPL 0x80, HPR 0x40) since the last status |
| 8 | 1 | `uint8_t` | `adc_flags` | register 36 now: PGAs at gain, ADCs powered |
| 9 | 1 | `uint8_t` | `dac_flags` | register 37 now: DACs and drivers powered |
| 10 | 1 | `uint8_t` | `state` | WP_ST_* |
| 11 | 1 | `uint8_t` | `reserved` | |
| 12 | 2 | `uint16_t` | `underruns` | OUT samples the line wanted and the host hadn't sent |
| 14 | 2 | `uint16_t` | `overruns` | samples dropped: OUT too far ahead, or IN not taken |
| 16 | 4 | `uint32_t` | `samples_in` | the line's samples handed to the host |
| 20 | 4 | `uint32_t` | `samples_out` | the host's samples played |
| 24 | 2 | `uint16_t` | `resyncs` | the I2S slave lost its place and started again |
| 26 | 2 | `uint16_t` | `reserved2` | |
### `wp_event_t`
8 bytes.
| offset | size | type | field | |
|---|---|---|---|---|
| 0 | 1 | `uint8_t` | `state` | WP_ST_* |
| 1 | 1 | `uint8_t` | `seq` | +1 each event |
| 2 | 1 | `uint8_t` | `ovf` | register 42 bits since the last event |
| 3 | 1 | `uint8_t` | `oc` | register 44's over-current bits since the last event |
| 4 | 2 | `uint16_t` | `underruns` | |
| 6 | 2 | `uint16_t` | `overruns` | |
+1 -1
View File
@@ -2,7 +2,7 @@
For host apps (the D-Modem app, a desktop tool) that want more than a plain
serial port gives: the board's role, every modem line, and settings. The
C definitions are in [`src/core/protocol.h`](src/core/protocol.h).
C definitions are in [`src/core/protocol.h`](src/core/protocol.h), and every value of them in the [protocol reference](../docs/protocol-reference.md).
## Finding it
+245
View File
@@ -0,0 +1,245 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright (C) 2026 D-Modem Authors
"""docs/protocol-reference.md from the protocol headers the firmware and the
host apps share: every constant, request, enum and wire struct (with each
field's offset and size), and the comments beside them.
python3 firmware/tools/protocol_ref.py # writes the page
python3 firmware/tools/protocol_ref.py --check # exit 1 if it is out of date
firmware/PROTOCOL.md says how the protocols are used; this page is what
the headers define, so the two can't disagree on a value.
"""
import os
import re
import sys
ROOT = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", ".."))
OUT = os.path.join(ROOT, "docs", "protocol-reference.md")
HEADERS = [
("pico-serial control", "firmware/src/core/protocol.h", "the DB25 and W boards' control interface"),
("Line stick", "firmware/src/core/line_proto.h", "pico-serial Line, the phone-line stick"),
("WB stick", "firmware/src/core/wb_proto.h", "pico-serial WB, the wideband stick"),
]
SIZES = {"uint8_t": 1, "int8_t": 1, "uint16_t": 2, "int16_t": 2, "uint32_t": 4, "int32_t": 4}
def cell(s):
s = s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
return " ".join(s.replace("|", "\\|").split())
def split_comment(line):
"""(code, trailing comment) of a line."""
m = re.match(r"(.*?)\s*//\s?(.*)$", line)
return (m.group(1), m.group(2).strip()) if m else (line, "")
def int_value(v):
v = v.strip()
if re.fullmatch(r"0[xX][0-9a-fA-F]+|\d+", v):
return int(v, 0)
return None
def parse(text):
"""The header's blocks in order: ("intro", text), ("defines", note,
rows), ("enum", name, note, rows), ("struct", name, note, rows, size)."""
lines = text.splitlines()
blocks, note, intro, i = [], [], None, 0
# the licence comment, then the header's own comment
while i < len(lines) and not lines[i].startswith("//"):
i += 1
while i < len(lines) and lines[i].startswith("//"):
note.append(lines[i][2:].strip())
i += 1
blocks.append(("intro", " ".join(note)))
note = []
while i < len(lines):
line = lines[i]
s = line.strip()
if s.startswith("//"):
note.append(s[2:].strip())
i += 1
continue
if not s or s.startswith(("#pragma", "#include")):
if not s:
note = [] if not blocks or blocks[-1][0] != "defines" else note
i += 1
continue
if s.startswith("#define"):
rows = []
text_note = " ".join(note)
note = []
while i < len(lines) and lines[i].startswith("#define"):
code, com = split_comment(lines[i])
m = re.match(r"#define\s+(\w+)\s+(.*)", code)
rows.append((m.group(1), m.group(2).strip(), com))
i += 1
blocks.append(("defines", text_note, rows))
continue
m = re.match(r"enum\s*(\w*)\s*\{(.*)", s)
if m:
name, rest = m.group(1), m.group(2)
text_note = " ".join(note)
note = []
body = []
if "}" in rest:
code, com = split_comment(rest)
inner = code.split("}")[0]
for part in [p for p in inner.split(",") if p.strip()]:
body.append((part.strip(), ""))
if com:
text_note = (text_note + " " + com).strip()
i += 1
else:
i += 1
while not lines[i].strip().startswith("}"):
t = lines[i].strip()
if t.startswith("//"):
body.append((None, t[2:].strip()))
elif t:
code, com = split_comment(t)
body.append((code.rstrip(","), com))
i += 1
i += 1
rows, nxt = [], 0
for code, com in body:
if code is None:
rows.append((None, None, com))
continue
n, _, v = code.partition("=")
val = int_value(v) if v else nxt
rows.append((n.strip(), val if val is not None else v.strip(), com))
nxt = (val if isinstance(val, int) else 0) + 1
blocks.append(("enum", name, text_note, rows))
continue
if s.startswith("typedef struct"):
text_note = " ".join(note)
note = []
i += 1
rows, off = [], 0
while not lines[i].strip().startswith("}"):
code, com = split_comment(lines[i].strip())
fm = re.match(r"(\w+)\s+(.*);", code)
if fm:
typ = fm.group(1)
for f in fm.group(2).split(","):
f = f.strip()
am = re.match(r"(\w+)\[(\d+)\]", f)
count = int(am.group(2)) if am else 1
size = SIZES[typ] * count
rows.append((off, size, typ + ("[%d]" % count if am else ""),
am.group(1) if am else f, com))
off += size
i += 1
name = re.match(r"\}\s*(\w+)", lines[i].strip()).group(1)
blocks.append(("struct", name, text_note, rows, off))
i += 1
continue
if s.startswith("_Static_assert"):
m = re.match(r"_Static_assert\(sizeof\((\w+)\)\s*==\s*(\d+)", s)
if m:
for b in blocks:
if b[0] == "struct" and b[1] == m.group(1) and b[4] != int(m.group(2)):
sys.exit("protocol_ref: %s is %d bytes here, %s bytes in the header"
% (b[1], b[4], m.group(2)))
i += 1
continue
# anything else (an inline function): to its end
note = []
if "{" in s and "}" not in s:
depth = 0
while True:
depth += lines[i].count("{") - lines[i].count("}")
i += 1
if depth <= 0:
break
else:
i += 1
return blocks
def show(v):
if isinstance(v, int):
return "`%d`" % v if v < 16 else "`%d` (`0x%02X`)" % (v, v)
return "`%s`" % v
def sentence(s):
s = s.strip()
if not s:
return ""
first = s.split()[0]
if "_" not in first and "." not in first.rstrip(".") and not first.startswith("pico-"):
s = s[0].upper() + s[1:]
return s if s.endswith((".", ")", ":")) else s + "."
def page():
b = ["<!-- Made by firmware/tools/protocol_ref.py from the protocol headers: "
"change the headers (or the script), not this page. -->\n\n",
"# Protocol reference\n\n",
"What the firmware's protocol headers define, value by value: the "
"requests, constants and wire structs a host app shares with the "
"boards. [PROTOCOL.md](../firmware/PROTOCOL.md) says how to use them. "
"Every struct is packed and little-endian.\n\n"]
for title, rel, what in HEADERS:
with open(os.path.join(ROOT, rel), encoding="utf-8") as f:
blocks = parse(f.read())
b.append("## %s\n\n" % title)
b.append("%s: [`%s`](../%s).\n\n" % (sentence(what)[:-1], os.path.basename(rel), rel))
for blk in blocks:
kind = blk[0]
if kind == "intro":
if blk[1]:
b.append(sentence(blk[1]) + "\n\n")
elif kind == "defines":
if blk[1]:
b.append(sentence(blk[1]) + "\n\n")
b.append("| name | value | |\n|---|---|---|\n")
for n, v, com in blk[2]:
iv = int_value(v)
b.append("| `%s` | %s | %s |\n" % (n, show(iv) if iv is not None else "`%s`" % cell(v), cell(com)))
b.append("\n")
elif kind == "enum":
_, name, text_note, rows = blk
b.append("### %s\n\n" % ("`enum %s`" % name if name else
"`%s_*`" % os.path.commonprefix([r[0] for r in rows if r[0]]).rstrip("_")))
if text_note:
b.append(sentence(text_note) + "\n\n")
b.append("| name | value | |\n|---|---|---|\n")
for n, v, com in rows:
if n is None:
b.append("| | | *%s* |\n" % cell(com))
else:
b.append("| `%s` | %s | %s |\n" % (n, show(v), cell(com)))
b.append("\n")
else:
_, name, text_note, rows, size = blk
b.append("### `%s`\n\n%d bytes.%s\n\n" % (name, size, " " + sentence(text_note) if text_note else ""))
b.append("| offset | size | type | field | |\n|---|---|---|---|---|\n")
for off, sz, typ, f, com in rows:
b.append("| %d | %d | `%s` | `%s` | %s |\n" % (off, sz, typ, f, cell(com)))
b.append("\n")
return "".join(b).rstrip("\n") + "\n"
def main():
text = page()
old = open(OUT, encoding="utf-8").read() if os.path.exists(OUT) else None
if "--check" in sys.argv[1:]:
if old != text:
print("protocol_ref: docs/protocol-reference.md is out of date: "
"run python3 firmware/tools/protocol_ref.py", file=sys.stderr)
return 1
return 0
if old != text:
with open(OUT, "w", encoding="utf-8") as f:
f.write(text)
return 0
if __name__ == "__main__":
sys.exit(main())
+43
View File
@@ -0,0 +1,43 @@
INHERIT: .site/mkdocs-base.yml
# The hardware's site, https://d-modem.github.io/hardware/: docs/, the README
# and the pages site_include lists, with brand's shared theme and workflow
# (brand's docs/sites.md). docs/protocol-reference.md is made from the
# protocol headers (python3 firmware/tools/protocol_ref.py).
site_name: D-Modem hardware
site_description: pico-serial, the D-Modem suite's RS-232 adapters and phone-line sticks
site_url: https://d-modem.github.io/hardware/
repo_url: https://github.com/D-Modem/hardware
repo_name: D-Modem/hardware
docs_dir: .site/docs
extra:
repo: hardware
site_include:
- SPEC*.md
- LICENSING.md
- firmware/*.md
- hardware/README.md
- hardware/enclosure/**/*.md
- hardware/netlist/*.md
nav:
- Home: index.md
- Specs:
- pico-serial: SPEC.md
- pico-serial W: SPEC-W.md
- pico-serial Line: SPEC-L.md
- pico-serial WB: SPEC-WB.md
- Firmware:
- Firmware: firmware/README.md
- Control protocol: firmware/PROTOCOL.md
- Protocol reference: protocol-reference.md
- Hardware:
- Board: hardware/README.md
- Enclosures: hardware/enclosure/README.md
- Placement (DB25): hardware/enclosure/out/placement.md
- Placement (Line): hardware/enclosure/out-line/placement.md
- Placement (WB): hardware/enclosure/out-wb/placement.md
- Net list (DB25): hardware/netlist/NETLIST.md
- Net list (W): hardware/netlist/NETLIST-w.md
- Net list (Line): hardware/netlist/NETLIST-line.md
- Net list (WB): hardware/netlist/NETLIST-wb.md
- Licensing: LICENSING.md
- Documentation index: README.md