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.
This commit is contained in:
robert-hh 2022-10-16 13:49:03 +02:00 committed by Damien George
parent 5d473d3093
commit d68e3b03b1
2 changed files with 4 additions and 1 deletions

View File

@ -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;

View File

@ -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);