Trying to disable REPL from Uart0 on ESP32

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
uCTRL
Posts: 42
Joined: Fri Oct 12, 2018 11:50 pm

Trying to disable REPL from Uart0 on ESP32

Post by uCTRL » Mon Sep 16, 2019 12:24 am

I need non blocking stdio character reading through USB-Uart0 port on esp32 development board.
As I understand there is NO non-blocking stdio reading available.
So I would like to disable REPL from USB-Uart0 port and use the same port with standard Uart functionality including non-blocking read.

To disable REPL.

Have tried redirecting uos.dupterm(None, 1), throws ValueError: invalid dupterm index.

Can anyone suggest a solution.

Thanks

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

Re: Trying to disable REPL from Uart0 on ESP32

Post by jimmo » Mon Sep 16, 2019 1:42 am

Even if you can detach the REPL using dupterm, you won't be able to access the UART with the current firmware.

From machine_uart.c

Code: Select all

    // Attempts to use UART0 from Python has resulted in all sorts of fun errors.
    // FIXME: UART0 is disabled for now.
    if (uart_num == UART_NUM_0) {
        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "UART(%d) is disabled (dedicated to REPL)", uart_num));
    }
I don't know the history of these "fun errors" unfortunately.

However, the feature you need (non-blocking stdio read) might be a very simple change:
https://github.com/micropython/micropython/issues/4849
implemented for ESP8266 in https://github.com/micropython/micropython/pull/4859

Maybe ESP32 is very simple to implement?

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

Re: Trying to disable REPL from Uart0 on ESP32

Post by jimmo » Mon Sep 16, 2019 2:00 am

Actually, ESP32 was already implemented a few days later. So if you're running the latest nightly build (e.g. esp32-20190916-v1.11-312-g22099ab88.bin) then you should be able to do this.

I used

Code: Select all

select.select([sys.stdin], [], [], 0)
as an equivalent of "any()".

According to that PR, you should be able to use sys.stdin.buffer.read(1) as the non-blocking version of sys.stdin.read(1) but this didn't work for me.

You can also use the select.poll API. Weirdly the class is called "poll" not "Poll"?

Code: Select all

p = select.poll()
p.register(sys.stdin)
p.poll(0) # returns empty list if no data ready

uCTRL
Posts: 42
Joined: Fri Oct 12, 2018 11:50 pm

Re: Trying to disable REPL from Uart0 on ESP32

Post by uCTRL » Mon Sep 16, 2019 3:42 am

Thanks jimmo,

poll works for me, as well as code below.

Code: Select all

import sys
import select
import time

while 1:
  time.sleep(0.1)
   
  while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
    ch = sys.stdin.read(1)
    print(ch)
    
  print("prove it doesn't block")

wangshujun@tom.com
Posts: 4
Joined: Fri Feb 15, 2019 9:22 am

Re: Trying to disable REPL from Uart0 on ESP32

Post by wangshujun@tom.com » Thu Jan 09, 2020 3:17 am

I have also encountered similar problems, which can be solved in the following ways through exploration.

1. Directly define other UART ports to overwrite repl on gpio1 and gpio3, but it will cause complete failure of repl, including ctrl-c. if the automatic operation of main.py is set, the board cannot be connected again.

uart = machine.UART(1, tx=1, rx=3 ,baudrate=115200)

2. By setting a main.py identification to restrict the operation of the application program, it can be realized that when the specific pin is pulled down, the automatic running program will not be started to avoid the loss of repl forever.

main.py

from machine import Pin

key_1=Pin(27,Pin.IN,Pin.PULL_UP)
key_2=Pin(25,Pin.IN,Pin.PULL_UP)
key_3=Pin(32,Pin.IN,Pin.PULL_UP)
print("if pin 25,27.32 any pin is low , exit app")
if key_1.value()==1 and key_2.value()==1 and key_3.value()==1:
print("Start App.....")
exec(open('./app.py').read(),globals())

Post Reply