Skip to content

Update TinyUSB and close device endpoints #10194

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
Mar 28, 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 lib/tinyusb
Submodule tinyusb updated 92 files
+8 −0 .gitignore
+14 −0 .idea/debugServers/rp2040.xml
+14 −0 .idea/debugServers/rp2350.xml
+13 −0 .idea/debugServers/rt1060.xml
+13 −0 .idea/debugServers/rt1064.xml
+13 −0 .idea/debugServers/sam21.xml
+13 −0 .idea/debugServers/sam51.xml
+65 −57 examples/dual/host_info_to_device_cdc/src/main.c
+1 −0 examples/host/CMakeLists.txt
+114 −140 examples/host/bare_api/src/main.c
+2 −1 examples/host/bare_api/src/tusb_config.h
+1 −1 examples/host/cdc_msc_hid/src/cdc_app.c
+1 −2 examples/host/cdc_msc_hid/src/hid_app.c
+6 −10 examples/host/cdc_msc_hid/src/main.c
+2 −1 examples/host/cdc_msc_hid/src/tusb_config.h
+1 −1 examples/host/cdc_msc_hid_freertos/src/cdc_app.c
+0 −6 examples/host/cdc_msc_hid_freertos/src/main.c
+1 −1 examples/host/cdc_msc_hid_freertos/src/msc_app.c
+2 −1 examples/host/cdc_msc_hid_freertos/src/tusb_config.h
+1 −1 examples/host/device_info/src/main.c
+3 −3 examples/host/device_info/src/tusb_config.h
+6 −11 examples/host/hid_controller/src/hid_app.c
+2 −1 examples/host/hid_controller/src/tusb_config.h
+32 −0 examples/host/midi_rx/CMakeLists.txt
+13 −0 examples/host/midi_rx/Makefile
+20 −0 examples/host/midi_rx/only.txt
+123 −0 examples/host/midi_rx/src/main.c
+118 −0 examples/host/midi_rx/src/tusb_config.h
+2 −0 examples/host/msc_file_explorer/src/msc_app.c
+2 −1 examples/host/msc_file_explorer/src/tusb_config.h
+1 −5 hw/bsp/family_support.cmake
+2 −2 hw/bsp/imxrt/boards/mimxrt1015_evk/board/clock_config.c
+33 −33 hw/bsp/imxrt/boards/mimxrt1015_evk/board/clock_config.h
+27 −8 hw/bsp/imxrt/boards/mimxrt1015_evk/board/pin_mux.c
+19 −11 hw/bsp/imxrt/boards/mimxrt1015_evk/board/pin_mux.h
+44 −19 hw/bsp/imxrt/boards/mimxrt1015_evk/mimxrt1015_evk.mex
+2 −2 hw/bsp/imxrt/boards/mimxrt1064_evk/board/clock_config.c
+50 −50 hw/bsp/imxrt/boards/mimxrt1064_evk/board/clock_config.h
+2 −11 hw/bsp/imxrt/boards/mimxrt1064_evk/board/pin_mux.c
+1 −0 hw/bsp/imxrt/boards/mimxrt1064_evk/board/pin_mux.h
+16 −20 hw/bsp/imxrt/boards/mimxrt1064_evk/mimxrt1064_evk.mex
+2 −2 hw/bsp/imxrt/boards/mimxrt1170_evkb/board/clock_config.c
+37 −19 hw/bsp/imxrt/boards/mimxrt1170_evkb/board/pin_mux.c
+17 −10 hw/bsp/imxrt/boards/mimxrt1170_evkb/board/pin_mux.h
+43 −30 hw/bsp/imxrt/boards/mimxrt1170_evkb/mimxrt1170_evkb.mex
+6 −4 hw/bsp/imxrt/family.c
+0 −4 hw/bsp/imxrt/family.cmake
+3 −1 hw/bsp/rp2040/family.c
+1 −0 hw/bsp/rp2040/family.cmake
+8 −0 hw/bsp/same70_xplained/board.mk
+1 −0 src/CMakeLists.txt
+1 −0 src/class/audio/audio.h
+12 −16 src/class/audio/audio_device.c
+20 −18 src/class/cdc/cdc_device.c
+16 −6 src/class/cdc/cdc_device.h
+2 −2 src/class/cdc/cdc_host.c
+4 −4 src/class/cdc/cdc_host.h
+111 −0 src/class/midi/README_midi_host.md
+76 −87 src/class/midi/midi.h
+20 −27 src/class/midi/midi_device.c
+622 −0 src/class/midi/midi_host.c
+193 −0 src/class/midi/midi_host.h
+1 −1 src/class/vendor/vendor_device.c
+26 −16 src/common/tusb_common.h
+5 −2 src/common/tusb_fifo.c
+6 −6 src/common/tusb_private.h
+10 −4 src/common/tusb_types.h
+15 −7 src/device/usbd.c
+3 −0 src/host/hcd.h
+64 −33 src/host/usbh.c
+22 −6 src/host/usbh.h
+0 −4 src/host/usbh_pvt.h
+18 −22 src/portable/analog/max3421/hcd_max3421.c
+63 −0 src/portable/analog/max3421/hcd_max3421.h
+139 −120 src/portable/ehci/ehci.c
+153 −161 src/portable/ehci/ehci.h
+5 −0 src/portable/mentor/musb/hcd_musb.c
+5 −0 src/portable/nxp/khci/hcd_khci.c
+3 −2 src/portable/nxp/lpc_ip3511/dcd_lpc_ip3511.c
+5 −1 src/portable/ohci/ohci.c
+5 −0 src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
+35 −22 src/portable/raspberrypi/rp2040/dcd_rp2040.c
+5 −1 src/portable/raspberrypi/rp2040/hcd_rp2040.c
+5 −0 src/portable/renesas/rusb2/hcd_rusb2.c
+6 −1 src/portable/synopsys/dwc2/hcd_dwc2.c
+14 −35 src/portable/template/hcd_template.c
+1 −0 src/tinyusb.mk
+21 −6 src/tusb.c
+4 −0 src/tusb.h
+27 −25 test/hil/hil_test.py
+24 −5 test/hil/tinyusb.json
+1 −1 tools/get_deps.py
2 changes: 1 addition & 1 deletion ports/raspberrypi/lib/Pico-PIO-USB
22 changes: 22 additions & 0 deletions shared-bindings/usb/core/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include "py/objproperty.h"
#include "shared-bindings/usb/core/Device.h"
#include "shared-bindings/util.h"
#include "py/runtime.h"

