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

Re: Pi-4 Activating additional UART ports

Tue Aug 06, 2019 3:42 pm

i2c0 is irrelevant when you have i2c1!

But seriously, it depends on your application - if you are building a network-connected serial port concentrator then i2c is probably not that important.

danjperron
Posts: 3440
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Pi-4 Activating additional UART ports

Tue Aug 06, 2019 4:58 pm

i2c0 is irrelevant when you have i2c1!
This mean that ID_SD(pin 27) and ID_SC (pin 28) could be used for other things?
I though they were reserved for eerom on hat board ?

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

Re: Pi-4 Activating additional UART ports

Tue Aug 06, 2019 6:35 pm

On all Pis with 40-pin headers, pins 27 and 28 are wired to GPIOs 0 & 1, which are configured for I2C0. GPIOs 2 & 3, which can be configured for I2C1, are available on other header pins (3 & 5). This means that I2C0 is notionally reserved for the firmware (e.g. for driving the camera), but in practise on many Pis (including the 4B) by the time the ARMs are started the firmware will not use I2C0 again unless you use the camera.

danjperron
Posts: 3440
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: Pi-4 Activating additional UART ports

Tue Aug 06, 2019 7:53 pm

Thanks PhilE,

Are you sure that is pin 28 and 29 . I think it is 27 and 28.

To summarize.

uart2 uses pins that could conflict with the camera and any kind of "hat" with eerom. Also If you enable the handshake, I2C1 has to be disabled.
uart3 use GPIO4 then you should consider to change 1 wire GPIO pin if you need 1 wire.
uart4 use the SPI GPIOs. SPI needs to be disabled.
No conflict with the uart5. This is the safest one to use.

oops uart3 uses GPIO7 if you enable handshake then you need to disable SPI.

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

Re: Pi-4 Activating additional UART ports

Tue Aug 06, 2019 8:20 pm

> Are you sure that is pin 28 and 29 . I think it is 27 and 28.

You're right - corrected. It comes from preferring to count from zero.

asafyurdakul
Posts: 2
Joined: Fri Dec 29, 2017 3:32 pm

Re: Pi-4 Activating additional UART ports

Wed Aug 07, 2019 10:08 am

Thank you PhilE
I have found my problem.

I have checked command "raspi-gpio get 0-15". I see that there is no problem like your result.
But when I run my program , the result of this command changed. 4,5 (Uart3) and 13 (Uart5) gpio pins "alt=4" properties are cleared and "func" properties are changed.

My program is runnig on mono c# platform.
I am using Unosquare library "https://github.com/unosquare/raspberryio". That is perfect library for mono.


On my program I was enabled SPI. I noticed that when I was done it 4,5 and 13 pins properties changed.
Cancelled codes are below
Pi.Init<BootstrapWiringPi>();
Pi.Spi.Channel0Frequency = SpiChannel.MinFrequency;

So, I cancelled these SPI init codes and problem solved. Now all 5 uarts works without any problem.

Best regards

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

Re: Pi-4 Activating additional UART ports

Wed Aug 07, 2019 10:15 am

I'm happy you found the problem. A lot of libraries talk directly to the hardware, bypassing the Linux drivers, sometimes for good reasons (the old sysfs API to GPIOs is notoriously slow - the new ioctl-based gpiolib interface is much quicker), sometimes not. If the Unosquare library had used a Linux API then it would have received an error indicating that the pins were in use, which should have been easier to track down.

leoP4
Posts: 6
Joined: Mon Aug 05, 2019 6:58 pm

Re: Pi-4 Activating additional UART ports

Wed Aug 07, 2019 1:31 pm

Hi everyone

I'm interested in the topic, but how to use the script? I can't understand the passage.
Sorry for the trivial question.

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

Re: Pi-4 Activating additional UART ports

Wed Aug 07, 2019 1:36 pm

Which script are you referring to? If you just want to enable one of the additional serial ports on Pi4, just add "dtoverlay=uart2" to config.txt, where "uart2" could also be "uart3", "uart4" or "uart5". After rebooting, and if you don't have a clash with something else that needs the same pins, you will find a new /dev/ttyAMA*.

