chipace
Posts: 120
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

[SOLVED] raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Sun Jul 07, 2019 10:06 pm

I am using python serial on the latest raspbian buster and have noticed that my uart script does not run properly on my new RPi4. I take the SD card out and plug it into my RP3B+ and it works as expected.
When I test the RPi4 uart in loopback, it performs as expected. When I use a USB uart to talk with it, the serial stream is corrupted. The same USB uart works just fine on the RPi3B+.

The only difference is the RPi4 and RPi3B+.
Any ideas as to what could be happening would be appreciated.

Code: Select all

#!/usr/bin/python

import time
import serial

# configure the serial connections (the parameters differs on the device you are connecting to)
ser_0 = serial.Serial(
#	port='/dev/ttyUSB0',
	port='/dev/ttyS0',
	baudrate=115200,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.SEVENBITS,
	xonxoff=False,
	rtscts=False,
	dsrdtr=False
)

ser_1 = serial.Serial(
	port='/dev/ttyUSB0',
#	port='/dev/ttyS0',
	baudrate=115200,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.SEVENBITS,
	xonxoff=False,
	rtscts=False,
	dsrdtr=False
)

ser_0.isOpen()
ser_0.reset_input_buffer()
ser_0.reset_output_buffer()
ser_1.isOpen()
ser_1.reset_input_buffer()
ser_1.reset_output_buffer()

print 'Enter your commands below.\r\nInsert "exit" to leave the application.'

input=1
while 1 :
    # get keyboard input
    input = raw_input(">> ")
        # Python 3 users
        # input = input(">> ")
    if input == 'exit':
        ser_0.close()
        ser_1.close()
        exit()
    else:
        # send the character to the device
	ser_0.write(input)
        out = ''
        # let's wait one second before reading output (let's give device time to answer)
        time.sleep(1)
        while ser_1.inWaiting() > 0:
            out += ser_1.read(1)

        if out != '':
            print "ser_1 rx >>" + out
	
	    ser_1.write(input)
            out = ''
            # let's wait one second before reading output (let's give device time to answer)
            time.sleep(1)
            while ser_0.inWaiting() > 0:
                out += ser_0.read(1)

            if out != '':
                print "ser_0 rx >>" + out

Kai-wob
Posts: 2
Joined: Sun Jul 07, 2019 11:12 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Sun Jul 07, 2019 11:21 pm

My older C program work again on pi4 when I set the double baudrate as needed. Seems like a bug with the clock source of the uart I use ttyS0.
I found out by looking at my. All bit timings were x2 long then set.

chipace
Posts: 120
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Mon Jul 08, 2019 2:16 am

Kai-wob wrote:
Sun Jul 07, 2019 11:21 pm
My older C program work again on pi4 when I set the double baudrate as needed. Seems like a bug with the clock source of the uart I use ttyS0.
I found out by looking at my. All bit timings were x2 long then set.
Thank you Kai-wob! Doubling the ttyS0 baudrate fixed my python script.

chipace
Posts: 120
Joined: Sat Jun 29, 2019 2:56 am
Location: brown paper bag in a septic tank

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Tue Jul 09, 2019 12:41 am

Firmware update ("sudo rpi-update") has fixed this issue. I no longer need to specify twice the baud rate for ttyS0.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Tue Jul 09, 2019 7:55 am

That's good to hear. We've been through a number of clocking changes with the 4B, but things are settling down now.

Kai-wob
Posts: 2
Joined: Sun Jul 07, 2019 11:12 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu Jul 11, 2019 10:27 pm

Yes, fixed now by the last update/upgrade.

pfletch101
Posts: 629
Joined: Sat Feb 24, 2018 4:09 am
Location: Buffalo, NY, USA

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Fri Aug 30, 2019 10:32 pm

It is now nearly September, and my 4B has been regularly updated/upgraded since I got it, earlier this month, but I am also seeing problems with serial transmission with a small beta HAT that uses UART0 and works perfectly on a 3. I don't have an oscilloscope or another serial terminal I can test it with, but the symptoms seem similar to what the OP originally reported. Could the issue have become 'unfixed'?

{EDIT} Never mind! The problem was user error! I had not properly enabled the UART.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 1:20 pm

We're having an issue where our serial communication (port ttyS0) is working perfectly on or RPi 3 B/B+ devices, but using the same SD card on the RPi 4B is not working. Any thoughts on this? All configuration settings are the same between the RPis. We're using CuteCom to test with a 3rd party peripheral.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 4:12 pm

When you say it is not working, what are the symptoms?

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 5:56 pm

CuteCom is not receiving a response from the device.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 6:28 pm

1. Do you have "enable_uart=1" in config.txt?
2. Are you explicitly setting the baud rate using CuteCom?
3. With "dtoverlay=miniuart-bt" in config.txt, do you get a response using /dev/ttyAMA0?

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 9:24 pm

Yes to questions 1 and 2.

For question 3, what kind of response should be expected?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 9:37 pm

All I'm asking you to do is try the other UART. You wrote:
CuteCom is not receiving a response from the device.
so you must know what response to expect.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 9:50 pm

okay. We'll try the other UART using the same peripheral.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Wed May 06, 2020 10:13 pm

