Skip to content

Commit f3560f9

Browse files
committed
handle XFER_RESULT_FAILED with exception
Previously, usb.core.Device.ctrl_transfer() did not raise an exception when TinyUSB returned an XFER_RESULT_FAILED result code. This change raises an exception which prevents a failed transfer from returning an all zero result buffer.
1 parent e9da6bc commit f3560f9

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

shared-module/usb/core/Device.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,30 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,
374374
RUN_BACKGROUND_TASKS;
375375
}
376376
if (mp_hal_is_interrupted()) {
377+
// Handle case of VM being interrupted by Ctrl-C or autoreload
377378
tuh_edpt_abort_xfer(xfer.daddr, xfer.ep_addr);
378379
return 0;
379380
}
381+
// Handle control transfer result code from TinyUSB
380382
xfer_result_t result = _xfer_result;
381383
_xfer_result = XFER_RESULT_INVALID;
382384
switch (result) {
383385
case XFER_RESULT_SUCCESS:
384386
return _actual_len;
385387
case XFER_RESULT_FAILED:
388+
mp_raise_usb_core_USBError(NULL);
386389
break;
387390
case XFER_RESULT_STALLED:
388391
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Pipe error"));
389392
break;
390393
case XFER_RESULT_TIMEOUT:
394+
// This timeout comes from TinyUSB, so assume that it has stopped the
395+
// transfer (note: timeout logic may be unimplemented on TinyUSB side)
396+
mp_raise_usb_core_USBTimeoutError();
391397
break;
392398
case XFER_RESULT_INVALID:
399+
// This timeout comes from CircuitPython, not TinyUSB, so tell TinyUSB
400+
// to stop the transfer
393401
tuh_edpt_abort_xfer(xfer.daddr, xfer.ep_addr);
394402
mp_raise_usb_core_USBTimeoutError();
395403
break;

0 commit comments

Comments
 (0)