Skip to content

Commit daef1c1

Browse files
robert-hhdpgeorge
authored andcommitted
samd/machine_uart: Support default instance and TX/RX pin values.
If a board configures a default UART instance and/or TX/RX pins then the user can create a default UART object using `machine.UART()`. Signed-off-by: robert-hh <[email protected]>
1 parent 62ed69b commit daef1c1

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

ports/samd/machine_uart.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "py/ringbuf.h"
3333
#include "samd_soc.h"
3434
#include "pin_af.h"
35+
#include "genhdr/pins.h"
3536
#include "shared/runtime/softtimer.h"
3637

3738
#define DEFAULT_UART_BAUDRATE (115200)
@@ -298,7 +299,7 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
298299
}
299300

300301
mp_printf(print, "UART(%u, baudrate=%u, bits=%u, parity=%s, stop=%u, "
301-
"timeout=%u, timeout_char=%u, rxbuf=%d"
302+
"tx=\"%q\", rx=\"%q\", timeout=%u, timeout_char=%u, rxbuf=%d"
302303
#if MICROPY_HW_UART_TXBUF
303304
", txbuf=%d"
304305
#endif
@@ -310,7 +311,8 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
310311
#endif
311312
")",
312313
self->id, self->baudrate, self->bits, _parity_name[self->parity],
313-
self->stop + 1, self->timeout, self->timeout_char, rxbuf_len
314+
self->stop + 1, pin_find_by_id(self->tx)->name, pin_find_by_id(self->rx)->name,
315+
self->timeout, self->timeout_char, rxbuf_len
314316
#if MICROPY_HW_UART_TXBUF
315317
, txbuf_len
316318
#endif
@@ -448,7 +450,7 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
448450

449451
// Check the rx/tx pin assignments
450452
if (self->tx == 0xff || self->rx == 0xff || (self->tx / 4) != (self->rx / 4)) {
451-
mp_raise_ValueError(MP_ERROR_TEXT("Non-matching or missing rx/tx"));
453+
mp_raise_ValueError(MP_ERROR_TEXT("invalid or missing rx/tx"));
452454
}
453455
self->rx_pad_config = get_sercom_config(self->rx, self->id);
454456
self->tx_pad_config = get_sercom_config(self->tx, self->id);
@@ -479,10 +481,16 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
479481
}
480482

481483
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) {
482-
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
484+
mp_arg_check_num(n_args, n_kw, MICROPY_HW_DEFAULT_UART_ID < 0 ? 1 : 0, MP_OBJ_FUN_ARGS_MAX, true);
483485

484486
// Get UART bus.
485-
int uart_id = mp_obj_get_int(args[0]);
487+
int uart_id = MICROPY_HW_DEFAULT_UART_ID;
488+
489+
if (n_args > 0) {
490+
uart_id = mp_obj_get_int(args[0]);
491+
n_args--;
492+
args++;
493+
}
486494
if (uart_id < 0 || uart_id > SERCOM_INST_NUM) {
487495
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("UART(%d) doesn't exist"), uart_id);
488496
}
@@ -495,8 +503,15 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
495503
self->stop = 0;
496504
self->timeout = 1;
497505
self->timeout_char = 1;
506+
#if defined(pin_TX) && defined(pin_RX)
507+
// Initialize with the default pins
508+
self->tx = mp_hal_get_pin_obj((mp_obj_t)pin_TX);
509+
self->rx = mp_hal_get_pin_obj((mp_obj_t)pin_RX);
510+
#else
511+
// Have to be defined
498512
self->tx = 0xff;
499513
self->rx = 0xff;
514+
#endif
500515
#if MICROPY_HW_UART_RTSCTS
501516
self->rts = 0xff;
502517
self->cts = 0xff;
@@ -510,7 +525,7 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
510525

511526
mp_map_t kw_args;
512527
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
513-
mp_machine_uart_init_helper(self, n_args - 1, args + 1, &kw_args);
528+
mp_machine_uart_init_helper(self, n_args, args, &kw_args);
514529

515530
return MP_OBJ_FROM_PTR(self);
516531
}

0 commit comments

Comments
 (0)