Selecting "/dev/ttyAMA0" after setting "dtoverlay=miniuart-bt" did not work either (no response from the peripheral).

In the Raspberry Pi doc, I noticed the instructions: "miniuart-bt switches the Raspberry Pi 3 and Raspberry Pi Zero W Bluetooth function to use the mini UART (ttyS0), and restores UART0/ttyAMA0 to GPIOs 14 and 15. ... It is also necessary to edit /lib/systemd/system/hciuart.service and replace ttyAMA0 with ttyS0"

Does the "hciuart.service" file need to be changed as well? I don't see anywhere to replace "ttyAMA0" with "ttyS0" in that file as the instructions indicate.

...thanks for your help

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 7:44 am

That documentation is out of date - the necessary changes are now automatic. But for the sake of this test you can ignore Bluetooth and use "dtoverlay=disable-bt" instead, which will just leave /dev/ttyAMA0 on GPIOs 14 & 15.

Also, make sure that there isn't a "console=ttyAMA0", "console=ttyS0" or "console=serial0" in cmdline.txt.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 12:59 pm

I've confirmed the settings you've suggested and still no luck. FYI, I've also confirmed that there is not a problem with the wiring/connections to headers. As mentioned, it all works perfectly on the RPi 3B using the same SD card and wiring. What's different on the RPi 4?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 2:08 pm

There should be nothing different on the Pi 4 at the user level. As far as I know, nobody else is having this problem so there must be something different about your setup.

Please provide:

1. the output of "raspi-gpio get",

2. the contents of config.txt, and

3. the contents of cmdline.txt.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 2:46 pm

BANK0 (GPIO 0 to 27):
GPIO 0: level=1 fsel=0 func=INPUT pull=UP
GPIO 1: level=1 fsel=0 func=INPUT pull=UP
GPIO 2: level=1 fsel=4 alt=0 func=SDA1 pull=UP
GPIO 3: level=1 fsel=4 alt=0 func=SCL1 pull=UP
GPIO 4: level=1 fsel=0 func=INPUT pull=UP
GPIO 5: level=1 fsel=0 func=INPUT pull=UP
GPIO 6: level=1 fsel=0 func=INPUT pull=UP
GPIO 7: level=1 fsel=1 func=OUTPUT pull=UP
GPIO 8: level=1 fsel=1 func=OUTPUT pull=UP
GPIO 9: level=0 fsel=4 alt=0 func=SPI0_MISO pull=DOWN
GPIO 10: level=1 fsel=4 alt=0 func=SPI0_MOSI pull=DOWN
GPIO 11: level=0 fsel=4 alt=0 func=SPI0_SCLK pull=DOWN
GPIO 12: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 13: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 14: level=1 fsel=4 alt=0 func=TXD0 pull=NONE
GPIO 15: level=0 fsel=4 alt=0 func=RXD0 pull=UP
GPIO 16: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 17: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 18: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 19: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 20: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 21: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 22: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 23: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 24: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 25: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 26: level=0 fsel=0 func=INPUT pull=DOWN
GPIO 27: level=0 fsel=0 func=INPUT pull=DOWN


Cmdline.txt:
console=tty1 root=PARTUUID=6c586e13-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

Config.txt:
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1920
#framebuffer_height=1080

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
hdmi_group=2
hdmi_mode=14

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
dtparam=spi=on

# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on

[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2

[all]
#dtoverlay=vc4-fkms-v3d
enable_uart=1
dtoverlay=disable-bt

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 3:01 pm

That all looks correct.

Have you tried connecting the Pi UART GPIOs (and a GND, of course) to something else, like a USB serial cable?

Are you sure the voltage levels are correct for the device you are driving? Is it possible that you're seeing an analogue electrical problem? BCM2711 does have different GPIO drive circuitry, so something which is out of spec might work on BCM2837 but not BCM2711 (and vice versa).

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 4:15 pm

The power and ground of the peripheral is driven independent of the Pi, so that shouldn't be a problem. We have not tested a different serial device yet. We do have other RPi 4s and they exhibit the same behavior. If you have any other suggestions, pls LMK.

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 4:22 pm

The power and ground of the peripheral is driven independent of the Pi
But they do have a common ground, don't they? If not, no wonder there are comms problems.

If you want to go right back to basic you can do a loopback from TX to RX on the 4B. If that works, use a null-modem hookup between the Pi 4 and the Pi 3 - pin 6<=>6, 8<=>10, 10<=>8 - and run CuteCom on both ends.

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 4:29 pm

Yes, common ground.

Good suggestion about using two Pis. Thx

AmpedUp
Posts: 11
Joined: Wed May 06, 2020 12:24 pm

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 9:04 pm

So the peripheral is 5v TTL and the Pi is 3.3v TTL. The RPi 3 must be more tolerant. We're looking into a level shifter. Is there a good single channel unit you'd recommend?

PhilE
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 3152
Joined: Mon Sep 29, 2014 1:07 pm
Location: Cambridge

Re: raspberry pi 4 gpio uart (ttyS0) not working like 3B+

Thu May 07, 2020 9:06 pm

Don't ask me - I'm just a softie.

Return to “Device Tree”