Using UART

Having issues with your DietPi installation, or, found a bug? Post it here.
MrGlasspoole
Posts: 8
Joined: Tue Feb 27, 2018 6:29 pm

Using UART

Post by MrGlasspoole » Sun May 20, 2018 5:57 am

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 [email protected] ?

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:
http://hallard.me/enable-serial-port-on-raspberry-pi/
Is it enough to disable BT in dietpi-config?

Then what about boot/cmdline.txt: https://www.raspberrypi.org/documentati ... on/uart.md
Do i have to manually remove console=serial0,115200 with DietPi?

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

MrGlasspoole
Posts: 8
Joined: Tue Feb 27, 2018 6:29 pm

Re: Using UART

Post by MrGlasspoole » Sun Jun 03, 2018 3:07 pm

Nobody?

User avatar
MichaIng
Site Admin
Posts: 1882
Joined: Sat Nov 18, 2017 5:21 pm

Re: Using UART

Post by MichaIng » Sun Jun 03, 2018 4:29 pm

@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 [email protected] as TTY1 is always needed as fallback login console. You might be left without the possibility to login ;).
  • 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?

MrGlasspoole
Posts: 8
Joined: Tue Feb 27, 2018 6:29 pm

Re: Using UART

Post by MrGlasspoole » Mon Jun 04, 2018 2:26 am

MichaIng wrote:
Sun Jun 03, 2018 4:29 pm
What you mean by /dev? Which entry in which file?
I mean what serial to use.
I tried:

Code: Select all

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.

pingouin84k
Posts: 1
Joined: Thu Sep 13, 2018 8:43 am

Re: Using UART

Post by pingouin84k » Thu Sep 13, 2018 8:46 am

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:

Code: Select all

device reports readiness to read but returned no data
Please advise!

boumboauto
Posts: 1
Joined: Thu May 30, 2019 4:38 am

Re: Using UART

Post by boumboauto » Thu May 30, 2019 4:45 am

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

User avatar
MichaIng
Site Admin
Posts: 1882
Joined: Sat Nov 18, 2017 5:21 pm

Re: Using UART

Post by MichaIng » Mon Jun 10, 2019 1:37 am

@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 [email protected] 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.

jhorv
Posts: 9
Joined: Thu May 02, 2019 8:03 pm

Re: Using UART

Post by jhorv » Wed Jul 17, 2019 8:02 pm

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?

jhorv
Posts: 9
Joined: Thu May 02, 2019 8:03 pm

Re: Using UART

Post by jhorv » Thu Jul 18, 2019 7:16 am

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 <module>
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?

jhorv
Posts: 9
Joined: Thu May 02, 2019 8:03 pm

Re: Using UART

Post by jhorv » Thu Aug 01, 2019 9:00 pm

I think I found a solution, well kind of :roll:. 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:

Code: Select all

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?

Post Reply