@@ -115,11 +115,11 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
115
115
116
116
size_t USBCDC::setRxBufferSize (size_t rx_queue_len){
117
117
if (rx_queue){
118
+ vQueueDelete (rx_queue);
119
+ rx_queue = NULL ;
118
120
if (!rx_queue_len){
119
- vQueueDelete (rx_queue);
120
- rx_queue = NULL ;
121
+ return 0 ;
121
122
}
122
- return 0 ;
123
123
}
124
124
rx_queue = xQueueCreate (rx_queue_len, sizeof (uint8_t ));
125
125
if (!rx_queue){
@@ -133,7 +133,8 @@ void USBCDC::begin(unsigned long baud)
133
133
if (tx_lock == NULL ) {
134
134
tx_lock = xSemaphoreCreateMutex ();
135
135
}
136
- setRxBufferSize (256 );// default if not preset
136
+ // if rx_queue was set before begin(), keep it
137
+ if (!rx_queue) setRxBufferSize (256 ); // default if not preset
137
138
devices[itf] = this ;
138
139
}
139
140
@@ -246,14 +247,27 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari
246
247
void USBCDC::_onRX (){
247
248
uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1 ];
248
249
uint32_t count = tud_cdc_n_read (itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
250
+
251
+ if (rx_queue == NULL ) {
252
+ return ;
253
+ }
254
+ if (uxQueueSpacesAvailable (rx_queue) < count) {
255
+ // this VTaskDelay gives, to Arduino's task, time to the CPU do its processing
256
+ // without it, data may be lost when the number of bytes received is higher than CDC buffer size
257
+ vTaskDelay (10 );
258
+ }
249
259
for (uint32_t i=0 ; i<count; i++){
250
- if (rx_queue == NULL || !xQueueSend (rx_queue, buf+i, 0 )){
251
- return ;
260
+ if (!xQueueSend (rx_queue, buf+i, 0 )){
261
+ // rx_queue overflow - data will be lost
262
+ count = i;
263
+ break ;
252
264
}
253
265
}
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);
266
+ if (count) {
267
+ arduino_usb_cdc_event_data_t p;
268
+ p.rx .len = count;
269
+ arduino_usb_event_post (ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof (arduino_usb_cdc_event_data_t ), portMAX_DELAY);
270
+ }
257
271
}
258
272
259
273
void USBCDC::_onTX (){
0 commit comments