balena-os / balena-allwinner

Apache License 2.0
21 stars 14 forks source link

UART Communications on OrangePi Zero Pins #102

Open Andrew-Wallace opened 4 years ago

Andrew-Wallace commented 4 years ago

Hello,

I am having trouble connecting a UART device to the OrangePi Zero from the onboard pins. When physically connected, the /dev/ttyS1 does not present itself to the kernel and it does not show that a device is connected. It appears that these ports are not enabled. Any help on this would be great!

Thank you

acostach commented 4 years ago

@Andrew-Wallace thank you for letting us know, we'll look into this with high priority and get back to you as soon as possible

acostach commented 4 years ago

Hi @Andrew-Wallace ,

Currently I don't have an Orange Pi Zero to test on, as the Orange PI Zero is currently community supported.

However, I have an Orange Pi Lite v1 that I used for the purpose of this test. Please see below how I enabled uarts 1 and 2 on it. The procedure should be the pretty much the same on the Zero.

I've also released BalenaOS 2.45.1+rev1 for the Orange Pi Zero in the staging dashboard. Please try with this latest release, since the old one doesn't have support for overlayfs, nor does it contain the dtbos for enabling additional uarts.

Load the overlays:

root@010706e:~# mkdir -p /sys/kernel/config/device-tree/overlays/ttys1
root@010706e:~# cat /mnt/boot/dtb/overlay/sun8i-h3-uart1.dtbo > /sys/kernel/config/device-tree/overlays/ttys1/dtbo
root@010706e:~# mkdir -p /sys/kernel/config/device-tree/overlays/ttys2
root@010706e:~# cat /mnt/boot/dtb/overlay/sun8i-h3-uart2.dtbo > /sys/kernel/config/device-tree/overlays/ttys2/dtbo

Check dmesg to see the devices:

root@010706e:~# dmesg | grep tty
[   70.203647] 1c28400.serial: ttyS1 at MMIO 0x1c28400 (irq = 145, base_baud = 1500000) is a U6_16550A
[   81.883428] 1c28800.serial: ttyS2 at MMIO 0x1c28800 (irq = 146, base_baud = 1500000) is a U6_16550A

Switch baudrate to test with PC:

root@010706e:~# stty -F /dev/ttyS1 115200
root@010706e:~# stty -F /dev/ttyS2 115200

Connect uart cable from PC to pins to 11 & 13 for uart2 and to 38 & 40 for uart1 and test with minicom:

root@010706e:~# cat /dev/ttyS1 
this is a test on uart1

root@010706e:/# cat /dev/ttyS2
this is another test

As I said, the procedure for loading the overlays on the Orange PI Zero should be pretty much the same, only the actual pins on the header will probably differ, for when you connect your cables.

Andrew-Wallace commented 4 years ago

Hello @acostach,

After trying your solution to my OrangePi zero using the new BalenaOS 2.45.1+rev1, ttyS1 and ttyS2 are now working. Thank you very much for your help!

Andrew-Wallace commented 4 years ago

After working with the serial ports, I noticed that this must be done each time the device is power cycled. Is there a way to automate this so it runs these commands on boot? I tried to run these in a node-red container I have on my device and /sys/kernel/config/device-tree is not presented to the container. What could I do to fix this issue?

Thank you

floion commented 4 years ago

Hi, are you using a multicontainer app or a single container? If single container, is the container running in privileged mode?

Andrew-Wallace commented 4 years ago

Hello, it is a multicontainer environment, but they are independent of each other. The container using the UART is a node-red container that is running in privileged mode.

acostach commented 4 years ago

Hi @Andrew-Wallace ,

In the hostOS, the path you mention is mounted as a configfs by the sys-kernel-config automount.

Therefore, for the container can you please try mounting it like this:

mount -t configfs none /sys/kernel/config

before loading the dtbo files as usual?

Andrew-Wallace commented 4 years ago

Hello @acostach,

After running the mount command this is as far as I got: `root@173d061f4c1c:/usr/src/node-red# mount -t configfs none /sys/kernel/config

root@173d061f4c1c:/usr/src/node-red# mkdir -p /sys/kernel/config/device-tree/overlays/ttys1

root@173d061f4c1c:/usr/src/node-red# cat /mnt/boot/dtb/overlay/sun8i-h3-uart1.dtbo > /sys/kernel/config/device-tree/overlays/ttys1/dtbo

cat: /mnt/boot/dtb/overlay/sun8i-h3-uart1.dtbo: No such file or directory`

Thank you

acostach commented 4 years ago

Hi @Andrew-Wallace ,

/mnt/boot is a path accessible only from the rootfs, not from container.

Please store the dtbos either in a volume of your application, or add them to your application container directory using COPY in your Dockerfile:

https://github.com/balena-io-projects/multicontainer-getting-started/blob/89ae9a1f756843db93cf0fa02ce7f2c4eb4db6ea/data/Dockerfile.template#L13

https://www.balena.io/docs/learn/develop/multicontainer/#named-volumes