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).
diff --git a/ports/esp8266/esp_mphal.c b/ports/esp8266/esp_mphal.c
index 9f4f051..df97a73 100644
--- a/ports/esp8266/esp_mphal.c
+++ b/ports/esp8266/esp_mphal.c
@@ -35,15 +35,18 @@
 #include "extmod/misc.h"
 #include "lib/utils/pyexec.h"
 
-STATIC byte input_buf_array[256];
-ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
+STATIC byte stdin_ringbuf_array[256];
+ringbuf_t stdin_ringbuf = {stdin_ringbuf_array, sizeof(stdin_ringbuf_array), 0, 0};
 void mp_hal_debug_tx_strn_cooked(void *env, const char *str, uint32_t len);
 const mp_print_t mp_debug_print = {NULL, mp_hal_debug_tx_strn_cooked};
 
+int uart_attached_to_dupterm;
+
 void mp_hal_init(void) {
     //ets_wdt_disable(); // it's a pain while developing
     mp_hal_rtc_init();
     uart_init(UART_BIT_RATE_115200, UART_BIT_RATE_115200);
+    uart_attached_to_dupterm = 0;
 }
 
 void mp_hal_delay_us(uint32_t us) {
@@ -55,7 +58,7 @@
 
 int mp_hal_stdin_rx_chr(void) {
     for (;;) {
-        int c = ringbuf_get(&input_buf);
+        int c = ringbuf_get(&stdin_ringbuf);
         if (c != -1) {
             return c;
         }
@@ -80,19 +83,11 @@
 #endif
 
 void mp_hal_stdout_tx_str(const char *str) {
-    const char *last = str;
-    while (*str) {
-        uart_tx_one_char(UART0, *str++);
-    }
-    mp_uos_dupterm_tx_strn(last, str - last);
+    mp_uos_dupterm_tx_strn(str, strlen(str));
 }
 
 void mp_hal_stdout_tx_strn(const char *str, uint32_t len) {
-    const char *last = str;
-    while (len--) {
-        uart_tx_one_char(UART0, *str++);
-    }
-    mp_uos_dupterm_tx_strn(last, str - last);
+    mp_uos_dupterm_tx_strn(str, len);
 }
 
 void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
@@ -102,13 +97,11 @@
             if (str > last) {
                 mp_uos_dupterm_tx_strn(last, str - last);
             }
-            uart_tx_one_char(UART0, '\r');
-            uart_tx_one_char(UART0, '\n');
             mp_uos_dupterm_tx_strn("\r\n", 2);
             ++str;
             last = str;
         } else {
-            uart_tx_one_char(UART0, *str++);
+            ++str;
         }
     }
     if (str > last) {
@@ -166,7 +159,7 @@
         if (c < 0) {
             break;
         }
-        ringbuf_put(&input_buf, c);
+        ringbuf_put(&stdin_ringbuf, c);
     }
     mp_hal_signal_input();
     lock = 0;