From d68e3b03b1053a6de0c7eb28f5989132c138364b Mon Sep 17 00:00:00 2001 From: robert-hh Date: Sun, 16 Oct 2022 13:49:03 +0200 Subject: [PATCH] esp32/usb: Add a timeout to usb_tx_strn(). If USB CDC is connected and the board sends data, but the host does not receive the data, the device locks up. This is fixed in this commit by having a timeout of 500ms, after which time the transmission is skipped. --- ports/esp32/usb.c | 3 ++- ports/esp32/usb.h | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index 5a613d2441..953c5422b2 100644 --- a/ports/esp32/usb.c +++ b/ports/esp32/usb.c @@ -88,7 +88,8 @@ void usb_init(void) { void usb_tx_strn(const char *str, size_t len) { // Write out the data to the CDC interface, but only while the USB host is connected. - while (usb_cdc_connected && len) { + uint64_t timeout = esp_timer_get_time() + (uint64_t)(MICROPY_HW_USB_CDC_TX_TIMEOUT * 1000); + while (usb_cdc_connected && len && esp_timer_get_time() < timeout) { size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len); str += l; len -= l; diff --git a/ports/esp32/usb.h b/ports/esp32/usb.h index a103780333..009bf42624 100644 --- a/ports/esp32/usb.h +++ b/ports/esp32/usb.h @@ -26,6 +26,8 @@ #ifndef MICROPY_INCLUDED_ESP32_USB_H #define MICROPY_INCLUDED_ESP32_USB_H +#define MICROPY_HW_USB_CDC_TX_TIMEOUT (500) + void usb_init(void); void usb_tx_strn(const char *str, size_t len);