stm32/uart: Add rxbuf keyword arg to UART constructor and init method.

As per the machine.UART documentation, this is used to set the length of
the RX buffer.  The legacy read_buf_len argument is retained for backwards
compatibility, with rxbuf overriding it if provided.
This commit is contained in:
Damien George 2018-12-05 13:24:11 +11:00
parent c6365ffb92
commit 8007d0bd16
3 changed files with 26 additions and 5 deletions

View File

@ -607,7 +607,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
mp_print_str(print, "CTS");
}
}
mp_printf(print, ", timeout=%u, timeout_char=%u, read_buf_len=%u)",
mp_printf(print, ", timeout=%u, timeout_char=%u, rxbuf=%u)",
self->timeout, self->timeout_char,
self->read_buf_len == 0 ? 0 : self->read_buf_len - 1); // -1 to adjust for usable length of buffer
}
@ -634,12 +634,13 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const
{ MP_QSTR_flow, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = UART_HWCONTROL_NONE} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
{ MP_QSTR_timeout_char, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_read_buf_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
{ MP_QSTR_rxbuf, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_read_buf_len, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} }, // legacy
};
// parse args
struct {
mp_arg_val_t baudrate, bits, parity, stop, flow, timeout, timeout_char, read_buf_len;
mp_arg_val_t baudrate, bits, parity, stop, flow, timeout, timeout_char, rxbuf, read_buf_len;
} args;
mp_arg_parse_all(n_args, pos_args, kw_args,
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
@ -719,6 +720,10 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, size_t n_args, const
}
self->read_buf_head = 0;
self->read_buf_tail = 0;
if (args.rxbuf.u_int >= 0) {
// rxbuf overrides legacy read_buf_len
args.read_buf_len.u_int = args.rxbuf.u_int;
}
if (args.read_buf_len.u_int <= 0) {
// no read buffer
self->read_buf_len = 0;

View File

@ -30,3 +30,15 @@ print(uart.write(b'1'))
print(uart.write(b'abcd'))
print(uart.writechar(1))
print(uart.read(100))
# set rxbuf
uart.init(9600, rxbuf=8)
print(uart)
uart.init(9600, rxbuf=0)
print(uart)
# set read_buf_len (legacy, use rxbuf instead)
uart.init(9600, read_buf_len=4)
print(uart)
uart.init(9600, read_buf_len=0)
print(uart)

View File

@ -12,8 +12,8 @@ UART XB
UART YA
UART YB
ValueError Z
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, read_buf_len=64)
UART(1, baudrate=2400, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=7, read_buf_len=64)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, rxbuf=64)
UART(1, baudrate=2400, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=7, rxbuf=64)
0
3
4
@ -22,3 +22,7 @@ None
4
None
None
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, rxbuf=8)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, rxbuf=0)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, rxbuf=4)
UART(1, baudrate=9600, bits=8, parity=None, stop=1, flow=0, timeout=1000, timeout_char=3, rxbuf=0)