leoP4
Posts: 6
Joined: Mon Aug 05, 2019 6:58 pm

Re: Pi-4 Activating additional UART ports

Wed Aug 07, 2019 1:43 pm

I was referring to the script mentioned above, that is #! / bin / sh

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

Re: Pi-4 Activating additional UART ports

Mon Aug 12, 2019 3:39 pm

That script just sends the characters 0-5 to ttyAMA0-5 every second. Attaching a serial cable to one of the groups of UART pins, the character that appears in your terminal emulator reveals which ttyAMA* it is connected to.

martinslater
Posts: 3
Joined: Mon Jun 27, 2016 8:31 am

Re: Pi-4 Activating additional UART ports

Thu Nov 14, 2019 10:22 am

Are these extra UART ports on the PI Zero W as well?
Last edited by martinslater on Thu Nov 14, 2019 10:27 am, edited 1 time in total.

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

Re: Pi-4 Activating additional UART ports

Thu Nov 14, 2019 10:27 am

No - they were new in the BCM2711, which is only used in the 4B.

martinslater
Posts: 3
Joined: Mon Jun 27, 2016 8:31 am

Re: Pi-4 Activating additional UART ports

Thu Nov 14, 2019 10:35 am

Darn. Thanks Phil

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Fri Jan 03, 2020 7:05 am

PhilE wrote:
Mon Jul 29, 2019 12:07 pm
On my Pi4 it appears as /dev/ttyAMA1 - there are no "serial<x>" aliases for the additional UARTs yet.
The two UARTS 0 and 1 appear to be connected to the same GPIO pins.
Could that solve one of my yet unsolved problem: Having two programs reading the same RX information?
I have currently on my RPi3b+ swapped the UARTS to have /dev/ttyAMA0 on GPIO14/15.
The first program (a blackbox for me) takes full control of /dev/tty AMA0
Would I then on a RPi4 be able to read the rx info again from /dev/tty AMA1?

User avatar
rpdom
Posts: 16151
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi-4 Activating additional UART ports

Fri Jan 03, 2020 7:21 am

Only one of the UARTs can be active on the pins at any time. You cannot have two different functions set on a GPIO.

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Fri Jan 03, 2020 8:15 am

Thank you for the quick reply, I was referring to page 10 of the preliminary RPi4 documentation.
5.1.2 GPIO Alternate Functions
Default
GPIO Pull ALT0 ALT1 ALT2 ALT3 ALT4 ALT5
0 High SDA0 SA5 PCLK SPI3 CE0 N TXD2 SDA6
1 High SCL0 SA4 DE SPI3 MISO RXD2 SCL6
2 High SDA1 SA3 LCD VSYNC SPI3 MOSI CTS2 SDA3
3 High SCL1 SA2 LCD HSYNC SPI3 SCLK RTS2 SCL3
4 High GPCLK0 SA1 DPI D0 SPI4 CE0 N TXD3 SDA3
5 High GPCLK1 SA0 DPI D1 SPI4 MISO RXD3 SCL3
6 High GPCLK2 SOE N DPI D2 SPI4 MOSI CTS3 SDA4
7 High SPI0 CE1 N SWE N DPI D3 SPI4 SCLK RTS3 SCL4
8 High SPI0 CE0 N SD0 DPI D4 - TXD4 SDA4
9 Low SPI0 MISO SD1 DPI D5 - RXD4 SCL4
10 Low SPI0 MOSI SD2 DPI D6 - CTS4 SDA5
11 Low SPI0 SCLK SD3 DPI D7 - RTS4 SCL5
12 Low PWM0 SD4 DPI D8 SPI5 CE0 N TXD5 SDA5
13 Low PWM1 SD5 DPI D9 SPI5 MISO RXD5 SCL5
14 Low TXD0 SD6 DPI D10 SPI5 MOSI CTS5 TXD1
15 Low RXD0 SD7 DPI D11 SPI5 SCLK RTS5 RXD1

