Using UART

I want to connect a device to the Zero’s UART (GPIO 15,16) but can’t figure it out.

Do i have to set Advanced Options > Serial console: on or off ?
Do i have to set enable_uart to 1 (DietPi/config.txt) ?
Do i have to set CONFIG_SERIAL_CONSOLE_ENABLE to 1 (DietPi/dietpi.txt) ?
Do i need to run systemctl disable getty@tty1.service ?

In config.txt it says:

enabled for 1st run only, if you want to keep this setting, please set CONFIG_SERIAL_CONSOLE_ENABLE=1 in dietpi.txt

To me console means showing booting stuff and the like?

Then here on the bottom it says that the Pi3/Zero uses UART for BT:

Is it enough to disable BT in dietpi-config?

Then what about boot/cmdline.txt: Raspberry Pi Documentation - Configuration
Do i have to manually remove console=serial0,115200 with DietPi?

And what is the right one to choose from /dev?

Nobody?

MrGlasspoole
I never configured a serial console, UART or something like that, but you will get the important points here: https://github.com/Fourdee/DietPi/issues/1759

  • Enable serial console with dietpi-config should already do all steps for you, but to be sure:
  • Verify that /DietPi/config.txt contains the line enable_uart=1.
  • Verify that /DietPi/dietpi.txt contains the line CONFIG_SERIAL_CONSOLE_ENABLE=1.
  • NEVER do systemctl disable getty@tty1.service as TTY1 is always needed as fallback login console. You might be left without the possibility to login :wink:.
  • It seems cmdline does not need to contain anything about that, as UART entry in config.txt seems to replace it (as far as I understand dietpi-config code…). console=serial0,115200 should be not present within the file, as we disable serial console by default (and remove the line by this).
  • Disabling BT seems to be recommended. Jep just do it as well via dietpi-config.
  • What you mean by /dev? Which entry in which file?

I mean what serial to use.
I tried:

screen /dev/tty0 9600,cs7,parodd,-cstopb
screen /dev/tty11 9600,cs7,parodd,-cstopb
screen /dev/ttyAMA0 9600,cs7,parodd,-cstopb
screen /dev/serial0 9600,cs7,parodd,-cstopb

But nothing but a blank screen…

I know the Atmega is sending via serial because connecting it to Windows with a USB-Serial adapter i can see everything in PuTTY.

Hi!

I have the same problem.
Does anyone have a solution to this?

I am with RPi3.
I manage to have some data exchange but after a while of random duration my python script (using serial module) crash with an error containing:

device reports readiness to read but returned no data

Please advise!

Hi,

I have the same problem here. i understand that there are Two UART in the Rasp3 and that one has its own hardware clock, the other is isug the CPU

The bluetooth adapter is using on of these uarts, the GPIO is using another. It is possible to mix and match which UART is used by which service. For example Bluetooth on AMA0 and some hardware on the GPIOs on S0

It’s very hard to understand how to configure :

  • Bluetooth using the hardware UART with it"s own clock
  • Serial port configured at 1200 bauds, 7 bits on the GPIO forcing the CPU clock at 250 Mhz

Could someone please explain how to do this on a DietPi ditsto?

Many thanks

boumboauto
You already explained everything correctly:

  • By default on RPi3 the hardware UART (aka full UART) on /dev/ttyAMA0 is used by the Bluetooth device.
  • But if you disable Bluetooth completely, you can still use it for other tasks.
  • Otherwise the software UART (aka mini UART) is available on /dev/ttyS0 which is as well by default configured to be available on the GPIO ports, so being the primary UART, on RPi3.
  • You can switch this by some config.txt entry, but lets not make things more complicated.

Important when using UART devices for something else then a serial login console, is that you disable serial console usage. On DietPi do:
dietpi-config > Advanced Options > Serial/UART

  • In the “Toggle serial consoles” part disable the serial console on /dev/ttyS0.
  • This will disable the serial-getty@ttyS0 service which allows login via serial terminal. And it disables boot messages being printed to this device/console.

And jep, having /dev/ttyS0 enabled forces CPU clock at 250 MHz.

Hi,

I am running DietPi version 6.25.3 and have been running into the same problem. I have done the following:

