6161///
6262/// Usage Model:
6363///
64- /// All CPU Pins are predefined as pyb.Pin.cpu.Name
65- ///
66- /// GPIO9_pin = pyb.Pin.cpu.GPIO9
67- ///
68- /// g = pyb.Pin(pyb.Pin.cpu.GPIO9, 0, pyb.Pin.IN)
69- ///
70- /// CPU pins which correspond to the board pins are available
71- /// as `pyb.cpu.Name`.
72- ///
73- /// You can also use strings:
74- ///
75- /// g = pyb.Pin('GPIO9', 0)
76- ///
77- /// And finally, you can also pass a pin number directly:
78- ///
79- /// g = pyb.Pin(64, 0)
80- ///
81- /// To summarise, the following order determines how things get mapped into
82- /// an ordinal pin number:
83- ///
84- /// 1. Directly specify a Pin object
85- /// 2. Supply a string which matches a CPU pin name
86- /// 3. Provide a pin number
64+ /// g = pyb.Pin('GPIO9', af=0, mode=pyb.Pin.IN, type=pyb.Pin.STD, strength=pyb.Pin.S2MA)
8765///
8866/// \Interrupts:
8967//// You can also configure the Pin to generate interrupts
9068///
9169/// Example callback:
9270///
9371/// def pincb(pin):
94- /// print(pin.pin() )
72+ /// print(pin.get_config().name )
9573///
9674/// extint = pyb.Pin('GPIO10', 0, pyb.Pin.INT_RISING, pyb.GPIO.STD_PD, pyb.S2MA)
9775/// extint.callback (intmode=pyb.Pin.INT_RISING, handler=pincb)
@@ -166,28 +144,18 @@ void pin_init0(void) {
166144pin_obj_t * pin_find (mp_obj_t user_obj ) {
167145 pin_obj_t * pin_obj ;
168146
169- // If a pin was provided, then use it
147+ // if a pin was provided, use it
170148 if (MP_OBJ_IS_TYPE (user_obj , & pin_type )) {
171149 pin_obj = user_obj ;
172150 return pin_obj ;
173151 }
174152
175- // See if the pin name matches a cpu pin
153+ // otherwise see if the pin name matches a cpu pin
176154 pin_obj = pin_find_named_pin (& pin_cpu_pins_locals_dict , user_obj );
177155 if (pin_obj ) {
178156 return pin_obj ;
179157 }
180158
181- // See if the pin number matches a cpu pin
182- mp_int_t pin_num ;
183- if (mp_obj_get_int_maybe (user_obj , & pin_num )) {
184- // The Pins dictionary has pin indexes, so we must substract one from the value passed
185- pin_obj = pin_find_pin (& pin_cpu_pins_locals_dict , (pin_num - 1 ));
186- if (pin_obj ) {
187- return pin_obj ;
188- }
189- }
190-
191159 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
192160}
193161
@@ -387,7 +355,7 @@ STATIC const mp_arg_t pin_init_args[] = {
387355 { MP_QSTR_af , MP_ARG_REQUIRED | MP_ARG_INT },
388356 { MP_QSTR_mode , MP_ARG_INT , {.u_int = GPIO_DIR_MODE_OUT } },
389357 { MP_QSTR_type , MP_ARG_INT , {.u_int = PIN_TYPE_STD } },
390- { MP_QSTR_str , MP_ARG_INT , {.u_int = PIN_STRENGTH_4MA } },
358+ { MP_QSTR_strength , MP_ARG_INT , {.u_int = PIN_STRENGTH_4MA } },
391359};
392360#define pin_INIT_NUM_ARGS MP_ARRAY_SIZE(pin_init_args)
393361
@@ -431,17 +399,18 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
431399/// Return a string describing the pin object.
432400STATIC void pin_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
433401 pin_obj_t * self = self_in ;
434- uint32_t af = MAP_PinModeGet ( self -> pin_num ) ;
435- uint32_t type = pin_get_type ( self ) ;
436- uint32_t strength = pin_get_strenght ( self ) ;
402+ uint32_t af = self -> af ;
403+ uint32_t type = self -> type ;
404+ uint32_t strength = self -> strength ;
437405
438406 // pin name
439407 mp_printf (print , "<Pin.cpu.%q, af=%u" , self -> name , af );
440408
409+ // pin mode
441410 if (af == PIN_MODE_0 ) {
442411 // IO mode
443412 qstr mode_qst ;
444- uint32_t mode = pin_get_mode ( self ) ;
413+ uint32_t mode = self -> mode ;
445414 if (mode == GPIO_DIR_MODE_IN ) {
446415 mode_qst = MP_QSTR_IN ;
447416 } else {
@@ -465,9 +434,9 @@ STATIC void pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t
465434 } else {
466435 type_qst = MP_QSTR_OD_PD ;
467436 }
468- mp_printf (print , ", pull =Pin.%q" , type_qst );
437+ mp_printf (print , ", type =Pin.%q" , type_qst );
469438
470- // Strength
439+ // pin strength
471440 qstr str_qst ;
472441 if (strength == PIN_STRENGTH_2MA ) {
473442 str_qst = MP_QSTR_S2MA ;
@@ -554,65 +523,25 @@ STATIC mp_obj_t pin_toggle(mp_obj_t self_in) {
554523}
555524STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_toggle_obj , pin_toggle );
556525
557- /// \method name()
558- /// Get the pin name.
559- STATIC mp_obj_t pin_name (mp_obj_t self_in ) {
560- pin_obj_t * self = self_in ;
561- return MP_OBJ_NEW_QSTR (self -> name );
562- }
563- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_name_obj , pin_name );
564-
565- /// \method port()
566- /// Get the pin port.
567- STATIC mp_obj_t pin_port (mp_obj_t self_in ) {
568- pin_obj_t * self = self_in ;
569- return mp_obj_new_int (self -> port );
570- }
571- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_port_obj , pin_port );
572-
573- /// \method pin()
574- /// Get the pin number.
575- STATIC mp_obj_t pin_pin (mp_obj_t self_in ) {
576- pin_obj_t * self = self_in ;
577- return MP_OBJ_NEW_SMALL_INT (self -> pin_num );
578- }
579- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_pin_obj , pin_pin );
580-
581- /// \method mode()
582- /// Returns the currently configured mode of the gpio pin. The integer returned
583- /// will match one of the allowed constants for the mode argument to the init
584- /// function.
585- STATIC mp_obj_t pin_mode (mp_obj_t self_in ) {
586- return MP_OBJ_NEW_SMALL_INT (pin_get_mode (self_in ));
587- }
588- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_mode_obj , pin_mode );
589-
590- /// \method type()
591- /// Returns the currently configured type of the pin. The integer returned
592- /// will match one of the allowed constants for the type argument to the init
593- /// function.
594- STATIC mp_obj_t pin_type_get (mp_obj_t self_in ) {
595- return MP_OBJ_NEW_SMALL_INT (pin_get_type (self_in ));
596- }
597- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_type_obj , pin_type_get );
598-
599- /// \method strength()
600- /// Returns the currently configured drive strength of the pin. The integer returned
601- /// will match one of the allowed constants for the strength argument to the init
602- /// function.
603- STATIC mp_obj_t pin_strength (mp_obj_t self_in ) {
604- return MP_OBJ_NEW_SMALL_INT (pin_get_strenght (self_in ));
605- }
606- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_strenght_obj , pin_strength );
526+ /// \method get_config()
527+ /// Returns a named tupple with the current configuration of the gpio pin
528+ STATIC mp_obj_t pin_get_config (mp_obj_t self_in ) {
529+ STATIC const qstr pin_config_fields [] = {
530+ MP_QSTR_name , MP_QSTR_af , MP_QSTR_mode ,
531+ MP_QSTR_type , MP_QSTR_strength
532+ };
607533
608- /// \method af()
609- /// Returns the currently configured alternate function of the gpio pin. The integer returned
610- /// will match one of the allowed constants for the af argument to the init function.
611- STATIC mp_obj_t pin_af (mp_obj_t self_in ) {
612534 pin_obj_t * self = self_in ;
613- return MP_OBJ_NEW_SMALL_INT (MAP_PinModeGet (self -> pin_num ));
535+ mp_obj_t pin_config [5 ];
536+ pin_config [0 ] = MP_OBJ_NEW_QSTR (self -> name );
537+ pin_config [1 ] = mp_obj_new_int (self -> af );
538+ pin_config [2 ] = mp_obj_new_int (self -> mode );
539+ pin_config [3 ] = mp_obj_new_int (self -> type );
540+ pin_config [4 ] = mp_obj_new_int (self -> strength );
541+
542+ return mp_obj_new_attrtuple (pin_config_fields , 5 , pin_config );
614543}
615- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_af_obj , pin_af );
544+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pin_get_config_obj , pin_get_config );
616545
617546/// \method callback(method, intmode, priority, pwrmode)
618547/// Creates a callback object associated to a pin
@@ -750,18 +679,9 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
750679 { MP_OBJ_NEW_QSTR (MP_QSTR_low ), (mp_obj_t )& pin_low_obj },
751680 { MP_OBJ_NEW_QSTR (MP_QSTR_high ), (mp_obj_t )& pin_high_obj },
752681 { MP_OBJ_NEW_QSTR (MP_QSTR_toggle ), (mp_obj_t )& pin_toggle_obj },
753- { MP_OBJ_NEW_QSTR (MP_QSTR_name ), (mp_obj_t )& pin_name_obj },
754- { MP_OBJ_NEW_QSTR (MP_QSTR_port ), (mp_obj_t )& pin_port_obj },
755- { MP_OBJ_NEW_QSTR (MP_QSTR_pin ), (mp_obj_t )& pin_pin_obj },
756- { MP_OBJ_NEW_QSTR (MP_QSTR_mode ), (mp_obj_t )& pin_mode_obj },
757- { MP_OBJ_NEW_QSTR (MP_QSTR_type ), (mp_obj_t )& pin_type_obj },
758- { MP_OBJ_NEW_QSTR (MP_QSTR_strength ), (mp_obj_t )& pin_strenght_obj },
759- { MP_OBJ_NEW_QSTR (MP_QSTR_af ), (mp_obj_t )& pin_af_obj },
682+ { MP_OBJ_NEW_QSTR (MP_QSTR_get_config ), (mp_obj_t )& pin_get_config_obj },
760683 { MP_OBJ_NEW_QSTR (MP_QSTR_callback ), (mp_obj_t )& pin_callback_obj },
761684
762- // class attributes
763- { MP_OBJ_NEW_QSTR (MP_QSTR_cpu ), (mp_obj_t )& pin_cpu_pins_obj_type },
764-
765685 // class constants
766686 /// \constant IN - set the pin to input mode
767687 /// \constant OUT - set the pin to output mode
0 commit comments