Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A64 => Topic started by: zharry on November 17, 2019, 05:24:44 PM

Title: RTS for UART1 and UART2
Post by: zharry on November 17, 2019, 05:24:44 PM
Hi.
I've enabled the UART1 and 2 overlays by downloading dts files and adding them with the armbian-add-overlay. Then I edited /boot/armbianEnv.txt, where I found the line

user_overlays=sun50i-a64-uart2 sun50i-a64-uart1
and after it added lines

param_uart1_rtscts=1
param_uart2_rtscts=1

What I am getting, is working UART TX and RX pins, but I can not get the RTS to work for any of those pins. I checked the overlay data - pin names are correct. Is this a bug or am I missing something? If it is a bug, can anyone direct me to a solution? Sorry for noob question, first time working with overlays.
Title: Re: RTS for UART1 and UART2
Post by: LubOlimex on November 18, 2019, 11:45:32 AM
Hey,

1) Where did you get the overlays exactly?

2) Can you paste the modified overlays that you used for UART1 and UART2 (with the RTS and CTS addition)?

3) Where exactly on the board do you access/measure UART1 and UART2 pins (RX1/2, TX1/2, RTS1/2, CTS1/2)?

Thanks.
Title: Re: RTS for UART1 and UART2
Post by: selfbg on November 18, 2019, 11:50:42 AM
Try the following overlay:
/dts-v1/;
/plugin/;

/ {
 fragment@0 {
 target = <&uart1>;
 __overlay__ {
 pinctrl-names = "default";
 pinctrl-0 = <&uart1_pins>, <&uart1_rts_cts_pins>;
 status = "okay";
 };
 };

 fragment@1 {
 target = <&pio>;
 __overlay__ {
 uart2_rts_cts_pins: uart2_rts_cts {
 pins = "PA2", "PA3";
 function = "uart2";
 };
 };
 };

 fragment@2 {
 target = <&uart2>;
 __overlay__ {
 pinctrl-names = "default";
 pinctrl-0 = <&uart2_pins>, <&uart2_rts_cts_pins>;
 status = "okay";
 };
 };
};

Remove sun50i-a64-uart2 and sun50i-a64-uart1. Also remove the params.
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 18, 2019, 02:36:05 PM
Hi, thanks for replayes,

Quote from: LubOlimex on November 18, 2019, 11:45:32 AM1) Where did you get the overlays exactly?

From official armbian github repo:
https://github.com/armbian/sunxi-DT-overlays/tree/master/sun50i-a64

Quote from: LubOlimex on November 18, 2019, 11:45:32 AM2) Can you paste the modified overlays that you used for UART1 and UART2 (with the RTS and CTS addition)?

I did not modify them - they seem to already have the RTC and CTS enabled, or am I reading it wrong?
In this overlay:
https://github.com/armbian/sunxi-DT-overlays/blob/master/sun50i-a64/sun50i-a64-uart2.dts
They have defined:
uart2_rts_cts_pins: uart2_rts_cts_pins {
pins = "PB2", "PB3";
function = "uart2";
};
Which corresponds to A64 datasheet pins for RTS and CTS.

Quote from: LubOlimex on November 18, 2019, 11:45:32 AM3) Where exactly on the board do you access/measure UART1 and UART2 pins (RX1/2, TX1/2, RTS1/2, CTS1/2)?

And I have connected CP2012 USB serial for terminal monitoring and hooked Tek TDS2024 scope on pins 10, 12 and 14 of GPIO1 connector on the A64-OLinuXino-2Ge8G-IND board. Pins 10, 12 and 14 correspond to PB0, PB1 and PB2 which in turn correspond to TX, RX and RTS of UART2 judging by boards schematic and CPU datasheet.

For UART1 I used the same setup, soldered with coil winding wires to pins 42 through 44 of U11 Combo Module, which is not fitted in my board (Idustrial grade version).

For both ports I get RX and TX pins to function as expected, so I conclude that the pins, data pin part of Armbian provided overlays and general idea should be somewhat in the right direction.

Quote from: selfbg on November 18, 2019, 11:50:42 AMpins = "PA2", "PA3";
Regarding this overlay - are you sure about PA2 and PA3? There is no Port A available on the package, judging by both - board schematic and CPU datasheet.
Never the less I will try to use it and tell you what happened in one hour.
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 18, 2019, 03:35:34 PM
Quote from: selfbg on November 18, 2019, 11:50:42 AMTry the following overlay:

Well... It killed both USBs, so I have no WiFi or keyboard anymore. I guess Poort A is used for something internally.

I do not have output on debug serial either, so I can't even test if TTY1 and 2 work, so will have to remove the overlay by editing the /boot on SDcard from another machine.

So question still stands.

EDIT: Tried the same suggested overlay with PB instead of PA - still kills USB.
Title: Re: RTS for UART1 and UART2
Post by: LubOlimex on November 18, 2019, 03:48:58 PM
Quotepins = "PA2", "PA3";