16 Low FL0 SD8 DPI D12 CTS0 SPI1 CE2 N CTS1
17 Low FL1 SD9 DPI D13 RTS0 SPI1 CE1 N RTS1
18 Low PCM CLK SD10 DPI D14 SPI6 CE0 N SPI1 CE0 N PWM0
19 Low PCM FS SD11 DPI D15 SPI6 MISO SPI1 MISO PWM1
20 Low PCM DIN SD12 DPI D16 SPI6 MOSI SPI1 MOSI GPCLK0
21 Low PCM DOUT SD13 DPI D17 SPI6 SCLK SPI1 SCLK GPCLK1
22 Low SD0 CLK SD14 DPI D18 SD1 CLK ARM TRST SDA6
23 Low SD0 CMD SD15 DPI D19 SD1 CMD ARM RTCK SCL6
24 Low SD0 DAT0 SD16 DPI D20 SD1 DAT0 ARM TDO SPI3 CE1 N
25 Low SD0 DAT1 SD17 DPI D21 SD1 DAT1 ARM TCK SPI4 CE1 N
26 Low SD0 DAT2 TE0 DPI D22 SD1 DAT2 ARM TDI SPI5 CE1 N
27 Low SD0 DAT3 TE1 DPI D23 SD1 DAT3 ARM TMS SPI6 CE1 N
But the alternate 5 allocation of GPIO 14,15 is probably thought to use them while keeping UART0 on Bluetooth?
That looks interesting. How to enable that?
Last edited by rin67630 on Fri Jan 03, 2020 8:43 am, edited 1 time in total.

User avatar
rpdom
Posts: 16151
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Pi-4 Activating additional UART ports

Fri Jan 03, 2020 8:41 am

rin67630 wrote:
Fri Jan 03, 2020 8:15 am
But the alternate 5 allocation of GPIO 14,15 is probably thought to use them while keeping UART0 on Bluetooth?
That looks interesting. How to enable that?
I guessing that UART0 for Bluetooth is via ALT2 on GPIOs 36 and 37. Setting ALT5 on GPIO 14 and 15 will also mean they are connected to the same UART, but I don't know how the Pi would react to having two potential ports on different pins.
rin67630 wrote:
Fri Jan 03, 2020 8:15 am
the preliminary RPi5 documentation.
Pi 5? I haven't seen that one yet.

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Fri Jan 03, 2020 8:56 am

rpdom wrote:
Fri Jan 03, 2020 8:41 am
Pi 5? I haven't seen that one yet.
I've always been ahead of my time... ;-)
Mistake corrected.
rpdom wrote:
Fri Jan 03, 2020 8:41 am
I guessing that UART0 for Bluetooth is via ALT2 on GPIOs 36 and 37. Setting ALT5 on GPIO 14 and 15 will also mean they are connected to the same UART, but I don't know how the Pi would react to having two potential ports on different pins.
IMHO setting Alt 5 on GPIO 14 and 15 should connect /dev/ttyAMA1 to them leaving /dev/ttyAMA0 to Bluetooth. with no need for swapping the mini-uart any more, Right?

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

Re: Pi-4 Activating additional UART ports

Mon Jan 06, 2020 10:36 am

On all Pis, UART0 is a PL011 that appear to Linux as ttyAMA<n>, and UART1 is an 8250 clone that appears as ttyS0. On a Pi4, UART2-5 are additional PL011s that also appear as ttyAMA<n>. The number starts at 0 for the first enabled PL011 and counts up through all the enabled interfaces. The numbering is stable for any given combination of UARTs, but enabling or disabling one can change the number assignments of others.

Adding "enable_uart=1" to config.txt on a Pi4 enables ttyS0 (UART1) on GPIOs 14 & 15 (Alt5), leaving UART0 driving the Bluetooth interface on 30-33 (Alt3). Adding "dtoverlay=disable-bt" switches the UART roles so that UART0 is mapped to 14 & 15 (Alt0), leaving UART1 unmapped.

