From 1a5b3e2e0dd0fe5d3baa94915b5f41bff32bf252 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 10 Mar 2022 18:41:07 -0300 Subject: [PATCH 1/3] Fixes USB CDC setRxBufferSize(), begin(), _onRX() --- cores/esp32/USBCDC.cpp | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index a7b75eada4f..985df954148 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -115,11 +115,11 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback size_t USBCDC::setRxBufferSize(size_t rx_queue_len){ if(rx_queue){ + vQueueDelete(rx_queue); + rx_queue = NULL; if(!rx_queue_len){ - vQueueDelete(rx_queue); - rx_queue = NULL; + return 0; } - return 0; } rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t)); if(!rx_queue){ @@ -133,7 +133,8 @@ void USBCDC::begin(unsigned long baud) if(tx_lock == NULL) { tx_lock = xSemaphoreCreateMutex(); } - setRxBufferSize(256);//default if not preset + // if rx_queue was set before begin(), keep it + if (!rx_queue) setRxBufferSize(256); //default if not preset devices[itf] = this; } @@ -246,14 +247,27 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari void USBCDC::_onRX(){ uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1]; uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE); + + if(rx_queue == NULL) { + return; + } + if (uxQueueSpacesAvailable(rx_queue) < count) { + //this VTaskDelay gives, to Arduino's task, time to the CPU do its processing + //without it, data may be lost when the number of bytes received is higher than CDC buffer size + vTaskDelay(10); + } for(uint32_t i=0; i Date: Fri, 11 Mar 2022 12:23:07 -0300 Subject: [PATCH 2/3] Fixes SetRxBufferSize(0) with end() --- cores/esp32/USBCDC.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index 985df954148..2dbfb176e4b 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -114,12 +114,15 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback } size_t USBCDC::setRxBufferSize(size_t rx_queue_len){ - if(rx_queue){ - vQueueDelete(rx_queue); - rx_queue = NULL; - if(!rx_queue_len){ - return 0; - } + size_t currentQueueSize = rx_queue ? + uxQueueSpacesAvailable(rx_queue) + uxQueueMessagesWaiting(rx_queue) : 0; + + if (rx_queue && (!rx_queue_len || rx_queue_len != currentQueueSize)) { + vQueueDelete(rx_queue); + rx_queue = NULL; + } + if(!rx_queue_len || rx_queue_len == currentQueueSize){ + return 0; } rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t)); if(!rx_queue){ From 9e9099e7929b935fe260e061ff5e1bc3b88714de Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Fri, 11 Mar 2022 14:51:16 -0300 Subject: [PATCH 3/3] Fixes reset when 2x call to end() --- cores/esp32/USBCDC.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index 2dbfb176e4b..6c2c69a66f0 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -148,6 +148,7 @@ void USBCDC::end() setRxBufferSize(0); if(tx_lock != NULL) { vSemaphoreDelete(tx_lock); + tx_lock = NULL; } }