Skip to content

Commit

Permalink
Merge pull request #9672 from samblenny/usb-device
Browse files Browse the repository at this point in the history
Improve usb.core.Device exception handling and reporting
  • Loading branch information
tannewt authored Oct 8, 2024
2 parents 44748ab + d2e82bd commit 6ca0380
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions shared-module/usb/core/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ static size_t _xfer(tuh_xfer_t *xfer, mp_int_t timeout) {
xfer->complete_cb = _transfer_done_cb;
if (!tuh_edpt_xfer(xfer)) {
mp_raise_usb_core_USBError(NULL);
return 0;
}
uint32_t start_time = supervisor_ticks_ms32();
while ((timeout == 0 || supervisor_ticks_ms32() - start_time < (uint32_t)timeout) &&
Expand Down Expand Up @@ -206,6 +207,7 @@ static bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) {

if (self->configuration_descriptor == NULL) {
mp_raise_usb_core_USBError(MP_ERROR_TEXT("No configuration set"));
return false;
}

tusb_desc_configuration_t *desc_cfg = (tusb_desc_configuration_t *)self->configuration_descriptor;
Expand Down Expand Up @@ -240,6 +242,7 @@ static bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) {
mp_int_t common_hal_usb_core_device_write(usb_core_device_obj_t *self, mp_int_t endpoint, const uint8_t *buffer, mp_int_t len, mp_int_t timeout) {
if (!_open_endpoint(self, endpoint)) {
mp_raise_usb_core_USBError(NULL);
return 0;
}
tuh_xfer_t xfer;
xfer.daddr = self->device_number;
Expand All @@ -252,6 +255,7 @@ mp_int_t common_hal_usb_core_device_write(usb_core_device_obj_t *self, mp_int_t
mp_int_t common_hal_usb_core_device_read(usb_core_device_obj_t *self, mp_int_t endpoint, uint8_t *buffer, mp_int_t len, mp_int_t timeout) {
if (!_open_endpoint(self, endpoint)) {
mp_raise_usb_core_USBError(NULL);
return 0;
}
tuh_xfer_t xfer;
xfer.daddr = self->device_number;
Expand Down Expand Up @@ -286,6 +290,7 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,

if (!tuh_control_xfer(&xfer)) {
mp_raise_usb_core_USBError(NULL);
return 0;
}
uint32_t start_time = supervisor_ticks_ms32();
while ((timeout == 0 || supervisor_ticks_ms32() - start_time < (uint32_t)timeout) &&
Expand Down

0 comments on commit 6ca0380

Please sign in to comment.