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

未关闭
#6,303 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
28/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
go, raspberry-pi
领域
embedded-iot

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Go
星标
17.8k
派生
1.1k
平均合并
1 天 4 小时
30 天内合并 PR
380

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tinygo-org/tinygo 的其他 Issue

查看 tinygo-org/tinygo 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。