This seems like a typo/mistake. Just use the pins from the datasheet - PB2, PB3 I believe for UART2.
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 18, 2019, 06:00:05 PM
Quote from: LubOlimex on November 18, 2019, 03:48:58 PMJust use the pins from the datasheet - PB2, PB3 I believe for UART2.

I did. With same overlay it kills USB, so I lose Ethernet and keyboard.

Anyway - I went back to original overlay and checked the register values:


root@olinuxino:/home/harry# busybox devmem 0x01c28810
0x0000002B
root@olinuxino:/home/harry# busybox devmem 0x01c28808
0x000000C1
root@olinuxino:/home/harry# busybox devmem 0x01c20824
0x77772222


So AutoFlow is on, FIFO is enabled and Port B pins 0 through 3 are muxed to UART. Still nothing on the RTS pin. It just stays in perpetual 0. Even went as far as to add an external pullup - still nothing. Out of ideas here.

EDIT:
Just to double check for stupid mistakes I turned off the AutoFlow bit and cleared the RTS bit:
root@olinuxino:/home/harry# busybox devmem 0x01c28810 w 0x0B
root@olinuxino:/home/harry# busybox devmem 0x01c28810 w 0x09
And the RTS pin went hihg, as expectd.

So is this a silicon bug of A64? Has anyone ever seen the RTS pin actually working in auto flow control mode?

