Skip to content

Commit afd0701

Browse files
committed
esp8266: Change UART(0) to attach to REPL via uos.dupterm interface.
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).
1 parent 2923671 commit afd0701

7 files changed

Lines changed: 73 additions & 29 deletions

File tree

ports/esp8266/esp_mphal.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@
3535
#include "extmod/misc.h"
3636
#include "lib/utils/pyexec.h"
3737

38-
STATIC byte input_buf_array[256];
39-
ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
38+
STATIC byte stdin_ringbuf_array[256];
39+
ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
4040
void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
4141
const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
4242

43+
int uart_attached_to_dupterm;
44+
4345
void mp_hal_init(void) {
4446
//ets_wdt_disable(); // it's a pain while developing
4547
mp_hal_rtc_init();
4648
uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
49+
uart_attached_to_dupterm = 0;
4750
}
4851

4952
void mp_hal_delay_us(uint32_t us) {
@@ -55,7 +58,7 @@ void mp_hal_delay_us(uint32_t us) {
5558

5659
int mp_hal_stdin_rx_chr(void) {
5760
for (;;) {
58-
int c = ringbuf_get(&input_buf);
61+
int c = ringbuf_get(&stdin_ringbuf);
5962
if (c != -1) {
6063
return c;
6164
}
@@ -80,19 +83,11 @@ void mp_hal_debug_str(const char *str) {
8083
#endif
8184

8285
void mp_hal_stdout_tx_str(const char *str) {
83-
const char *last = str;
84-
while (*str) {
85-
uart_tx_one_char(UART0, *str++);
86-
}
87-
mp_uos_dupterm_tx_strn(last, str - last);
86+
mp_uos_dupterm_tx_strn(str, strlen(str));
8887
}
8988

9089
void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
91-
const char *last = str;
92-
while (len--) {
93-
uart_tx_one_char(UART0, *str++);
94-
}
95-
mp_uos_dupterm_tx_strn(last, str - last);
90+
mp_uos_dupterm_tx_strn(str, len);
9691
}
9792

9893
void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
@@ -102,13 +97,11 @@ void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
10297
if (str > last) {
10398
mp_uos_dupterm_tx_strn(last, str - last);
10499
}
105-
uart_tx_one_char(UART0, '\r');
106-
uart_tx_one_char(UART0, '\n');
107100
mp_uos_dupterm_tx_strn("\r\n", 2);
108101
++str;
109102
last = str;
110103
} else {
111-
uart_tx_one_char(UART0, *str++);
104+
++str;
112105
}
113106
}
114107
if (str > last) {
@@ -166,7 +159,7 @@ STATIC void dupterm_task_handler(os_event_t *evt) {
166159
if (c < 0) {
167160
break;
168161
}
169-
ringbuf_put(&input_buf, c);
162+
ringbuf_put(&stdin_ringbuf, c);
170163
}
171164
mp_hal_signal_input();
172165
lock = 0;

ports/esp8266/esp_mphal.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ struct _mp_print_t;
3434
// Structure for UART-only output via mp_printf()
3535
extern const struct _mp_print_t mp_debug_print;
3636

37-
extern ringbuf_t input_buf;
38-
// Call this after putting data to input_buf
37+
extern ringbuf_t stdin_ringbuf;
38+
// Call this after putting data to stdin_ringbuf
3939
void mp_hal_signal_input(void);
4040
// Call this when data is available in dupterm object
4141
void mp_hal_signal_dupterm_input(void);
4242

43+
// This variable counts how many times the UART is attached to dupterm
44+
extern int uart_attached_to_dupterm;
45+
4346
void mp_hal_init(void);
4447
void mp_hal_rtc_init(void);
4548

ports/esp8266/main.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "py/mperrno.h"
3434
#include "py/mphal.h"
3535
#include "py/gc.h"
36+
#include "extmod/misc.h"
3637
#include "lib/mp-readline/readline.h"
3738
#include "lib/utils/pyexec.h"
3839
#include "gccollect.h"
@@ -65,6 +66,25 @@ STATIC void mp_reset(void) {
6566
pyexec_file("main.py");
6667
}
6768
#endif
69+
70+
// Check if there are any dupterm objects registered and if not then
71+
// activate UART(0), or else there will never be any chance to get a REPL
72+
size_t idx;
73+
for (idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
74+
if (MP_STATE_VM(dupterm_objs[idx]) != MP_OBJ_NULL) {
75+
break;
76+
}
77+
}
78+
if (idx == MICROPY_PY_OS_DUPTERM) {
79+
mp_obj_t args[2];
80+
args[0] = MP_OBJ_NEW_SMALL_INT(0);
81+
args[1] = MP_OBJ_NEW_SMALL_INT(115200);
82+
args[0] = pyb_uart_type.make_new(&pyb_uart_type, 2, 0, args);
83+
args[1] = MP_OBJ_NEW_SMALL_INT(1);
84+
extern mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args);
85+
os_dupterm(2, args);
86+
mp_hal_stdout_tx_str("Activated UART(0) for REPL\r\n");
87+
}
6888
}
6989

7090
void soft_reset(void) {

ports/esp8266/modules/inisetup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def setup():
4444
# This file is executed on every boot (including wake-boot from deepsleep)
4545
#import esp
4646
#esp.osdebug(None)
47+
import uos, machine
48+
uos.dupterm(machine.UART(0, 115200), 1)
4749
import gc
4850
#import webrepl
4951
#webrepl.start()

ports/esp8266/moduos.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ STATIC mp_obj_t os_urandom(mp_obj_t num) {
7676
}
7777
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_urandom_obj, os_urandom);
7878

79+
// We wrap the mp_uos_dupterm function to detect if a UART is attached or not
80+
mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args) {
81+
mp_obj_t prev_obj = mp_uos_dupterm_obj.fun.var(n_args, args);
82+
if (mp_obj_get_type(args[0]) == &pyb_uart_type) {
83+
++uart_attached_to_dupterm;
84+
}
85+
if (mp_obj_get_type(prev_obj) == &pyb_uart_type) {
86+
--uart_attached_to_dupterm;
87+
}
88+
return prev_obj;
89+
}
90+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_dupterm_obj, 1, 2, os_dupterm);
91+
7992
STATIC mp_obj_t os_dupterm_notify(mp_obj_t obj_in) {
8093
(void)obj_in;
8194
mp_hal_signal_dupterm_input();
@@ -88,7 +101,7 @@ STATIC const mp_rom_map_elem_t os_module_globals_table[] = {
88101
{ MP_ROM_QSTR(MP_QSTR_uname), MP_ROM_PTR(&os_uname_obj) },
89102
{ MP_ROM_QSTR(MP_QSTR_urandom), MP_ROM_PTR(&os_urandom_obj) },
90103
#if MICROPY_PY_OS_DUPTERM
91-
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&mp_uos_dupterm_obj) },
104+
{ MP_ROM_QSTR(MP_QSTR_dupterm), MP_ROM_PTR(&os_dupterm_obj) },
92105
{ MP_ROM_QSTR(MP_QSTR_dupterm_notify), MP_ROM_PTR(&os_dupterm_notify_obj) },
93106
#endif
94107
#if MICROPY_VFS_FAT

