From afc906cf2f49e168dfcf6055449211d35b8b2f27 Mon Sep 17 00:00:00 2001 From: Jean-Pierre Martineau Date: Tue, 6 Oct 2026 00:19:58 -0400 Subject: [PATCH] On a uConsole, the AIO's radio is named the uConsole AIO The radio on spidev1.0 was listed as a bare "SPI radio" and reported "MeshTerm SPI node", because spidev1.0 alone could be any Pi. A uConsole now proves itself by its own screen: ClockworkPi's uConsole overlay declares the panel `cw,cwu50`, which no other machine carries (the DevTerm's is cw,cwd686; the CM's model string and the AXP228 and backlight are shared). Seen on JP's CM5 uConsole. `BuiltinRadio` takes a device-tree `compatible` beside its marker path, read once a run from /sys/firmware/devicetree/base. The named AIO comes first in BUILTIN_RADIOS; without the panel, the unnamed entry still lists spidev1.0 on the AIO's wiring as before. Co-Authored-By: Claude Opus 5.5 (1M context) --- docs/devices/uconsole.md | 10 +++--- docs/guide/configuration.md | 3 +- meshterm/core/spiradio.py | 64 +++++++++++++++++++++++++++++++------ tests/test_spiradio.py | 38 ++++++++++++++++++++++ 4 files changed, 100 insertions(+), 15 deletions(-) diff --git a/docs/devices/uconsole.md b/docs/devices/uconsole.md index 02d5e0c..ce9d352 100644 --- a/docs/devices/uconsole.md +++ b/docs/devices/uconsole.md @@ -184,12 +184,14 @@ meshterm --spi ``` `--spi` finds the AIO's radio at `/dev/spidev1.0` and starts a node on it. The startup -device picker also lists it — a row reading **SPI radio (/dev/spidev1.0)** with a 📍 icon — -whenever that device node exists, so `meshterm` with no flags gets you there too. +device picker also lists it — a row reading **uConsole AIO (/dev/spidev1.0)** with a 📍 +icon — whenever that device node exists, so `meshterm` with no flags gets you there too. +MeshTerm knows it's on a uConsole from the uConsole's own screen, which ClockworkPi's +uConsole overlay declares. Without that overlay, the row reads **SPI radio** instead. You're connected when **Device info** or the dashboard shows a node name instead of a -connection error. MeshTerm reports the device model as **MeshTerm SPI node**, and the -header's connection label reads **SPI**. +connection error. MeshTerm reports the device model as **uConsole AIO** (or **MeshTerm SPI +node** without the overlay), and the header's connection label reads **SPI**. **What happens underneath.** MeshTerm starts the node when it connects and ends it when you disconnect or quit — including a crash: the node exits with MeshTerm, and the kernel diff --git a/docs/guide/configuration.md b/docs/guide/configuration.md index c27305a..1ae9c9d 100644 --- a/docs/guide/configuration.md +++ b/docs/guide/configuration.md @@ -80,7 +80,8 @@ It needs Linux and the radio library; [the uConsole manual](../devices/uconsole. shows how to install that. **Two boards need no configuration.** An AIO v1's wiring is MeshTerm's default, so -whenever `/dev/spidev1.0` exists the device screen lists it as **SPI radio**. M5Stack's +whenever `/dev/spidev1.0` exists the device screen lists it: as **uConsole AIO** on a +uConsole, and as **SPI radio** on any other machine. M5Stack's Cap LoRa-1262 on a Cardputer Zero ships with MeshTerm too, and is listed as **Cap LoRa-1262** on a Cardputer Zero (see [the Cardputer Zero page](../devices/cardputer-zero.md#the-cap-lora-1262)). Either way, `meshterm --spi` reaches it. Any other board is added with a profile that diff --git a/meshterm/core/spiradio.py b/meshterm/core/spiradio.py index f4841ae..e18d702 100644 --- a/meshterm/core/spiradio.py +++ b/meshterm/core/spiradio.py @@ -33,6 +33,7 @@ with it its place in everyone else's contact list. from __future__ import annotations import asyncio +import functools import json import logging import os @@ -229,30 +230,72 @@ CARDPUTER_ZERO_CAP = SpiWiring( ) +#: Where Linux exposes the device tree the machine booted with. +DEVICE_TREE = Path("/sys/firmware/devicetree/base") + + +@functools.cache +def device_tree_compatibles(root: Path = DEVICE_TREE) -> frozenset[str]: + """Every ``compatible`` string in the booted device tree; empty where there is none. + + Read once a run: the tree is fixed at boot, and the device screen asks on every pass. + """ + found: set[str] = set() + try: + for path in root.rglob("compatible"): + try: + found.update( + v.decode("ascii", "replace") for v in path.read_bytes().split(b"\0") if v + ) + except OSError: + continue + except OSError: + pass + return frozenset(found) + + @dataclass(frozen=True) class BuiltinRadio: """A radio whose wiring ships with MeshTerm, listed with no profile where its board is. Attributes: - name: What the device screen calls it; empty for the plain "SPI radio". + name: What the device screen calls it, and what its node reports as its model; + empty for the plain "SPI radio". wiring: How it is wired. marker: A path only its board has, for a radio whose ``/dev/spidev*`` node alone - would claim every Raspberry Pi with SPI switched on. Empty when the node is - telling enough on its own (the AIO's ``spidev1.0``). + would claim every Raspberry Pi with SPI switched on. Empty for none. + compatible: A device-tree ``compatible`` only its board's tree carries, for a board + that marks itself there rather than with a path of its own. Empty for none. """ name: str wiring: SpiWiring marker: str = "" + compatible: str = "" def present(self) -> bool: - """Whether this machine is its board: the SPI node exists, and the marker if any.""" - return spi_present(self.wiring) and (not self.marker or os.path.exists(self.marker)) + """Whether this machine is its board: the SPI node exists, and every marker it has.""" + return ( + spi_present(self.wiring) + and (not self.marker or os.path.exists(self.marker)) + and (not self.compatible or self.compatible in device_tree_compatibles()) + ) -#: The radios MeshTerm knows the wiring of, in the order they are listed. A profile on the -#: same SPI node wins over any of them. +#: The uConsole's own 5-inch screen, as ClockworkPi's uConsole overlay declares it. A +#: uConsole runs a stock Compute Module, whose model string says only that, and its power +#: chip and backlight are the DevTerm's too; the panel is the one part no other machine +#: has (the DevTerm's is ``cw,cwd686``). Seen on JP's uConsole, a CM5, at +#: ``/axi/pcie@1000120000/rp1/dsi@128000/panel@0``: matched by compatible, not by path, +#: since the path moves with the Compute Module. +UCONSOLE_PANEL = "cw,cwu50" + +#: The radios MeshTerm knows the wiring of, in the order they are listed; the first that is +#: present on a node is the one used there. A profile on the same node wins over any of them. +#: The AIO is named only where the machine is certainly a uConsole, and listed unnamed on +#: any other board with ``spidev1.0``, where its default wiring is still the best guess. BUILTIN_RADIOS = ( + BuiltinRadio("uConsole AIO", SpiWiring(), compatible=UCONSOLE_PANEL), BuiltinRadio("", SpiWiring()), BuiltinRadio("Cap LoRa-1262", CARDPUTER_ZERO_CAP, marker="/sys/class/leds/ext_5v_out"), ) @@ -261,9 +304,10 @@ BUILTIN_RADIOS = ( def board_name(wiring: SpiWiring) -> str: """The name of the shipped board ``wiring``'s radio is on, or ``""`` for none. - What the node reports as its model, so the Cap reads as "Cap LoRa-1262" wherever a - device's model is shown. Matched by the SPI node on this machine's board rather than - by the whole wiring, so a profile that adjusts the Cap's pins is still the Cap. + What the node reports as its model, so the Cap reads as "Cap LoRa-1262" and the AIO + as "uConsole AIO" wherever a device's model is shown. Matched by the SPI node on this + machine's board rather than by the whole wiring, so a profile that adjusts the Cap's + pins is still the Cap. """ for radio in BUILTIN_RADIOS: if radio.name and radio.wiring.spidev == wiring.spidev and radio.present(): diff --git a/tests/test_spiradio.py b/tests/test_spiradio.py index 635b904..60190b5 100644 --- a/tests/test_spiradio.py +++ b/tests/test_spiradio.py @@ -292,6 +292,44 @@ def test_the_cap_reports_itself_by_name(cardputer: Path) -> None: assert spiradio.board_name(spiradio.CARDPUTER_ZERO_CAP) == "" # some other Pi +@pytest.fixture() +def uconsole(monkeypatch: pytest.MonkeyPatch) -> set[str]: + """A machine with the AIO's SPI node; add the uConsole's panel to make it a uConsole.""" + tree: set[str] = {"raspberrypi,5-compute-module", "brcm,bcm2712"} + monkeypatch.setattr(spiradio, "spi_present", lambda w: w.spidev == "/dev/spidev1.0") + monkeypatch.setattr(spiradio, "device_tree_compatibles", lambda: frozenset(tree)) + return tree + + +def test_the_aio_is_named_on_a_uconsole(uconsole: set[str]) -> None: + """The uConsole's own screen in the device tree is what makes the AIO the uConsole AIO.""" + uconsole.add(spiradio.UCONSOLE_PANEL) + radios = spiradio.spi_radios({}) + assert [(d.port, d.name) for d in radios] == [("/dev/spidev1.0", "uConsole AIO")] + assert spiradio.board_name(SpiWiring()) == "uConsole AIO" + + +def test_the_aio_s_node_elsewhere_stays_unnamed(uconsole: set[str]) -> None: + """Any other Pi with spidev1.0 is listed on the AIO's wiring, but never called one.""" + radios = spiradio.spi_radios({}) + assert [(d.port, d.name) for d in radios] == [("/dev/spidev1.0", None)] + assert spiradio.board_name(SpiWiring()) == "" + + +def test_the_device_tree_is_read_whole(tmp_path: Path) -> None: + """Every compatible, at any depth, each of a node's NUL-separated strings.""" + panel = tmp_path / "axi" / "dsi@128000" / "panel@0" + panel.mkdir(parents=True) + (panel / "compatible").write_bytes(b"cw,cwu50\0") + (tmp_path / "compatible").write_bytes(b"raspberrypi,5-compute-module\0brcm,bcm2712\0") + assert spiradio.device_tree_compatibles(tmp_path) == { + "cw,cwu50", + "raspberrypi,5-compute-module", + "brcm,bcm2712", + } + assert spiradio.device_tree_compatibles(tmp_path / "nowhere") == frozenset() + + def test_a_profile_on_the_cap_s_node_wins(cardputer: Path) -> None: """A profile on the same node is the owner's word about it, and replaces the shipped one.""" mine = replace(spiradio.CARDPUTER_ZERO_CAP, use_dio3_tcxo=False)