diff options
| author | Dan Halbert <halbert@halwitz.org> | 2018-09-18 15:38:12 -0400 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2018-09-18 15:38:12 -0400 |
| commit | bc510e714f4fde7af949c525ced5c8a4db53bce0 (patch) | |
| tree | 40be4302742b016cc98f4f3966058e84756b7f99 /shared-bindings/busio | |
| parent | 53fcc5a3a207d606c7e3ee2c0c877295a0fb4e2f (diff) | |
| parent | 2dd9407f2125e724b4b9a1cdd4d0d0cc01284b31 (diff) | |
merge 3.0.2 to master
Diffstat (limited to 'shared-bindings/busio')
| -rw-r--r-- | shared-bindings/busio/UART.c | 39 | ||||
| -rw-r--r-- | shared-bindings/busio/UART.h | 1 |
2 files changed, 39 insertions, 1 deletions
diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index 359334e5c..e7b8fc0c5 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -67,7 +67,11 @@ extern const busio_uart_parity_obj_t busio_uart_parity_odd_obj; STATIC mp_obj_t busio_uart_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); - busio_uart_obj_t *self = m_new_obj(busio_uart_obj_t); + // 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; mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args); @@ -249,6 +253,36 @@ const mp_obj_property_t busio_uart_baudrate_obj = { (mp_obj_t)&mp_const_none_obj}, }; +//| .. attribute:: in_waiting +//| +//| The number of bytes in the input buffer, available to be read +//| +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); + raise_error_if_deinited(common_hal_busio_uart_deinited(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}, +}; + +//| .. method:: reset_input_buffer() +//| +//| Discard any unread characters in the input buffer. +//| +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); + raise_error_if_deinited(common_hal_busio_uart_deinited(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); + //| .. class:: busio.UART.Parity //| //| Enum-like class to define the parity used to verify correct data transfer. @@ -303,8 +337,11 @@ STATIC const mp_rom_map_elem_t busio_uart_locals_dict_table[] = { { 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) }, // Nested Enum-like Classes. { MP_ROM_QSTR(MP_QSTR_Parity), MP_ROM_PTR(&busio_uart_parity_type) }, diff --git a/shared-bindings/busio/UART.h b/shared-bindings/busio/UART.h index fa39d8ad5..513ff50c8 100644 --- a/shared-bindings/busio/UART.h +++ b/shared-bindings/busio/UART.h @@ -60,6 +60,7 @@ extern void common_hal_busio_uart_set_baudrate(busio_uart_obj_t *self, uint32_t extern uint32_t common_hal_busio_uart_rx_characters_available(busio_uart_obj_t *self); +extern void common_hal_busio_uart_clear_rx_buffer(busio_uart_obj_t *self); extern bool common_hal_busio_uart_ready_to_tx(busio_uart_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_UART_H |
