@@ -352,18 +352,7 @@ STATIC void pyb_uart_print(const mp_print_t *print, mp_obj_t self_in, mp_print_k
352352    }
353353}
354354
355- STATIC  const  mp_arg_t  pyb_uart_init_args [] =  {
356-     { MP_QSTR_baudrate ,     MP_ARG_REQUIRED  | MP_ARG_INT ,  },
357-     { MP_QSTR_bits ,         MP_ARG_KW_ONLY   | MP_ARG_INT ,  {.u_int  =  8 } },
358-     { MP_QSTR_parity ,       MP_ARG_KW_ONLY   | MP_ARG_OBJ ,  {.u_obj  =  mp_const_none } },
359-     { MP_QSTR_stop ,         MP_ARG_KW_ONLY   | MP_ARG_INT ,  {.u_int  =  1 } },
360-     { MP_QSTR_pins ,         MP_ARG_KW_ONLY   | MP_ARG_OBJ ,  {.u_obj  =  MP_OBJ_NULL } },
361- };
362- STATIC  mp_obj_t  pyb_uart_init_helper (pyb_uart_obj_t  * self , mp_uint_t  n_args , const  mp_obj_t  * pos_args , mp_map_t  * kw_args ) {
363-     // parse args 
364-     mp_arg_val_t  args [MP_ARRAY_SIZE (pyb_uart_init_args )];
365-     mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (pyb_uart_init_args ), pyb_uart_init_args , args );
366- 
355+ STATIC  mp_obj_t  pyb_uart_init_helper (pyb_uart_obj_t  * self , mp_arg_val_t  * args ) {
367356    // get the baudrate 
368357    if  (args [0 ].u_int  <= 0 ) {
369358        goto error ;
@@ -445,12 +434,41 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
445434    nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
446435}
447436
448- STATIC  mp_obj_t  pyb_uart_make_new (mp_obj_t  type_in , mp_uint_t  n_args , mp_uint_t  n_kw , const  mp_obj_t  * args ) {
449-     // check arguments 
450-     mp_arg_check_num (n_args , n_kw , 1 , MP_ARRAY_SIZE (pyb_uart_init_args ), true);
437+ STATIC  const  mp_arg_t  pyb_uart_init_args [] =  {
438+     { MP_QSTR_id ,                             MP_ARG_OBJ ,  {.u_obj  =  mp_const_none } },
439+     { MP_QSTR_baudrate ,                       MP_ARG_INT ,  {.u_int  =  9600 } },
440+     { MP_QSTR_bits ,                           MP_ARG_INT ,  {.u_int  =  8 } },
441+     { MP_QSTR_parity ,                         MP_ARG_OBJ ,  {.u_obj  =  mp_const_none } },
442+     { MP_QSTR_stop ,                           MP_ARG_INT ,  {.u_int  =  1 } },
443+     { MP_QSTR_pins ,         MP_ARG_KW_ONLY   | MP_ARG_OBJ ,  {.u_obj  =  MP_OBJ_NULL } },
444+ };
445+ STATIC  mp_obj_t  pyb_uart_make_new (mp_obj_t  type_in , mp_uint_t  n_args , mp_uint_t  n_kw , const  mp_obj_t  * all_args ) {
446+     // parse args 
447+     mp_map_t  kw_args ;
448+     mp_map_init_fixed_table (& kw_args , n_kw , all_args  +  n_args );
449+     mp_arg_val_t  args [MP_ARRAY_SIZE (pyb_uart_init_args )];
450+     mp_arg_parse_all (n_args , all_args , & kw_args , MP_ARRAY_SIZE (args ), pyb_uart_init_args , args );
451451
452452    // work out the uart id 
453-     int32_t  uart_id  =  mp_obj_get_int (args [0 ]);
453+     uint8_t  uart_id ;
454+     if  (args [0 ].u_obj  ==  mp_const_none ) {
455+         if  (args [5 ].u_obj  !=  MP_OBJ_NULL ) {
456+             mp_obj_t  * pins ;
457+             mp_uint_t  n_pins  =  2 ;
458+             mp_obj_get_array (args [5 ].u_obj , & n_pins , & pins );
459+             // check the Tx pin (or the Rx if Tx is None) 
460+             if  (pins [0 ] ==  mp_const_none ) {
461+                 uart_id  =  pin_find_peripheral_unit (pins [1 ], PIN_FN_UART , PIN_TYPE_UART_RX );
462+             } else  {
463+                 uart_id  =  pin_find_peripheral_unit (pins [0 ], PIN_FN_UART , PIN_TYPE_UART_TX );
464+             }
465+         } else  {
466+             // default id 
467+             uart_id  =  0 ;
468+         }
469+     } else  {
470+         uart_id  =  mp_obj_get_int (args [0 ].u_obj );
471+     }
454472
455473    if  (uart_id  <  PYB_UART_0  ||  uart_id  >  PYB_UART_1 ) {
456474        nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
@@ -461,18 +479,17 @@ STATIC mp_obj_t pyb_uart_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
461479    self -> base .type  =  & pyb_uart_type ;
462480    self -> uart_id  =  uart_id ;
463481
464-     if  (n_args  >  1  ||  n_kw  >  0 ) {
465-         // start the peripheral 
466-         mp_map_t  kw_args ;
467-         mp_map_init_fixed_table (& kw_args , n_kw , args  +  n_args );
468-         pyb_uart_init_helper (self , n_args  -  1 , args  +  1 , & kw_args );
469-     }
482+     // start the peripheral 
483+     pyb_uart_init_helper (self , & args [1 ]);
470484
471485    return  self ;
472486}
473487
474- STATIC  mp_obj_t  pyb_uart_init (mp_uint_t  n_args , const  mp_obj_t  * args , mp_map_t  * kw_args ) {
475-     return  pyb_uart_init_helper (args [0 ], n_args  -  1 , args  +  1 , kw_args );
488+ STATIC  mp_obj_t  pyb_uart_init (mp_uint_t  n_args , const  mp_obj_t  * pos_args , mp_map_t  * kw_args ) {
489+     // parse args 
490+     mp_arg_val_t  args [MP_ARRAY_SIZE (pyb_uart_init_args ) -  1 ];
491+     mp_arg_parse_all (n_args  -  1 , pos_args  +  1 , kw_args , MP_ARRAY_SIZE (args ), & pyb_uart_init_args [1 ], args );
492+     return  pyb_uart_init_helper (pos_args [0 ], args );
476493}
477494STATIC  MP_DEFINE_CONST_FUN_OBJ_KW (pyb_uart_init_obj , 1 , pyb_uart_init );
478495
0 commit comments