From 1a5b3e2e0dd0fe5d3baa94915b5f41bff32bf252 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 10 Mar 2022 18:41:07 -0300 Subject: [PATCH 1/5] 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/5] 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/5] 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; } } From 8044a3810048e0175c4f45d850ef0652a03c1315 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Thu, 17 Mar 2022 10:14:10 -0300 Subject: [PATCH 4/5] Adds RX_OVERFLOW_EVENT and Queue Copy in setBufferSize --- cores/esp32/USBCDC.cpp | 60 ++++++++++++------- cores/esp32/USBCDC.h | 4 ++ docs/source/api/usb_cdc.rst | 1 + .../CompositeDevice/CompositeDevice.ino | 5 +- .../USB/examples/USBSerial/USBSerial.ino | 5 +- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index 6c2c69a66f0..d4aabf45b6e 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -117,16 +117,39 @@ size_t USBCDC::setRxBufferSize(size_t rx_queue_len){ 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){ - return 0; + if (rx_queue_len != currentQueueSize) { + xQueueHandle new_rx_queue = NULL; + if (rx_queue_len) { + new_rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t)); + if(!new_rx_queue){ + log_e("CDC Queue creation failed."); + return 0; + } + if (rx_queue) { + size_t copySize = uxQueueMessagesWaiting(rx_queue); + if (copySize > 0) { + for(size_t i = 0; i < copySize; i++) { + uint8_t ch = 0; + xQueueReceive(rx_queue, &ch, 0); + if (!xQueueSend(new_rx_queue, &ch, 0)) { + arduino_usb_cdc_event_data_t p; + p.rx_overflow.dropped_bytes = copySize - i; + arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_OVERFLOW, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY); + log_e("CDC RX Overflow."); + break; + } + } + } + vQueueDelete(rx_queue); + } + rx_queue = new_rx_queue; + return rx_queue_len; + } else { + if (rx_queue) { + vQueueDelete(rx_queue); + rx_queue = NULL; + } + } } return rx_queue_len; } @@ -249,26 +272,19 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari } void USBCDC::_onRX(){ + arduino_usb_cdc_event_data_t p; 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; irx_overflow.dropped_bytes); + break; + default: break; } diff --git a/libraries/USB/examples/USBSerial/USBSerial.ino b/libraries/USB/examples/USBSerial/USBSerial.ino index bb23418f507..77f29d7bec4 100644 --- a/libraries/USB/examples/USBSerial/USBSerial.ino +++ b/libraries/USB/examples/USBSerial/USBSerial.ino @@ -52,7 +52,10 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve } HWSerial.println(); break; - + case ARDUINO_USB_CDC_OVERFLOW: + HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes); + break; + default: break; } From d9938092f638a539a8c43e1dddc5b2b069cbd0d3 Mon Sep 17 00:00:00 2001 From: Rodrigo Garcia Date: Sat, 19 Mar 2022 09:32:09 -0300 Subject: [PATCH 5/5] changed event name to ARDUINO_USB_CDC_RX_OVERFLOW_EVENT --- cores/esp32/USBCDC.cpp | 4 ++-- cores/esp32/USBCDC.h | 2 +- docs/source/api/usb_cdc.rst | 2 +- libraries/USB/examples/CompositeDevice/CompositeDevice.ino | 2 +- libraries/USB/examples/USBSerial/USBSerial.ino | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cores/esp32/USBCDC.cpp b/cores/esp32/USBCDC.cpp index d4aabf45b6e..a7b675529d3 100644 --- a/cores/esp32/USBCDC.cpp +++ b/cores/esp32/USBCDC.cpp @@ -134,7 +134,7 @@ size_t USBCDC::setRxBufferSize(size_t rx_queue_len){ if (!xQueueSend(new_rx_queue, &ch, 0)) { arduino_usb_cdc_event_data_t p; p.rx_overflow.dropped_bytes = copySize - i; - arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_OVERFLOW, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY); + arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_OVERFLOW_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY); log_e("CDC RX Overflow."); break; } @@ -278,7 +278,7 @@ void USBCDC::_onRX(){ for(uint32_t i=0; irx_overflow.dropped_bytes); break; diff --git a/libraries/USB/examples/USBSerial/USBSerial.ino b/libraries/USB/examples/USBSerial/USBSerial.ino index 77f29d7bec4..bb7a0768ff4 100644 --- a/libraries/USB/examples/USBSerial/USBSerial.ino +++ b/libraries/USB/examples/USBSerial/USBSerial.ino @@ -52,7 +52,7 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve } HWSerial.println(); break; - case ARDUINO_USB_CDC_OVERFLOW: + case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT: HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes); break;