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?

rmalchow
  • 221
  • 2
  • 7

1 Answers1

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?

rmalchow
  • 221
  • 2
  • 7