Improving Arduino Leonardo USB CDC Baudrate and Data transfer.

Hi Everyone,
ATmega32u4 can provide 12Mbps Baudrate; however current configuration of Arduino Leonardo is not up to the scratch!

I have tested transferring 128Bytes of data from PC to Arduino Leonardo via COM port and received the data in approx. 0.466ms, which gives Baudrate of 10*128Byte/0.466ms = 2.76Mbps

Here goes the code;

char buf[128];
int count=0;

void setup() {
 delay(9000);
//   DDRD = DDRD | B11110011;
 pinMode(2, OUTPUT); 
  Serial.begin(9600);
  while (!Serial) { ;}
}

void loop() {
 
  digitalWrite(2,LOW);
  while (Serial.available()){
  digitalWrite(2,HIGH);
  for (count=0; count < 128; count++) {
    buf[count] = Serial.read();
  } 
  digitalWrite(2,LOW);
}
delay(1000);
}}

and the Timing

Question is: How to improve this rate at-least 2 or 3 fold?

  while (Serial.available()){
  digitalWrite(2,HIGH);
  for (count=0; count < 128; count++) {
    buf[count] = Serial.read();
  }

If there is at least one byte to read, read all 128 of them. Useless crap!

  Serial.begin(9600);

It doesn't take a degree in rocket science to figure out how to improve the baud rate.

Come on Paul... what do you drink Tea/Coffee?

Here reading all the bytes together with Serial.readBytes()

  char buf[128];
  int count=0;
  
void setup() {
  delay(9000);
//   DDRD = DDRD | B11110011;
pinMode(2, OUTPUT); 
  Serial.begin(0);
  while (!Serial) { ;}
}

void loop() {
 
digitalWrite(2,LOW);
while (Serial.available()){
  digitalWrite(2,HIGH);
  Serial.readBytes(buf,128);
//  for (count=0; count < 128; count++) {
//   buf[count] = Serial.read();
//  } 
  digitalWrite(2,LOW);
}
delay(1000);
}

Its gone even worse to 1.68ms than 0.466ms previously!!

BTWay Serial.begin(Any integer goes); for Arduino Leonardo

How about using flush()??

PaulS has some points here.

your code shows that the Arduino can read at 2.7 Mbps in a tight loop. If data would come in much faster you would not be able to read it probably.

You might have a look at the teensy (implementation) - which has a really fast USB port

Found this from Device Manager..

Wondering Arduino Leonardo Serial.read() is Interrupt or Bulk transfer type?
And how to increase the packat size to 128Bytes? Can this increase the Data received per 1ms interval??

robtillaart:
PaulS has some points here.

Thanks...I realized now....but just read other posts on this forum and Paul seems to in Attacking mode today! :zipper_mouth_face:

and Paul seems to in Attacking mode today

I'm only attacking code that contains dumb assumptions.

Wondering Arduino Leonardo Serial.read() is Interrupt or Bulk transfer type?

This question is meaningless. It is neither. The Serial.read() function gets the next character from the incoming serial data buffer. How the data gets into that buffer may be Interrupt or Bulk Transfer, but how it gets out is neither.

robtillaart:
You might have a look at the teensy (implementation) - which has a really fast USB port

Would love to but Teensy is not opensource and can't find the code in readable format....
BTWay we are Team Arduino!!

PaulS:

Wondering Arduino Leonardo Serial.read() is Interrupt or Bulk transfer type?

This question is meaningless. It is neither. The Serial.read() function gets the next character from the incoming serial data buffer. How the data gets into that buffer may be Interrupt or Bulk Transfer, but how it gets out is neither.

Agree..but how to increase packet size? I tried in CDC.cpp but it has no effect what so ever... =( =(
any suggestion?

but how to increase packet size?

The packet size of what? What are you trying to do?

Paul,
This is my understanding....
Packet size of Data received by Arduino.
vMaxPacketSize: Its the amount of Byte received (don't know which one is for Host PC or Arduino) for a specific Interval defined by: bInterval in ms.

Meaning in Interrupt mode: 16byte can be transferred in 64ms
another is Interrupt mode: 64byte can be transferred in 1ms
then Bulk mode: 64byte can be transferred in no interval??

So which mode or interval Arduino Leonardo is using for communication, when the data is transferred from PC to Arduino via Serial port??