Skip to content

Commit

Permalink
esp8266/machine_uart: Add rxbuf keyword arg to UART constructor/init.
Browse files Browse the repository at this point in the history
As per the machine.UART documentation, this is used to set the length of
the UART RX buffer.
  • Loading branch information
dpgeorge committed Dec 5, 2018
1 parent 9ddc182 commit 52bec93
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
21 changes: 18 additions & 3 deletions ports/esp8266/machine_uart.c
Expand Up @@ -57,20 +57,21 @@ STATIC const char *_parity_name[] = {"None", "1", "0"};

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} },
{ MP_QSTR_parity, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ 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} },
};
Expand Down Expand Up @@ -144,6 +145,20 @@ STATIC void pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const mp_o
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;

Expand Down
1 change: 1 addition & 0 deletions ports/esp8266/mpconfigport.h
Expand Up @@ -201,6 +201,7 @@ extern const struct _mp_obj_module_t mp_module_onewire;
#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>
Expand Down
15 changes: 14 additions & 1 deletion ports/esp8266/uart.c
Expand Up @@ -36,7 +36,7 @@ static os_event_t uart_evt_queue[16];

// 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);
Expand Down Expand Up @@ -269,6 +269,19 @@ void ICACHE_FLASH_ATTR uart_setup(uint8 uart) {
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"
Expand Down
6 changes: 6 additions & 0 deletions ports/esp8266/uart.h
Expand Up @@ -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,
Expand Down Expand Up @@ -91,6 +93,8 @@ typedef struct {
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);
Expand All @@ -99,6 +103,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);
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);
Expand Down

0 comments on commit 52bec93

Please sign in to comment.