UART inverted

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

UART inverted

Post by MaFr » Sun Feb 02, 2020 3:57 pm

Hi,

I need to implement an UART with RX,TX,RTS & CTS and also inverted lines on ESP32.

I have tried the following:

Code: Select all

uart.init(921600, bits=8, stop=1, parity=None, rx=rx, tx=tx, cts=cts, rts=rts, invert=[UART.INV_TX, UART.INV_RX, UART.INV_RTS, UART.INV_CTS]) 
but it thows me the following error:
TypeError: can't convert list to int
So, obviously an integer need to assigned to "invert". But, how to invert all lines?

Thank you very much for your help.

Best regards

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART inverted

Post by Roberthh » Sun Feb 02, 2020 4:26 pm

These UART_INVxx are numeric constants, which have to be or'ed. Use:

Code: Select all

uart.init(921600, bits=8, stop=1, parity=None, rx=rx, tx=tx, cts=cts, rts=rts, 
             invert=UART.INV_TX | UART.INV_RX | UART.INV_RTS | UART.INV_CTS) 
The documentation lacks an example for it.

MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

Re: UART inverted

Post by MaFr » Sun Feb 02, 2020 4:28 pm

wow, that was a really fast response.....Thank you so much!
Lol, I indeed about this syntax, but was not sure, if I can use it here...Thank you once again.

MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

Re: UART inverted

Post by MaFr » Mon Feb 03, 2020 4:43 pm

I noticed at my ESP32 that the RTS pin is not changing it's level.

At the moment I use the following command and I would expect the RTS level to fall to low before TX is sending. I can see TX sending, but RTS stays high all the time.

uart.init(921600, bits=8, stop=1, parity=None, rx=rx, tx=tx, cts=cts, rts=rts)

Could anybody confirm that it is possible to use RTS and CTS with ESP32? Which pins would you recommend? There might be any limitations?

Thank you so much...

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART inverted

Post by Roberthh » Mon Feb 03, 2020 5:17 pm

It looks like the hardware flow control is not implemented. The parameters are collected, but the actual system call, like uart_set_hw_flow_ctrl() is missing. Obviously no one noticed until now. It is easy tim implement, just call
uart_set_hw_flow_ctrl() around line 168 of machuart.c with the UART number and one of the modes of the enum below (I just copy it for [my] convenience):

Code: Select all

typedef enum {
    UART_HW_FLOWCTRL_DISABLE = 0x0,   /*!< disable hardware flow control*/
    UART_HW_FLOWCTRL_RTS     = 0x1,   /*!< enable RX hardware flow control (rts)*/
    UART_HW_FLOWCTRL_CTS     = 0x2,   /*!< enable TX hardware flow control (cts)*/
    UART_HW_FLOWCTRL_CTS_RTS = 0x3,   /*!< enable hardware flow control*/
    UART_HW_FLOWCTRL_MAX     = 0x4,
} uart_hw_flowcontrol_t;
Edit: I do not mean that you should do it. I just collected here the information needed, so I have not to search again.

MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

Re: UART inverted

Post by MaFr » Mon Feb 03, 2020 6:19 pm

Thanks for letting me know...Yeah, I would like to try to change it, but I'm afraid I would need some more information to actually do it. Do you mean to look for this file at https://github.com/micropython/micropython, change it and build it from source?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART inverted

Post by Roberthh » Mon Feb 03, 2020 7:29 pm

Actually this file: https://github.com/micropython/micropyt ... ine_uart.c in the repository. I have changed it already, and it compiles. I "just" have to test whether it works. Setting up the build environment is a little bit of work, but is surely worth the effort. @MostlyHarmless here on the board have made a docker container for it. see that post: viewtopic.php?f=18&t=7138&p=41480&hilit=docker#p41480

I'll give the machuart.c a try and report back. Since it looks like an unintended omission, it should be fixed.

MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

Re: UART inverted

Post by MaFr » Mon Feb 03, 2020 8:27 pm

Wow, thank you so much....well, then I will be looking forward for the upcoming version...As soon as the bin gets available, I will test it and report if working fine...

THANK YOU SO MUCH FOR YOUR HELP 👍

Have a nice evening.....

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART inverted

Post by Roberthh » Mon Feb 03, 2020 8:31 pm

According to my first test, it works. When the buffer is at 256 bytes, it raises RTS (from low -> high). After a uart.read() it reverts.
In the first attempt, I have set the threshold to 1. That means, that 1 + buffer size = 257 bytes are buffered until RTS is raised. Then, still FIFO-1 bytes = 127 bytes can be buffered. Setting the threshold to 0 has a strange side effect, that with every character receive there is a 8 char time pulse on RTS, which is not an intended behavior.
If you tell me, whether you have a device with or without SPIRAM, I can prepare an image for you.

MaFr
Posts: 12
Joined: Sun Feb 02, 2020 3:52 pm

Re: UART inverted

Post by MaFr » Mon Feb 03, 2020 8:36 pm

Awesome!

Well, I have this one:
ESP Huzzah32 Feather
https://www.adafruit.com/product/3405
They say it has 4MB of SPI Flash

Is this the information you are looking for?

Would be great if you could prepare the image for me.
Im really happy...Thanks again.

Post Reply