@@ -114,16 +114,42 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
114114}
115115
116116size_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
246274void 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
259293void USBCDC::_onTX (){
0 commit comments