← index #5405PR #3784
Related · medium · value 0.828
QUERY · ISSUE

Move esp32 REPL to a different UART

openby lerouxbopened 2019-12-09updated 2021-06-20

Hi.

I'm trying to change the ESP32 REPL to use UART2 rather than UART0. I'm using an Adafruit Feather Huzzah32 and I want to be able to use the REPL over the feather's RX/16 and TX/17 pins rather than the USB port. I believe this is UART2.

I've tried the following:

diff --git a/ports/esp32/mphalport.c b/ports/esp32/mphalport.c
index 305e87593..7edb40321 100644
--- a/ports/esp32/mphalport.c
+++ b/ports/esp32/mphalport.c
@@ -38,6 +38,8 @@
 #include "rom/uart.h"
 #endif

+#include "driver/uart.h"
+
 #include "py/obj.h"
 #include "py/stream.h"
 #include "py/mpstate.h"
@@ -80,6 +82,7 @@ void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
     if (release_gil) {
         MP_THREAD_GIL_EXIT();
     }
+    uart_tx_switch(UART_NUM_2);
     for (uint32_t i = 0; i < len; ++i) {
         uart_tx_one_char(str[i]);
     }
diff --git a/ports/esp32/uart.c b/ports/esp32/uart.c
index 10a4ba462..1de31dc0c 100644
--- a/ports/esp32/uart.c
+++ b/ports/esp32/uart.c
@@ -37,13 +37,13 @@ STATIC void uart_irq_handler(void *arg);

 void uart_init(void) {
     uart_isr_handle_t handle;
-    uart_isr_register(UART_NUM_0, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
-    uart_enable_rx_intr(UART_NUM_0);
+    uart_isr_register(UART_NUM_2, uart_irq_handler, NULL, ESP_INTR_FLAG_LOWMED | ESP_INTR_FLAG_IRAM, &handle);
+    uart_enable_rx_intr(UART_NUM_2);
 }

 // all code executed in ISR must be in IRAM, and any const data must be in DRAM
 STATIC void IRAM_ATTR uart_irq_handler(void *arg) {
-    volatile uart_dev_t *uart = &UART0;
+    volatile uart_dev_t *uart = &UART2;
     uart->int_clr.rxfifo_full = 1;
     uart->int_clr.frm_err = 1;
     uart->int_clr.rxfifo_tout = 1;

But this doesn't seem to be enough. I've searched all over this codebase and cannot figure out how this fits together. Is there something obvious I'm missing?

CANDIDATE · PULL REQUEST

esp8266: Change UART(0) to attach to REPL via uos.dupterm interface.

closedby dpgeorgeopened 2018-05-15updated 2019-07-05

This patch makes it so that UART(0) can by dynamically attached to and
detached from the REPL by using the uos.dupterm function. Since WebREPL
uses dupterm slot 0 the UART uses dupterm slot 1 (a slot which is newly
introduced by this patch). UART(0) must now be attached manually in
boot.py (or otherwise) and inisetup.py is changed to provide code to do
this. For example, to attach use:

import uos, machine
uart = machine.UART(0, 115200)
uos.dupterm(uart, 1)

and to detach use:

uos.dupterm(None, 1)

When attached, all incoming chars on UART(0) go straight to stdin so
uart.read() will always return None. Use sys.stdin.read() if it's needed
to read characters from the UART(0) while it's also used for the REPL (or
detach, read, then reattach). When detached the UART(0) can be used for
other purposes.

If there are no objects in any of the dupterm slots when the REPL is
started (on hard or soft reset) then UART(0) is automatically attached.
Without this, the only way to recover a board without a REPL would be to
completely erase and reflash (which would install the default boot.py which
attaches the REPL).

See #2891 and #3277 for previous attempts at this.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied