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).
This commit is contained in:
Damien George
2018-05-15 15:13:58 +10:00
parent 2923671a0c
commit afd0701bf7
7 changed files with 73 additions and 29 deletions

View File

@@ -33,6 +33,7 @@
#include "py/mperrno.h"
#include "py/mphal.h"
#include "py/gc.h"
#include "extmod/misc.h"
#include "lib/mp-readline/readline.h"
#include "lib/utils/pyexec.h"
#include "gccollect.h"
@@ -65,6 +66,25 @@ STATIC void mp_reset(void) {
pyexec_file("main.py");
}
#endif
// Check if there are any dupterm objects registered and if not then
// activate UART(0), or else there will never be any chance to get a REPL
size_t idx;
for (idx = 0; idx < MICROPY_PY_OS_DUPTERM; ++idx) {
if (MP_STATE_VM(dupterm_objs[idx]) != MP_OBJ_NULL) {
break;
}
}
if (idx == MICROPY_PY_OS_DUPTERM) {
mp_obj_t args[2];
args[0] = MP_OBJ_NEW_SMALL_INT(0);
args[1] = MP_OBJ_NEW_SMALL_INT(115200);
args[0] = pyb_uart_type.make_new(&pyb_uart_type, 2, 0, args);
args[1] = MP_OBJ_NEW_SMALL_INT(1);
extern mp_obj_t os_dupterm(size_t n_args, const mp_obj_t *args);
os_dupterm(2, args);
mp_hal_stdout_tx_str("Activated UART(0) for REPL\r\n");
}
}
void soft_reset(void) {