circuitpython/shared-bindings/busio/UART.c

431 lines
18 KiB
C
Raw Normal View History

/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdint.h>
#include "shared-bindings/busio/UART.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/util.h"
#include "lib/utils/context_manager_helpers.h"
#include "lib/utils/interrupt_char.h"
#include "py/ioctl.h"
#include "py/objproperty.h"
#include "py/runtime.h"
#include "py/stream.h"
2018-07-31 19:53:54 -04:00
#include "supervisor/shared/translate.h"
#define STREAM_DEBUG(...) (void)0
// #define STREAM_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
2020-04-29 15:20:05 -04:00
//| class UART:
//| """A bidirectional serial protocol"""
2020-07-25 04:58:37 -04:00
//| def __init__(self, tx: microcontroller.Pin, rx: microcontroller.Pin, *, baudrate: int = 9600, bits: int = 8, parity: Optional[Parity] = None, stop: int = 1, timeout: float = 1, receiver_buffer_size: int = 64) -> None:
2020-04-29 15:20:05 -04:00
//| """A common bidirectional serial protocol that uses an an agreed upon speed
//| rather than a shared clock line.
2020-04-25 15:25:31 -04:00
//|
2020-04-29 15:20:05 -04:00
//| :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 ~microcontroller.Pin rts: the pin for rts, or ``None`` if rts not in use.
//| :param ~microcontroller.Pin cts: the pin for cts, or ``None`` if cts not in use.
2020-06-09 21:28:02 -04:00
//| :param ~microcontroller.Pin rs485_dir: the output pin for rs485 direction setting, or ``None`` if rs485 not in use.
//| :param bool rs485_invert: rs485_dir pin active high when set. Active low otherwise.
2020-04-29 15:20:05 -04:00
//| :param int baudrate: the transmit and receive speed.
2021-02-19 08:06:00 -05:00
//| :param int bits: the number of bits per byte, 5 to 8.
2020-04-29 15:20:05 -04:00
//| :param Parity parity: the parity used for error checking.
//| :param int stop: the number of stop bits, 1 or 2.
//| :param float timeout: the timeout in seconds to wait for the first character and between subsequent characters when reading. Raises ``ValueError`` if timeout >100 seconds.
//| :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.)
2020-04-25 15:25:31 -04:00
//|
2020-04-29 15:20:05 -04:00
//| *New in CircuitPython 4.0:* ``timeout`` has incompatibly changed units from milliseconds to seconds.
//| The new upper limit on ``timeout`` is meant to catch mistaken use of milliseconds."""
//| ...
2020-04-29 15:55:06 -04:00
//|
typedef struct {
mp_obj_base_t base;
} busio_uart_parity_obj_t;
extern const busio_uart_parity_obj_t busio_uart_parity_even_obj;
extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj;
STATIC void validate_timeout(mp_float_t timeout) {
if (timeout < (mp_float_t) 0.0f || timeout > (mp_float_t) 100.0f) {
2019-11-27 14:49:39 -05:00
mp_raise_ValueError(translate("timeout must be 0.0-100.0 seconds"));
}
}
STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
// Always initially allocate the UART object within the long-lived heap.
// This is needed to avoid crashes with certain UART implementations which
// cannot accomodate being moved after creation. (See
// https://github.com/adafruit/circuitpython/issues/1056)
busio_uart_obj_t *self = m_new_ll_obj(busio_uart_obj_t);
self->base.type = &busio_uart_type;
2020-02-17 19:09:35 -05:00
enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_timeout, ARG_receiver_buffer_size,
ARG_rts, ARG_cts, ARG_rs485_dir,ARG_rs485_invert};
static const mp_arg_t allowed_args[] = {
2020-06-09 21:28:02 -04:00
{ MP_QSTR_tx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_rx, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 9600} },
{ 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_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_QSTR_rts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_cts, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
2020-02-17 19:09:35 -05:00
{ MP_QSTR_rs485_dir, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none } },
{ MP_QSTR_rs485_invert, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false } },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
const mcu_pin_obj_t* rx = validate_obj_is_free_pin_or_none(args[ARG_rx].u_obj);
const mcu_pin_obj_t* tx = validate_obj_is_free_pin_or_none(args[ARG_tx].u_obj);
if ( (tx == NULL) && (rx == NULL) ) {
mp_raise_ValueError(translate("tx and rx cannot both be None"));
}
2021-02-19 08:06:00 -05:00
if (args[ARG_bits].u_int < 5 || args[ARG_bits].u_int > 8) {
mp_raise_ValueError(translate("bits must be between 5 and 8"));
}
2021-02-19 08:06:00 -05:00
uint8_t bits = args[ARG_bits].u_int;
2020-06-09 21:28:02 -04:00
busio_uart_parity_t parity = BUSIO_UART_PARITY_NONE;
if (args[ARG_parity].u_obj == &busio_uart_parity_even_obj) {
2020-06-09 21:28:02 -04:00
parity = BUSIO_UART_PARITY_EVEN;
} else if (args[ARG_parity].u_obj == &busio_uart_parity_odd_obj) {
2020-06-09 21:28:02 -04:00
parity = BUSIO_UART_PARITY_ODD;
}
uint8_t stop = args[ARG_stop].u_int;
if (stop != 1 && stop != 2) {
2018-07-31 19:53:54 -04:00
mp_raise_ValueError(translate("stop must be 1 or 2"));
}
mp_float_t timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
validate_timeout(timeout);
const mcu_pin_obj_t* rts = validate_obj_is_free_pin_or_none(args[ARG_rts].u_obj);
const mcu_pin_obj_t* cts = validate_obj_is_free_pin_or_none(args[ARG_cts].u_obj);
const mcu_pin_obj_t* rs485_dir = validate_obj_is_free_pin_or_none(args[ARG_rs485_dir].u_obj);
const bool rs485_invert = args[ARG_rs485_invert].u_bool;
2020-02-17 19:09:35 -05:00
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
args[ARG_baudrate].u_int, bits, parity, stop, timeout,
args[ARG_receiver_buffer_size].u_int, NULL, false);
return (mp_obj_t)self;
}
2020-07-02 13:28:36 -04:00
//| def deinit(self) -> None:
2020-04-29 15:45:19 -04:00
//| """Deinitialises the UART and releases any hardware resources for reuse."""
//| ...
2020-04-29 15:55:06 -04:00
//|
STATIC mp_obj_t busio_uart_obj_deinit(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
common_hal_busio_uart_deinit(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_deinit_obj, busio_uart_obj_deinit);
STATIC void check_for_deinit(busio_uart_obj_t *self) {
if (common_hal_busio_uart_deinited(self)) {
raise_deinited_error();
}
}
2020-07-02 13:28:36 -04:00
//| def __enter__(self) -> UART:
2020-04-29 15:45:19 -04:00
//| """No-op used by Context Managers."""
//| ...
2020-04-29 15:55:06 -04:00
//|
// Provided by context manager helper.
2020-07-02 13:28:36 -04:00
//| def __exit__(self) -> None:
2020-04-29 15:45:19 -04:00
//| """Automatically deinitializes the hardware when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
2020-04-29 15:55:06 -04:00
//|
STATIC mp_obj_t busio_uart_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_busio_uart_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_uart_obj___exit__);
// These are standard stream methods. Code is in py/stream.c.
//
2020-07-03 13:49:00 -04:00
//| def read(self, nbytes: Optional[int] = None) -> Optional[bytes]:
2020-04-29 15:45:19 -04:00
//| """Read characters. If ``nbytes`` is specified then read at most that many
//| bytes. Otherwise, read everything that arrives until the connection
//| times out. Providing the number of bytes expected is highly recommended
//| because it will be faster.
//|
2020-04-29 15:45:19 -04:00
//| :return: Data read
//| :rtype: bytes or None"""
//| ...
2020-04-29 15:55:06 -04:00
//|
2020-04-25 15:25:31 -04:00
//| def readinto(self, buf: WriteableBuffer) -> Optional[int]:
2020-04-29 15:45:19 -04:00
//| """Read bytes into the ``buf``. Read at most ``len(buf)`` bytes.
//|
2020-04-29 15:45:19 -04:00
//| :return: number of bytes read and stored into ``buf``
//| :rtype: int or None (on a non-blocking error)
//|
2020-04-29 15:45:19 -04:00
//| *New in CircuitPython 4.0:* No length parameter is permitted."""
//| ...
2020-04-29 15:55:06 -04:00
//|
2020-07-02 13:28:36 -04:00
//| def readline(self) -> bytes:
2020-07-02 16:56:09 -04:00
//| """Read a line, ending in a newline character, or
//| return None if a timeout occurs sooner, or
2020-07-02 18:11:40 -04:00
//| return everything readable if no newline is found and timeout=0
//|
2020-04-29 15:45:19 -04:00
//| :return: the line read
2020-07-02 16:56:09 -04:00
//| :rtype: bytes or None"""
2020-04-29 15:45:19 -04:00
//| ...
2020-04-29 15:55:06 -04:00
//|
2020-04-25 15:25:31 -04:00
//| def write(self, buf: WriteableBuffer) -> Optional[int]:
2020-04-29 15:45:19 -04:00
//| """Write the buffer of bytes to the bus.
//|
2020-04-29 15:45:19 -04:00
//| *New in CircuitPython 4.0:* ``buf`` must be bytes, not a string.
//|
2020-04-29 15:45:19 -04:00
//| :return: the number of bytes written
//| :rtype: int or None"""
//| ...
2020-04-29 15:55:06 -04:00
//|
// These three methods are used by the shared stream methods.
STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
STREAM_DEBUG("busio_uart_read stream %d\n", size);
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
byte *buf = buf_in;
// make sure we want at least 1 char
if (size == 0) {
return 0;
}
return common_hal_busio_uart_read(self, buf, size, errcode);
}
STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
const byte *buf = buf_in;
return common_hal_busio_uart_write(self, buf, size, errcode);
}
STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_uint_t ret;
if (request == MP_IOCTL_POLL) {
mp_uint_t flags = arg;
ret = 0;
if ((flags & MP_IOCTL_POLL_RD) && common_hal_busio_uart_rx_characters_available(self) > 0) {
ret |= MP_IOCTL_POLL_RD;
}
if ((flags & MP_IOCTL_POLL_WR) && common_hal_busio_uart_ready_to_tx(self)) {
ret |= MP_IOCTL_POLL_WR;
}
} else {
*errcode = MP_EINVAL;
ret = MP_STREAM_ERROR;
}
return ret;
}
2020-07-25 04:58:37 -04:00
//| baudrate: int
2020-04-29 15:45:19 -04:00
//| """The current baudrate."""
2020-04-29 15:20:05 -04:00
//|
STATIC mp_obj_t busio_uart_obj_get_baudrate(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_busio_uart_get_baudrate(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_get_baudrate_obj, busio_uart_obj_get_baudrate);
STATIC mp_obj_t busio_uart_obj_set_baudrate(mp_obj_t self_in, mp_obj_t baudrate) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_busio_uart_set_baudrate(self, mp_obj_get_int(baudrate));
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(busio_uart_set_baudrate_obj, busio_uart_obj_set_baudrate);
const mp_obj_property_t busio_uart_baudrate_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&busio_uart_get_baudrate_obj,
(mp_obj_t)&busio_uart_set_baudrate_obj,
(mp_obj_t)&mp_const_none_obj},
};
2020-07-25 04:58:37 -04:00
//| in_waiting: int
2020-04-29 15:45:19 -04:00
//| """The number of bytes in the input buffer, available to be read"""
2020-04-29 15:20:05 -04:00
//|
STATIC mp_obj_t busio_uart_obj_get_in_waiting(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_busio_uart_rx_characters_available(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_get_in_waiting_obj, busio_uart_obj_get_in_waiting);
const mp_obj_property_t busio_uart_in_waiting_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&busio_uart_get_in_waiting_obj,
(mp_obj_t)&mp_const_none_obj,
(mp_obj_t)&mp_const_none_obj},
};
2020-07-25 04:58:37 -04:00
//| timeout: float
2020-04-29 15:45:19 -04:00
//| """The current timeout, in seconds (float)."""
2020-04-29 15:20:05 -04:00
//|
STATIC mp_obj_t busio_uart_obj_get_timeout(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_float(common_hal_busio_uart_get_timeout(self));
}
MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_get_timeout_obj, busio_uart_obj_get_timeout);
STATIC mp_obj_t busio_uart_obj_set_timeout(mp_obj_t self_in, mp_obj_t timeout) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
mp_float_t timeout_float = mp_obj_get_float(timeout);
validate_timeout(timeout_float);
common_hal_busio_uart_set_timeout(self, timeout_float);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(busio_uart_set_timeout_obj, busio_uart_obj_set_timeout);
const mp_obj_property_t busio_uart_timeout_obj = {
.base.type = &mp_type_property,
.proxy = {(mp_obj_t)&busio_uart_get_timeout_obj,
(mp_obj_t)&busio_uart_set_timeout_obj,
(mp_obj_t)&mp_const_none_obj},
};
2020-07-02 13:28:36 -04:00
//| def reset_input_buffer(self) -> None:
2020-07-02 10:23:17 -04:00
//| """Discard any unread characters in the input buffer."""
//| ...
2020-04-29 15:20:05 -04:00
//|
STATIC mp_obj_t busio_uart_obj_reset_input_buffer(mp_obj_t self_in) {
busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
common_hal_busio_uart_clear_rx_buffer(self);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(busio_uart_reset_input_buffer_obj, busio_uart_obj_reset_input_buffer);
2020-04-29 15:45:19 -04:00
//| class Parity:
2020-04-29 15:20:05 -04:00
//| """Enum-like class to define the parity used to verify correct data transfer."""
//|
2020-07-25 04:58:37 -04:00
//| ODD: int
2020-05-13 11:36:16 -04:00
//| """Total number of ones should be odd."""
2020-04-29 15:20:05 -04:00
//|
2020-07-25 04:58:37 -04:00
//| EVEN: int
2020-05-13 11:36:16 -04:00
//| """Total number of ones should be even."""
2020-04-29 15:55:06 -04:00
//|
const mp_obj_type_t busio_uart_parity_type;
const busio_uart_parity_obj_t busio_uart_parity_odd_obj = {
{ &busio_uart_parity_type },
};
const busio_uart_parity_obj_t busio_uart_parity_even_obj = {
{ &busio_uart_parity_type },
};
STATIC const mp_rom_map_elem_t busio_uart_parity_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ODD), MP_ROM_PTR(&busio_uart_parity_odd_obj) },
{ MP_ROM_QSTR(MP_QSTR_EVEN), MP_ROM_PTR(&busio_uart_parity_even_obj) },
};
STATIC MP_DEFINE_CONST_DICT(busio_uart_parity_locals_dict, busio_uart_parity_locals_dict_table);
STATIC void busio_uart_parity_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
qstr parity = MP_QSTR_ODD;
if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&busio_uart_parity_even_obj)) {
parity = MP_QSTR_EVEN;
}
mp_printf(print, "%q.%q.%q.%q", MP_QSTR_busio, MP_QSTR_UART, MP_QSTR_Parity, parity);
}
const mp_obj_type_t busio_uart_parity_type = {
{ &mp_type_type },
.name = MP_QSTR_Parity,
.print = busio_uart_parity_print,
.locals_dict = (mp_obj_t)&busio_uart_parity_locals_dict,
};
STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&busio_uart_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&busio_uart___exit___obj) },
// Standard stream methods.
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj)},
{ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset_input_buffer), MP_ROM_PTR(&busio_uart_reset_input_buffer_obj) },
// Properties
{ MP_ROM_QSTR(MP_QSTR_baudrate), MP_ROM_PTR(&busio_uart_baudrate_obj) },
{ MP_ROM_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&busio_uart_in_waiting_obj) },
{ MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&busio_uart_timeout_obj) },
2018-07-31 19:53:54 -04:00
// Nested Enum-like Classes.
{ MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&busio_uart_parity_type) },
};
STATIC MP_DEFINE_CONST_DICT(busio_uart_locals_dict, busio_uart_locals_dict_table);
STATIC const mp_stream_p_t uart_stream_p = {
protocols: Allow them to be (optionally) type-safe Protocols are nice, but there is no way for C code to verify whether a type's "protocol" structure actually implements some particular protocol. As a result, you can pass an object that implements the "vfs" protocol to one that expects the "stream" protocol, and the opposite of awesomeness ensues. This patch adds an OPTIONAL (but enabled by default) protocol identifier as the first member of any protocol structure. This identifier is simply a unique QSTR chosen by the protocol designer and used by each protocol implementer. When checking for protocol support, instead of just checking whether the object's type has a non-NULL protocol field, use `mp_proto_get` which implements the protocol check when possible. The existing protocols are now named: protocol_framebuf protocol_i2c protocol_pin protocol_stream protocol_spi protocol_vfs (most of these are unused in CP and are just inherited from MP; vfs and stream are definitely used though) I did not find any crashing examples, but here's one to give a flavor of what is improved, using `micropython_coverage`. Before the change, the vfs "ioctl" protocol is invoked, and the result is not intelligible as json (but it could have resulted in a hard fault, potentially): >>> import uos, ujson >>> u = uos.VfsPosix('/tmp') >>> ujson.load(u) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: syntax error in JSON After the change, the vfs object is correctly detected as not supporting the stream protocol: >>> ujson.load(p) Traceback (most recent call last): File "<stdin>", line 1, in <module> OSError: stream operation not supported
2019-12-03 15:50:37 -05:00
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_stream)
.read = busio_uart_read,
.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 = {
{ &mp_type_type },
.name = MP_QSTR_UART,
.make_new = busio_uart_make_new,
Merge tag 'v1.9.1' Fixes for stmhal USB mass storage, lwIP bindings and VFS regressions This release provides an important fix for the USB mass storage device in the stmhal port by implementing the SCSI SYNCHRONIZE_CACHE command, which is now require by some Operating Systems. There are also fixes for the lwIP bindings to improve non-blocking sockets and error codes. The VFS has some regressions fixed including the ability to statvfs the root. All changes are listed below. py core: - modbuiltins: add core-provided version of input() function - objstr: catch case of negative "maxsplit" arg to str.rsplit() - persistentcode: allow to compile with complex numbers disabled - objstr: allow to compile with obj-repr D, and unicode disabled - modsys: allow to compile with obj-repr D and PY_ATTRTUPLE disabled - provide mp_decode_uint_skip() to help reduce stack usage - makeqstrdefs.py: make script run correctly with Python 2.6 - objstringio: if created from immutable object, follow copy on write policy extmod: - modlwip: connect: for non-blocking mode, return EINPROGRESS - modlwip: fix error codes for duplicate calls to connect() - modlwip: accept: fix error code for non-blocking mode - vfs: allow to statvfs the root directory - vfs: allow "buffering" and "encoding" args to VFS's open() - modframebuf: fix signed/unsigned comparison pendantic warning lib: - libm: use isfinite instead of finitef, for C99 compatibility - utils/interrupt_char: remove support for KBD_EXCEPTION disabled tests: - basics/string_rsplit: add tests for negative "maxsplit" argument - float: convert "sys.exit()" to "raise SystemExit" - float/builtin_float_minmax: PEP8 fixes - basics: convert "sys.exit()" to "raise SystemExit" - convert remaining "sys.exit()" to "raise SystemExit" unix port: - convert to use core-provided version of built-in import() - Makefile: replace references to make with $(MAKE) windows port: - convert to use core-provided version of built-in import() qemu-arm port: - Makefile: adjust object-file lists to get correct dependencies - enable micropython.mem_*() functions to allow more tests stmhal port: - boards: enable DAC for NUCLEO_F767ZI board - add support for NUCLEO_F446RE board - pass USB handler as parameter to allow more than one USB handler - usb: use local USB handler variable in Start-of-Frame handler - usb: make state for USB device private to top-level USB driver - usbdev: for MSC implement SCSI SYNCHRONIZE_CACHE command - convert from using stmhal's input() to core provided version cc3200 port: - convert from using stmhal's input() to core provided version teensy port: - convert from using stmhal's input() to core provided version esp8266 port: - Makefile: replace references to make with $(MAKE) - Makefile: add clean-modules target - convert from using stmhal's input() to core provided version zephyr port: - modusocket: getaddrinfo: Fix mp_obj_len() usage - define MICROPY_PY_SYS_PLATFORM (to "zephyr") - machine_pin: use native Zephyr types for Zephyr API calls docs: - machine.Pin: remove out_value() method - machine.Pin: add on() and off() methods - esp8266: consistently replace Pin.high/low methods with .on/off - esp8266/quickref: polish Pin.on()/off() examples - network: move confusingly-named cc3200 Server class to its reference - uos: deconditionalize, remove minor port-specific details - uos: move cc3200 port legacy VFS mounting functions to its ref doc - machine: sort machine classes in logical order, not alphabetically - network: first step to describe standard network class interface examples: - embedding: use core-provided KeyboardInterrupt object
2017-06-20 13:56:05 -04:00
.getiter = mp_identity_getiter,
.iternext = mp_stream_unbuffered_iter,
.protocol = &uart_stream_p,
.locals_dict = (mp_obj_dict_t*)&busio_uart_locals_dict,
};