UART0 & 1
UART0 is available on all rPI’s to date. It is available to users as /dev/ttyAMA0. By default it is used as a console and so to be able to use the uart this needs to be reconfigured. UART0 and UART1 are both on GPIO pins 14 & 15 so you are physically restricted to just having one of them. UART1 is 16550 compliant (well almost – and hence its different name BTW) so if this matters you should probably enable UART1 and disable UART0. Librpip will detect and whatever UARTs are visible.
/boot/config.txt
UART0
UART0 shows up in linux as /dev/ttyAMA0. By default it is enabled so do not need to make any changes to /boot/config.txt to make it appear.
dtparam=uart0=off
UART1
UART1 shows up in linux as /dev/ttyS0. It needs to be enabled in the /boot/config.txt like this:
dtparam=uart0=off dtoverlay=uart1
If you are seeing both /dev/ttyAMA0 and /dev/ttyS0 then the dtparam=uart0=off is missing. You don’t magically get 2 UARTS with this config. UART1 takes precedence and UART0 is mute.
Since config.txt is only read during reboots you must reboot for any changes to take affect.
Kernel Module
No configuration required.
System Config
By default UART0 is as a console (i.e. so you can log in over a serial connection). You have to modify one or two files to stop this.
Remove the ‘console=/dev/ttyAMA0,115200’ option form the /boot/cmdline.txt. If you have a ‘kgdboc=ttyAMA0,115200’ remove that too. e.g. change:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
to:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
Arch
Reboot now and you should have a properly configured device:
$ ls -l /dev/ttyAMA0 crw-rw---- 1 root uucp 204, 64 Jan 19 13:46 /dev/ttyAMA0
Now just add yourself to the uucp group .
Raspbian
If running Wheezy remove the respawn command from /etc/inittab by commenting it out. e.g. change:
#Spawn a getty on Raspberry Pi serial line T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
to
#Spawn a getty on Raspberry Pi serial line #T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
Reboot now and you should have a properly configured device:
$ ls -l /dev/ttyAMA0 crw-rw---- 1 root dialout 204, 64 Jan 19 13:46 /dev/ttyAMA0
Or if using UART1
$ ls -l /dev/ttyS0 crw-rw---- 1 root dialout 4, 64 Feb 19 06:13 /dev/ttyS0
Users are allowed to access the uarts if they are in the ‘dialout’ group.
So now add yourself in to the group if you are not already in it.
sudo usermod -a -G dialout <acccount>
Note: if the permissions on the /dev/ttyAMA0 look like this:
$ ls -l /dev/ttyAMA0 crw--w---- 1 root tty 204, 64 Jan 19 13:17 /dev/ttyAMA0
Then the uart is being used as console by the operating system. Resist the temptation to sudo chmod chown your way out and just check your /boot/cmdline.txt and|or /etc/inittab as per the instructions above.
Issues
The uarts on the rPi use 3.3v TTL – 0 = 0v and 1 = 3.3v, however:
- The uarts on things like avr microcontrollers (arduino) and pc usb ports are using 5v TTL – 0 = 0v and 1 = 5v. You must use a voltage level converter or adaptor cable or you will fry your rPi
- The db-9 serial ports have 0 = < -3 to 15v and 1 > 3v to 15v (RS232). You must use a something like a max3232 to level shift or you will fry your rPi
So you need to be very very careful when connecting the uarts. You have to find out if the device you are connecting to is using 3.3v TTL, 5v TTL or RS232 and then use the appropriate converter. If you do not you will damage your rPi.