26
Jan
2015

Raspberry Pi: Increase UART speed

The Raspberry Pi has an on-board UART port attached to the GPIO pins. This is useful if you want to send serial data out through a UART to another device (such as a PC etc). The Pi comes shipped with the UART set to 115200 baud (or 14.4 kB/s), which is “fast”, but we can go faster!

I will be using a logic analyser to determine the maximum possible baud rate that the Pi can reliably output. I’ll be sending out a capital “U”, which corresponds to 85 in ASCII, which then sends 1010101 to the output, looking like a square wave:

echo -n U > /dev/ttyAMA0

Screenshot - 250115 - 23:31:16

The frequency recorded for this square wave was 57.62 kHz. The UART speed was also reported as 115200 by the logic analyser software. The letter U was also decoded properly. In the following captures, measurements are taken and displayed just above the waveform. “W” means how long the character took to send, “f” means the number of characters per second that can be sent, and “<f>” represents the averaged frequency of the square wave.

Screenshot - 250115 - 23:33:50

You can use the following command to change the UART speed on the fly:

sudo stty -F /dev/ttyAMA0 115200

For example, if we set it to 110 baud (13.75 bytes/second):

Screenshot - 250115 - 23:39:01

The average frequency now becomes 54.99 Hz, more than a thousand times slower. This means only 12.2 characters would be sent every second, as opposed to 12.8 thousand per second with 115200 baud. It would take over 20 hours to send a 1MB (1,000,000 bytes) text file over this link (at 13.75 bytes/sec). But we don’t want to go slower, we want to go faster!

So lets punch in the next baud rate level over 115200 into the Pi:

sudo stty -F /dev/ttyAMA0 230400

But we get:

stty: /dev/ttyAMA0: unable to perform all requested operations

We need to edit the /boot/config.txt file to enable higher speeds! Add the following line to the bottom of the file:

init_uart_clock=64000000

Reboot the Pi and try setting the baud to 230400 (28.8 kB/s) again. It should work straight away, and here is what the logic analyser has to say:

Screenshot - 250115 - 23:53:05

Great! The average frequency is now 115.4 kHz, double what we had as the default baud rate. We can now send 25.6 thousand characters per second. That 1 MB file we mentioned would take 34.7 seconds to transfer (still very slow by today’s standards). But can we go even faster?

The standard baud rates double in speed, from 115200 to 230400, then 460800, then 921600. After that, it follows subdivisions of 3,000,000, so the next would be 1,000,000, then 1,500,000, 2,000,000, etc.

Here are the scope shots of each baud rate as I increase it:

460800 (57.6 kB/s):

Screenshot - 260115 - 00:09:27

Average frequency 229.7 kHz, 1 MB would take 17 seconds.

921600 (115.2 kB/s):

Screenshot - 260115 - 00:13:22

Average frequency 459.3 kHz, 1 MB would take 8.6 seconds.

1,000,000 (125 kB/s):

Screenshot - 260115 - 00:15:40

Average frequency 500 kHz, 1 MB would take 8.0 seconds.

1,500,000 (187.5 kB/s):

Screenshot - 260115 - 00:21:10

Average frequency 750 kHz, 1 MB would take 5.3 seconds.

2,000,000 (250 kB/s):

Screenshot - 260115 - 00:23:40

Average frequency 1 MHz, 1 MB would take 4 seconds. Getting faster with 222.2 thousand characters sent per second!

2,500,000 (312.5 kB/s):

Screenshot - 260115 - 00:33:09

Average frequency 1.263 MHz, 1 MB would take 3.2 seconds. Nearly 300k characters per second!

3,000,000 (375 kB/s):

Screenshot - 260115 - 00:36:25

Flippin heck, average frequency 1.5 MHz, 1MB would take 2.6 seconds.

3,500,000 (437.5 kB/s):

Screenshot - 260115 - 00:41:58

Jeeeeez, average frequency 1.745 MHz, 1MB would take 2.3 seconds. Just shy of 400k characters per second!

4,000,000 (500 kB/s):

Screenshot - 260115 - 00:41:58

 

Okay, 2 MHz average frequency, 1MB would take 2 seconds, and over 444k characters per second!

However, there’s a snag at these speeds:

Screenshot - 260115 - 00:51:16

As you can see, the UART stutters and causes gaps to appear. This may be an issue if you have equipment sensitive to this effect. This was the highest I was able to set the UART speed to, anything higher refused to work.

Looking back, I found that 2,000,000 baud was quite stable, but I could trigger off gaps above 1 microsecond once every so often, so it still exhibits some instability. 1,500,000 proved to be *almost* rock-solid, taking a really long time until a gap 100 nanoseconds longer than the waveform period. 1,000,000 was perfect though, with no gaps seen at all!

So for pure data transfer, crank it all the way up to 4,000,000. If you need stability though, don’t go above 1,000,000!

Use two flash drives in RAID-1 for important files!
Ubuntu: No sound from Pioneer DDJ-SB!

{5 Responses to “Raspberry Pi: Increase UART speed”}

  1. Thanks so much for taking the time to do this. Planning on using 921,600 to communicate with an LED controller, and now I don’t have to do the measurements to prove it will work. 🙂

    Mark
  2. Hi! Fantastic benchmark, i’ve been looking for the actual UART speeds and this is the most comprehensive list I’ve found +1!

    One question: Which model Pi did you use for your bench-marking?

    Anonymous

Trackbacks & Pings

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.