Skip to content

usb/cdc: writing small blocks should not require allocation #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion micropython/usb/usb-device-cdc/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
metadata(version="0.1.1")
metadata(version="0.1.2")
require("usb-device")
package("usb")
9 changes: 6 additions & 3 deletions micropython/usb/usb-device-cdc/usb/device/cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,9 @@ def _rd_cb(self, ep, res, num_bytes):
###

def write(self, buf):
# use a memoryview to track how much of 'buf' we've written so far
# (unfortunately, this means a 1 block allocation for each write, but it's otherwise allocation free.)
start = time.ticks_ms()
mv = memoryview(buf)
mv = buf

while True:
# Keep pushing buf into _wb into it's all gone
nbytes = self._wb.write(mv)
Expand All @@ -362,6 +361,10 @@ def write(self, buf):
if nbytes == len(mv):
return len(buf) # Success

# if buf couldn't be fully written on the first attempt
# convert it to a memoryview to track partial writes
if mv is buf:
mv = memoryview(buf)
mv = mv[nbytes:]

# check for timeout
Expand Down
Loading