ports/esp8266/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
#define MICROPY_PY_WEBREPL_DELAY (20)
8787
#define MICROPY_PY_FRAMEBUF (1)
8888
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
89-
#define MICROPY_PY_OS_DUPTERM (1)
89+
#define MICROPY_PY_OS_DUPTERM (2)
9090
#define MICROPY_CPYTHON_COMPAT (1)
9191
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
9292
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)

ports/esp8266/uart.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ static int uart_os = UART_OS;
3434
static os_event_t uart_evt_queue[16];
3535
#endif
3636

37+
// A small, static ring buffer for incoming chars
38+
// This will only be populated if the UART is not attached to dupterm
39+
static byte uart_ringbuf_array[16];
40+
static ringbuf_t uart_ringbuf = {uart_ringbuf_array, sizeof(uart_ringbuf_array), 0, 0};
41+
3742
static void uart0_rx_intr_handler(void *para);
3843

3944
void soft_reset(void);
@@ -170,18 +175,26 @@ static void uart0_rx_intr_handler(void *para) {
170175

171176
while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
172177
uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
173-
if (RcvChar == mp_interrupt_char) {
174-
mp_keyboard_interrupt();
178+
// For efficiency, when connected to dupterm we put incoming chars
179+
// directly on stdin_ringbuf, rather than going via uart_ringbuf
180+
if (uart_attached_to_dupterm) {
181+
if (RcvChar == mp_interrupt_char) {
182+
mp_keyboard_interrupt();
183+
} else {
184+
ringbuf_put(&stdin_ringbuf, RcvChar);
185+
}
175186
} else {
176-
ringbuf_put(&input_buf, RcvChar);
187+
ringbuf_put(&uart_ringbuf, RcvChar);
177188
}
178189
}
179190

180-
mp_hal_signal_input();
181-
182191
// Clear pending FIFO interrupts
183192
WRITE_PERI_REG(UART_INT_CLR(UART_REPL), UART_RXFIFO_TOUT_INT_CLR | UART_RXFIFO_FULL_INT_ST);
184193
ETS_UART_INTR_ENABLE();
194+
195+
if (uart_attached_to_dupterm) {
196+
mp_hal_signal_input();
197+
}
185198
}
186199
}
187200

@@ -190,7 +203,7 @@ static void uart0_rx_intr_handler(void *para) {
190203
bool uart_rx_wait(uint32_t timeout_us) {
191204
uint32_t start = system_get_time();
192205
for (;;) {
193-
if (input_buf.iget != input_buf.iput) {
206+
if (uart_ringbuf.iget != uart_ringbuf.iput) {
194207
return true; // have at least 1 char ready for reading
195208
}
196209
if (system_get_time() - start >= timeout_us) {
@@ -201,7 +214,7 @@ bool uart_rx_wait(uint32_t timeout_us) {
201214
}
202215

203216
int uart_rx_any(uint8 uart) {
204-
if (input_buf.iget != input_buf.iput) {
217+
if (uart_ringbuf.iget != uart_ringbuf.iput) {
205218
return true; // have at least 1 char ready for reading
206219
}
207220
return false;
@@ -217,7 +230,7 @@ int uart_tx_any_room(uint8 uart) {
217230

218231
// Returns char from the input buffer, else -1 if buffer is empty.
219232
int uart_rx_char(void) {
220-
return ringbuf_get(&input_buf);
233+
return ringbuf_get(&uart_ringbuf);
221234
}
222235

223236
int uart_rx_one_char(uint8 uart_no) {

0 commit comments

Comments
 (0)