Unverified Commit 6014ff43 authored by Rodrigo Garcia's avatar Rodrigo Garcia Committed by GitHub

Fixes USB CDC setRxBufferSize(), begin(), _onRX() (#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
parent 77e95311
...@@ -114,16 +114,42 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback ...@@ -114,16 +114,42 @@ void USBCDC::onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback
} }
size_t USBCDC::setRxBufferSize(size_t rx_queue_len){ size_t USBCDC::setRxBufferSize(size_t rx_queue_len){
if(rx_queue){ size_t currentQueueSize = rx_queue ?
if(!rx_queue_len){ uxQueueSpacesAvailable(rx_queue) + uxQueueMessagesWaiting(rx_queue) : 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_RX_OVERFLOW_EVENT, &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); vQueueDelete(rx_queue);
rx_queue = NULL; rx_queue = NULL;
} }
return 0;
} }
rx_queue = xQueueCreate(rx_queue_len, sizeof(uint8_t));
if(!rx_queue){
return 0;
} }
return rx_queue_len; return rx_queue_len;
} }
...@@ -133,7 +159,8 @@ void USBCDC::begin(unsigned long baud) ...@@ -133,7 +159,8 @@ void USBCDC::begin(unsigned long baud)
if(tx_lock == NULL) { if(tx_lock == NULL) {
tx_lock = xSemaphoreCreateMutex(); 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; devices[itf] = this;
} }
...@@ -144,6 +171,7 @@ void USBCDC::end() ...@@ -144,6 +171,7 @@ void USBCDC::end()
setRxBufferSize(0); setRxBufferSize(0);
if(tx_lock != NULL) { if(tx_lock != NULL) {
vSemaphoreDelete(tx_lock); vSemaphoreDelete(tx_lock);
tx_lock = NULL;
} }
} }
...@@ -244,16 +272,22 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari ...@@ -244,16 +272,22 @@ void USBCDC::_onLineCoding(uint32_t _bit_rate, uint8_t _stop_bits, uint8_t _pari
} }
void USBCDC::_onRX(){ void USBCDC::_onRX(){
arduino_usb_cdc_event_data_t p;
uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1]; uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE+1];
uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE); uint32_t count = tud_cdc_n_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE);
for(uint32_t i=0; i<count; i++){ for(uint32_t i=0; i<count; i++){
if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 0)){ if(rx_queue == NULL || !xQueueSend(rx_queue, buf+i, 10)) {
return; p.rx_overflow.dropped_bytes = count - i;
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.");
count = i;
break;
} }
} }
arduino_usb_cdc_event_data_t p; if (count) {
p.rx.len = count; p.rx.len = count;
arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY); arduino_usb_event_post(ARDUINO_USB_CDC_EVENTS, ARDUINO_USB_CDC_RX_EVENT, &p, sizeof(arduino_usb_cdc_event_data_t), portMAX_DELAY);
}
} }
void USBCDC::_onTX(){ void USBCDC::_onTX(){
......
...@@ -33,6 +33,7 @@ typedef enum { ...@@ -33,6 +33,7 @@ typedef enum {
ARDUINO_USB_CDC_LINE_CODING_EVENT, ARDUINO_USB_CDC_LINE_CODING_EVENT,
ARDUINO_USB_CDC_RX_EVENT, ARDUINO_USB_CDC_RX_EVENT,
ARDUINO_USB_CDC_TX_EVENT, ARDUINO_USB_CDC_TX_EVENT,
ARDUINO_USB_CDC_RX_OVERFLOW_EVENT,
ARDUINO_USB_CDC_MAX_EVENT, ARDUINO_USB_CDC_MAX_EVENT,
} arduino_usb_cdc_event_t; } arduino_usb_cdc_event_t;
...@@ -50,6 +51,9 @@ typedef union { ...@@ -50,6 +51,9 @@ typedef union {
struct { struct {
size_t len; size_t len;
} rx; } rx;
struct {
size_t dropped_bytes;
} rx_overflow;
} arduino_usb_cdc_event_data_t; } arduino_usb_cdc_event_data_t;
class USBCDC: public Stream class USBCDC: public Stream
......
...@@ -34,6 +34,7 @@ Where ``event`` can be: ...@@ -34,6 +34,7 @@ Where ``event`` can be:
* ARDUINO_USB_CDC_LINE_CODING_EVENT * ARDUINO_USB_CDC_LINE_CODING_EVENT
* ARDUINO_USB_CDC_RX_EVENT * ARDUINO_USB_CDC_RX_EVENT
* ARDUINO_USB_CDC_TX_EVENT * ARDUINO_USB_CDC_TX_EVENT
* ARDUINO_USB_CDC_RX_OVERFLOW_EVENT
* ARDUINO_USB_CDC_MAX_EVENT * ARDUINO_USB_CDC_MAX_EVENT
setRxBufferSize setRxBufferSize
......
...@@ -73,6 +73,9 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve ...@@ -73,6 +73,9 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
} }
HWSerial.println(); HWSerial.println();
break; break;
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
break;
default: default:
break; break;
......
...@@ -52,6 +52,9 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve ...@@ -52,6 +52,9 @@ static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t eve
} }
HWSerial.println(); HWSerial.println();
break; break;
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
break;
default: default:
break; break;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment