Skip to content

Commit 154ef2e

Browse files
committed
Change USB NULL errors to short strings
shared-module/usb/core/Device.c had several spots where it would raise a MicroPython exception with a NULL argument. That made it very difficult for CircuitPython code to attempt to recover from fault conditions caused by quirky USB device behavior. This adds short error strings to distinguish the different types of faults.
1 parent ec5cca6 commit 154ef2e

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

shared-module/usb/core/Device.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static size_t _xfer(tuh_xfer_t *xfer, mp_int_t timeout) {
159159
_xfer_result = 0xff;
160160
xfer->complete_cb = _transfer_done_cb;
161161
if (!tuh_edpt_xfer(xfer)) {
162-
mp_raise_usb_core_USBError(NULL);
162+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Xfer"));
163163
}
164164
uint32_t start_time = supervisor_ticks_ms32();
165165
while ((timeout == 0 || supervisor_ticks_ms32() - start_time < (uint32_t)timeout) &&
@@ -176,7 +176,7 @@ static size_t _xfer(tuh_xfer_t *xfer, mp_int_t timeout) {
176176
xfer_result_t result = _xfer_result;
177177
_xfer_result = 0xff;
178178
if (result == XFER_RESULT_STALLED) {
179-
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Pipe error"));
179+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Stall"));
180180
}
181181
if (result == 0xff) {
182182
tuh_edpt_abort_xfer(xfer->daddr, xfer->ep_addr);
@@ -205,7 +205,7 @@ static bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) {
205205
}
206206

207207
if (self->configuration_descriptor == NULL) {
208-
mp_raise_usb_core_USBError(MP_ERROR_TEXT("No configuration set"));
208+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("NoCfg"));
209209
}
210210

211211
tusb_desc_configuration_t *desc_cfg = (tusb_desc_configuration_t *)self->configuration_descriptor;
@@ -239,7 +239,8 @@ static bool _open_endpoint(usb_core_device_obj_t *self, mp_int_t endpoint) {
239239

240240
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) {
241241
if (!_open_endpoint(self, endpoint)) {
242-
mp_raise_usb_core_USBError(NULL);
242+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Endpt"));
243+
return 0;
243244
}
244245
tuh_xfer_t xfer;
245246
xfer.daddr = self->device_number;
@@ -251,7 +252,8 @@ mp_int_t common_hal_usb_core_device_write(usb_core_device_obj_t *self, mp_int_t
251252

252253
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) {
253254
if (!_open_endpoint(self, endpoint)) {
254-
mp_raise_usb_core_USBError(NULL);
255+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Endpt"));
256+
return 0;
255257
}
256258
tuh_xfer_t xfer;
257259
xfer.daddr = self->device_number;
@@ -285,7 +287,7 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,
285287
_xfer_result = 0xff;
286288

287289
if (!tuh_control_xfer(&xfer)) {
288-
mp_raise_usb_core_USBError(NULL);
290+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Xfer"));
289291
}
290292
uint32_t start_time = supervisor_ticks_ms32();
291293
while ((timeout == 0 || supervisor_ticks_ms32() - start_time < (uint32_t)timeout) &&
@@ -302,7 +304,7 @@ mp_int_t common_hal_usb_core_device_ctrl_transfer(usb_core_device_obj_t *self,
302304
xfer_result_t result = _xfer_result;
303305
_xfer_result = 0xff;
304306
if (result == XFER_RESULT_STALLED) {
305-
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Pipe error"));
307+
mp_raise_usb_core_USBError(MP_ERROR_TEXT("Stall"));
306308
}
307309
if (result == 0xff) {
308310
tuh_edpt_abort_xfer(xfer.daddr, xfer.ep_addr);

0 commit comments

Comments
 (0)