Skip to content

Serial Connection

thanoskoutr edited this page Jul 30, 2021 · 6 revisions

Serial Connection

In order to have an interaction with the kernel, without having video or keyboard drivers available, we can use the serial connection that is available from the UART interface on the Raspberry Pi.

Required Components

In order to make that connection we need some components:

  • A Raspberry Pi.
  • A USB to TTL serial cable.
  • An a SD Card with Raspbian OS installed.
  • A Linux/Unix development environment.

We need the cable to properly convert the USB signal from the computer to a serial for the UART interface on the Pi. Also, we need the SD to be formated with the Raspbian OS (now Raspberry Pi OS), in order to be properly formatted to boot our kernel. The booting sequence of the Raspberry Pi is explained in the Bootloader page.

Install Raspberry Pi OS

The way to install the Raspberry Pi OS on a Pi is covered by the official Rasberry Pi documentation, so it suggested to follow the Installing operating system images guide, depending on the host OS.

Using Raspberry Pi Imager

A basic overview of the installation steps is the following:

  • Download the latest version of Raspberry Pi Imager and install it.
  • Connect an SD card reader with the SD card inside.
  • Open Raspberry Pi Imager and choose the required OS from the list presented.
    • Choose the 32-bit Lite version with no Desktop Environment as it is much lighter and smaller.
  • Choose the SD card you wish to write your image to.
  • Review your selections and click 'WRITE' to begin writing data to the SD card.

Using the Terminal

Download from Raspberry Pi site the Raspbian Pi OS 32bit Lite version:

wget https://downloads.raspberrypi.org/raspios_lite_armhf/images/raspios_lite_armhf-2021-01-12/2021-01-11-raspios-buster-armhf-lite.zip

Insert SD Card on computer.

Unmount SD Card, if it mounted by the OS:

sudo umount /dev/mmcblk0p*

or if inserted with a USB stick:

sudo umount /dev/sdb*

Write Pi OS *.img to SD card device:

sudo dd if=raspbian.img of=/dev/mmcblk0 bs=4M status=progress

or if inserted with a USB stick (find your /dev/sd*, or else you could write to your hard drive):

sudo dd if=raspbian.img of=/dev/sdb bs=4M status=progress

Drivers for Linux Host

Linux Kernels 2.4.31 and above already have the PL2303 and CP210X USB driver for the Console Lead built-in, so you should not need to install that.

But you need to install the screen command if not installed on your distro.

Install screen:

sudo apt-get install screen

Connect the USB to TTL Leads

The Console lead has four female connections that can be plugged directly onto the GPIO header of the Raspberry Pi.

Most USB console cables have 3.3V logic, so its safe to use with your Pi.

The connections will be the following:

  • The red lead should be unconnected. (It was used to deliver power to the Pi, but for versions > 2 won't work)
  • The black lead to GND (Physical Pin 6)
  • The white lead to TXD (GPIO14) on the Pi (Physical Pin 8)
  • The green lead to RXD (GPIO15) on the pI (Physical Pin 10)

pi-gpio

Enable Serial Console - Raspberry Pi OS

Firstly, we are going to configure the serial connection with the Pi using the Raspberry Pi OS, in order to make sure the connection is working and stable, and then try to connect with our custom kernel.

We can enable/disable the serial console with either editing /boot/config.txt or using raspi-config (which will edit /boot/config.txt for you). We present both options.

Option 1 - Enabling UART via /boot/config.txt

Insert your SD card into a computer.

Edit config.txt with a text editor.

vim media/{your-SD-name}/boot/config.txt

At the bottom, last line, add enable_uart=1 (for Pi >= 3) on it's own line:

enable_uart=1

Example of config.txt, in condext with rest lines in the file:

#...
[all]
#dtoverlay=vc4-fkms-v3d
enable_uart=1

Option 2 - Enabling UART via raspi-config

  • Boot to the Raspberry Pi OS, log in to a shell and run:
sudo raspi-config
  • Go to Interface Options
  • Go to P6 Serial Port
  • Select Yes on You would like a login shell to be accessible over serial?
  • After that, it should be enabled.
  • When it asks you to reboot, press Yes.
  • If it is not shown, reboot it yourself.

Test and Configure

After that you have connected the leads to the Pi, connect the USB on the linux host and power up the Pi.

Open a terminal on the linux host and find in which serial port the Pi is connected. Usually, the device is called /dev/ttyUSB0.

It is best to run:

sudo dmesg

after plugging in and looking for hints on what the device is called.

After you found it (e.g. /dev/ttyUSB0) check the permissions:

ls -l /dev/ttyUSB0

Usually root user can read/write and the group dialout can read/write.

Make sure your user is in the dialout group, with:

id

If you do not see dialout listed, add yourself with the command:

sudo usermod -a -G dialout <username>

After you found it (e.g. /dev/ttyUSB0) connect to the Pi with:

sudo screen /dev/ttyUSB0 115200

After logging in with your user account, you should have a shell for your Pi.

Exit Screen

In order to exit from screen press the following keys:

  • Ctrl+a and then d

Enable Serial Console - Custom kernel

After you had a successful serial connection to the Pi with the USB to TTL cable using the Raspberry Pi OS, you can try the serial connection with the custom kernel.

See the Installation page in order to build and run the kernel using the serial console.

Resources

Guide to help with the serial connection with the Pi, and official documentation about the UART interface on the Raspberry Pi:

Previous Page

Install Toolchain

Next Page

Installation

Clone this wiki locally