UART Questions

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
cebersp
Posts: 30
Joined: Mon Feb 08, 2021 12:07 pm

UART Questions

Post by cebersp » Mon Feb 08, 2021 12:12 pm

1. I have to invert an UART output- How can I do this?

Code: Select all

# Test UART for Midi Input
# https://docs.micropython.org/en/latest/library/machine.UART.html
import utime
from machine import UART, Pin
rxPin=Pin(1,Pin.IN,Pin.PULL_UP)
txPin=Pin(0,Pin.OUT)
#uart=UART(0,31250, rx=rxPin, tx=txPin, invert=UART.INV_TX) # does not work       #init with given baudrate
uart=UART(0,31250, rx=rxPin, tx=txPin) # does work       #init with given baudrate
while True:
    uart.write("hello")     #write "hello"
    utime.sleep(1)
2. Is there an example, how to use interrupts with UART?

Many thanks in advance, Christof

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

Re: UART Questions

Post by Roberthh » Mon Feb 08, 2021 1:27 pm

At the rp2040 port the invert keyword is not supported. Maybe yet. So you have to use an external inverter. A single 2n7000 transistor + resistor may be sufficient, or a CMOS inverter (e.g. 74hc04). Or you could try one of the PIO examples for UART, where you can control the pin levels yourself.

cebersp
Posts: 30
Joined: Mon Feb 08, 2021 12:07 pm

Re: UART Questions

Post by cebersp » Mon Feb 08, 2021 1:40 pm

Thanks!
I will use hardware then.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: UART Questions

Post by dhylands » Tue Feb 09, 2021 5:02 pm

There is a uart _tx PIO example for the pico. You could modify this to produce an inverted uart output.

fdufnews
Posts: 76
Joined: Mon Jul 25, 2016 11:31 am

Re: UART Questions

Post by fdufnews » Wed Feb 10, 2021 9:54 am

In the datasheet, chapter 2.19 table 296 concerning GPIOs, there are GPIOx_CTRL registers (one for each GPIO) in wich you can change the behavior of the GPIO. The OUTOVER bits allow to modify the polarity of the output.

cebersp
Posts: 30
Joined: Mon Feb 08, 2021 12:07 pm

Re: UART Questions

Post by cebersp » Wed Feb 10, 2021 5:13 pm

Setting registers: Yes, I had seen, that the hardware can do it. I have not yet done inline assembler in MicroPython, so I don't know how to access the registers directly.

PIO: Well, I can understand some of the PIO examples, but I do not feel able to make this quickly...

In my case, this serial line is going to an optocoupler for Midi, so here it is really simple to do it with hardware...
Thanks!

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

Re: UART Questions

Post by Roberthh » Wed Feb 10, 2021 5:38 pm

I have made a PR for setting invert mode. https://github.com/micropython/micropython/pull/6870
It provides the timeout setting as well.

Post Reply