Skip to content

Commit 1d595fd

Browse files
authored
Workaround for when USB CDC is unplugged (espressif#7583)
* Workaround for when USB CDC is unplugged * Considers default TX timeout Sets back default TX timeout whenever USB is plugged, otherwise it is kept as zero. * fixed left over code
1 parent b2b4bf8 commit 1d595fd

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Diff for: cores/esp32/HWCDC.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ static uint8_t rx_data_buf[64];
3232
static intr_handle_t intr_handle = NULL;
3333
static volatile bool initial_empty = false;
3434
static xSemaphoreHandle tx_lock = NULL;
35-
static uint32_t tx_timeout_ms = 200;
35+
36+
// workaround for when USB CDC is not connected
37+
static uint32_t tx_timeout_ms = 0;
38+
static bool tx_timeout_change_request = false;
39+
3640
static esp_event_loop_handle_t arduino_hw_cdc_event_loop_handle = NULL;
3741

3842
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) {
7276
if (usb_serial_jtag_ll_txfifo_writable() == 1) {
7377
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
7478
usb_serial_jtag_ll_disable_intr_mask(USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
75-
7679
if(!initial_empty){
7780
initial_empty = true;
81+
// First time USB is plugged and the application has not explicitly set TX Timeout, set it to default 100ms.
82+
// Otherwise, USB is still unplugged and the timeout will be kept as Zero in order to avoid any delay in the
83+
// application whenever it uses write() and the TX Queue gets full.
84+
if (!tx_timeout_change_request) {
85+
tx_timeout_ms = 100;
86+
}
7887
//send event?
7988
//ets_printf("CONNECTED\n");
8089
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()
197206

198207
void HWCDC::setTxTimeoutMs(uint32_t timeout){
199208
tx_timeout_ms = timeout;
209+
// it registers that the user has explicitly requested to use a value as TX timeout
210+
// used for the workaround with unplugged USB and TX Queue Full that causes a delay on every write()
211+
tx_timeout_change_request = true;
200212
}
201213

202214
/*

0 commit comments

Comments
 (0)