Add USB CDC diagnostics, verified against real hardware

Presents a USB CDC serial port alongside the RS485 protocol, so the node
can be verified with nothing but a USB cable — no SWD probe and no RS485
adapter. Motivated by having neither available: probe-rs needs SWD and
defmt logs travel over RTT, so with only the USB bootloader attached
there was no way to observe the sensor at all.

Two tasks, both at the lowest priority so they can never delay a bus
reply:

- usb_irq (binds USBCTRL_IRQ) services the stack and drains the RX
  buffer, which has to happen even though the port is output-only.
- usb_report emits one diagnostic line per second.

The report deliberately exposes raw internal state rather than the
protocol's flag encoding: "is the sensor working and what does it read"
is a different question from what a bus master asks.

Writes discard whatever does not fit rather than blocking, so an
unattended port cannot stall the firmware. The USB bus allocator lives
in an #[init(local = ...)] resource, which supplies the 'static the
device and class borrow from without a static mut.

Verified on hardware. The SHT31 reports serial 0x2d5ac752, read from its
factory serial register and CRC-8 validated, with readings of 27.3 C and
43.5 %RH varying by a few LSB between samples — the noise floor of a live
16-bit conversion rather than a stuck cache.

Costs ~17 KiB of flash and ~1.3 KiB of RAM, taking the image to ~34.5 KiB
of 2 MiB and ~2.5 KiB of 256 KiB.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Oliver Walter
2026-07-28 18:32:52 +02:00
parent 93b4e2c4f8
commit bcc6869965
5 changed files with 280 additions and 3 deletions
+7
View File
@@ -35,3 +35,10 @@ fugit = "0.3"
defmt = { workspace = true }
defmt-rtt = "1.0"
panic-probe = { version = "1.0", features = ["print-defmt"] }
# USB CDC diagnostics. Lets the node be verified over a plain USB cable with no
# SWD probe attached. The usb-device version must track rp2040-hal's own so the
# UsbBus trait implementations line up.
usb-device = "0.3"
usbd-serial = "0.2"
heapless = "0.8"