extmod/machine_uart: Factor ports' UART Python bindings to common code.

This is a code factoring to have the Python bindings in one location, and
all the ports use those same bindings.  For all ports except the two listed
below there is no functional change.

The nrf port has UART.sendbreak() removed, but this method previously did
nothing.

The zephyr port has the following methods added:
- UART.init(): supports setting timeout and timeout_char.
- UART.deinit(): does nothing, just returns None.
- UART.flush(): raises OSError(EINVAL) because it's not implemented.
- UART.any() and UART.txdone(): raise NotImplementedError.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George
2023-10-10 23:46:07 +11:00
parent 95d8b5fd55
commit 5b4a2baff6
65 changed files with 661 additions and 996 deletions

View File

@@ -57,7 +57,7 @@ typedef enum {
// OR-ed IRQ flags which should not be touched by the user
#define MP_UART_RESERVED_FLAGS UART_FLAG_RXNE
typedef struct _pyb_uart_obj_t {
typedef struct _machine_uart_obj_t {
mp_obj_base_t base;
USART_TypeDef *uartx;
pyb_uart_t uart_id : 8;
@@ -75,34 +75,33 @@ typedef struct _pyb_uart_obj_t {
uint16_t mp_irq_trigger; // user IRQ trigger mask
uint16_t mp_irq_flags; // user IRQ active IRQ flags
mp_irq_obj_t *mp_irq_obj; // user IRQ object
} pyb_uart_obj_t;
} machine_uart_obj_t;
extern const mp_obj_type_t pyb_uart_type;
extern const mp_irq_methods_t uart_irq_methods;
void uart_init0(void);
void uart_deinit_all(void);
bool uart_exists(int uart_id);
bool uart_init(pyb_uart_obj_t *uart_obj,
bool uart_init(machine_uart_obj_t *uart_obj,
uint32_t baudrate, uint32_t bits, uint32_t parity, uint32_t stop, uint32_t flow);
void uart_irq_config(pyb_uart_obj_t *self, bool enable);
void uart_set_rxbuf(pyb_uart_obj_t *self, size_t len, void *buf);
void uart_deinit(pyb_uart_obj_t *uart_obj);
void uart_irq_config(machine_uart_obj_t *self, bool enable);
void uart_set_rxbuf(machine_uart_obj_t *self, size_t len, void *buf);
void uart_deinit(machine_uart_obj_t *uart_obj);
void uart_irq_handler(mp_uint_t uart_id);
void uart_attach_to_repl(pyb_uart_obj_t *self, bool attached);
uint32_t uart_get_source_freq(pyb_uart_obj_t *self);
uint32_t uart_get_baudrate(pyb_uart_obj_t *self);
void uart_set_baudrate(pyb_uart_obj_t *self, uint32_t baudrate);
void uart_attach_to_repl(machine_uart_obj_t *self, bool attached);
uint32_t uart_get_source_freq(machine_uart_obj_t *self);
uint32_t uart_get_baudrate(machine_uart_obj_t *self);
void uart_set_baudrate(machine_uart_obj_t *self, uint32_t baudrate);
mp_uint_t uart_rx_any(pyb_uart_obj_t *uart_obj);
bool uart_rx_wait(pyb_uart_obj_t *self, uint32_t timeout);
int uart_rx_char(pyb_uart_obj_t *uart_obj);
bool uart_tx_wait(pyb_uart_obj_t *self, uint32_t timeout);
size_t uart_tx_data(pyb_uart_obj_t *self, const void *src_in, size_t num_chars, int *errcode);
void uart_tx_strn(pyb_uart_obj_t *uart_obj, const char *str, uint len);
mp_uint_t uart_rx_any(machine_uart_obj_t *uart_obj);
bool uart_rx_wait(machine_uart_obj_t *self, uint32_t timeout);
int uart_rx_char(machine_uart_obj_t *uart_obj);
bool uart_tx_wait(machine_uart_obj_t *self, uint32_t timeout);
size_t uart_tx_data(machine_uart_obj_t *self, const void *src_in, size_t num_chars, int *errcode);
void uart_tx_strn(machine_uart_obj_t *uart_obj, const char *str, uint len);
static inline bool uart_tx_avail(pyb_uart_obj_t *self) {
static inline bool uart_tx_avail(machine_uart_obj_t *self) {
#if defined(STM32F4) || defined(STM32L1)
return self->uartx->SR & USART_SR_TXE;
#elif defined(STM32G0) || defined(STM32H7) || defined(STM32WL)