Page 1 of 1

ESP32 native USB transfert array data very slow

Posted: Thu Feb 16, 2023 8:43 am
by Thierry44800
Hi all,
I am new on this forum,
and I want use ESP32 S3 with native USB to transfert Data from ESP to a computer and/or a Smartphone.

My problem is : transfering 3076 bytes is very very slow (around 1400 ms !).
I have configured the serial port at 115200 bauds (in peripherals settings) and in my application 115200 bauds.
I have tested on a Windows computer and on an Android Smartphone and I have the same time.
When I use WIFI, this operation take arround 20-30 ms (good time).


This is my code.
I use the example "USB serial" on Arduino IDE with a little modification for send 3076 bytes.
I use a loop of 400 bytes, because more bytes (1000, 2000, 3076) crash :


#include "USB.h"
USBCDC USBSerial;
uint8_t frameTT[3076];
uint8_t Mybuffer[400];
int BufferMax = 3076;
int BufferSendPosition = 0;


static void usbEventCallback(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data){
if(event_base == ARDUINO_USB_EVENTS){
arduino_usb_event_data_t * data = (arduino_usb_event_data_t*)event_data;
switch (event_id){
case ARDUINO_USB_STARTED_EVENT:
//HWSerial.println("USB PLUGGED");
break;
case ARDUINO_USB_STOPPED_EVENT:
//HWSerial.println("USB UNPLUGGED");
break;
case ARDUINO_USB_SUSPEND_EVENT:
//HWSerial.printf("USB SUSPENDED: remote_wakeup_en: %u\n", data->suspend.remote_wakeup_en);
break;
case ARDUINO_USB_RESUME_EVENT:
//HWSerial.println("USB RESUMED");
break;
default:
break;
}
} else if(event_base == ARDUINO_USB_CDC_EVENTS){
arduino_usb_cdc_event_data_t * data = (arduino_usb_cdc_event_data_t*)event_data;
switch (event_id){
case ARDUINO_USB_CDC_CONNECTED_EVENT:
//HWSerial.println("CDC CONNECTED");
break;
case ARDUINO_USB_CDC_DISCONNECTED_EVENT:
//HWSerial.println("CDC DISCONNECTED");
break;
case ARDUINO_USB_CDC_LINE_STATE_EVENT:
//HWSerial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts);
break;
case ARDUINO_USB_CDC_LINE_CODING_EVENT:
//HWSerial.printf("CDC LINE CODING: bit_rate: %u, data_bits: %u, stop_bits: %u, parity: %u\n", data->line_coding.bit_rate, data->line_coding.data_bits, data->line_coding.stop_bits, data->line_coding.parity);
break;
case ARDUINO_USB_CDC_RX_EVENT:
//HWSerial.printf("CDC RX [%u]:", data->rx.len);
{
char buf[data->rx.len];
size_t len = USBSerial.read(buf, data->rx.len);
//HWSerial.write(buf, len);
String randomString(buf);
String NewString = randomString.substring(0,len);
if (NewString.startsWith("SENDDATA"))
{
int Size = 0;
if ((BufferSendPosition + 400) > BufferMax) {Size = (BufferMax - BufferSendPosition);}
else {Size = 400;}
copyPartArray(BufferSendPosition, Size);
USBSerial.write((const uint8_t*)&Mybuffer, Size);
BufferSendPosition = BufferSendPosition + 400;
if (BufferSendPosition > BufferMax) {BufferSendPosition = 0;}
}
}
//HWSerial.println();
break;
case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT:
//HWSerial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes);
break;
default:
break;
}
}
}


void setup() {
uint8_t t1 = 55;
uint8_t t2 = 54;
uint8_t t3 = 53;
uint8_t t4 = 52;
for (long w=0; w<3076; w=w+4) {
frameTT[w] = t1;
frameTT[w+1] = t2;
frameTT[w+2] = t3;
frameTT[w+3] = t4;
}
USB.onEvent(usbEventCallback);
USBSerial.onEvent(usbEventCallback);
USBSerial.begin();
USB.begin();
}

void loop() {
while (1) {delay(10);}
}


void copyPartArray(int start, int len) {
int j=0;
for (int i=start; i<(start+len); i++) {
Mybuffer[j] = frameTT;
j++;
}
}


Have you a solution and/or an example code for transfering quickly data (around 4000 bytes) ?
Idealy, I want transfert at 2000000 bauds.
thanks in advance.