2016年3月14日 星期一

Raspberry Pi UART speed changing.

This is done on a RPI2 with Jessie, Rasbpian installed.

Changing UART baud rate, using "stty" command.
$ stty -F /dev/ttyAMA0
speed 115200 baud; line = 0;
-brkint ixoff -imaxbel iutf8
-iexten

The result without baudrate set, is showing the current baud rate.
Next is to change it.
$ stty -F /dev/ttyAMA0 230400
stty: /dev/ttyAMA0: unable to perform all requested operations

Seems speed cannot be changed, result still showing in previous baudrate (115200 baud in my case).

Then try a lower speed UART, 57600 baud:
$ stty -F /dev/ttyAMA0 57600
" some scrambled text shown....".  As terminal is still setting at 115200 baud.
Changing my Teraterm's baudrate to 57600 baud and type "Enter" several times, and I can see the normal prompt showing.  i.e. baudrate changed to 57600 successfully.

Next you can try setting back to 115200 baud with same prcedure.  It works for me.

Then try increasing the UART input clock to make faster UART speed possible.
$ sudo vi /boot/config.txt
Insert in last line:
init_uart_clock=16000000

Save the file, and sudo reboot.

System rebooted, and scrambled text appeared which should be boot up logs.
Now try changing the Teraterm terminal's baudrate to 4 times faster, 460800 baud.
Normal text can be seen again.

After logged in, check UART speed using command again:
$ stty -F /dev/ttyAMA0
speed 460800 baud; line = 0;
-brkint ixoff -imaxbel iutf8
-iexten

New speed shown!!!

Wait!  The UART option of my FTDI USB-to-UART cable shows maximum of 921600 baud.
I think I shall have a try.  Changing the /boot/config.txt line to:
init_uart_clock=32000000

Reboot again, scrambled text appear again!  Teraterm setting change to 921600 baud, maximum for my FTDI chip & Win 10 driver.  Normal text appeared again, the speed is now near 1M baud:
$ stty -F /dev/ttyAMA0
speed 921600 baud; line = 0;
-brkint ixoff -imaxbel iutf8
-iexten

Now try if this speed can really transfer large files.  Using my favourite zmodem protocol.
Install the sz & rz in Raspbian:
sudo apt-get install lrzsz

Zmodem sending file from Win 10 PC to RPI2, at average data rate is 57.5kB/s.
Since this is in bytes of unit, N81, should include at least 1 start bit, 8 data bit and 1 stop bit.
So, exclude any zmodem rate control headers, the baud rate must be >575k baud.
15% of 107,466,570 bytes; for 4:40 minutes or 280 seconds.
i.e. 57.57kB/s.

Then sending file from RPI2 back to PC, speed is now 68.2kB/s.
Faster than transferring in other direction.  The same file compared, no problem found.

(Note: if the file size is too large, you may escape the transfer by pressing CTRL-X repeatedly on the receiving side after pressed ESC key.)

This file transfer method is good when you are writing programs where size is at most several tens of KB.  If file size too big, better use RJ45 LAN port where transfer rate can be up to 11MB/s.

沒有留言:

張貼留言