E28 — RP counter → CH32 DVP → USB2 high speed → PC: maximum verified stream rate
| ID | E28 |
| Date | 2026-12-03 |
| Status | done: 20 MS/s of 10-bit samples (40 MB/s) sustained for 30 s, 21.5 MS/s peak, every sample checked; the limit is the USB2 link/PC, not the CH32. Test suite: 37/37 pass |
| Decisions | D107, D108, D109 (DECISIONS.md) |
| Board | #1 (rev A, Y2 = 12 MHz); RP on the PC (J3), J1 (silk “USB3”) on the bench hub (1-5.4.1), RP usbsel usb, capsel rp |
| Firmware + results | Suite run (final): fw/tests/2026-12-03-6-streamsuite: both chips’ sources and binaries (RP rp/ + rp417_rp.uf2; CH32 usbhs/ + usbhs_stream.bin, A/B variants, main rp417_ch32.bin), the pytest suite, results/. First exploration: fw/tests/2026-12-03-5-dvpusb |
| Scripts | test suite fw/tools/streamtest (pytest), dvpusb.py, fw/usbhs/stream.c, fw/usbhs/build.sh (APP=stream) |
Summary: The RP’s PIO counter feeds the CH32’s camera port (DVP), and the CH32 streams those samples to the PC over USB2 high speed. The PC checks every sample. The path holds 20 MS/s of 10-bit samples (40 MB/s) for 30 s, with a 21.5 MS/s peak over 2 s (the test suite below, 384 KB ring). The first sweep (256 KB ring) gave 20.5 MS/s, but only over 2 s. The CH32 alone can send 42.7 MB/s over this link to this PC, so the limit is the link and the PC, not the CH32. Two problems turned up along the way:
- DMA alignment: a buffer that is not 32-byte aligned loses its last 16 bytes.
- PC reads: the reading program must not allocate a new buffer for each read.
What
- Firmware (
fw/usbhs/build.sh APP=stream): the WCH USBHS stack from E27.stream.creplaces the example’s main loop. Commands arrive on EP1 OUT, data goes out on EP1 IN in burst mode (UEP_TX_BURST/_MODE, RM V1.8 25.2.1.17–18): one DMA set-up sends up to 64 KB as 512-byte packets, with no zero-length packet. This is the method of the open-source CH32H417 logic analyzer.G len n: sends a RAM buffer of 10-bit counter values n times. This measures USB alone.D chunk n burst: the DVP runs in 10-bit JPEG mode,chunksamples per DMA row. Its BUF0/BUF1 ping-pong is re-pointed along a 256 KB ring. Finished slots are sent in bursts of at mostburstbytes. The CH32 stops with OVERRUN when the DVP would write into a slot not yet sent. One UART line reports each run.
- Host (
dvpusb.py): flashes, power-cycles the CH32 through the RP, then runs thegensweep. For each PCLK rate it sets the RP counter, arms the DVP over USB and starts the frame with the RP’s HSYNC/VSYNC (hv, asdvpcap.py). It reads 2 s of data and checks every sample (+1 mod 1024, nothing above bit 9). It reports the rate from the read timestamps.
Results
USB alone (gen, 64 MiB per size, all counters correct, PC reads into buffers allocated beforehand):
| burst | 2 KB | 8 KB | 16 KB | 32 KB | 62 KB |
|---|---|---|---|---|---|
| MB/s, 1 MiB reads | 30.5 | 37.7 | 40.3 | 41.1 | 41.7 |
| MB/s, 4 MiB reads | 30.8 | 38.2 | 40.9 | 42.1 | 42.7 |
DVP → USB (chunk 4096 samples, 32 KB bursts, 4 MiB preallocated reads, 2 s per rate, 256 KB ring = 32 slots):
| PCLK (RP counter) | rate received | samples checked | wrong | max backlog |
|---|---|---|---|---|
| 5 MHz | 10.0 MB/s | 10.0 M | 0 | 7/32 |
| 10 MHz | 20.0 MB/s | 20.0 M | 0 | 14/32 |
| 15 MHz | 30.0 MB/s | 30.0 M | 0 | 24/32 |
| 18.75 MHz | 37.5 MB/s | 37.5 M | 0 | 25/32 |
| 20 MHz | 40.0 MB/s | 40.0 M | 0 | 29/32 |
| 20.5 MHz | 41.0 MB/s | 41.0 M | 0 | 29/32 |
| 21 / 21.5 MHz | — | — | 0 in what arrived | OVERRUN after 4–8 MiB (with a 384 KB ring: after 44 MiB) |
- The 32-byte rule (A/B): the first build placed the ring at 0x20112ad0, 16-byte aligned. Every pass round the
ring, its last 16 bytes stayed zero: the 8 samples were replaced, and the count continued after them. The DMA
bus is 256 bits wide (RM V1.8 31.6.3), and every 8 KB slot ended halfway through a 32-byte word. In the middle of the
ring, the next chunk completed that word. After the last slot, the next chunk went back to slot 0, so the word was
never written. Aligned to 32 bytes: 0 errors. Moved 16 bytes off again (
usbhs_stream_ofs16.bin): the same 9 bad steps per pass. - The PC’s read gap sets the buffer need. pyusb allocates and copies a new array for every read. With 4 MiB reads, that gap (≥ 8 ms) overran the 256 KB ring (7.7 ms at 33 MB/s) at the first read boundary. With 1 MiB reads it capped the stream at 16.7 MS/s. Reading into buffers allocated beforehand raised the ceiling to 20.5 MS/s.
- 64 KB bursts do worse than 32 KB: the burst in flight ties up 8 of the 32 slots, so the PC’s gaps overrun sooner.
- A 384 KB ring (48 slots) does not add a rate step: 21 MS/s = 42 MB/s is the link’s sustained maximum on this PC.
Test suite (D109)
fw/tools/streamtest is a pytest suite that runs
on the board and checks every sample: RP417_BOARD_LOCK=… python3 -m pytest fw/tools/streamtest -v --with-ab
(7 min 40 s). Its rule for every DVP run is lossless or loud: either the run is complete and clean, or the CH32
stopped with OVERRUN, and in both cases the data that reached the PC is clean. Final run (archive 2026-12-03-6):
37/37 pass.
| test | result |
|---|---|
| link, firmware | 480 Mb/s, bcdUSB 2.00; USBHS PLL locked, PLLCFGR2 0008072b |
| RP counter PCLK (1 / 10 / 20.5 MHz) | RP’s own count on GPIO30: 0 / −6 / −8 ppm from the setting |
| USB alone, 64 MiB per burst size | 31.6 / 39.5 / 42.2 / 43.3 / 44.0 MB/s (2 / 8 / 16 / 32 / 62 KB), 0 wrong |
| USB alone, 512 MiB | 43.2 MB/s, 0 wrong |
| DVP → USB, 1 … 20.5 MS/s | all complete, 0 wrong, rate = 2 × PCLK; backlog ≤ 15/48 slots |
| maximum 2 s rate (0.25 MHz steps) | 21.5 MS/s = 43 MB/s clean; 21.75 overruns |
| chunk sizes 256 … 4096 at 10 MS/s | all complete and clean (256-sample rows: 39 k DMA rows/s, backlog 73/768) |
| burst caps 8 … 64 KB at 18.75 MS/s | all complete and clean |
| overrun at 23 MS/s | OVERRUN reported, 2.7 M samples received, 0 wrong |
| restart after overrun, 5 streams + RAM burst | all clean |
| endurance: 20 MS/s for 30 s | 600 M samples (1.2 GB), 0 wrong, backlog peak 26/48 |
| bad commands (unknown, chunk 100, chunk 3000, gen len 1000) | err replies with the run id; link still in step |
| alignment A/B (ring 16 B off) | 225 bad steps = 9 × 25 ring passes, all at the ring’s last 9 indices; default clean |
What the suite changed:
- The ring is 384 KB (48 slots) by default. The 2 s peak of 20.5 MS/s was not a sustained rate. With 256 KB, 20 MS/s overran after 7–20 s; 19.5 MS/s held for 30 s. With 384 KB, 20 MS/s holds for 30 s. A 30 s run is the figure to quote; a 2 s run is the peak.
- Command run ids: the stream firmware echoes an id in every UART report. The first suite run took late replies for current ones: a burst still in flight after an overrun was read as the next test’s first data, and every later report came one run late. Now each run drains EP1 IN first and matches replies by id. The read timeout also scales with the rate (a 4 MiB read at 1 MS/s takes 2 s).
- Read buffers are allocated before arming. Allocating 1.1 GB inside the reader thread let a 30 s stream overrun before the first read.
Device class: vendor bulk kept (D108)
The CH32 could also look like a standard webcam (UVC) or sound card (UAC2), so the PC’s own driver would read the stream. These options were compared on paper, not built:
| vendor bulk (E28, kept) | UVC over bulk | UVC isochronous | UAC2 isochronous | |
|---|---|---|---|---|
| PC side | libusb (dvpusb.py) |
uvcvideo, /dev/videoN, ffmpeg/V4L2 |
same | ALSA/CoreAudio, arecord/Audacity |
| Ceiling on USB2 HS | 41.0 MB/s measured (42.7 MB/s USB alone) | ≈ the same bulk rate, minus a 12-byte header per transfer | 24.6 MB/s cap (3 × 1024 B per 125 µs microframe) | MHz sample rates unusual (driver/tool limits to verify) |
| Lost data | none: bulk retries, the CH32 stops with OVERRUN | none (bulk), dropped frames on driver errors | lossy: no retries | lossy |
| Extra firmware | none | UVC descriptors + probe/commit requests + payload headers | + isochronous set-up | UAC2 descriptors, clock source, rate feedback |
The requirement is the highest lossless bandwidth, and that rules out both isochronous classes (no retries, ≈ 60 % of the bulk ceiling). UVC over bulk would add work and a frame layer without adding rate. The stream stays vendor bulk with a libusb reader. The rate left to gain is on the PC side: an async reader that keeps several transfers queued (41 → ≈ 43 MB/s). A sound card class could later suit a slow, decimated channel; it is not on the list.
Lesson
DMA buffers on the CH32H417 must start (and every slot must end) on 32-byte boundaries. Check a stream at its ends and at its wrap points, not only in the middle. Throughput tests over USB measure the reading program as much as the device: allocate the buffers beforehand, and log the device’s backlog to see who is late.
Chip facts and numbers: fw/LEARNINGS.md. All experiments: index.