@@ -32,7 +32,11 @@ static uint8_t rx_data_buf[64];
32
32
static intr_handle_t intr_handle = NULL ;
33
33
static volatile bool initial_empty = false ;
34
34
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
+
36
40
static esp_event_loop_handle_t arduino_hw_cdc_event_loop_handle = NULL ;
37
41
38
42
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) {
72
76
if (usb_serial_jtag_ll_txfifo_writable () == 1 ) {
73
77
// We disable the interrupt here so that the interrupt won't be triggered if there is no data to send.
74
78
usb_serial_jtag_ll_disable_intr_mask (USB_SERIAL_JTAG_INTR_SERIAL_IN_EMPTY);
75
-
76
79
if (!initial_empty){
77
80
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
+ }
78
87
// send event?
79
88
// ets_printf("CONNECTED\n");
80
89
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()
197
206
198
207
void HWCDC::setTxTimeoutMs (uint32_t timeout){
199
208
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 ;
200
212
}
201
213
202
214
/*
0 commit comments