The serial connection

Embedded devices don’t have monitors or keyboards for IO. Sometimes even SSH or Telnet aren’t available. Do not despair though, for there is light in the end of the tunnel in the form of the serial console.

A forest of unclear terminology

One can easily be intimidated by the terminology surrounding the serial console. Some of the terms that can be encountered are:

  • UART
  • Bit rate
  • Baud rate
  • RS232
  • TTL

UART

It stands for Universal Asynchronous Receiver Transmitter… yeah.

It is a chip that converts a parallel stream of data into a serial one. Stripped to the core concept: you input a byte of data through 8 inputs and it comes out as a serial stream on one output.

Asynchronous means there isn’t a common clock source between the two communicating points. Therefor the two points must know the rates at which they’ll exchange information before the communication starts. The speed at which they communicate is called the baud rate, and it will be addressed further.

The binary data being transmitted isn’t send on it’s own. It is enclosed by additional non-data bits to help error detection (parity) and synchronize communication.

This leads to 4 main UART parameters :

  • Baud rate
  • Data bits
  • Non-data bits - Parity
  • Non-data bits - Synchronization

A popular parameter combination is 9600/8-N-1, implying 8 data bits, no parity bits and a single bit for synchronization (which translates to one bit before the data bits and one bit after). The baud rate is 9600.

Baud rate vs bit rate

What is a bit rate?

The speed at which bits are transfered, expressed in bits/seconds.

What is a baud rate?

The speed at which symbols are transfered, expressed in in bits/seconds.

What is a symbol?

Whatever we chose to be a carrier of our information.

Here are some examples to help clarify the difference:

Example 1 Two people in eyesight of each other want to exchange a byte of data. Beforehand they decided that each will be given flags that have numbers from 0-9 printed on them. They also agreed that they will change the flag every second.

They will be exchanging the phone number at a rate of 1 digit per second. The baud rate is 1 symbol per second. This examples has no concept of bits.

Example 2 Two people in eyesight of each other want to exchange a byte of data. Beforehand they decided that each will be given flags that have digits 0 and 1 printed on them. They also agreed that they will change the flag every second.

They will be exchanging the bit of data at a rate of 1 digit per second. The baud rate is 1 symbol per second. The bit rate is the same because 1 bit is the same as 1 symbol.

Example 3 Two people in eyesight of each other want to exchange a byte of data. Beforehand they decided that each will be given flags that have digits from 0-3 printed on them. They also agreed that they will change the flag every second. They also agreed on the following symbol to bit mapping:

0 - 00
1 - 01
2 - 10
3 - 11

They will be exchanging the byte of data at a rate of 1 digit per second. The baud rate is 1 symbol per second. The bit rate is 2 because one symbol carries 2 bits of information.

Do note, sometimes the following terminology is also used which might cause confusion:

  • bit rate -> bit rate of the data payload
  • baud rate -> bit rate of both data payload and the protocol overhead

RS-232 and TTL

Notice that UART was explained using digital concepts. Those 0s and 1s need to have a representation in the physical world. This is where RS232 and TTL come into play.

They define how logical levels relate to voltage levels. We have to take them into account when connecting the board. Use 5V where the board expect 3.3V and you’ll damage it.

One of the popular cables come from the FDTI’s TTL-232R series. Notice the above mentioned TTL voltage differences in the cable naming TTL-232R-3V3 and TTL-232R-5V.

The FTDI cables are of high build quality and reliable, but come with a price. If you’re ready to sacrifice usability look for alternatives from Prolific. They tend to crash from time to time but cost almost 10x less.

Running the serial console

Now that the concepts have been clarified, let’s explore how to actually use the serial console. I’m assuming you’re using Linux, there might be similarities on UNIX machines. The example is done using the FTDI TTL-232R-3V3 and would work the same with Prolific just with different names. The board used in the example will be Raspberry Pi 3.

Physical connection

To connect physically to the board we need to locate three pins TXD, RXD and GND. For this check the schematic or the pinnout documentation of your board. For the Raspberry Pi this is a great reference.

The TTL-232R-3V3 documentation shows there are also cable endings named TXD, RXD and GND.

The connection should be done as follows:

  • TXD <-> RXD
  • RXD <-> TXD
  • GND <-> GND

Accessing the console

The default for Raspberry Pi 3 is to disable UART. To enable it you need to change enable_uart=1 in /boot/config.txt. Ref.. If you’re using another board and you can’t get a connection double check there isn’t a board specific setting preventing the console.

Below is the most commonly used working example that will result in a Raspberry Pi login screen achieved through a serial console.

sudo apt-get install picocom
chmod 777 /dev/ttyUSB0
picocom -b 115200 /dev/ttyUSB0

The application I use for this purpose is picocom. It is a small terminal emulation program. In short, it takes the data from the cable and presents it as an interactive terminal.

The cable is usually detected as /dev/ttyUSB0 in the Linux device structure. In the second step the permissions are changed so all future commands don’t need to be executed with sudo. Sometimes the cable is detected under a different name. In case you get errors about non existing devices, check the output of dmesg once you’ve physically plugged in the cable in the USB.

Finally the serial terminal is started with the baud rate 115200 which is the default rate fo the Raspberry Pi. What about the rest of the parameters we mentioned?

flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8

In this case the defaults are fine so they aren’t explicitly stated when starting the console. picocom prints them out once started.

Exiting the console

picocom has a nonintuitive escape key. When you want to exit the terminal you need to press Ctrl + a + x in order without releasing the keys you already pressed.

If you want to change the baud rate while inside the terminal you use Ctrl + a + u or Ctrl + a + d.

Other options are listed under man picocom, the thing to notice is that when they say C- that means Ctrl + a +.

Written on April 24, 2017