USB CDC RX silently drops data when the receive buffer is full

Aperta
#6,303 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
28/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
go, raspberry-pi
Ambito
embedded-iot

Direzione di ricerca

Start from cdcCallbackRx, which copies incoming USB data into the RX ring buffer and drops whatever exceeds USB.rx.Free(). Reproduce with the linked s1 program on a Pico, then look at how cdc.USB.ReadByte() drains that buffer. Done means the transfer is backpressured instead of truncated, so a 1024-byte send with a 5-second read pause arrives intact. The fix needs a flow-control design choice, so confirm the approach with a maintainer first.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

As mentioned in #6253, I found this while setting up a test environment for that work. Since it causes silent data loss, I'm reporting it separately.

USB CDC RX data is lost on a Raspberry Pi Pico when the application stops reading for 5 seconds.

With the same test program, a 512-byte transfer is received correctly (byte count and SHA-256 match), but a 1024-byte transfer results in only 513 bytes being read.

Steps to reproduce

Minimal reproducer: https://github.com/rdon-key/tinygo-usb-test/tree/main/s1

From the repository root, flash the Pico with TinyGo 0.44.0:

tinygo flash -target=pico -scheduler=tasks ./s1/tinygo/main.go

Run the host program on Windows (adjust the port as needed):

go -C s1/host run . -port COM12 -size 512

Reset the Pico, then run it again with 1024 bytes:

go -C s1/host run . -port COM12 -size 1024

The host sends size bytes where data[i] = byte(i).

On the Pico, the program reads the first byte using cdc.USB.ReadByte(), sleeps for 5 seconds without reading, and then reads for another 3 seconds.

The Pico sends back the number of bytes actually read and their SHA-256 hash. The host compares both with the transmitted data.

Actual result

512 bytes — PASS

S1 - USB CDC RX Data Loss Reproducer
Sent     : 512 B
Received : 512 B
SHA-256  : MATCH
Result   : PASS

1024 bytes — FAIL

S1 - USB CDC RX Data Loss Reproducer
Sent     : 1024 B
Received : 513 B
SHA-256  : MISMATCH
Result   : FAIL
Expected result

USB CDC should not silently lose data when the application temporarily stops reading.

When the receiver cannot accept more data, USB transfers should be backpressured rather than silently discarded.

Environment
  • Board: Raspberry Pi Pico (RP2040)
  • TinyGo: 0.44.0
  • Target: pico
  • Scheduler: tasks
  • Host: Windows AMD64
Notes

The CDC RX ring buffer has a capacity of 512 bytes.

In TinyGo 0.44.0, cdcCallbackRx limits incoming data to the available RX buffer space:

free := USB.rx.Free()
USB.rx.Put(b[:min(len(b), int(free))])

This silently discards any bytes that do not fit. It matches the observed result: 1 byte consumed by ReadByte() + 512 bytes buffered = 513 bytes.

The results above were obtained on Windows. It has not been tested with a Linux host yet.

Lingua principale
Go
Stelle
17.8k
Fork
1.1k
Merge medio
1g 4h
PR unite (30g)
380

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tinygo-org/tinygo

Tutte le issue di tinygo-org/tinygo

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.