From 1e4849557d23f3b194f3a056e28f7a8c14dac050 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 27 Oct 2021 20:15:04 +1100 Subject: [PATCH] esp32/usb: Further improve speed of USB CDC output. Following on from ba940250a5b630018c8d9b0e21c5ed858a20450f, the change here makes output about 15 times faster (now up to about 550 kbytes/sec). tinyusb_cdcacm_write_queue will return the number of bytes written, so there's no need to use tud_cdc_n_write_available. Signed-off-by: Damien George --- ports/esp32/usb.c | 29 ++++------------------------- 1 file changed, 4 insertions(+), 25 deletions(-) diff --git a/ports/esp32/usb.c b/ports/esp32/usb.c index 95c8277299..5a613d2441 100644 --- a/ports/esp32/usb.c +++ b/ports/esp32/usb.c @@ -87,34 +87,13 @@ void usb_init(void) { } void usb_tx_strn(const char *str, size_t len) { - // If no HOST is connected, we can exit this early. - if (usb_cdc_connected == 0) { - return; - } - - while (len) { - // Get amount of CDC output buffer space available, making sure - // there is at least one byte available. - size_t avail = tud_cdc_n_write_available(CDC_ITF); - if (avail == 0) { - if (tinyusb_cdcacm_write_flush(CDC_ITF, pdMS_TO_TICKS(1000)) != ESP_OK) { - return; - } - avail = tud_cdc_n_write_available(CDC_ITF); - } - - // Write as much data as possible. - size_t l = len; - if (l > avail) { - l = avail; - } - tud_cdc_n_write(CDC_ITF, (uint8_t *)str, l); + // Write out the data to the CDC interface, but only while the USB host is connected. + while (usb_cdc_connected && len) { + size_t l = tinyusb_cdcacm_write_queue(CDC_ITF, (uint8_t *)str, len); str += l; len -= l; + tud_cdc_n_write_flush(CDC_ITF); } - - // Queue a flush to write out the data in the CDC buffer (if any). - tud_cdc_n_write_flush(CDC_ITF); } #endif // CONFIG_USB_ENABLED