dietpi-config->Advanced Options->Serial/UART->ttyS0- => ON
save and reboot
dietpi-config->Advanced Options->Serial/UART->ttyS0- console => OFF
save and reboot

I tried this loopback test connecting GPIO 14 and 15 together:

#!/usr/bin/env python
import time
import serial

ser = serial.Serial(
port=’ /dev/ttyS0’, #Replace ttyS0 with ttyAM0 for Pi1,Pi2,Pi0
baudrate = 9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
counter=0

while 1:
ser.write(“Write counter: %d \n”%(counter))
time.sleep(1)
counter += 1

while 2:
x=ser.readline()
print x

I get no errors when I run this script but unfortunately nothing happens. With ttyS0 there are some limitations so I also set the serial parameters without baudrate, parity, stopbits, and bytesize yet the result was stll the same. I tried using serial0 instead of ttyS0 and then I got a {} error, that is, it couldn’t find the serial port. This is strange since ttyS0 and serial0 are linked just as serial1 is linked with ttyAMA0 (checked with ls -l /dev). So it seems I am still missing something in this configuration. Any ideas what else needs to be done?

A quick update:

I tried using AMA0/serial1 with the console setting in dietp-config set to off. Bluetooth is also off. The output of the loopback script is the same:

Traceback (most recent call last):
File “/usr/local/lib/python3.5/dist-packages/serial/serialposix.py”, line 265, in open
self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
FileNotFoundError: [Errno 2] No such file or directory: ’ /dev/serial1’

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “./loopback.py”, line 11, in
timeout=1
File “/usr/local/lib/python3.5/dist-packages/serial/serialutil.py”, line 240, in init
self.open()
File “/usr/local/lib/python3.5/dist-packages/serial/serialposix.py”, line 268, in open
raise SerialException(msg.errno, “could not open port {}: {}”.format(self._port, msg))
serial.serialutil.SerialException: [Errno 2] could not open port /dev/serial1: [Errno 2] No such file or directory: ’ /dev/serial1’

Meanwhile, ls -l /dev shows that it is there.

Finally, dmesg gives the following:

[ 1.235831] uart-pl011 20201000.serial: cts_event_workaround enabled
[ 1.245609] 20201000.serial: ttyAMA0 at MMIO 0x20201000 (irq = 81, base_baud = 0) is a PL011 rev2

Any ideas?

I think I found a solution, well kind of :roll_eyes:. I was able to finally get ttyS0 working but still was unable to do anything with ttyAMA0. :cry: I guess it has to do with interference from Bluetooth even though I have Bluetooth switched off.

Nevertheless, the problem with ttys0 appears to be with permissions. When I do this:

chmod 666 /dev/ttyS0

I am then able to access ttyS0 no problem and my loopback script works. Also I was able to connect my ESP8266 module and send/receive data via Node-Red. Actually, it was through Node-Red that I discovered the solution because it clearly gave me a “permission denied” error. Python simply said that it “could not open port” and “/dev/ttyS0 [Errno 2] No such file or directory” which is quite vague and misleading.

Anyway, my problem now is that the permission settings for the UART are not permanent, that is, it has to be reset if there is a reboot or shutdown. Anyone know how to set the permission for the UART at boot time so it will always be available?

jhorv
Sorry for missing reply.

Instead of making the tty world-writeable, please add the nodered user to the tty group: usermod -aG tty nodered

Instead of only disabling Bluetooth, might might be required to switch Bluetooth serial device usage by adding dtoverlay=miniuart-bt to /DietPi/config.txt. AFAIK otherwise ttyAMA0 does not listen to the GPIO ports.
EDIT: Ah nope, disable-bt should be sufficient to allow ttyAMA0 listening to GPIO ports.

Many thanks for the hint, better late than never. :smiley:

I had been thinking of adding chmod a+rw /dev/ttyS0 to the /etc/rc.local file but that’s absolutely not secure and your suggestion is obviously the best way to do it.

Meanwhile, after I updated DietPi from v6.25.3 to v6.26.3 I have been able to now access ttyAMA0 through node-red and python. Bluetooth is still disabled but since I don’t use it (yet) that’s not much of a concern for the time being.

Thanks again!