As rpdom says, you can only enable one function on any pin at once, and enabling the same alternate function (e.g. RXD0) on multiple pins usually results in failure (outputs tend to work, inputs don't, but it is essentially undefined behaviour).

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Mon Jan 13, 2020 8:17 pm

PhilE wrote:
Mon Jan 06, 2020 10:36 am
Adding "enable_uart=1" to config.txt on a Pi4 enables ttyS0 (UART1) on GPIOs 14 & 15 (Alt5), leaving UART0 driving the Bluetooth interface on 30-33 (Alt3). Adding "dtoverlay=disable-bt" switches the UART roles so that UART0 is mapped to 14 & 15 (Alt0), leaving UART1 unmapped.
So, if I understood you well TXD1 and RXD1 in ALT5 config still are the mini-uart @/dev/ttyS0 as on the Pi3?
If i want to use one of the new UARTS2-5 it MUST be with ALT4 on other GPIO pins?
If I never plan to use HATs EEPROM, I could use UART2 on GPIO0 and 1?

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

Re: Pi-4 Activating additional UART ports

Mon Jan 13, 2020 9:35 pm

Yes, I believe that's all correct.

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Tue Jan 14, 2020 9:34 am

rin67630 wrote:
Mon Jan 13, 2020 8:17 pm
If I never plan to use HATs EEPROM, I could use UART2 on GPIO0 and 1?
So we still have got an apparent contradiction between the Pi4 specs for UART2 with ALT4 and the statement on the schematic:
DO NOT USE these pins for anything other than attaching an I2C ID EEPROM. Leave unconnected if ID EEPROM not required.
These pins are reserved for HAT ID EEPROM. At boot time this I2C interface will be interrogated to look for an EEPROM
that identifyes the attached board and allows automagic setup of the GPIOs (and optionally, Linux drivers).
Does the boot procedure cope for that and bypass the HAT sensing if ALT<>0 is set on GPIO 0,1 , or does it just send a bench of weird signals to TX and give up since it gets no usable response?

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

Re: Pi-4 Activating additional UART ports

Tue Jan 14, 2020 9:52 am

So we still have got an apparent contradiction between the Pi4 specs for UART2 with ALT4 and the statement on the schematic:
There's no contradiction - the GPIO capabilities are a feature of the 2711 SoC, while the pin usage is a feature of the Pi 4 board. The 2711 has potentially wider applications than just Pi 4, so this is to be expected.
Does the boot procedure cope for that and bypass the HAT sensing if ALT<>0 is set on GPIO 0,1 , or does it just send a bench of weird signals to TX and give up since it gets no usable response?
No pin function can be set (until the kernel boots, at which point the HAT probing is long since finished) except via the firmware. There are two ways to specify a pin function - dt-blob.bin and config.txt "gpio ..." directive. If the dt-blob doesn't have a valid "ID_SDA" declaration it skips the HAT probing. In addition, probing is also skipped if (at the point of probing, which is after the "gpio ..." handling) the declared ID_SDA and ID_SCL pins aren't inputs.

User avatar
rin67630
Posts: 891
Joined: Fri Mar 04, 2016 10:15 am

Re: Pi-4 Activating additional UART ports

Tue Jan 14, 2020 10:48 am

PhilE wrote:
Tue Jan 14, 2020 9:52 am
No pin function can be set (until the kernel boots, at which point the HAT probing is long since finished) except via the firmware. There are two ways to specify a pin function - dt-blob.bin and config.txt "gpio ..." directive. If the dt-blob doesn't have a valid "ID_SDA" declaration it skips the HAT probing. In addition, probing is also skipped if (at the point of probing, which is after the "gpio ..." handling) the declared ID_SDA and ID_SCL pins aren't inputs.
Thank you, I am trying to understand your statements.

Did I understand right, that without a customized dt-blob, (I will not yet go into the experience of compiling a new one yet), the HAT probing will occur anyway?
If the UART of another device (e.g. an Arduino pro mini) is attached, its RX-line will then get some (from its point of view) weird signals from (at that time) SDA0.
On the other Pin GPIO1 the Pi may* have a short output conflict: the UART of the Arduino TX could* drive a <20mA "high" to SCL0.
The Pi should IMHO survive that.
So if the Arduino flushes the UART after startup it should just be OK.

I think I will try that...
* I think I can prevent the output conflict on the arduino side.

Return to “Device Tree”