Skip to content

Commit

Permalink
esp8266/uart: enable/disable uart via uart_nostdio
Browse files Browse the repository at this point in the history
  • Loading branch information
mhoffma committed Jan 31, 2017
1 parent 300ecac commit 6305378
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
18 changes: 15 additions & 3 deletions esp8266/esp_mphal.c
Expand Up @@ -38,6 +38,10 @@

STATIC byte input_buf_array[256];
ringbuf_t input_buf = {input_buf_array, sizeof(input_buf_array)};
// Separate buffer for duplicate terminal (WebREPL) so that UART can be detached from REPL
STATIC byte dupterm_buf_array[256];
ringbuf_t dupterm_buf = {dupterm_buf_array, sizeof(dupterm_buf_array)};

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};

Expand All @@ -56,10 +60,17 @@ void mp_hal_delay_us(uint32_t us) {

int mp_hal_stdin_rx_chr(void) {
for (;;) {
int c = ringbuf_get(&input_buf);
//int c = ringbuf_get(&input_buf);
int c = uart_std_getc();
if (c != -1) {
return c;
}
// Read next char from duplicate terminal (WebREPL) - note that UART has priority when enabled
c = ringbuf_get(&dupterm_buf);
if (c != -1) {
return c;
}

#if 0
// Idles CPU but need more testing before enabling
if (!ets_loop_iter()) {
Expand All @@ -72,7 +83,8 @@ int mp_hal_stdin_rx_chr(void) {
}

void mp_hal_stdout_tx_char(char c) {
uart_tx_one_char(UART0, c);
//uart_tx_one_char(UART0, c);
uart_std_putc(c);
mp_uos_dupterm_tx_strn(&c, 1);
}

Expand Down Expand Up @@ -195,7 +207,7 @@ STATIC void dupterm_task_handler(os_event_t *evt) {
if (c < 0) {
break;
}
ringbuf_put(&input_buf, c);
ringbuf_put(&dupterm_buf, c);
}
mp_hal_signal_input();
lock = 0;
Expand Down
10 changes: 10 additions & 0 deletions esp8266/modesp.c
Expand Up @@ -519,6 +519,15 @@ void error_check(bool status, const char *msg) {
}
}



STATIC mp_obj_t esp_uart_nostdio(mp_obj_t val) {
extern void uart_os_nostdio(int);
uart_os_nostdio(mp_obj_get_int(val));
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_uart_nostdio_obj, esp_uart_nostdio);

STATIC mp_obj_t esp_osdebug(mp_obj_t val) {
if (val == mp_const_none) {
uart_os_config(-1);
Expand Down Expand Up @@ -826,6 +835,7 @@ STATIC const mp_map_elem_t esp_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_esp) },

{ MP_OBJ_NEW_QSTR(MP_QSTR_osdebug), (mp_obj_t)&esp_osdebug_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_uart_nostdio), (mp_obj_t)&esp_uart_nostdio_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_type), (mp_obj_t)&esp_sleep_type_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_deepsleep), (mp_obj_t)&esp_deepsleep_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_flash_id), (mp_obj_t)&esp_flash_id_obj },
Expand Down
26 changes: 25 additions & 1 deletion esp8266/uart.c
Expand Up @@ -29,6 +29,7 @@ extern UartDevice UartDev;

// the uart to which OS messages go; -1 to disable
static int uart_os = UART_OS;
static int uart_stdio_disable = 0;

#if MICROPY_REPL_EVENT_DRIVEN
static os_event_t uart_evt_queue[16];
Expand Down Expand Up @@ -141,6 +142,11 @@ uart_os_config(int uart) {
uart_os = uart;
}

void
uart_os_nostdio(int dis) {
uart_stdio_disable = dis;
}

/******************************************************************************
* FunctionName : uart0_rx_intr_handler
* Description : Internal used function
Expand Down Expand Up @@ -170,7 +176,7 @@ static void uart0_rx_intr_handler(void *para) {

while (READ_PERI_REG(UART_STATUS(uart_no)) & (UART_RXFIFO_CNT << UART_RXFIFO_CNT_S)) {
uint8 RcvChar = READ_PERI_REG(UART_FIFO(uart_no)) & 0xff;
if (RcvChar == mp_interrupt_char) {
if (RcvChar == mp_interrupt_char && !uart_stdio_disable) {
mp_keyboard_interrupt();
} else {
ringbuf_put(&input_buf, RcvChar);
Expand Down Expand Up @@ -200,6 +206,24 @@ bool uart_rx_wait(uint32_t timeout_us) {
}
}

void uart_std_putc(uint8 c)
{
if (uart_stdio_disable) {
return;
}
uart_tx_one_char(UART0, c);
}

// Returns char from the input buffer, else -1 if buffer is empty.
int uart_std_getc(void) {
// DBE - Disconnect REPL
if (uart_stdio_disable) {
return -1;
}
return ringbuf_get(&input_buf);
}


// Returns char from the input buffer, else -1 if buffer is empty.
int uart_rx_char(void) {
return ringbuf_get(&input_buf);
Expand Down
3 changes: 3 additions & 0 deletions esp8266/uart.h
Expand Up @@ -99,5 +99,8 @@ void uart_tx_one_char(uint8 uart, uint8 TxChar);
void uart_flush(uint8 uart);
void uart_os_config(int uart);
void uart_setup(uint8 uart);
// UART functions used by STDIO (REPL)
int uart_std_getc(void);
void uart_std_putc(uint8 c);

#endif // _INCLUDED_UART_H_

0 comments on commit 6305378

Please sign in to comment.