Perhaps anyone has tried using software controlled 485 DE/RE toggling in UART driver, like the em485 patch or something similar? Any help at this point would be appreciated, I really would not wish to add external USB serial ports just to handle a stupid direction change pin :(
Title: Re: RTS for UART1 and UART2
Post by: martinayotte on November 18, 2019, 10:00:03 PM
Quote from: zharry on November 18, 2019, 06:00:05 PMPerhaps anyone has tried using software controlled 485 DE/RE toggling in UART driver, like the em485 patch or something similar?

Right ! You absolutely need to use em485 patch, otherwise the dw-apb-uart driver (aka 8250_dw.c) won't support RS485 direction control.
Title: Re: RTS for UART1 and UART2
Post by: HarrisonCreates on November 18, 2019, 11:55:23 PM
Quote from: LubOlimex on November 18, 2019, 03:48:58 PM
Quotepins = "PA2", "PA3";

This seems like a typo/mistake. Just use the pins from the datasheet - PB2, PB3 I believe for UART2.

Where is the datasheet exactly? I can't find it anywhere (not even on the product page) and it would help me out a bunch with my own question...
Title: Re: RTS for UART1 and UART2
Post by: LubOlimex on November 19, 2019, 08:12:43 AM
The A64's datasheet. Just search for "Allwinner A64 Datasheet".
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 19, 2019, 01:56:55 PM
Quote from: HarrisonCreates on November 18, 2019, 11:55:23 PMWhere is the datasheet exactly?

Datasheet effectively has only the pinout of package, I got confused a bit too.

But if you go to LINUX-SUNXI community wiki (https://linux-sunxi.org/A64), under Documentation you will see the Datasheet v1.1 and User Manual v1.0 - If you want the memory map, register maps and descriptions and so on - you want the User Manual not Datasheet.
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 19, 2019, 03:41:21 PM
Quote from: martinayotte on November 18, 2019, 10:00:03 PMYou absolutely need to use em485 patch,

I'm looking at https://docs.armbian.com/Developer-Guide_Build-Preparation/
I've got as far as to the kernel configuration. It does not use 8250_dw.c by default, it uses uart.c (which is some very simple character put and get thing) from  sun50iw1p1/drivers/uart

Is the method described in armbian docs even right for compiling kernel for Olimex A64 boad?

Should I apply some patches from Olimex before running build.sh?

This is all so confusing, is there a known working tutorial / howto / guide / anything on building custom kernel for olinuxino64?
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 19, 2019, 03:50:22 PM


Quote from: zharry on November 19, 2019, 03:41:21 PMThis is all so confusing, is there a known working tutorial / howto / guide / anything on building custom kernel for olinuxino64?

Let me rephrase it - is there a guide on how exactly was this image:

ftp://staging.olimex.com/Allwinner_Images/a64-olinuxino/linux/1.latest_images/bionic/images/

built?
Title: Re: RTS for UART1 and UART2
Post by: martinayotte on November 19, 2019, 04:05:17 PM
Quote from: zharry on November 19, 2019, 03:41:21 PMIt does not use 8250_dw.c by default, it uses uart.c
Where this statement comes from ? There is no such uart.c file in Mainline kernel ...
All the SoC from AllWinner is using the driver from drivers/tty/serial/8250/8250_dw.c
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 19, 2019, 04:47:56 PM
Quote from: martinayotte on November 19, 2019, 04:05:17 PMWhere this statement comes from ?

Well, when compiling kernel with default settings while following the Armbian guide, using the configuration for lime64 (I guess that's how they call olinuxino64, as there there was nothing else similar) the 8250 driver is not selected. The SUNXI-UART Controller is though.

Sorry, it's sunxi-uart.c not uart.c

It's located in:
build/cache/sources/linux-pine64/my-hacks-1.2/drivers/tty/serial$

Or am I getting it wrong?

And while we are at it, is there a way to build just that one driver as kernel module to be inserted by modprobe or does one absolutely must compile and change whole kernel + uboot?

Title: Re: RTS for UART1 and UART2
Post by: LubOlimex on November 19, 2019, 04:50:59 PM
Quote from: zharry on November 19, 2019, 03:50:22 PM
Quote from: zharry on November 19, 2019, 03:41:21 PMThis is all so confusing, is there a known working tutorial / howto / guide / anything on building custom kernel for olinuxino64?

Let me rephrase it - is there a guide on how exactly was this image:

ftp://staging.olimex.com/Allwinner_Images/a64-olinuxino/linux/1.latest_images/bionic/images/

built?


I believe we usually build it with:

git clone --branch olimex https://github.com/olimex/build

cd build

touch .ignore_changes

./compile.sh \
                        BRANCH=next \
                        BOARD=olinuxino-a64 \
                        USE_MAINLINE_GOOGLE_MIRROR=yes \
                        NO_APT_CACHER=yes \
                        WIREGUARD=no \
                        EXTRAWIFI=no
Title: Re: RTS for UART1 and UART2
Post by: martinayotte on November 20, 2019, 11:36:48 PM
Quote from: zharry on November 19, 2019, 04:47:56 PMSorry, it's sunxi-uart.c not uart.c
I think you are mixing branches, this one still doesn't exist in Mainline, it is only in Legacy BSP 3.4.y, and this driver doesn't handle RS485 either ...
Title: Re: RTS for UART1 and UART2
Post by: zharry on November 23, 2019, 04:16:39 AM
Quote from: LubOlimex on November 19, 2019, 04:50:59 PMI believe we usually build it with:

Code Select Expand
git clone --branch olimex https://github.com/olimex/build

cd build

touch .ignore_changes

./compile.sh \
                        BRANCH=next \
                        BOARD=olinuxino-a64 \
                        USE_MAINLINE_GOOGLE_MIRROR=yes \
                        NO_APT_CACHER=yes \
                        WIREGUARD=no \
                        EXTRAWIFI=no

Yes, thanks you, it works.

Quote from: martinayotte on November 20, 2019, 11:36:48 PMI think you are mixing branches, this one still doesn't exist in Mainline, it is only in Legacy BSP 3.4.y, and this driver doesn't handle RS485 either ...

Yes, I was trying to build the Armbian main branch. With the branch LubOlimex provided, I can build same kernel I have and it runs.

Now next question is how do I apply a patch to it? It seems that if I just patch by copying patch files next to drivers directory and running
patch -p1 < patchFile
directly files in build/cache/sources/linux-mainline/linux-5.2.y, driver is patched successfully, but when I run ./compile.sh it seems that the script cleans the patch files and changes to sources.

So is there some special place to put the patches or is there a parameter to use so the driver stays patched?
Title: Re: RTS for UART1 and UART2
Post by: martinayotte on November 23, 2019, 03:33:15 PM
Quote from: zharry on November 23, 2019, 04:16:39 AMSo is there some special place to put the patches
In the Armbian build tree, you simply need to place the patch under userpatches/kernel/sunxi-dev/
Title: Re: RTS for UART1 and UART2
Post by: zharry on December 07, 2019, 02:54:04 PM
Done it. Took a while, patch, driver and build system documentation is a bit lacking or I'm just not used to open source. Anyway - with some tweaks to termios in c application 485 Works great after patching tyhe kernel with em485 patches, some lurking and some help from friends.

So first:

Quote from: martinayotte on November 23, 2019, 03:33:15 PMuserpatches/kernel/sunxi-dev/

No not exaclty. Seems they actually have to be in userpatches/kernel/sunxi-next-olinuxino. Well, at least that worked for me.

And 2nd - the patch files should end with *.patch or the build script will not find them.


So ToDo list for getting 485 working is:
1. Make new DTS remapping RTS pin to UART
2. Compile, apply DTS
3. Set up VM with Ubuntu
4. Use LubOlimex build instructions to get the toolchain
5. Save patches form kernel mailing list to *.patch files
6. Put patches in build patch folder
7. Build it with kernel only and switch
8. transfer *.deb to board
9. install *.deb and reboot
10. write the c app to use 485 features

Do you think there would be point in dropping my patches, test apps, DTS and notes to GitHub and make a detailed tutorial on this? Or nobody else is interested in it or everyone else knows how to do it already and there is no point spending a day to write that?

Title: Re: RTS for UART1 and UART2
Post by: Klaus on March 17, 2020, 11:47:45 AM
Hi zharry,
I would be very interested in your detailed tutorial, as I'm trying to implement a RS485 functionality on the A20 based olimex board.

Thanks,
Klaus

@martinayotte: nice to meet you here too. :)