So you want to use the UART...

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

So you want to use the UART...

Post by jms » Wed Jun 29, 2016 10:22 am

...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'
>>>
Attachments
Tx.png
Tx.png (12.11 KiB) Viewed 14186 times
Last edited by jms on Wed Jun 29, 2016 11:13 am, edited 2 times in total.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Wed Jun 29, 2016 10:56 am

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.

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

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

Post by jms » Wed Jun 29, 2016 11:02 am

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Wed Jun 29, 2016 12:38 pm

It's not implemented yet, I took a stab at it...

Photon Peddler
Posts: 7
Joined: Tue Jul 08, 2014 5:30 pm
Location: Connecticut, USA

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

Post by Photon Peddler » Wed Jun 29, 2016 7:32 pm

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Wed Jun 29, 2016 8:53 pm

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.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Thu Jun 30, 2016 8:25 am

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

markxr
Posts: 62
Joined: Wed Jun 01, 2016 3:41 pm

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

Post by markxr » Thu Jun 30, 2016 12:36 pm

Is it possible to use the 2nd UART (UART1) ? Or does that send garbage at startup too?

Mark

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Thu Jun 30, 2016 12:46 pm

Both uarts send diagnostic messages at startup, and the second UART only has a TX pin (no RX), so it's not that useful.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

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

Post by deshipu » Thu Jun 30, 2016 6:40 pm

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.

Post Reply