esp8266/machine_uart: Add rxbuf keyword arg to UART constructor/init.
As per the machine.UART documentation, this is used to set the length of
the UART RX buffer.
diff --git a/ports/esp8266/machine_uart.c b/ports/esp8266/machine_uart.c
index e8be5e5..21336c7 100644
--- a/ports/esp8266/machine_uart.c
+++ b/ports/esp8266/machine_uart.c
@@ -57,13 +57,13 @@
STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
- mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, timeout=%u, timeout_char=%u)",
+ mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, rxbuf=%u, timeout=%u, timeout_char=%u)",
self->uart_id, self->baudrate, self->bits, _parity_name[self->parity],
- self->stop, self->timeout, self->timeout_char);
+ self->stop, uart0_get_rxbuf_len() - 1, self->timeout, self->timeout_char);
}
STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_timeout_char };
+ enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_rxbuf, ARG_timeout, ARG_timeout_char };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = 0} },
@@ -71,6 +71,7 @@
{ MP_QSTR_stop, MP_ARG_INT, {.u_int = 0} },
//{ MP_QSTR_tx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
//{ MP_QSTR_rx, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
+ { MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
};
@@ -144,6 +145,20 @@
break;
}
+ // set rx ring buffer
+ if (args[ARG_rxbuf].u_int > 0) {
+ uint16_t len = args[ARG_rxbuf].u_int + 1; // account for usable items in ringbuf
+ byte *buf;
+ if (len <= UART0_STATIC_RXBUF_LEN) {
+ buf = uart_ringbuf_array;
+ MP_STATE_PORT(uart0_rxbuf) = NULL; // clear any old pointer
+ } else {
+ buf = m_new(byte, len);
+ MP_STATE_PORT(uart0_rxbuf) = buf; // retain root pointer
+ }
+ uart0_set_rxbuf(buf, len);
+ }
+
// set timeout
self->timeout = args[ARG_timeout].u_int;
diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h
index ab3fe35..b2a05e6 100644
--- a/ports/esp8266/mpconfigport.h
+++ b/ports/esp8266/mpconfigport.h
@@ -201,6 +201,7 @@
#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8]; \
mp_obj_t pin_irq_handler[16]; \
+ byte *uart0_rxbuf; \
// We need to provide a declaration/definition of alloca()
#include <alloca.h>
diff --git a/ports/esp8266/uart.c b/ports/esp8266/uart.c
index 52707f9..cf9e8f6 100644
--- a/ports/esp8266/uart.c
+++ b/ports/esp8266/uart.c
@@ -36,7 +36,7 @@
// A small, static ring buffer for incoming chars
// This will only be populated if the UART is not attached to dupterm
-static byte uart_ringbuf_array[16];
+uint8 uart_ringbuf_array[UART0_STATIC_RXBUF_LEN];
static ringbuf_t uart_ringbuf = {uart_ringbuf_array, sizeof(uart_ringbuf_array), 0, 0};
static void uart0_rx_intr_handler(void *para);
@@ -269,6 +269,19 @@
ETS_UART_INTR_ENABLE();
}
+int ICACHE_FLASH_ATTR uart0_get_rxbuf_len(void) {
+ return uart_ringbuf.size;
+}
+
+void ICACHE_FLASH_ATTR uart0_set_rxbuf(uint8 *buf, int len) {
+ ETS_UART_INTR_DISABLE();
+ uart_ringbuf.buf = buf;
+ uart_ringbuf.size = len;
+ uart_ringbuf.iget = 0;
+ uart_ringbuf.iput = 0;
+ ETS_UART_INTR_ENABLE();
+}
+
// Task-based UART interface
#include "py/obj.h"
diff --git a/ports/esp8266/uart.h b/ports/esp8266/uart.h
index 684689a..0e67783 100644
--- a/ports/esp8266/uart.h
+++ b/ports/esp8266/uart.h
@@ -6,6 +6,8 @@
#define UART0 (0)
#define UART1 (1)
+#define UART0_STATIC_RXBUF_LEN (16)
+
typedef enum {
UART_FIVE_BITS = 0x0,
UART_SIX_BITS = 0x1,
@@ -91,6 +93,8 @@
int buff_uart_no; //indicate which uart use tx/rx buffer
} UartDevice;
+extern uint8 uart_ringbuf_array[UART0_STATIC_RXBUF_LEN];
+
void uart_init(UartBautRate uart0_br, UartBautRate uart1_br);
int uart0_rx(void);
bool uart_rx_wait(uint32_t timeout_us);
@@ -99,6 +103,8 @@
void uart_flush(uint8 uart);
void uart_os_config(int uart);
void uart_setup(uint8 uart);
+int uart0_get_rxbuf_len(void);
+void uart0_set_rxbuf(uint8 *buf, int len);
// check status of rx/tx
int uart_rx_any(uint8 uart);
int uart_tx_any_room(uint8 uart);