Skip to content

Commit 14eb89a

Browse files
authored
Merge pull request #10194 from tannewt/update_tinyusb_close_ep
Update TinyUSB and close device endpoints
2 parents 5202806 + ca063ae commit 14eb89a

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

lib/tinyusb

Submodule tinyusb updated 92 files

shared-bindings/usb/core/Device.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
#include "py/objproperty.h"
4141
#include "shared-bindings/usb/core/Device.h"
42+
#include "shared-bindings/util.h"
4243
#include "py/runtime.h"
4344

4445
//| class Device:
@@ -49,6 +50,12 @@
4950
//| ...
5051
//|
5152

53+
static void check_for_deinit(usb_core_device_obj_t *self) {
54+
if (common_hal_usb_core_device_deinited(self)) {
55+
raise_deinited_error();
56+
}
57+
}
58+
5259
//| def __del__(self) -> None:
5360
//| """Closes any resources used for this device."""
5461
//| ...
@@ -64,6 +71,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_deinit_obj, usb_core_device_dei
6471
//| """The USB vendor ID of the device"""
6572
static mp_obj_t usb_core_device_obj_get_idVendor(mp_obj_t self_in) {
6673
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
74+
check_for_deinit(self);
6775
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idVendor(self));
6876
}
6977
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idVendor_obj, usb_core_device_obj_get_idVendor);
@@ -75,6 +83,7 @@ MP_PROPERTY_GETTER(usb_core_device_idVendor_obj,
7583
//| """The USB product ID of the device"""
7684
static mp_obj_t usb_core_device_obj_get_idProduct(mp_obj_t self_in) {
7785
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
86+
check_for_deinit(self);
7887
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_idProduct(self));
7988
}
8089
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_idProduct_obj, usb_core_device_obj_get_idProduct);
@@ -86,6 +95,7 @@ MP_PROPERTY_GETTER(usb_core_device_idProduct_obj,
8695
//| """The USB device's serial number string."""
8796
static mp_obj_t usb_core_device_obj_get_serial_number(mp_obj_t self_in) {
8897
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
98+
check_for_deinit(self);
8999
return common_hal_usb_core_device_get_serial_number(self);
90100
}
91101
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_serial_number_obj, usb_core_device_obj_get_serial_number);
@@ -97,6 +107,7 @@ MP_PROPERTY_GETTER(usb_core_device_serial_number_obj,
97107
//| """The USB device's product string."""
98108
static mp_obj_t usb_core_device_obj_get_product(mp_obj_t self_in) {
99109
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
110+
check_for_deinit(self);
100111
return common_hal_usb_core_device_get_product(self);
101112
}
102113
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_product_obj, usb_core_device_obj_get_product);
@@ -109,6 +120,7 @@ MP_PROPERTY_GETTER(usb_core_device_product_obj,
109120
//|
110121
static mp_obj_t usb_core_device_obj_get_manufacturer(mp_obj_t self_in) {
111122
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
123+
check_for_deinit(self);
112124
return common_hal_usb_core_device_get_manufacturer(self);
113125
}
114126
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_manufacturer_obj, usb_core_device_obj_get_manufacturer);
@@ -121,6 +133,7 @@ MP_PROPERTY_GETTER(usb_core_device_manufacturer_obj,
121133
//|
122134
static mp_obj_t usb_core_device_obj_get_bus(mp_obj_t self_in) {
123135
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
136+
check_for_deinit(self);
124137
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_bus(self));
125138
}
126139
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_bus_obj, usb_core_device_obj_get_bus);
@@ -134,6 +147,7 @@ MP_PROPERTY_GETTER(usb_core_device_bus_obj,
134147
//|
135148
static mp_obj_t usb_core_device_obj_get_port_numbers(mp_obj_t self_in) {
136149
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
150+
check_for_deinit(self);
137151
return common_hal_usb_core_device_get_port_numbers(self);
138152
}
139153
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_port_numbers_obj, usb_core_device_obj_get_port_numbers);
@@ -147,6 +161,7 @@ MP_PROPERTY_GETTER(usb_core_device_port_numbers_obj,
147161
//|
148162
static mp_obj_t usb_core_device_obj_get_speed(mp_obj_t self_in) {
149163
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
164+
check_for_deinit(self);
150165
return MP_OBJ_NEW_SMALL_INT(common_hal_usb_core_device_get_speed(self));
151166
}
152167
MP_DEFINE_CONST_FUN_OBJ_1(usb_core_device_get_speed_obj, usb_core_device_obj_get_speed);
@@ -171,6 +186,7 @@ static mp_obj_t usb_core_device_set_configuration(size_t n_args, const mp_obj_t
171186
{ MP_QSTR_configuration, MP_ARG_INT, {.u_int = 1} },
172187
};
173188
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
189+
check_for_deinit(self);
174190
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
175191
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
176192

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

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

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

@@ -310,6 +329,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(usb_core_device_ctrl_transfer_obj, 2, usb_core_device
310329
//|
311330
static mp_obj_t usb_core_device_is_kernel_driver_active(mp_obj_t self_in, mp_obj_t interface_in) {
312331
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
332+
check_for_deinit(self);
313333
mp_int_t interface = mp_obj_get_int(interface_in);
314334
bool active = common_hal_usb_core_device_is_kernel_driver_active(self, interface);
315335
return mp_obj_new_bool(active);
@@ -327,6 +347,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_is_kernel_driver_active_obj, usb_core_
327347
//|
328348
static mp_obj_t usb_core_device_detach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) {
329349
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
350+
check_for_deinit(self);
330351
mp_int_t interface = mp_obj_get_int(interface_in);
331352
common_hal_usb_core_device_detach_kernel_driver(self, interface);
332353
return mp_const_none;
@@ -343,6 +364,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(usb_core_device_detach_kernel_driver_obj, usb_core_dev
343364
//|
344365
static mp_obj_t usb_core_device_attach_kernel_driver(mp_obj_t self_in, mp_obj_t interface_in) {
345366
usb_core_device_obj_t *self = MP_OBJ_TO_PTR(self_in);
367+
check_for_deinit(self);
346368
mp_int_t interface = mp_obj_get_int(interface_in);
347369
common_hal_usb_core_device_attach_kernel_driver(self, interface);
348370
return mp_const_none;

shared-bindings/usb/core/Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
extern const mp_obj_type_t usb_core_device_type;
1414

1515
bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t device_number);
16+
bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self);
1617
void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self);
1718
uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self);
1819
uint16_t common_hal_usb_core_device_get_idProduct(usb_core_device_obj_t *self);

shared-module/usb/core/Device.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,22 @@ bool common_hal_usb_core_device_construct(usb_core_device_obj_t *self, uint8_t d
4949
return true;
5050
}
5151

52+
bool common_hal_usb_core_device_deinited(usb_core_device_obj_t *self) {
53+
return self->device_address == 0;
54+
}
55+
5256
void common_hal_usb_core_device_deinit(usb_core_device_obj_t *self) {
53-
// TODO: Close all of the endpoints we've opened. Some drivers store state
54-
// for each open endpoint. If we don't close them, then we'll leak memory.
55-
// Waiting for TinyUSB to add this.
57+
if (common_hal_usb_core_device_deinited(self)) {
58+
return;
59+
}
60+
size_t open_size = sizeof(self->open_endpoints);
61+
for (size_t i = 0; i < open_size; i++) {
62+
if (self->open_endpoints[i] != 0) {
63+
tuh_edpt_close(self->device_address, self->open_endpoints[i]);
64+
self->open_endpoints[i] = 0;
65+
}
66+
}
67+
self->device_address = 0;
5668
}
5769

5870
uint16_t common_hal_usb_core_device_get_idVendor(usb_core_device_obj_t *self) {

0 commit comments

Comments
 (0)