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

@@ -102,7 +102,6 @@ SRC_C += \
machine_pin.c \
machine_rtc.c \
machine_spi.c \
machine_uart.c \
main.c \
modmachine.c \
modsamd.c \

View File

@@ -71,8 +71,6 @@ typedef struct _machine_i2c_obj_t {
uint8_t *buf;
} machine_i2c_obj_t;
extern Sercom *sercom_instance[];
STATIC void i2c_send_command(Sercom *i2c, uint8_t command) {
i2c->I2CM.CTRLB.bit.CMD = command;
while (i2c->I2CM.SYNCBUSY.bit.SYSOP) {

View File

@@ -60,8 +60,6 @@ typedef struct _machine_spi_obj_t {
size_t rxlen;
} machine_spi_obj_t;
extern Sercom *sercom_instance[];
void common_spi_irq_handler(int spi_id) {
// handle Sercom IRQ RXC
machine_spi_obj_t *self = MP_STATE_PORT(sercom_table[spi_id]);

View File

@@ -24,17 +24,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "py/runtime.h"
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
// This file is never compiled standalone, it's included directly from
// extmod/machine_uart.c via MICROPY_PY_MACHINE_UART_INCLUDEFILE.
#include "py/mphal.h"
#include "py/stream.h"
#include "py/ringbuf.h"
#include "modmachine.h"
#include "samd_soc.h"
#include "pin_af.h"
#include "clock_config.h"
#define DEFAULT_UART_BAUDRATE (115200)
#define DEFAULT_BUFFER_SIZE (256)
@@ -43,6 +40,8 @@
#define FLOW_CONTROL_RTS (1)
#define FLOW_CONTROL_CTS (2)
#define MICROPY_PY_MACHINE_UART_CLASS_CONSTANTS
typedef struct _machine_uart_obj_t {
mp_obj_base_t base;
uint8_t id;
@@ -69,30 +68,6 @@ typedef struct _machine_uart_obj_t {
#endif
} machine_uart_obj_t;
Sercom *sercom_instance[] = SERCOM_INSTS;
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);
// Common Sercom functions used by all Serial devices
void sercom_enable(Sercom *uart, int state) {
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
// Wait for the Registers to update.
while (uart->USART.SYNCBUSY.bit.ENABLE) {
}
}
void sercom_deinit_all(void) {
for (int i = 0; i < SERCOM_INST_NUM; i++) {
Sercom *uart = sercom_instance[i];
uart->USART.INTENCLR.reg = 0xff;
sercom_register_irq(i, NULL);
sercom_enable(uart, 0);
MP_STATE_PORT(sercom_table[i]) = NULL;
}
}
#endif
#if MICROPY_PY_MACHINE_UART
STATIC const char *_parity_name[] = {"None", "", "0", "1"}; // Is defined as 0, 2, 3
// Irq handler
@@ -137,7 +112,7 @@ void common_uart_irq_handler(int uart_id) {
}
}
STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
STATIC void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, "
"timeout=%u, timeout_char=%u, rxbuf=%d"
@@ -160,7 +135,7 @@ STATIC void machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_pri
);
}
STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
STATIC void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_baudrate, ARG_bits, ARG_parity, ARG_stop, ARG_tx, ARG_rx,
ARG_timeout, ARG_timeout_char, ARG_rxbuf, ARG_txbuf, ARG_rts, ARG_cts };
static const mp_arg_t allowed_args[] = {
@@ -373,11 +348,9 @@ STATIC mp_obj_t machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args
sercom_enable(uart, 1);
}
return MP_OBJ_FROM_PTR(self);
}
STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
STATIC mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
// Get UART bus.
@@ -405,17 +378,12 @@ STATIC mp_obj_t machine_uart_make_new(const mp_obj_type_t *type, size_t n_args,
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
return machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
mp_machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
return MP_OBJ_FROM_PTR(self);
}
// uart.init(baud, [kwargs])
STATIC mp_obj_t machine_uart_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
return machine_uart_init_helper(args[0], n_args - 1, args + 1, kw_args);
}
MP_DEFINE_CONST_FUN_OBJ_KW(machine_uart_init_obj, 1, machine_uart_init);
STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC void mp_machine_uart_deinit(machine_uart_obj_t *self) {
// Check if it is the active object.
if (MP_STATE_PORT(sercom_table)[self->id] == self) {
Sercom *uart = sercom_instance[self->id];
@@ -426,18 +394,23 @@ STATIC mp_obj_t machine_uart_deinit(mp_obj_t self_in) {
sercom_enable(uart, 0);
}
}
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_deinit_obj, machine_uart_deinit);
STATIC mp_obj_t machine_uart_any(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
return MP_OBJ_NEW_SMALL_INT(ringbuf_avail(&self->read_buffer));
STATIC mp_int_t mp_machine_uart_any(machine_uart_obj_t *self) {
return ringbuf_avail(&self->read_buffer);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_any_obj, machine_uart_any);
STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
STATIC bool mp_machine_uart_txdone(machine_uart_obj_t *self) {
Sercom *uart = sercom_instance[self->id];
return uart->USART.INTFLAG.bit.DRE
#if MICROPY_HW_UART_TXBUF
&& ringbuf_avail(&self->write_buffer) == 0
#endif
&& uart->USART.INTFLAG.bit.TXC;
}
STATIC void mp_machine_uart_sendbreak(machine_uart_obj_t *self) {
uint32_t break_time_us = 13 * 1000000 / self->baudrate;
// Wait for the tx buffer to drain.
@@ -457,43 +430,9 @@ STATIC mp_obj_t machine_uart_sendbreak(mp_obj_t self_in) {
mp_hal_pin_high(self->tx);
// Enable Mux again
mp_hal_set_pin_mux(self->tx, self->tx_pad_config.alt_fct);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_sendbreak_obj, machine_uart_sendbreak);
STATIC mp_obj_t machine_uart_txdone(mp_obj_t self_in) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
Sercom *uart = sercom_instance[self->id];
if (uart->USART.INTFLAG.bit.DRE
#if MICROPY_HW_UART_TXBUF
&& ringbuf_avail(&self->write_buffer) == 0
#endif
&& uart->USART.INTFLAG.bit.TXC) {
return mp_const_true;
} else {
return mp_const_false;
}
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_uart_txdone_obj, machine_uart_txdone);
STATIC const mp_rom_map_elem_t machine_uart_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_uart_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_uart_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR_any), MP_ROM_PTR(&machine_uart_any_obj) },
{ MP_ROM_QSTR(MP_QSTR_sendbreak), MP_ROM_PTR(&machine_uart_sendbreak_obj) },
{ MP_ROM_QSTR(MP_QSTR_txdone), MP_ROM_PTR(&machine_uart_txdone_obj) },
{ MP_ROM_QSTR(MP_QSTR_flush), MP_ROM_PTR(&mp_stream_flush_obj) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_stream_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
};
STATIC MP_DEFINE_CONST_DICT(machine_uart_locals_dict, machine_uart_locals_dict_table);
STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
STATIC mp_uint_t mp_machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
Sercom *uart = sercom_instance[self->id];
uint64_t t = mp_hal_ticks_ms_64() + self->timeout;
@@ -523,7 +462,7 @@ STATIC mp_uint_t machine_uart_read(mp_obj_t self_in, void *buf_in, mp_uint_t siz
return size;
}
STATIC mp_uint_t 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_write(mp_obj_t self_in, const void *buf_in, mp_uint_t size, int *errcode) {
machine_uart_obj_t *self = MP_OBJ_TO_PTR(self_in);
size_t i = 0;
const uint8_t *src = buf_in;
@@ -562,7 +501,7 @@ STATIC mp_uint_t machine_uart_write(mp_obj_t self_in, const void *buf_in, mp_uin
return size;
}
STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint_t arg, int *errcode) {
STATIC mp_uint_t mp_machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
machine_uart_obj_t *self = self_in;
mp_uint_t ret;
Sercom *uart = sercom_instance[self->id];
@@ -588,7 +527,7 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint
#endif
) * 13000 * 2 / self->baudrate;
do {
if (machine_uart_txdone((mp_obj_t)self) == mp_const_true) {
if (mp_machine_uart_txdone(self)) {
return 0;
}
MICROPY_EVENT_POLL_HOOK
@@ -601,21 +540,3 @@ STATIC mp_uint_t machine_uart_ioctl(mp_obj_t self_in, mp_uint_t request, mp_uint
}
return ret;
}
STATIC const mp_stream_p_t uart_stream_p = {
.read = machine_uart_read,
.write = machine_uart_write,
.ioctl = machine_uart_ioctl,
.is_text = false,
};
MP_DEFINE_CONST_OBJ_TYPE(
machine_uart_type,
MP_QSTR_UART,
MP_TYPE_FLAG_ITER_IS_STREAM,
make_new, machine_uart_make_new,
print, machine_uart_print,
protocol, &uart_stream_p,
locals_dict, &machine_uart_locals_dict
);
#endif

View File

@@ -40,9 +40,6 @@ extern const mp_obj_type_t machine_pin_type;
extern const mp_obj_type_t machine_spi_type;
#endif
extern const mp_obj_type_t machine_timer_type;
#if MICROPY_PY_MACHINE_UART
extern const mp_obj_type_t machine_uart_type;
#endif
#if MICROPY_PY_MACHINE_RTC
extern const mp_obj_type_t machine_rtc_type;
#endif

View File

@@ -116,6 +116,8 @@
#ifndef MICROPY_PY_MACHINE_UART
#define MICROPY_PY_MACHINE_UART (1)
#endif
#define MICROPY_PY_MACHINE_UART_INCLUDEFILE "ports/samd/machine_uart.c"
#define MICROPY_PY_MACHINE_UART_SENDBREAK (1)
#define MICROPY_PY_MACHINE_TIMER (1)
#define MICROPY_SOFT_TIMER_TICKS_MS systick_ms
#define MICROPY_PY_OS_DUPTERM (3)

View File

@@ -128,3 +128,28 @@ void samd_init(void) {
machine_rtc_start(false);
#endif
}
#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SPI || MICROPY_PY_MACHINE_UART
Sercom *sercom_instance[] = SERCOM_INSTS;
MP_REGISTER_ROOT_POINTER(void *sercom_table[SERCOM_INST_NUM]);
// Common Sercom functions used by all Serial devices
void sercom_enable(Sercom *uart, int state) {
uart->USART.CTRLA.bit.ENABLE = state; // Set the state on/off
// Wait for the Registers to update.
while (uart->USART.SYNCBUSY.bit.ENABLE) {
}
}
void sercom_deinit_all(void) {
for (int i = 0; i < SERCOM_INST_NUM; i++) {
Sercom *uart = sercom_instance[i];
uart->USART.INTENCLR.reg = 0xff;
sercom_register_irq(i, NULL);
sercom_enable(uart, 0);
MP_STATE_PORT(sercom_table[i]) = NULL;
}
}
#endif

View File

@@ -30,6 +30,8 @@
#include "sam.h"
#include "clock_config.h"
extern Sercom *sercom_instance[];
void samd_init(void);
void samd_main(void);