Page 1 of 3

So you want to use the UART...

Posted: Wed Jun 29, 2016 10:22 am
by jms
...to control something but can't because of the junk output and baud rate.

I want to share my solutions to the obvious problems using the UART to communicate with some other bit of kit.
  • Junk at startup and regular intervals. Some of this, such as "ip=..." comes from Espressif's binary blobs and short of patching those which might be worth trying my solution is with hardware
  • Baud rate - easy to fix
  • Non-blocking read - again straightforward
Image

The schematic is self-explanatory. There are plenty of perfectly valid ways of doing this and partly depends on what components you have lying about. If you have a board that already has pullups on some GPIO then you only need to add one common NPN transistor and one resistor.

When you actually want to transmit make the GPIO an output and pull it low and afterwards (allowing for transmission time) send it high or switch it back to input.

You should in any case call esp.osdebug(None) at startup as this reduces the chances of other unwanted output.

No doubt with better documentation one might be able to use official API but in the meantime...

Code: Select all

def baudrate(rate):
	machine.mem32[0x60000014] = int(80000000/rate)
You can use regular print to produce output but you want to read without blocking. With a heavyweight OS there are a multitude of methods but for now

Code: Select all

>>> uart = machine.UART(0)
>>> time.sleep(2); uart.read()
brexit yawn
b'brexit yawn'
>>>

Re: So you want to use the UART...

Posted: Wed Jun 29, 2016 10:56 am
by deshipu
I think you can just use machine.UART to re-initialize it to a different baud rate, no need for the memory poking.

EDIT: I was wrong, seems it's not implemented yet, you can only set timeout.

Re: So you want to use the UART...

Posted: Wed Jun 29, 2016 11:02 am
by jms
It looks like you should be able to re-initialise the machine.UART object and the init method takes a baudrate and if you can show me how then I'll admit it beat me.

Re: So you want to use the UART...

Posted: Wed Jun 29, 2016 12:38 pm
by deshipu
It's not implemented yet, I took a stab at it...

Re: So you want to use the UART...

Posted: Wed Jun 29, 2016 7:32 pm
by Photon Peddler
deshipu wrote:It's not implemented yet, I took a stab at it...
Excellent! Can you also prevent incoming Ctrl-C from stopping the interpreter? Receiving binary data is impossible without it.

I made a crude hack to do this, but an official way would be nice.

Re: So you want to use the UART...

Posted: Wed Jun 29, 2016 8:53 pm
by deshipu
Photon Peddler wrote:
deshipu wrote:It's not implemented yet, I took a stab at it...
Excellent! Can you also prevent incoming Ctrl-C from stopping the interpreter? Receiving binary data is impossible without it.

I made a crude hack to do this, but an official way would be nice.
I don't think I can do that, but I think you can prevent it by putting the REPL in raw mode. No idea how to do it programatically, though.

Re: So you want to use the UART...

Posted: Thu Jun 30, 2016 8:25 am
by deshipu
I submitted a pull request that allows you change the serial parameters through machine.UART.init. I would be grateful for testing and comments. https://github.com/micropython/micropython/pull/2205

Re: So you want to use the UART...

Posted: Thu Jun 30, 2016 12:36 pm
by markxr
Is it possible to use the 2nd UART (UART1) ? Or does that send garbage at startup too?

Mark

Re: So you want to use the UART...

Posted: Thu Jun 30, 2016 12:46 pm
by deshipu
Both uarts send diagnostic messages at startup, and the second UART only has a TX pin (no RX), so it's not that useful.

Re: So you want to use the UART...

Posted: Thu Jun 30, 2016 6:40 pm
by deshipu
You should be able to change the UART parameters both with machine.UART() and uart.init() now. Please let me know if you find any problems.