Use REPL uart to program device and to communicate to PC

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
elecJ
Posts: 2
Joined: Sat Jul 10, 2021 9:40 am

Use REPL uart to program device and to communicate to PC

Post by elecJ » Sat Jul 10, 2021 8:38 pm

Hello Team,

We are making a device based on ESP32 and micropython.
We are looking for a solution to use only ONE (USB<->UART adapter) in the board in order to:
1-Program the device from PC (using esptool)
2-Communicate with a PC program via UART.

As I know, the micropython UART 0 is used by REPL to develop & program the ESP32, via esptool. But, is there a possibility to disable it after boot/start and use this UART to communicate with a PC program?

This is the option that we are thinking to implement to solve this topic:

1-Add MUX and enable UART 1 or UART 2 in order to boot the device using UART0, and then switch the data that comes from the USB<->UART adapter to UART1 or UART2 via software.

Any recommendations?

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Use REPL uart to program device and to communicate to PC

Post by Christian Walther » Sun Jul 11, 2021 11:09 am

You probably don’t even need to disable it for that, you can use it as enabled as it is. The REPL is only active while your program is not running, so it won’t interfere with your own use. While your program is running, the UART is attached to stdin and stdout of your program and you can use it to communicate with the PC using these (sys.stdin, sys.stdout or even just print()). If the PC needs to send arbitrary binary data, disable Ctrl-C using micropython.kbd_intr(). If you do want to detach it and talk to the UART directly, that can be done using uos.dupterm() (more examples here).

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: Use REPL uart to program device and to communicate to PC

Post by Christian Walther » Sun Jul 11, 2021 2:07 pm

Correction: I just learned that detaching the UART using dupterm() does not work on the ESP32 because it’s not attached that way (“UART(0) is disabled (dedicated to REPL)”, esp32/machine_uart.c).

However you can run UART 1 on the same pins: machine.UART(1, tx=1, rx=3)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Use REPL uart to program device and to communicate to PC

Post by jimmo » Sun Jul 11, 2021 2:48 pm

elecJ wrote:
Sat Jul 10, 2021 8:38 pm
As I know, the micropython UART 0 is used by REPL to develop & program the ESP32, via esptool. But, is there a possibility to disable it after boot/start and use this UART to communicate with a PC program?
I'm not quite sure I follow... the REPL isn't involved with esptool.

When you use esptool, it puts the chip into the ESP32 bootloader (by (ab)using the DTR and RTS pins).

I think what you're asking though is that you want to have the REPL still active as well as a separate UART to communicate with the PC?

elecJ
Posts: 2
Joined: Sat Jul 10, 2021 9:40 am

Re: Use REPL uart to program device and to communicate to PC

Post by elecJ » Sun Jul 11, 2021 7:02 pm

This was super interesting and super useful: (I didn't know it):
When you use esptool, it puts the chip into the ESP32 bootloader (by (ab)using the DTR and RTS pins).
What I want is:

Using a micropython command, to disable or 'to free' all the information that micropython 'put' on the TX pin (OS debug info, print, REPL, etc..) and use TX and RX to comunicate with our PC program.
Example:
When I use micropython, with Thonny IDE, a lot of data is sent from the ESP32, not only the print("Hello world"): Rebooting... and other text messages are sent from the MP. Our PC program doens't want to receive this kind of data, this is why I want to disable/remove/move all of this data from the UART0 and free the UART0 to use it for our project, because the UART0 are connected directly to USB<->UART adapter and this is not an option to add another USB to UART adapter in the board.

What it means?
However you can run UART 1 on the same pins: machine.UART(1, tx=1, rx=3)
That once I execute this command, all the debug and 'print' will go thru UART1? and I will be able to read and write UART 0?

Post Reply