Skip to content

Commit a215fc4

Browse files
committed
usb-device-cdc: Fix short delays when timeout is 0.
Previously even with timeout set to zero, a read could block for up to 1ms + machine.idle() timeout period. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 163f8ea commit a215fc4

File tree

1 file changed

+4
-2
lines changed
  • micropython/usb/usb-device-cdc/usb/device

1 file changed

+4
-2
lines changed

micropython/usb/usb-device-cdc/usb/device/cdc.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,11 @@ def write(self, buf):
367367
mv = mv[nbytes:]
368368

369369
# check for timeout
370-
if time.ticks_diff(time.ticks_ms(), start) > self._timeout:
370+
if time.ticks_diff(time.ticks_ms(), start) >= self._timeout:
371371
return len(buf) - len(mv)
372372

373+
machine.idle()
374+
373375
def read(self, size):
374376
start = time.ticks_ms()
375377

@@ -404,7 +406,7 @@ def _readinto(self, b, start):
404406
if n == len(b):
405407
break # Done, exit before we reach the sleep
406408

407-
if time.ticks_diff(time.ticks_ms(), start) > self._timeout:
409+
if time.ticks_diff(time.ticks_ms(), start) >= self._timeout:
408410
break # Timed out
409411

410412
machine.idle()

0 commit comments

Comments
 (0)