fix: prevent USB CDC TX stall on ring buffer wrap#5538
Open
nobonobo wants to merge 3 commits into
Open
Conversation
Member
|
Please note test failure: |
Contributor
|
I'd suggest we reduce code size and simplify instead of adding more complexity on top of an already needlessly complex implementation, but oh well, such are the PRs we merge in tinygo |
Contributor
Author
|
Ideally, we should read 64 bytes at a time from the ring buffer and write them directly to _usbDPSRAM. |
Contributor
Author
The test in |
Contributor
Author
code to reproduce the issueserver.go: package main
import (
"strings"
"time"
"machine"
)
func main() {
for !machine.Serial.DTR() {
time.Sleep(100 * time.Millisecond)
}
tick := time.NewTicker(100 * time.Microsecond)
for range tick.C {
println(strings.Repeat("*", 259))
}
}
client.go: package main
import (
"bufio"
"go.bug.st/serial"
)
func main() {
port, err := serial.Open("COM3", &serial.Mode{})
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(port)
cnt := 0
for scanner.Scan() {
text := scanner.Text()
if cnt > 0 && len(text) != 259 {
println()
panic("wrong length: " + text)
} else {
print("\rcount:", cnt)
}
cnt++
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix USB CDC TX stall when TX ring buffer wraps
Fixes #5537
This patch fixes an occasional USB CDC TX stall when the TX ring buffer wraps.
When the ring buffer wrapped, the TX path could send a short packet before the remaining data.
Example:
Before:
Expected:
A short packet indicates the end of a USB bulk transfer. Although the packet sequence is valid at the USB protocol level, it can cause interoperability issues with some USB CDC host implementations.
The root cause was that the TX path only used the first contiguous segment returned by the ring buffer. When the buffer wrapped, the first segment could be smaller than the endpoint packet size, causing an unintended short packet.
This patch combines wrapped ring buffer segments to fill the USB endpoint packet size whenever possible.
The zero-copy path is preserved for contiguous data:
Only wrapped data requires a temporary buffer:
After this change, the TX stall could no longer be reproduced.