From f3ed69169b47fb5543ed4be1b4d9d5be1239994c Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Mon, 12 Dec 2022 15:17:38 -0300 Subject: [PATCH 1/3] Workaround for when USB CDC is unplugged --- cores/esp32/HWCDC.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 8912fed9330..a8025f879c5 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -32,7 +32,7 @@ static uint8_t rx_data_buf[64]; static intr_handle_t intr_handle = NULL; static volatile bool initial_empty = false; static xSemaphoreHandle tx_lock = NULL; -static uint32_t tx_timeout_ms = 200; +static uint32_t tx_timeout_ms = 0; // workaround for when USB CDC is not connected static esp_event_loop_handle_t arduino_hw_cdc_event_loop_handle = NULL; static esp_err_t arduino_hw_cdc_event_post(esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, BaseType_t *task_unblocked){ From 88f7a5ec0be8c6b2930516d4bcee1a2d9f7635a0 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 13 Dec 2022 19:06:48 -0300 Subject: [PATCH 2/3] Considers default TX timeout Sets back default TX timeout whenever USB is plugged, otherwise it is kept as zero. --- cores/esp32/HWCDC.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index a8025f879c5..1e0ce3bd21c 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -32,7 +32,11 @@ static uint8_t rx_data_buf[64]; static intr_handle_t intr_handle = NULL; static volatile bool initial_empty = false; static xSemaphoreHandle tx_lock = NULL; -static uint32_t tx_timeout_ms = 0; // workaround for when USB CDC is not connected + +// workaround for when USB CDC is not connected +static uint32_t tx_timeout_ms = 0; +static bool tx_timeout_change_request = false; + static esp_event_loop_handle_t arduino_hw_cdc_event_loop_handle = NULL; static esp_err_t arduino_hw_cdc_event_post(esp_event_base_t event_base, int32_t event_id, void *event_data, size_t event_data_size, BaseType_t *task_unblocked){ @@ -72,9 +76,15 @@ static void hw_cdc_isr_handler(void *arg) { if (usb_serial_jtag_ll_txfifo_writable() == 1) { // We disable the interrupt here so that the interrupt won't be triggered if there is no data to send. usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY); - + if ( if(!initial_empty){ initial_empty = true; + // First time USB is plugged and the application has not explicitly set TX Timeout, set it to default 100ms. + // Otherwise, USB is still unplugged and the timeout will be kept as Zero in order to avoid any delay in the + // application whenever it uses write() and the TX Queue gets full. + if (!tx_timeout_change_request) { + tx_timeout_ms = 100; + } //send event? //ets_printf("CONNECTED\n"); arduino_hw_cdc_event_post(ARDUINO_HW_CDC_EVENTS, ARDUINO_HW_CDC_CONNECTED_EVENT, &event, sizeof(arduino_hw_cdc_event_data_t), &xTaskWoken); @@ -197,6 +207,9 @@ void HWCDC::end() void HWCDC::setTxTimeoutMs(uint32_t timeout){ tx_timeout_ms = timeout; + // it registers that the user has explicitly requested to use a value as TX timeout + // used for the workaround with unplugged USB and TX Queue Full that causes a delay on every write() + tx_timeout_change_request = true; } /* From f8ed7eb38176963da317cd15fa262432085f7fd3 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Tue, 13 Dec 2022 19:12:35 -0300 Subject: [PATCH 3/3] fixed left over code --- cores/esp32/HWCDC.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 1e0ce3bd21c..25744f0b0a3 100644 --- a/cores/esp32/HWCDC.cpp +++ b/cores/esp32/HWCDC.cpp @@ -76,7 +76,6 @@ static void hw_cdc_isr_handler(void *arg) { if (usb_serial_jtag_ll_txfifo_writable() == 1) { // We disable the interrupt here so that the interrupt won't be triggered if there is no data to send. usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY); - if ( if(!initial_empty){ initial_empty = true; // First time USB is plugged and the application has not explicitly set TX Timeout, set it to default 100ms.