UART changes: timeout in secs, write bytes, etc.

This commit is contained in:
Dan Halbert 2018-12-03 11:00:35 -05:00
parent 57c2c4134c
commit 80db2cec99
8 changed files with 37 additions and 24 deletions

View File

@ -53,7 +53,7 @@ static void usart_async_rxc_callback(const struct usart_async_descriptor *const
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
uint8_t receiver_buffer_size) {
Sercom* sercom = NULL;
uint8_t sercom_index = 255; // Unset index
@ -74,7 +74,7 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
self->baudrate = baudrate;
self->character_bits = bits;
self->timeout_ms = timeout;
self->timeout_ms = timeout * 1000;
// This assignment is only here because the usart_async routines take a *const argument.
struct usart_async_descriptor * const usart_desc_p = (struct usart_async_descriptor * const) &self->usart_desc;
@ -324,10 +324,8 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
return MP_STREAM_ERROR;
}
struct usart_async_status async_status;
// Could return ERR_BUSY, but if that's true there's already a problem.
usart_async_get_status(usart_desc_p, &async_status);
return async_status.txcnt;
// All the characters got written.
return len;
}
uint32_t common_hal_busio_uart_get_baudrate(busio_uart_obj_t *self) {

View File

@ -39,7 +39,7 @@ extern UartDevice UartDev;
void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
uint8_t receiver_buffer_size) {
if (rx != mp_const_none || tx != &pin_GPIO2) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, translate("Only tx supported on UART1 (GPIO2).")));

View File

