Skip to content

Commit

Permalink
Workaround for when USB CDC is unplugged (espressif#7583)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
SuGlider authored and Human committed Dec 17, 2022
1 parent 85b65b4 commit 01e2bf2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions cores/esp32/HWCDC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

/*
Expand Down

0 comments on commit 01e2bf2

Please sign in to comment.