diff --git a/cores/esp32/HWCDC.cpp b/cores/esp32/HWCDC.cpp index 8912fed9330..25744f0b0a3 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 = 200; + +// 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,14 @@ 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(!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 +206,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; } /*