diff --git a/extmod/machine_uart.c b/extmod/machine_uart.c index dd556bbbbfafa..7298124a06aca 100644 --- a/extmod/machine_uart.c +++ b/extmod/machine_uart.c @@ -54,6 +54,10 @@ STATIC void mp_machine_uart_writechar(machine_uart_obj_t *self, uint16_t data); STATIC mp_irq_obj_t *mp_machine_uart_irq(machine_uart_obj_t *self, bool any_args, mp_arg_val_t *args); #endif +#if MICROPY_PY_MACHINE_UART_FIFO_BUFFER +STATIC mp_int_t mp_machine_uart_fifo_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags); +#endif + STATIC mp_uint_t mp_machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode); STATIC mp_uint_t mp_machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode); STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode); @@ -181,6 +185,9 @@ MP_DEFINE_CONST_OBJ_TYPE( make_new, mp_machine_uart_make_new, print, mp_machine_uart_print, protocol, &uart_stream_p, + #if MICROPY_PY_MACHINE_UART_FIFO_BUFFER + buffer, mp_machine_uart_fifo_buffer, + #endif locals_dict, &machine_uart_locals_dict ); diff --git a/extmod/modmachine.h b/extmod/modmachine.h index d186ce661510c..7e0710eed304e 100644 --- a/extmod/modmachine.h +++ b/extmod/modmachine.h @@ -88,6 +88,12 @@ #define MICROPY_PY_MACHINE_UART_IRQ (0) #endif +// Whether to enable the direct access to the UART FIFO through the buffer interface. +// Requires a port to implement mp_machine_uart_fifo_buffer(). +#ifndef MICROPY_PY_MACHINE_UART_FIFO_BUFFER +#define MICROPY_PY_MACHINE_UART_FIFO_BUFFER (0) +#endif + // Temporary support for legacy construction of SoftI2C via I2C type. #define MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args) \ do { \ diff --git a/ports/rp2/machine_uart.c b/ports/rp2/machine_uart.c index d751b7af07a2e..708bf6e6ea2b7 100644 --- a/ports/rp2/machine_uart.c +++ b/ports/rp2/machine_uart.c @@ -570,5 +570,15 @@ STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uint return ret; } +STATIC mp_int_t mp_machine_uart_fifo_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + machine_uart_obj_t *self = o_in; + + bufinfo->len = 4; + bufinfo->typecode = 'I'; + bufinfo->buf = (void *)&uart_get_hw(self->uart)->dr; + + return 0; +} + MP_REGISTER_ROOT_POINTER(void *rp2_uart_rx_buffer[2]); MP_REGISTER_ROOT_POINTER(void *rp2_uart_tx_buffer[2]); diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index 1cc9b96160396..79bfda9d806bf 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -138,6 +138,7 @@ #define MICROPY_PY_MACHINE_UART (1) #define MICROPY_PY_MACHINE_UART_INCLUDEFILE "ports/rp2/machine_uart.c" #define MICROPY_PY_MACHINE_UART_SENDBREAK (1) +#define MICROPY_PY_MACHINE_UART_FIFO_BUFFER (1) #define MICROPY_PY_MACHINE_WDT (1) #define MICROPY_PY_MACHINE_WDT_INCLUDEFILE "ports/rp2/machine_wdt.c" #define MICROPY_PY_ONEWIRE (1)