Pages: [1] 2 3   Go Down
Author Topic: Leonardo baud rate for USB CDC  (Read 6308 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
void Serial_::begin(uint16_t baud_count)
{
}
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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() {
}
« Last Edit: June 04, 2012, 04:22:15 pm by mkwired » Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

However this code:-
Code:
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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Changing the print statement to:-
Code:
Keyboard.println("The quick brown fox jumped over the lazy dog");
and only looping 100 times, with changing the calculation to:-
Code:
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.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So when I said:-
Quote
I don't think Serial.write is supported.
I was right and it is Serial1.write that is supported.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

CDC.cpp, line 191:

Code:
size_t Serial_::write(uint8_t c)

and line 230:

Code:
Serial_ Serial;
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So why did nothing come out of the original code?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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() {
}

Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34243
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is best if you use code tags not quotes, it is the # next to the quote.
Why
Code:
Serial.flush();
I can't see it doing anything for you.

Logged

Pages: [1] 2 3   Go Up
Jump to: