32
32
#include "py/ringbuf.h"
33
33
#include "samd_soc.h"
34
34
#include "pin_af.h"
35
+ #include "genhdr/pins.h"
35
36
#include "shared/runtime/softtimer.h"
36
37
37
38
#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_
298
299
}
299
300
300
301
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"
302
303
#if MICROPY_HW_UART_TXBUF
303
304
", txbuf=%d"
304
305
#endif
@@ -310,7 +311,8 @@ static void mp_machine_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_
310
311
#endif
311
312
")" ,
312
313
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
314
316
#if MICROPY_HW_UART_TXBUF
315
317
, txbuf_len
316
318
#endif
@@ -448,7 +450,7 @@ static void mp_machine_uart_init_helper(machine_uart_obj_t *self, size_t n_args,
448
450
449
451
// Check the rx/tx pin assignments
450
452
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" ));
452
454
}
453
455
self -> rx_pad_config = get_sercom_config (self -> rx , self -> id );
454
456
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,
479
481
}
480
482
481
483
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);
483
485
484
486
// 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
+ }
486
494
if (uart_id < 0 || uart_id > SERCOM_INST_NUM ) {
487
495
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("UART(%d) doesn't exist" ), uart_id );
488
496
}
@@ -495,8 +503,15 @@ static mp_obj_t mp_machine_uart_make_new(const mp_obj_type_t *type, size_t n_arg
495
503
self -> stop = 0 ;
496
504
self -> timeout = 1 ;
497
505
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
498
512
self -> tx = 0xff ;
499
513
self -> rx = 0xff ;
514
+ #endif
500
515
#if MICROPY_HW_UART_RTSCTS
501
516
self -> rts = 0xff ;
502
517
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
510
525
511
526
mp_map_t kw_args ;
512
527
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 );
514
529
515
530
return MP_OBJ_FROM_PTR (self );
516
531
}
0 commit comments