//| class Device:
Expand All @@ -49,6 +50,12 @@
//| ...
//|

static void check_for_deinit(usb_core_device_obj_t *self) {
if (common_hal_usb_core_device_deinited(self)) {
raise_deinited_error();
}
}

//| def __del__(self) -> None:
//| """Closes any resources used for this device."""
//| ...
Expand All @@ -64,6 +71,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_deinit_obj, usb_core_device_dei
//| """The USB vendor ID of the device"""
static mp_obj_t usb_core_device_obj_get_idVendor(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idVendor(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idVendor_obj, usb_core_device_obj_get_idVendor);
Expand All @@ -75,6 +83,7 @@ MP_PROPERTY_GETTER(usb_core_device_idVendor_obj,
//| """The USB product ID of the device"""
static mp_obj_t usb_core_device_obj_get_idProduct(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idProduct(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idProduct_obj, usb_core_device_obj_get_idProduct);
Expand All @@ -86,6 +95,7 @@ MP_PROPERTY_GETTER(usb_core_device_idProduct_obj,
//| """The USB device's serial number string."""
static mp_obj_t usb_core_device_obj_get_serial_number(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_usb_core_device_get_serial_number(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_serial_number_obj, usb_core_device_obj_get_serial_number);
Expand All @@ -97,6 +107,7 @@ MP_PROPERTY_GETTER(usb_core_device_serial_number_obj,
//| """The USB device's product string."""
static mp_obj_t usb_core_device_obj_get_product(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_usb_core_device_get_product(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_product_obj, usb_core_device_obj_get_product);
Expand All @@ -109,6 +120,7 @@ MP_PROPERTY_GETTER(usb_core_device_product_obj,
//|
static mp_obj_t usb_core_device_obj_get_manufacturer(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_usb_core_device_get_manufacturer(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_manufacturer_obj, usb_core_device_obj_get_manufacturer);
Expand All @@ -121,6 +133,7 @@ MP_PROPERTY_GETTER(usb_core_device_manufacturer_obj,
//|
static mp_obj_t usb_core_device_obj_get_bus(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_bus(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_bus_obj, usb_core_device_obj_get_bus);
Expand All @@ -134,6 +147,7 @@ MP_PROPERTY_GETTER(usb_core_device_bus_obj,
//|
static mp_obj_t usb_core_device_obj_get_port_numbers(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_usb_core_device_get_port_numbers(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_port_numbers_obj, usb_core_device_obj_get_port_numbers);
Expand All @@ -147,6 +161,7 @@ MP_PROPERTY_GETTER(usb_core_device_port_numbers_obj,
//|
static mp_obj_t usb_core_device_obj_get_speed(mp_obj_t self_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_speed(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_speed_obj, usb_core_device_obj_get_speed);
Expand All @@ -171,6 +186,7 @@ static mp_obj_t usb_core_device_set_configuration(size_t n_args, const mp_obj_t
{ MP_QSTR_configuration, MP_ARG_INT, {.u_int = 1} },
};
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

Expand All @@ -197,6 +213,7 @@ static mp_obj_t usb_core_device_write(size_t n_args, const mp_obj_t *pos_args, m
{ MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} },
};
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

Expand Down Expand Up @@ -228,6 +245,7 @@ static mp_obj_t usb_core_device_read(size_t n_args, const mp_obj_t *pos_args, mp
{ MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} },
};
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

Expand Down Expand Up @@ -277,6 +295,7 @@ static mp_obj_t usb_core_device_ctrl_transfer(size_t n_args, const mp_obj_t *pos
{ MP_QSTR_timeout, MP_ARG_INT, {.u_int = 0} },
};
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
check_for_deinit(self);
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

Expand Down Expand Up @@ -310,6 +329,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(usb_core_device_ctrl_transfer_obj, 2, usb_core_device
//|
static mp_obj_t usb_core_device_is_kernel_driver_active(mp_obj_t self_in, mp_obj_t interface_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_int_t interface = mp_obj_get_int(interface_in);
bool active = common_hal_usb_core_device_is_kernel_driver_active(self, interface);
return mp_obj_new_bool(active);
Expand All @@ -327,6 +347,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_is_kernel_driver_active_obj, usb_core_
//|
static mp_obj_t usb_core_device_detach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_int_t interface = mp_obj_get_int(interface_in);
common_hal_usb_core_device_detach_kernel_driver(self, interface);
return mp_const_none;
Expand All @@ -343,6 +364,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_detach_kernel_driver_obj, usb_core_dev
//|
static mp_obj_t usb_core_device_attach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) {
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_int_t interface = mp_obj_get_int(interface_in);
common_hal_usb_core_device_attach_kernel_driver(self, interface);
return mp_const_none;
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/usb/core/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
extern const mp_obj_type_t usb_core_device_type;

bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number);
bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self);
void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self);
uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self);
uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self);
Expand Down
18 changes: 15 additions & 3 deletions shared-module/usb/core/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,22 @@ bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t d
return true;
}

bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self) {
return self->device_address == 0;
}

void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self) {
// TODO: Close all of the endpoints we've opened. Some drivers store state
// for each open endpoint. If we don't close them, then we'll leak memory.
// Waiting for TinyUSB to add this.
if (common_hal_usb_core_device_deinited(self)) {
return;
}
size_t open_size = sizeof(self->open_endpoints);
for (size_t i = 0; i < open_size; i++) {
if (self->open_endpoints[i] != 0) {
tuh_edpt_close(self->device_address, self->open_endpoints[i]);
self->open_endpoints[i] = 0;
}
}
self->device_address = 0;
}

uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self) {
Expand Down