Go Down

Topic: Leonardo baud rate for USB CDC (Read 7514 times) previous topic - next topic

mkwired

Can someone explain what Serial.begin(9600); does on the new Leonardo board.  I ask because I need to know how fast I can read data from the USB port.

pylon

It does nothing, as you can see in the source code:

Code: [Select]
void Serial_::begin(uint16_t baud_count)
{
}

mkwired

Has anyone done a speed test?  I have a Leonardo, but I don't have the right cable.

Grumpy_Mike

Quote
I don't have the right cable.

What a USB cable?
What do you mean by a speed test, I could do one for you but I am not sure what you want to measure.

mkwired

#4
Jun 04, 2012, 11:03 pm Last Edit: Jun 04, 2012, 11:22 pm by mkwired Reason: 1

Quote
I don't have the right cable.

What a USB cable?


I don't have a USB cable with the micro connector.


What do you mean by a speed test, I could do one for you but I am not sure what you want to measure.


Something like the following.  It's not my best work.  I need to know how many bytes per second can be transferred through the virtual serial connection.
Code: [Select]

void setup() {
  int start = micros();
  for (int i=1000; i; --i)
     Serial.write('A');
  int end = micros();
 
  float delta = end-start;
  Serial.print(1000 / delta);
  Serial.println(" bytes/second");
}

void loop() {
}

Grumpy_Mike

Well it doesn't produce any output at all.
I don't think Serial.write is supported.

Grumpy_Mike

However this code:-
Code: [Select]
void setup() {
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
    while(digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes low
    delay(50);
  }
   unsigned int start = millis();
   for (int i=0; i<1000; i++)
     Keyboard.print("A");
   unsigned int end = millis();
   
   unsigned int delta = end-start;
   Keyboard.print(1000000.0 / float(delta));
   Keyboard.print(" bytes/second");
// do nothing:
  while(digitalRead(2) == LOW);
  delay(100);
  while(digitalRead(2) == LOW); // get rid of any bounce
  delay(100);
}

When played into TextEdit on my Mac Book Pro gives an answer of:- 252.59 bytes/second.
Which corresponds well to my timing of it with the second hand on my watch.

The actual speed will depend upon the application and the system you are playing it into.

Grumpy_Mike

Changing the print statement to:-
Code: [Select]
Keyboard.println("The quick brown fox jumped over the lazy dog");
and only looping 100 times, with changing the calculation to:-
Code: [Select]
Keyboard.print((45.0 * 100.0 * 1000.0) / float(delta));
I included the LF in the value of 45 characters, gives a report of 254.18 bytes/second.

There is a lot more scrolling going on. I suppose a lot will depend on the speed and size of the input buffer in the receiving application.

pylon

Serial.write is well supported by the Leonardo but it's only usable for the (debugging) connection to the PC. The "standard" serial connection (pins 0 and 1) are on Serial1. You have to have the 1.0.1 version of the IDE though.

Grumpy_Mike

So when I said:-
Quote
I don't think Serial.write is supported.

I was right and it is Serial1.write that is supported.

pylon

Sorry, but I was right: Serial.write is supported:

CDC.cpp, line 191:

Code: [Select]
size_t Serial_::write(uint8_t c)

and line 230:

Code: [Select]
Serial_ Serial;

Grumpy_Mike

So why did nothing come out of the original code?

pylon

Because for the Leonardo you have to wait for the Serial to come up:

Code: [Select]
while (! Serial); // on the Leonardo wait for the Serial to start up

mkwired

The code I posted was untested.  The following works for the Uno and the Leonardo.

Quote

void setup() {
   
#if defined(USBCON)
  while (!Serial) ; // wait for a serial connection to be established
#endif

   Serial.begin(115200); // does nothing on the Leonardo
   uint32_t start = micros();
   for (int i=1000; i; --i)
      Serial.write('.');
   Serial.flush();
   uint32_t end = micros();
   
   uint32_t Bps = 1.0e9 / (end-start);
   Serial.println(""); 
   Serial.print(Bps);
   Serial.println(" bytes/second");
}

void loop() {
}


Grumpy_Mike

It is best if you use code tags not quotes, it is the # next to the quote.
Why
Code: [Select]

Serial.flush();

I can't see it doing anything for you.


Go Up