Skip to content

Commit 4766f56

Browse files
committed
esp32: Fix USB deinit/reinit path via soft reset.
Fixes problems with USB-CDC state after soft reset if USBDevice has been active with the default USB-CDC driver also enabled. This also brings ESP32 behaviour in line with RP2 and other ports, where boot.py is executed before the runtime USB device is initialised. This allows setting up a custom USB device in boot.py. There is still a bug here, because calling tud_disconnect() doesn't cause any UNPLUG or BUS_RESET events to arrive from TinyUSB - which means the USB device state stays out of sync until we call mp_usbd_init() again... This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au> Signed-off-by: Angus Gratton <angus@redyak.com.au>
1 parent 8d05a8f commit 4766f56

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

ports/esp32/main.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ void mp_task(void *pvParameter) {
107107
#if MICROPY_HW_ESP_USB_SERIAL_JTAG
108108
usb_serial_jtag_init();
109109
#elif MICROPY_HW_ENABLE_USBDEV
110-
usb_init();
110+
usb_phy_init();
111111
#endif
112112
#if MICROPY_HW_ENABLE_UART_REPL
113113
uart_stdout_init();
@@ -145,6 +145,11 @@ void mp_task(void *pvParameter) {
145145
// run boot-up scripts
146146
pyexec_frozen_module("_boot.py", false);
147147
int ret = pyexec_file_if_exists("boot.py");
148+
149+
#if MICROPY_HW_ENABLE_USBDEV
150+
mp_usbd_init();
151+
#endif
152+
148153
if (ret & PYEXEC_FORCED_EXIT) {
149154
goto soft_reset_exit;
150155
}
@@ -193,7 +198,7 @@ void mp_task(void *pvParameter) {
193198
mp_thread_deinit();
194199
#endif
195200

196-
#if MICROPY_HW_ENABLE_USB_RUNTIME_DEVICE
201+
#if MICROPY_HW_ENABLE_USBDEV
197202
mp_usbd_deinit();
198203
#endif
199204

@@ -219,6 +224,7 @@ void mp_task(void *pvParameter) {
219224

220225
mp_deinit();
221226
fflush(stdout);
227+
222228
goto soft_reset;
223229
}
224230

ports/esp32/usb.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
static usb_phy_handle_t phy_hdl;
4040

41-
void usb_init(void) {
41+
void usb_phy_init(void) {
4242
// ref: https://github.com/espressif/esp-usb/blob/4b6a798d0bed444fff48147c8dcdbbd038e92892/device/esp_tinyusb/tinyusb.c
4343

4444
// Configure USB PHY
@@ -51,10 +51,6 @@ void usb_init(void) {
5151

5252
// Init ESP USB Phy
5353
usb_new_phy(&phy_conf, &phy_hdl);
54-
55-
// Init MicroPython / TinyUSB
56-
mp_usbd_init();
57-
5854
}
5955

6056
#if CONFIG_IDF_TARGET_ESP32S3

ports/esp32/usb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#define MICROPY_HW_USB_CDC_TX_TIMEOUT_MS (500)
3030

31-
void usb_init(void);
31+
void usb_phy_init(void);
3232
void usb_usj_mode(void);
3333

3434
#endif // MICROPY_INCLUDED_ESP32_USB_H

0 commit comments

Comments
 (0)