From 4809c9235468914231e9ee1a7e454a9e42166e96 Mon Sep 17 00:00:00 2001 From: Adam Cummick Date: Wed, 30 Dec 2020 16:02:07 -0500 Subject: [PATCH 1/3] WIP - Add native helper based on displayio --- shared-bindings/busio/UART.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index bf0b7e721d..d605f71b55 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -35,6 +35,7 @@ #include "py/ioctl.h" #include "py/objproperty.h" +#include "py/objtype.h" #include "py/runtime.h" #include "py/stream.h" #include "supervisor/shared/translate.h" @@ -142,6 +143,18 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co return (mp_obj_t)self; } + +// Helper to ensure we have the native super class instead of a subclass. +busio_uart_obj_t* native_uart(mp_obj_t uart_obj) { + mp_obj_t native_uart = mp_instance_cast_to_native_base(uart_obj, &busio_uart_type); + if (native_uart == MP_OBJ_NULL) { + mp_raise_ValueError_varg(translate("Must be a %q subclass."), MP_QSTR_UART); + } + mp_obj_assert_native_inited(native_uart); + return MP_OBJ_TO_PTR(native_uart); +} + + //| def deinit(self) -> None: //| """Deinitialises the UART and releases any hardware resources for reuse.""" //| ... From 23c292a2a1b9122e0a8f0038cbdb264a374f0fd7 Mon Sep 17 00:00:00 2001 From: "adam_cummick:g9T51EHpC9gPQqG6sb9Q@gitlab.com" Date: Wed, 6 Jan 2021 08:54:26 -0500 Subject: [PATCH 2/3] Add native_uart calls --- shared-bindings/busio/UART.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index d605f71b55..1d9308edba 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -160,7 +160,7 @@ busio_uart_obj_t* native_uart(mp_obj_t uart_obj) { //| ... //| STATIC mp_obj_t busio_uart_obj_deinit(mp_obj_t self_in) { - busio_uart_obj_t *self = MP_OBJ_TO_PTR(self_in); + busio_uart_obj_t *self = native_uart(self_in); common_hal_busio_uart_deinit(self); return mp_const_none; } @@ -236,7 +236,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(busio_uart___exit___obj, 4, 4, busio_ // 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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); byte *buf = buf_in; @@ -249,7 +249,7 @@ STATIC mp_uint_t busio_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, } 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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); const byte *buf = buf_in; @@ -257,7 +257,7 @@ STATIC mp_uint_t busio_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_ } 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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); mp_uint_t ret; if (request == MP_IOCTL_POLL) { @@ -280,14 +280,14 @@ STATIC mp_uint_t busio_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t //| """The current baudrate.""" //| 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); + busio_uart_obj_t *self = native_uart(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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); common_hal_busio_uart_set_baudrate(self, mp_obj_get_int(baudrate)); return mp_const_none; @@ -306,7 +306,7 @@ const mp_obj_property_t busio_uart_baudrate_obj = { //| """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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_busio_uart_rx_characters_available(self)); } @@ -323,14 +323,14 @@ const mp_obj_property_t busio_uart_in_waiting_obj = { //| """The current timeout, in seconds (float).""" //| 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); + busio_uart_obj_t *self = native_uart(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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); mp_float_t timeout_float = mp_obj_get_float(timeout); validate_timeout(timeout_float); @@ -352,7 +352,7 @@ const mp_obj_property_t busio_uart_timeout_obj = { //| ... //| 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); + busio_uart_obj_t *self = native_uart(self_in); check_for_deinit(self); common_hal_busio_uart_clear_rx_buffer(self); return mp_const_none; From 916bd92b0cf8c46101d5d1ab88308593d6090e0e Mon Sep 17 00:00:00 2001 From: microDev <70126934+microDev1@users.noreply.github.com> Date: Tue, 13 Jul 2021 08:56:18 +0530 Subject: [PATCH 3/3] update native_uart implementation --- shared-bindings/busio/UART.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/busio/UART.c b/shared-bindings/busio/UART.c index 1e9f6f4a52..bd97910192 100644 --- a/shared-bindings/busio/UART.c +++ b/shared-bindings/busio/UART.c @@ -145,8 +145,8 @@ STATIC mp_obj_t busio_uart_make_new(const mp_obj_type_t *type, size_t n_args, co // Helper to ensure we have the native super class instead of a subclass. -busio_uart_obj_t* native_uart(mp_obj_t uart_obj) { - mp_obj_t native_uart = mp_instance_cast_to_native_base(uart_obj, &busio_uart_type); +busio_uart_obj_t *native_uart(mp_obj_t uart_obj) { + mp_obj_t native_uart = mp_obj_cast_to_native_base(uart_obj, &busio_uart_type); if (native_uart == MP_OBJ_NULL) { mp_raise_ValueError_varg(translate("Must be a %q subclass."), MP_QSTR_UART); }