Pages: 1 [2] 3   Go Down
Author Topic: Leonardo baud rate for USB CDC  (Read 6442 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

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.

Because on the Uno, Serial.write is non-blocking.
Logged

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

Quote
It is best if you use code tags not quotes, it is the # next to the quote.
Why
Because the box is smaller, it scrolls, and the forum doesn't scramble your code up so people can copy and paste the code and reproduce your problems.
 
And mainly because that is what people want around here and it show common decency to comply. After all you are the one asking the questions.

Quote
Because on the Uno, Serial.write is non-blocking
Only to the extent of not having a full buffer.
Logged

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

It is best if you use code tags not quotes, it is the # next to the quote.
Why
Because the box is smaller, it scrolls, and the forum doesn't scramble your code up so people can copy and paste the code and reproduce your problems.
 
And mainly because that is what people want around here and it show common decency to comply. After all you are the one asking the questions.

Quote within quote confusion... I didn't ask why.  I'm not trying to cause a problem.

Quote
Because on the Uno, Serial.write is non-blocking
Only to the extent of not having a full buffer.

True but not calling Serial.flush wouldn't show how fast bytes were being transferred out of the chip which was my goal.
Logged

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

Ok that last code as poster reports:-
Quote
39258 bytes/second
On a Leonardo.
Logged

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

Ok that last code as poster reports:-
Quote
39258 bytes/second
On a Leonardo.

It seems like the rate is highly dependent on the computer where it wasn't on the Uno.
Logged

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

That is no surprise is it?

It is still a USB device and that get polled at a maximum of 1KHz, so I guess it is down to how fast the host computer empties the buffer. Allowing more bytes to be transferred.
Logged

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

That is no surprise is it?

No.

It is still a USB device and that get polled at a maximum of 1KHz, so I guess it is down to how fast the host computer empties the buffer. Allowing more bytes to be transferred.

Where does the 1kHz come from?
Logged

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

Quote
Where does the 1kHz come from?
The polling rate of a USB HID device.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
blink. blink. blink. blink. blink.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That 1 ms polling rate is true only for HID (the keyboard and mouse) which use the USB interrupt transfer type.  CDC (which Leonardo refers to as Serial) uses the USB bulk transfer type which doesn't guarantee timing but does guarantee that it will transfer the data as fast as possible.  That "fast as possible" depends on what else is happening on the bus AND on the host computer.

In practice this does lead to different maximum transfer speeds for Serial on different computers (and even on the same computer under different workloads).  I don't have a board with me right now but, from memory, you can usually assume at least several tens of kb per second, up to a few hundreds of kbps.

It's not necessary to call Serial.begin() for the Leonardo since the CDC device is initialized elsewhere.  The stub was retained for backwards-compatibility. 

Serial (which talks CDC over USB to the host) and Serial1 (which talks to pins 0 and 1 via the UART) are both definitely available and usable on the Leonardo.
Logged

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

zeveland, I noticed much of the USB code for Arduino came from you.  Can you say how you learned to write "USB code?"  I read part of one book on the topic, but the material wasn't well written and as a result difficult to understand.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
blink. blink. blink. blink. blink.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

mkwired, sorry - had overlooked your message.  I can't take much credit - most of the implementation was done by a brilliant guy named Peter Barrett. 

Almost everything I know about USB has come from working with Dean Camera's LUFA project (http://fourwalledcubicle.com/LUFA.php), reading Jan Axelson's USB Complete book, and reading the specs until my eyes bled.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 229
Intransigent prototyper
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found this interesting:

It's not necessary to call Serial.begin() for the Leonardo since the CDC device is initialized elsewhere.  The stub was retained for backwards-compatibility. 

.. since it seems to say that the serial speed for the USB CDC is independent of whatever code you write. I'm inferring that since you don't need to open the port with a speed (e.g. Serial.begin(115200))

Does that sound right?

Which leads me to my second question:

Q: Is the serial monitor in the Arduino IDE monitoring the USB CDC serial port or the hardware serial port?

Thanks for any insights!

Daniel
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Q: Is the serial monitor in the Arduino IDE monitoring the USB CDC serial port or the hardware serial port?

It's the USB CDC (unless you connected a USB2Serial to the hardware serial port and to the PC too).

Quote
.. since it seems to say that the serial speed for the USB CDC is independent of whatever code you write. I'm inferring that since you don't need to open the port with a speed (e.g. Serial.begin(115200))

Does that sound right?

As you can see in the implementation:

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

... this is correct.
Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 229
Intransigent prototyper
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the clarification!

The documentation on serial ports fir the Leonardo is a bit lacking.

When you open the serial monitor, and are monitoring the USB(CDC) connection, does the baud rate pulldown have any effect?

D
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 111
Posts: 5228
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
When you open the serial monitor, and are monitoring the USB(CDC) connection, does the baud rate pulldown have any effect?

Theoretically yes, but it doesn't change any relevant parameter, it just sets some values in the driver on the PC. The counterpart on the Leonardo doesn't react on it.
Logged

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