Raspberry Pi’s onboard Serial connection

Posted on April 24, 2012

17


One of the reasons I was excited to play with the Pi was the rank of pins on one of its corners, the ‘low-level peripheral‘ pin-out. GPIO pins, SPI, I2C and even an onboard 3.3v serial connection (UART)! In fact, in the stock debian image, it is configured to output the bootup messages and the kernel errors to this port!

But it’s not just for that. Oh no. You can play with it too!

The serial device is at ‘/dev/ttyAMA0’ and by default, outputs kernel messages at 115200 baud. This is configured in /boot/cmdline.txt and you can remove the option, or alter the connection speed it outputs at. If you were going to use this port for your own purposes, I would recommend you disable the kernel logging for obvious reasons!

In /boot/cmdline.txt: (for example)

dwc_otg.lpm_enable=0 rpitestmode=1 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/sda1 rootfstype=ext4 rootwait

The “console” and “kgdboc” options that include “ttyAMA0” are the ones that are important here.

Connecting to the GPIO pins

Look at the pin-out (the board should be turned so you can read the text under the logo. Then top-left, etc should match the pinout below)

You see the pins marked 5v and the 3v3? You should never let them connect or you might do something nasty to your Pi! And the one below the 5v? That is marked as ‘Do Not Connect’ so you shouldn’t really connect anything to that either.

This isn’t really an interface that you should be poking wires at by hand, or even using crocodile clips. You need a connector that can clip onto this and lets you pick and choose your connections easily. There are some breakout boards already, such as the Slice Of Pi, or the Gertboard.

I used a floppy disc drive connector. It has the same pitch as the pins and you’ll find that you can slip most of the connector onto it nicely. There is a bit of overhang, but don’t worry about that!

I used a multimeter to make sure that the wires I was stripping and preparing at one end of the ribbon cable, were the ones I wanted to connect to. In this case the 5v, the 0v (Ground) and the two yellow pins, Tx and Rx.

If you have got this far, then this is it! If you have a serial port connector, like an USB FTDI cable, you can connect that to these and start to communicate!

See http://www.irrational.net/2012/04/19/using-the-raspberry-pis-serial-port/ for an excellent write-up on how to use this connection to interface with your Pi.

Let’s make it physical!

A few years back, I followed in the footsteps of others and hacked about with a receipt printer – a ‘microprinter‘ as they are sometimes referred to. In fact, I even got to the point of being able to push image data to it via an adapted serial connection, the details of which you can find here.

I pulled the bag with all the printer bits and pieces off the shelf and begin reconstructing what I had done, rebuilding the circuit that allowed this printer’s TTL serial to chat with the normal RS232 serial using a MAX232N chip. I was saved by my own post, as I couldn’t remember how to do it to save my life!

The printer also uses a much slower baud rate (9600) so I lowered that in the cmdline.txt and rebooted with everything connected.

The Result?

And once I got my old python libraries for chatting to the printer on the Pi:

UPDATE

Serial logging speed

Even though the printer was capturing the boot at the correct serial speed, kernel logging was still being emitted at 115200baud. It turns out that the serial connection for the logger is a virtual tty, and that to get the correct speed out for kernel panics, you have to set the tty’s speed to 9600 as well. This is from ‘Grey’ via popcornmix(Dom) on an unrelated github issue:

From Gray (not directly in response to you, but this question has been asked before):

An awful lot of what is printed during the boot sequence is output by the kernel during initialization – i.e. during the set-up of devices that are later used to support the operating system implemented on the root filie system.

One of the classes of devices that need to be set-up are terminal (tty) devices – so it kind-of follows that the thing being output to during this kernel initialization process isn’t really a tty device. The kernel calls it (well them actually) a ‘console’. The kernel command line allows you to identify and set the baud rate for these consoles and kernel output goes to them all (e.g. to the HDMI framebuffer console and to the UART console).

Each console normally ends up being presented as a separate tty device in /dev.

Once the operating system gets hold of the devices the kernel has left it, it configures them and uses them as it sees fit. In our case we do the standard thing of running a shell on just about any tty we can find. This is implemented in the file that controls what we do when control is first passed to the operating system – /etc/inittab.

In /etc/inittab each tty is read by a program ‘getty’ in its own process. This explains why, once you get to a log-on prompt, [1] the baud rate might change; and [2] the output is no longer the same as it is on other console/ttys. (You may have noticed that you can log on separately to a shell over the UART and a different one over the HDMI/keyboard.)

So, in short, edit /etc/inittab and change
/sbin/getty -L ttyAMA0 115200 vt100
to
/sbin/getty -L ttyAMA0 9600 vt100
if you want the operating system to run at 9600 baud.

Slice Of Pi board

I also picked up a Slice of Pi PCB and headers from Ciseco, as my hacky floppy drive connector was not up to scratch for prototyping. It’s meant to be a breakout board and allow for easy addition of an XBee module but I am pretty sure all I will use it for is as a breakout board. Once soldered together, it sat quite nicely over the Pi, but I couldn’t help feel that a supporting leg on one side would be helpful, to avoid stressing the Pi’s GPIO header when plugging in wires to the breakout board. The current supply (5v,3v3, 0v) is collected to one side, but the kit doesn’t include anything to solder in there for ease of use. I cut down a stackable header to size and soldered that in as you will see from the picture, but it would’ve been nice if this was part of the kit too.

Posted in: fun, linux