- Enable UART communication on Pi4 Ubuntu 20.04
- Connecting to Raspberry PI from Linux via UART
- Marcin Chwedczuk
- Saved searches
- Use saved searches to filter your results more quickly
- License
- themrleon/RpiSoft-UART
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
Enable UART communication on Pi4 Ubuntu 20.04
I have Ubuntu 20.04 (32 bit) installed on RPI4 (4GB). I have loopback wire installed on Pin 8,10. I need to use Bluetooth and also basic serial communication over Pin 8,10 to communicate with a Roboclaw motor controller. Did install RASPI-CONFIG on Ubuntu using script at https://github.com/EmilGus/install_raspi-config but even though it installed successfully it loads with improper boot configuration error. That led me to /boot/firmware directory where I found config.txt , cmdline.txt etc. I removed the console=.
$ dmesg | grep tty [ 0.001061] printk: console [tty0] enabled [ 1.919367] fe201000.serial: ttyAMA0 at MMIO 0xfe201000 (irq = 29, base_baud = 0) is a PL011 rev2 [ 1.924677] fe215040.serial: ttyS0 at MMIO 0x0 (irq = 31, base_baud = 62500000) is a 16550
$ ls -l /dev/ttyS[0123] crw-rw---- 1 root dialout 4, 64 Jun 27 12:32 /dev/ttyS0
$ ls -l /dev/serial lrwxrwxrwx 1 root root 7 Apr 1 11:23 /dev/serial1 -> ttyAMA0
Second when I run sudo minicom -b 115200 -o -D /dev/ttyS0 (with loopback in place — verified correctly installed) I see no characters. No verification of communication with serial. Tried to change baud, parity, flow control settings with no effect. Also have a simple serial test python which also fails to send/receive over serial at /dev/ttyS0 whether I run with sudo or without. UPDATE I got an updated install script (same github link above) and now have successfully installed RASPI-CONFIG, however while I can run it the directory structure on the RPi4 w/ Ubuntu 20.04 is different enough that it fails to find the config files it tries to change.
$ sudo raspi-config grep: /boot/cmdline.txt: No such file or directory sed: can't read /boot/cmdline.txt: No such file or directory sed: can't read /boot/cmdline.txt: No such file or directory
I presume that are other Raspberry Pi 4 owners who intend to use the serial port and there’s got to be a way to do what RASPI-CONFIG is trying to do on Ubuntu. I’ve found the files in /boot/firmware What should I change manually in them that RASPI-CONFIG would do via script?
Connecting to Raspberry PI from Linux via UART
In this blog post we will learn how to connect to a Raspberry PI via UART (think serial terminal). Before we begin we need to get a cheap USB UART converter. Make sure that the converter supports 3.3V voltage. Most of the converters support other voltages e.g. 5V and you can select which voltage you want by moving a jumper. If your converter has such a jumper make sure that it is in 3.3V position.
Although converters can have from four up to six pins, we will only need three: GND, RXD and TXD. We should connect RXD pin on the converter to TXD pin on the Raspberry PI and similarly TXD pin to the Raspberry’s RXD pin (crossover). We also need to connect the converter GND pin to Raspberry’s Ground pin. Do NOT connect VCC (or VCCIO) pin, we only need three wires. You can find the Raspberry PI pinout in the official docs: https://www.raspberrypi.org/documentation/usage/gpio/ — look at the five top left pins, the first two are +5V, then we have Ground, TXD and RXD pins.
Next thing that we need to do is to enable UART support in Raspbian, which for security reasons is disabled by default. We need to change config.txt file on the /boot partition on the Raspberry PI SD card. We need to add enable_uart=1 line before [pi4] section (or at the end of the file if the section is not present).
Another problem is that we are stuck with a fixed terminal size. If your Raspberry PI has xterm package installed (not available on Lite version) you can use resize command after you resize putty’s window. Otherwise you may use this script created by @pkh https://unix.stackexchange.com/a/283206:
To exit screen (which is on the same level of difficultly as exiting Vim) press Ctrl+A followed by \ (yup backslash). A question will appear at the left bottom of the screen, answer y.
If screen s window appears blank, press enter to reprint the prompt. For some reason programs like htop tend to look worse under screen (assuming that you set TERM variable and did resize ). Probably I am missing some extra setup, nevertheless they are usable.
Last but not least, for fans of ancient minicom if you want to connect via minicom remember to disable “Hardware Control Flow” option (they use RTS and CTS lines present on some converters that we do not use).
Marcin Chwedczuk
A programmer, a geek, a human
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
themrleon / RpiSoft-UART Public archive
A Raspberry Pi / BCM2835 Software-based UART Linux device driver
License
themrleon/RpiSoft-UART
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
This driver will create a software-based serial port/UART using any GPIO pin, similiar to /dev/tty* devices, with some unique features.
- Works with Minicom
- Works with cat and echo commands
- 256 bytes RX buffer (read the data whenever you want, no data lost!)
- 256 bytes TX buffer (no need to send each byte separately, send the whole array once!)
- RX buffer cleared automatically after a read operation (Ex: cat )
- TX buffer cleared automatically after a write operation (Ex: echo )
- No pooling needed
- Loopback mode
- Runtime changeable parameters (GPIO pins, baudrate and loopback mode)
Export the path location of your toolchain/GCC, Ex:
export PATH=$PATH:/root/raspberry/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin
Edit Makefile to match your kernel source directory and GCC prefix:
KERNEL_SRC_DIR := /root/raspberry/linux GCC_PREFIX := arm-bcm2708-linux-gnueabi-
Now type make and you have the softuart.ko driver.
The driver will be created at /sys/class/softuart/softuart/ :
root@raspberrypi:/home/pi# cat /sys/class/softuart/softuart/ baudrate data gpio_rx gpio_tx loopback power/ subsystem/ uevent
- baudrate — Selected baudrate
- data — Used to send and receive data, is the /dev/tty* like file
- gpio_rx — Selected GPIO pin to receive data
- gpio_tx — Selected GPIO pin to transmit data
- loopback — Loopback mode, 1 — on / 0 — off (default)
root@raspberrypi:/home/pi# modinfo softuart.ko . parm: BAUDRATE: Baudrate value [default = 4800] (min=1200, max=19200) (int) parm: GPIO_TX: GPIO TX pin [default = 4] (int) parm: GPIO_RX: GPIO RX pin [default = 2] (int)
Loading module with default parameters:
Loading module specifying all possible parameters:
insmod softuart.ko GPIO_TX=4 GPIO_RX=2 BAUDRATE=4800
minicom -b 4800 -D /sys/class/softuart/softuart/data
echo abc > /sys/class/softuart/softuart/data
cat /sys/class/softuart/softuart/data
echo 1 > /sys/class/softuart/softuart/loopback echo abc > /sys/class/softuart/softuart/data cat /sys/class/softuart/softuart/data abc
Change baudrate to 9600 kbps:
echo 9600 > /sys/class/softuart/softuart/baudrate
Just don’t send more than 256 bytes at once and all must be ok, 256 is enough for most applications, but if you need more just change in the source code.
Understanding the RX buffer
The driver have a 256 bytes RX buffer. This mean you can receive up to 256 bytes at once and they will be stored, without loss, if more bytes are received the buffer will be cleared and start store again. Any read operation at ‘data’ entry, will clear the buffer (Ex: cat ). The buffer have only effect without the use of a software like minicom (because have it’s own buffer, and due the pooling, keep the buffer clear).
Understanding the TX buffer
The driver have a 256 bytes TX buffer. This mean you can send up to 256 bytes at once and they will be stored and transmitted until the last byte, clearing the buffer automatically. If more bytes than the buffer capacity is written, the buffer will clear and start store again. Any write operation at ‘data’ entry, will start the transmission (Ex: echo ). The buffer have only effect without the use of a software like minicom (because transmit each byte separately, using just one byte of the TX buffer at time).
Remember, this is a software-based UART, subject to preemption, not a piece of dedicated hardware. Details, limitations and a user-space version can be found HERE.
Tag 1.0 released and a pull request was accept on the Master branch, seems to be that the new raspberry pi toolchain can’t understand some of the old symbols, so they were hardcoded.
Accepted pull request that adds support for the rpi version 2 and 3, however, I can’t test any pull requests because I haven’t the rpi hardware anymore, so if you are having problems with the master branch version (which contains all the pull requests), try the tag 1.0 that was fully developed and tested by me, using the rpi model B. A BIG thanks for all the contributors that keep updating the driver with the newer rpi models =]
About
A Raspberry Pi / BCM2835 Software-based UART Linux device driver