3

i am trying to connect two devices via UART to a raspberry 4. one being a stepper driver (i dont need much throughput), one being an image sensor (the higher the baud rate is i can get, the better). both are currently connected via USB-to-serial dongles, which i want to get rid of to get a better fit with the box it's housed in. i would be very happy if i could these to the pi using the 40-pin-header instead.

i have gone through what little documentation i could find, my config.txt looks like this (only the last two added by me):

dtparam=audio=on [pi4] dtoverlay=vc4-fkms-v3d max_framebuffers=2 [all] #enable_uart=1 dtoverlay = disable-bt dtoverlay=uartx 

and listing my serial devices looks like this:

root@raspberrypi:/home/pi# ls -la /dev/serial* lrwxrwxrwx 1 root root 5 Jun 6 08:13 /dev/serial0 -> ttyS0 lrwxrwxrwx 1 root root 7 Jun 6 08:13 /dev/serial1 -> ttyAMA0 

i have read this: Where are the uarts on the raspberry pi 4?

which states that there are more available PL011 UART ports - i take it that those could be used for my purpose, but they are not visible to the OS ... do i have to set them up from python with something like wiringpi? or is there another configuration that i missing?

12
  • 2
    You might like to read my old posts on how to use multiple UART ports on Rpi USB and on board: (1) penzu.com/journals/18951407/58119498, (2) raspberrypi.stackexchange.com/questions/96184/…. Cheers. Commented Jun 6, 2021 at 8:11
  • 2
    néih hóu. from the second post (and the links therein), i learned a lot of little things. thank you very much for that. however, the first link only points to a login page on penzu, and the second one (as far as i can see) does not contain anything about getting two serial ports on the 40pin. i am beginning to suspect that i am missing something important ... Commented Jun 6, 2021 at 8:30
  • 3
    Have you read through either the raspberrypi.org web site documentation for config.txt or /boot/overlays/README ? Commented Jun 6, 2021 at 8:39
  • 2
    @joan such a polite way of saying "it's right there in the documentation, you idiot" :) but it did help me to figure it out: it is dtoverlay=uart[X] where "X" is the number of the UART. also, tlfong's link spells it out. thank you! :) Commented Jun 6, 2021 at 8:51
  • 2
    @tlfong01 would you care to write a proper answer, so i can upvote you? if you don't feel like doing this, i'll do it myself. just so this answer is easier to find for the next person. Commented Jun 6, 2021 at 8:52

1 Answer 1

9

So there was one big misunderstanding. Thanks to @joan for pointing to the docs and @tlfong01 for their link to their article:

https://penzu.com/public/b94e6b30

Essentially, on the raspberry 4, there are 6 UARTs available, but two of them use the same pins by default (board pin 8 and 10). Additional UARTs can be enabled one-by-one, doing so eats into the available GPIO pins.

UARTs are enabled through separate "dtoverlay=" entries in /boot/config.txt so, all on looks like this:

dtoverlay=uart5 dtoverlay=uart4 dtoverlay=uart3 dtoverlay=uart2 dtoverlay=uart1 

these are mostly off, in favour of having the corresponding pins configured as "regular" GPIO pins. once enabled, they will appear as

/dev/ttyAMA[0,1,2 etc] 

This thread here with contributions by @joan and @milliways has some details on the default pins:

Where are the uarts on the raspberry pi 4?

and the README on overlays

here: https://github.com/raspberrypi/firmware/blob/master/boot/overlays/README

or on the pi at: /boot/overlays/README

has some additional details on available configuration options, e.g.:

Name: uart1 Info: Change the pin usage of uart1 Load: dtoverlay=uart1,<param>=<val> Params: txd1_pin GPIO pin for TXD1 (14, 32 or 40 - default 14) rxd1_pin GPIO pin for RXD1 (15, 33 or 41 - default 15) 

my setup is now like this:

dtoverlay=uart1 dtoverlay=uart5 dtoverlay=uart4 

and my loopback test:

import serial import time test_string = "[serial port test]".encode('utf-8') port_list = ["/dev/ttyAMA0","/dev/ttyAMA1","/dev/ttyAMA2","/dev/ttyAMA3","/dev/ttyAMA4" ] for port in port_list: ok = False try: buff = bytearray(len(test_string)) serialPort = serial.Serial(port, 115200, timeout = 2, writeTimeout = 2) bytes_sent = serialPort.write(test_string) time.sleep(1) bytes_read = serialPort.readinto(buff) ok = bytes_read == bytes_sent serialPort.close() except IOError: pass print("port %s is %s" % (port, "OK" if ok else "NOT OK")) 

with the the RX/TX pairs for 4 and 5 bridged like this:

enter image description here

gives me the expected results.

Where are the uarts on the raspberry pi 4?

1
  • 2
    Many thanks for your answer. Your explanation is concise and clear, and your loopback test program is professional, comparing with my long winded newbie level demo programs. Cheers Commented Jun 6, 2021 at 10:20

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.