From e21bc642b116edab7d268c6bc29cb8cc18bbabb4 Mon Sep 17 00:00:00 2001 From: "Rich O." Date: Sun, 1 Nov 2026 11:05:42 -0500 Subject: [PATCH] firmware: enable USB companion link, add bench query tool; verified on XIAO Flashed the UART companion build to the SB-LoRa XIAO. With ENABLE_USB_INTERFACE it also answers over USB, so it can be tested and configured without an adapter. companion_query.py (stdlib only) sends CMD_DEVICE_QUERY and got DEVICE_INFO for MeshCore v1.17.1; that frame is now a palm-client host test. --- docs/progress.md | 6 +- firmware/Makefile | 5 +- firmware/README.md | 14 ++- .../meshcore-xiao-nrf52-uart-companion.patch | 5 +- firmware/tools/companion_query.py | 89 +++++++++++++++++++ palm-client/tests/test_protocol.c | 26 ++++++ 6 files changed, 138 insertions(+), 7 deletions(-) create mode 100755 firmware/tools/companion_query.py diff --git a/docs/progress.md b/docs/progress.md index 7696ef5..1d5ac00 100644 --- a/docs/progress.md +++ b/docs/progress.md @@ -15,8 +15,9 @@ - [ ] In Visor: read/write SC16IS740 scratch register (SPR, 0x07) from Palm side ### Firmware -- [ ] Apply `firmware/meshcore-xiao-nrf52-uart-companion.patch` to MeshCore, build `Xiao_nrf52_companion_radio_uart` -- [ ] Bench-test over 3.3 V USB-TTL on D6/D7 with meshcore-cli +- [x] Apply `firmware/meshcore-xiao-nrf52-uart-companion.patch` to MeshCore, build `Xiao_nrf52_companion_radio_uart` (`firmware/Makefile`, MeshCore a366955c, GCC 14 on Apple Silicon) +- [x] Flashed to the XIAO; answers `CMD_DEVICE_QUERY` over USB with DEVICE_INFO (v1.17.1) +- [ ] Bench-test the D6/D7 UART over a 3.3 V USB-TTL adapter (`firmware/tools/companion_query.py`) - [ ] Plug stack into carrier; `CMD_DEVICE_QUERY` gets a DEVICE_INFO reply ### Palm client @@ -54,3 +55,4 @@ - Starting Palm client development. - macOS USB sync to the Visor works with pilot-link from desrod's repo. Fixed a race where macOS holds the Visor briefly when it appears, so pilot-link's single open attempt failed with exclusive access. The retry patch is saved in `palm-client/tools/`. - First on-device run: VisorMesh v0.1 installed with `make install` and launches on the Visor Neo. +- Firmware builds and runs. Fixed the patch's I2C override (`-U` flags broke Wire under PlatformIO; now `build_unflags`). Pinned native GCC 14 on Apple Silicon. Enabled the USB companion link alongside the UART. The XIAO answers DEVICE_QUERY over USB, and that real frame is now a Palm host test. diff --git a/firmware/Makefile b/firmware/Makefile index 4336587..097e293 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -1,7 +1,8 @@ # MeshCore companion firmware for the SB-LoRa XIAO nRF52840 + Wio-SX1262 stack. # # make clone MeshCore at MESHCORE_REF into meshcore/, apply the patch, build -# make upload build and flash the XIAO over USB (double-tap reset if it isn't found) +# make upload build and flash the XIAO over USB (UPLOAD_PORT=/dev/cu.usbmodemXXXX to pick one; +# double-tap reset for the bootloader if it isn't found) # make monitor serial monitor on the XIAO's USB port # make clean remove the meshcore/ checkout # @@ -37,7 +38,7 @@ build: meshcore/.patched cd meshcore && $(PIO) run -e $(ENV) upload: meshcore/.patched - cd meshcore && $(PIO) run -e $(ENV) -t upload + cd meshcore && $(PIO) run -e $(ENV) -t upload $(if $(UPLOAD_PORT),--upload-port $(UPLOAD_PORT)) monitor: cd meshcore && $(PIO) device monitor -b 115200 diff --git a/firmware/README.md b/firmware/README.md index 1f44dc0..93e2941 100644 --- a/firmware/README.md +++ b/firmware/README.md @@ -2,13 +2,14 @@ ## meshcore-xiao-nrf52-uart-companion.patch -Adds a `Xiao_nrf52_companion_radio_uart` PlatformIO env to MeshCore. It runs the companion protocol on the XIAO nRF52840's hardware UART, D6 (TX, P1.11) / D7 (RX, P1.12), at 115200 8N1, and moves I2C to unused internal pins 16/17. +Adds a `Xiao_nrf52_companion_radio_uart` PlatformIO env to MeshCore. It runs the companion protocol on the XIAO nRF52840's hardware UART, D6 (TX, P1.11) / D7 (RX, P1.12), at 115200 8N1, for the Visor. It also keeps the USB link (`ENABLE_USB_INTERFACE`) for bench testing and setup with meshcore-cli or the MeshCore app. I2C moves to unused internal pins 16/17. ```sh cd firmware make # clone MeshCore @ MESHCORE_REF into meshcore/, apply the patch, build make upload # flash the XIAO over USB make MESHCORE_URL=~/Code_Repo/MeshCore-Stock # clone from a local checkout instead of GitHub +make upload UPLOAD_PORT=/dev/cu.usbmodemXXXX # pick the port explicitly ``` The patch is generated against MeshCore `a366955c` (2026-09-30), pinned as `MESHCORE_REF` in the `Makefile`. `meshcore/` is a git-ignored build checkout. @@ -18,3 +19,14 @@ I2C is moved off D6/D7 with `build_unflags`. A `-U` flag in `build_flags` doesn' On Apple Silicon without Rosetta, the nRF52 platform's default GCC 7.2.1 (x86_64-only) won't run. The Makefile pins PlatformIO's native GCC 14.2.1 for the build checkout instead (`TOOLCHAIN_PIN`). Stock MeshCore nRF52 releases use GCC 7, so treat GCC 14 builds as untested until they've run on hardware. Verified: builds cleanly (RAM 60.8%, flash 54.0%); the compile flags show `PIN_WIRE_SCL=16`, `PIN_WIRE_SDA=17`, `SERIAL_TX=D6`, `SERIAL_RX=D7`. + +Verified on hardware (2026-11-01): flashed to the SB-LoRa XIAO (serial `87F2365FB1415308`). Over USB it answers `CMD_DEVICE_QUERY` with DEVICE_INFO: MeshCore v1.17.1, "Seeed Xiao-nrf52". The D6/D7 UART path still needs a 3.3 V USB-TTL adapter to test. + +## tools/companion_query.py + +Sends a companion frame and prints the reply frames. Uses only the Python standard library. + +```sh +tools/companion_query.py /dev/cu.usbmodemXXXX # CMD_DEVICE_QUERY over the XIAO's USB +tools/companion_query.py /dev/cu.usbserial-XXXX 16 03 # over a USB-TTL adapter on D6/D7 +``` diff --git a/firmware/meshcore-xiao-nrf52-uart-companion.patch b/firmware/meshcore-xiao-nrf52-uart-companion.patch index b163d17..3366483 100644 --- a/firmware/meshcore-xiao-nrf52-uart-companion.patch +++ b/firmware/meshcore-xiao-nrf52-uart-companion.patch @@ -16,10 +16,10 @@ index 89f0e6cb..c4a38045 100644 // platform file system diff --git a/variants/xiao_nrf52/platformio.ini b/variants/xiao_nrf52/platformio.ini -index 1076bd8d..6df3c80d 100644 +index 1076bd8d..e484d00a 100644 --- a/variants/xiao_nrf52/platformio.ini +++ b/variants/xiao_nrf52/platformio.ini -@@ -119,3 +119,30 @@ build_src_filter = ${Xiao_nrf52.build_src_filter} +@@ -119,3 +119,31 @@ build_src_filter = ${Xiao_nrf52.build_src_filter} +<../examples/kiss_modem/*.cpp> lib_deps = ${Xiao_nrf52.lib_deps} @@ -44,6 +44,7 @@ index 1076bd8d..6df3c80d 100644 + -D QSPIFLASH=1 + -D SERIAL_TX=D6 + -D SERIAL_RX=D7 ++ -D ENABLE_USB_INTERFACE=1 ; companion over USB too, for bench testing and setup +build_src_filter = ${Xiao_nrf52.build_src_filter} + +<../examples/companion_radio/*.cpp> + +<../examples/companion_radio/ui-orig/*.cpp> diff --git a/firmware/tools/companion_query.py b/firmware/tools/companion_query.py new file mode 100755 index 0000000..e332ab6 --- /dev/null +++ b/firmware/tools/companion_query.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +"""Bench tool: send companion-protocol frames to a MeshCore radio and dump the replies. + +Uses only the standard library (termios), so it runs anywhere with Python 3. + + companion_query.py /dev/cu.usbmodemXXXX # CMD_DEVICE_QUERY + companion_query.py /dev/cu.usbmodemXXXX 16 03 # same, payload given as hex bytes + companion_query.py /dev/cu.usbserial-XXXX --baud 115200 # via a USB-TTL adapter on D6/D7 + +Frames: app -> radio '<' len_lo len_hi payload; radio -> app '>' len_lo len_hi payload. +""" +import argparse +import os +import select +import sys +import termios +import time + +BAUDS = {9600: termios.B9600, 19200: termios.B19200, 38400: termios.B38400, + 57600: termios.B57600, 115200: termios.B115200} + + +def open_port(path, baud): + fd = os.open(path, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) + attrs = termios.tcgetattr(fd) + attrs[0] = 0 # iflag: raw + attrs[1] = 0 # oflag: raw + attrs[2] = termios.CS8 | termios.CREAD | termios.CLOCAL + attrs[3] = 0 # lflag: no echo, non-canonical + attrs[4] = attrs[5] = BAUDS[baud] + termios.tcsetattr(fd, termios.TCSANOW, attrs) + termios.tcflush(fd, termios.TCIOFLUSH) + return fd + + +def read_frames(fd, timeout): + """Yields (payload, raw_bytes) for each '>' frame until timeout passes with no data.""" + buf = bytearray() + deadline = time.monotonic() + timeout + while time.monotonic() < deadline: + r, _, _ = select.select([fd], [], [], 0.05) + if r: + buf += os.read(fd, 512) + deadline = time.monotonic() + timeout + while True: + start = buf.find(b'>') + if start < 0: + buf.clear() + break + del buf[:start] + if len(buf) < 3: + break + n = buf[1] | (buf[2] << 8) + if len(buf) < 3 + n: + break + yield bytes(buf[3:3 + n]), bytes(buf[:3 + n]) + del buf[:3 + n] + + +def main(): + ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + ap.add_argument('port') + ap.add_argument('payload', nargs='*', default=['16', '03'], help='payload bytes in hex (default: 16 03)') + ap.add_argument('--baud', type=int, default=115200, choices=sorted(BAUDS)) + ap.add_argument('--timeout', type=float, default=2.0, help='seconds of silence before stopping') + args = ap.parse_args() + + payload = bytes(int(b, 16) for b in args.payload) + frame = b'<' + len(payload).to_bytes(2, 'little') + payload + fd = open_port(args.port, args.baud) + try: + os.write(fd, frame) + print(f'TX {frame.hex(" ")}') + count = 0 + for body, raw in read_frames(fd, args.timeout): + count += 1 + code = body[0] if body else None + print(f'RX code=0x{code:02x} len={len(body)} {raw.hex(" ")}') + text = bytes(c if 32 <= c < 127 else 46 for c in body[1:]).decode() + print(f' ascii: {text}') + if not count: + print('no reply') + sys.exit(1) + finally: + os.close(fd) + + +if __name__ == '__main__': + main() diff --git a/palm-client/tests/test_protocol.c b/palm-client/tests/test_protocol.c index 2c02243..71e5cf4 100644 --- a/palm-client/tests/test_protocol.c +++ b/palm-client/tests/test_protocol.c @@ -73,6 +73,31 @@ static void test_decode_max_frame(void) CHECK(frames == 1 && d.len == VM_MAX_FRAME && d.payload[VM_MAX_FRAME - 1] == (VM_MAX_FRAME - 1)); } +/* DEVICE_INFO captured from the SB-LoRa XIAO (MeshCore v1.17.1) with firmware/tools/companion_query.py. */ +static void test_decode_real_device_info(void) +{ + static const UInt8 in[] = { + 0x3e, 0x52, 0x00, 0x0d, 0x0d, 0xaf, 0x28, 0x00, 0x00, 0x00, 0x00, 0x31, + 0x34, 0x20, 0x41, 0x75, 0x67, 0x20, 0x32, 0x30, 0x32, 0x36, 0x00, 0x53, + 0x65, 0x65, 0x65, 0x64, 0x20, 0x58, 0x69, 0x61, 0x6f, 0x2d, 0x6e, 0x72, + 0x66, 0x35, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x31, 0x2e, 0x31, 0x37, 0x2e, 0x31, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00 + }; + VmFrameDecoder d; + + VmDecoderReset(&d); + CHECK(feed(&d, in, sizeof(in)) == 1); + CHECK(d.len == 0x52); + CHECK(d.payload[0] == 0x0d); + CHECK(memcmp(&d.payload[8], "14 Aug 2026", 11) == 0); + CHECK(memcmp(&d.payload[20], "Seeed Xiao-nrf52", 16) == 0); + CHECK(memcmp(&d.payload[62], "v1.17.1", 7) == 0); + CHECK(sizeof(in) == VM_HEADER_LEN + 0x52); +} + int main(void) { test_encode_device_query(); @@ -80,6 +105,7 @@ int main(void) test_decode_back_to_back(); test_decode_oversize_resyncs(); test_decode_max_frame(); + test_decode_real_device_info(); if (failures) { printf("%d failure(s)\n", failures); return 1;