Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6014ff4

Browse filesBrowse files
authored
Fixes USB CDC setRxBufferSize(), begin(), _onRX() (espressif#6413)
* Fixes USB CDC setRxBufferSize(), begin(), _onRX() * Fixes SetRxBufferSize(0) with end() * Fixes reset when 2x call to end() * Adds RX_OVERFLOW_EVENT and Queue Copy in setBufferSize * changed event name to ARDUINO_USB_CDC_RX_OVERFLOW_EVENT
1 parent 77e9531 commit 6014ff4
Copy full SHA for 6014ff4

File tree

Expand file treeCollapse file tree

5 files changed

+62
-17
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+62
-17
lines changed

‎cores/esp32/USBCDC.cpp

Copy file name to clipboardExpand all lines: cores/esp32/USBCDC.cpp
+49-15Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,42 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
114114
}
115115

116116
size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
117-
if(rx_queue){
118-
if(!rx_queue_len){
119-
vQueueDelete(rx_queue);
120-
rx_queue = NULL;
117+
size_t currentQueueSize = rx_queue ?
118+
uxQueueSpacesAvailable(rx_queue) + uxQueueMessagesWaiting(rx_queue) : 0;
119+
120+
if (rx_queue_len != currentQueueSize) {
121+
xQueueHandle new_rx_queue = NULL;
122+
if (rx_queue_len) {
123+
new_rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
124+
if(!new_rx_queue){
125+
log_e("CDC Queue creation failed.");
126+
return 0;
127+
}
128+
if (rx_queue) {
129+
size_t copySize = uxQueueMessagesWaiting(rx_queue);
130+
if (copySize > 0) {
131+
for(size_t i = 0; i < copySize; i++) {
132+
uint8_t ch = 0;
133+
xQueueReceive(rx_queue, &ch, 0);
134+
if (!xQueueSend(new_rx_queue, &ch, 0)) {
135+
arduino_usb_cdc_event_data_t p;
136+
p.rx_overflow.dropped_bytes = copySize - i;
137+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_OVERFLOW_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
138+
log_e("CDC RX Overflow.");
139+
break;
140+
}
141+
}
142+
}
143+
vQueueDelete(rx_queue);
144+
}
145+
rx_queue = new_rx_queue;
146+
return rx_queue_len;
147+
} else {
148+
if (rx_queue) {
149+
vQueueDelete(rx_queue);
150+
rx_queue = NULL;
151+
}
121152
}
122-
return 0;
123-
}
124-
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
125-
if(!rx_queue){
126-
return 0;
127153
}
128154
return rx_queue_len;
129155
}
@@ -133,7 +159,8 @@ void USBCDC::begin(unsigned long baud)
133159
if(tx_lock == NULL) {
134160
tx_lock = xSemaphoreCreateMutex();
135161
}
136-
setRxBufferSize(256);//default if not preset
162+
// if rx_queue was set before begin(), keep it
163+
if (!rx_queue) setRxBufferSize(256); //default if not preset
137164
devices[itf] = this;
138165
}
139166

@@ -144,6 +171,7 @@ void USBCDC::end()
144171
setRxBufferSize(0);
145172
if(tx_lock != NULL) {
146173
vSemaphoreDelete(tx_lock);
174+
tx_lock = NULL;
147175
}
148176
}
149177

@@ -244,16 +272,22 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari
244272
}
245273

246274
void USBCDC::_onRX(){
275+
arduino_usb_cdc_event_data_t p;
247276
uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1];
248277
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
249278
for(uint32_t i=0; i<count; i++){
250-
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 0)){
251-
return;
279+
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 10)) {
280+
p.rx_overflow.dropped_bytes = count - i;
281+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_OVERFLOW_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
282+
log_e("CDC RX Overflow.");
283+
count = i;
284+
break;
252285
}
253286
}
254-
arduino_usb_cdc_event_data_t p;
255-
p.rx.len = count;
256-
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
287+
if (count) {
288+
p.rx.len = count;
289+
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
290+
}
257291
}
258292

259293
void USBCDC::_onTX(){

‎cores/esp32/USBCDC.h

Copy file name to clipboardExpand all lines: cores/esp32/USBCDC.h
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef enum {
3333
ARDUINO_USB_CDC_LINE_CODING_EVENT,
3434
ARDUINO_USB_CDC_RX_EVENT,
3535
ARDUINO_USB_CDC_TX_EVENT,
36+
ARDUINO_USB_CDC_RX_OVERFLOW_EVENT,
3637
ARDUINO_USB_CDC_MAX_EVENT,
3738
} arduino_usb_cdc_event_t;
3839

@@ -50,6 +51,9 @@ typedef union {
5051
struct {
5152
size_t len;
5253
} rx;
54+
struct {
55+
size_t dropped_bytes;
56+
} rx_overflow;
5357
} arduino_usb_cdc_event_data_t;
5458

5559
class USBCDC: public Stream

‎docs/source/api/usb_cdc.rst

Copy file name to clipboardExpand all lines: docs/source/api/usb_cdc.rst
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Where ``event`` can be:
3434
* ARDUINO_USB_CDC_LINE_CODING_EVENT
3535
* ARDUINO_USB_CDC_RX_EVENT
3636
* ARDUINO_USB_CDC_TX_EVENT
37+
* ARDUINO_USB_CDC_RX_OVERFLOW_EVENT
3738
* ARDUINO_USB_CDC_MAX_EVENT
3839

3940
setRxBufferSize

‎libraries/USB/examples/CompositeDevice/CompositeDevice.ino

Copy file name to clipboardExpand all lines: libraries/USB/examples/CompositeDevice/CompositeDevice.ino
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
7373
}
7474
HWSerial.println();
7575
break;
76-
76+
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
77+
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
78+
break;
79+
7780
default:
7881
break;
7982
}

‎libraries/USB/examples/USBSerial/USBSerial.ino

Copy file name to clipboardExpand all lines: libraries/USB/examples/USBSerial/USBSerial.ino
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
5252
}
5353
HWSerial.println();
5454
break;
55-
55+
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
56+
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
57+
break;
58+
5659
default:
5760
break;
5861
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.