@ -76,7 +76,7 @@ static void uart_callback_irq (const nrfx_uarte_event_t * event, void * context)
void common_hal_busio_uart_construct (busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
uint8_t receiver_buffer_size) {
if ( (tx == mp_const_none) && (rx == mp_const_none) ) {
mp_raise_ValueError(translate("tx and rx cannot both be None"));
@ -124,7 +124,7 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
}
self->baudrate = baudrate;
self->timeout_ms = timeout;
self->timeout_ms = timeout * 1000;
// queue 1-byte transfer for rx_characters_available()
self->rx_count = -1;
@ -317,7 +317,7 @@ static uint32_t get_nrf_baud (uint32_t baudrate)
void common_hal_busio_uart_construct (busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t bits, uart_parity_t parity, uint8_t stop, float timeout,
uint8_t receiver_buffer_size) {
mp_raise_NotImplementedError(translate("busio.UART not available"));
}

View File

@ -250,6 +250,9 @@ void mp_stream_write_adaptor(void *self, const char *buf, size_t len) {
STATIC mp_obj_t stream_write_method(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
if (!mp_get_stream(args[0])->is_text && MP_OBJ_IS_STR(args[1])) {
mp_raise_ValueError(translate("string not supported; use bytes or bytearray"));
}
size_t max_len = (size_t)-1;
size_t off = 0;
if (n_args == 3) {
@ -282,6 +285,9 @@ STATIC mp_obj_t stream_readinto(size_t n_args, const mp_obj_t *args) {
// https://docs.python.org/3/library/socket.html#socket.socket.recv_into
mp_uint_t len = bufinfo.len;
if (n_args > 2) {
if (mp_get_stream(args[0])->pyserial_compatibility) {
mp_raise_ValueError(translate("length argument not allowed for this type"));
}
len = mp_obj_get_int(args[2]);
if (len > bufinfo.len) {
len = bufinfo.len;

View File

@ -70,6 +70,7 @@ typedef struct _mp_stream_p_t {
mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode);
mp_uint_t is_text : 1; // default is bytes, set this for text stream
bool pyserial_compatibility: 1; // adjust API to match pyserial more closely
} mp_stream_p_t;
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj);

View File

@ -58,7 +58,7 @@
//| :param ~microcontroller.Pin scl: The clock pin
//| :param ~microcontroller.Pin sda: The data pin
//| :param int frequency: The clock frequency in Hertz
//| :param int timeout: The maximum clock stretching timeut - only for bitbang
//| :param int timeout: The maximum clock stretching timeut - (used only for bitbangio.I2C; ignored for busio.I2C)
//|
STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);

View File

@ -45,7 +45,7 @@
//| =================================================
//|
//|
//| .. class:: UART(tx, rx, \*, baudrate=9600, bits=8, parity=None, stop=1, timeout=1000, receiver_buffer_size=64)
//| .. class:: UART(tx, rx, \*, baudrate=9600, bits=8, parity=None, stop=1, timeout=1, receiver_buffer_size=64)
//|
//| A common bidirectional serial protocol that uses an an agreed upon speed
//| rather than a shared clock line.
@ -53,12 +53,14 @@
//| :param ~microcontroller.Pin tx: the pin to transmit with, or ``None`` if this ``UART`` is receive-only.
//| :param ~microcontroller.Pin rx: the pin to receive on, or ``None`` if this ``UART`` is transmit-only.
//| :param int baudrate: the transmit and receive speed.
/// :param int bits: the number of bits per byte, 7, 8 or 9.
/// :param Parity parity: the parity used for error checking.
/// :param int stop: the number of stop bits, 1 or 2.
/// :param int timeout: the timeout in milliseconds to wait for the first character and between subsequent characters.
/// :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.)
//| :param int bits: the number of bits per byte, 7, 8 or 9.
//| :param Parity parity: the parity used for error checking.
//| :param int stop: the number of stop bits, 1 or 2.
//| :param int timeout: the timeout in seconds to wait for the first character and between subsequent characters.
//| :param int receiver_buffer_size: the character length of the read buffer (0 to disable). (When a character is 9 bits the buffer will be 2 * receiver_buffer_size bytes.)
//|
//| *New in CircuitPython 4.0:* ``timeout`` has incompatibly changed units from milliseconds to seconds.
typedef struct {
mp_obj_base_t base;
} busio_uart_parity_obj_t;
@ -83,7 +85,7 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, si
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
{ MP_QSTR_parity, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} },
{ MP_QSTR_receiver_buffer_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 64} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@ -115,8 +117,9 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, si
}
common_hal_busio_uart_construct(self, tx, rx,
args[ARG_baudrate].u_int, bits, parity, stop, args[ARG_timeout].u_int,
args[ARG_receiver_buffer_size].u_int);
args[ARG_baudrate].u_int, bits, parity, stop,
mp_obj_get_float(args[ARG_timeout].u_obj),
args[ARG_receiver_buffer_size].u_int);
return (mp_obj_t)self;
}
@ -161,14 +164,15 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
//| :return: Data read
//| :rtype: bytes or None
//|
//| .. method:: readinto(buf, nbytes=None)
//| .. method:: readinto(buf)
//|
//| Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
//| that many bytes. Otherwise, read at most ``len(buf)`` bytes.
//| Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.
//|
//| :return: number of bytes read and stored into ``buf``
//| :rtype: bytes or None
//|
//| *New in CircuitPython 4.0:* No length parameter is permitted.
//| .. method:: readline()
//|
//| Read a line, ending in a newline character.
@ -180,6 +184,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_
//|
//| Write the buffer of bytes to the bus.
//|
//| *New in CircuitPython 4.0:* ``buf`` must be bytes, not a string.
//|
//| :return: the number of bytes written
//| :rtype: int or None
//|
@ -353,6 +359,8 @@ STATIC const mp_stream_p_t uart_stream_p = {
.write = busio_uart_write,
.ioctl = busio_uart_ioctl,
.is_text = false,
// Match PySerial when possible, such as disallowing optional length argument for .readinto()
.pyserial_compatibility = true,
};
const mp_obj_type_t busio_uart_type = {

View File

@ -41,7 +41,7 @@ typedef enum {
// Construct an underlying UART object.
extern void common_hal_busio_uart_construct(busio_uart_obj_t *self,
const mcu_pin_obj_t * tx, const mcu_pin_obj_t * rx, uint32_t baudrate,
uint8_t bits, uart_parity_t parity, uint8_t stop, uint32_t timeout,
uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout,
uint8_t receiver_buffer_size);
extern void common_hal_busio_uart_deinit(busio_uart_obj_t *self);