@@ -418,13 +418,15 @@ STATIC void EXTI_Handler(uint port) {
418418/// - `Pin.LOW_POWER` - 2ma drive strength
419419/// - `Pin.MED_POWER` - 4ma drive strength
420420/// - `Pin.HIGH_POWER` - 6ma drive strength
421+ /// - `alt` selects the alternate function (a number from 0 to 15).
421422///
422423/// Returns: `None`.
423424STATIC const mp_arg_t pin_init_args [] = {
424425 { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT },
425426 { MP_QSTR_pull , MP_ARG_INT , {.u_int = PIN_TYPE_STD } },
426427 { MP_QSTR_value , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
427428 { MP_QSTR_drive , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = PIN_STRENGTH_4MA } },
429+ { MP_QSTR_alt , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
428430};
429431#define pin_INIT_NUM_ARGS MP_ARRAY_SIZE(pin_init_args)
430432
@@ -455,7 +457,10 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
455457 uint strength = args [3 ].u_int ;
456458 pin_validate_drive (strength );
457459
458- int af = (mode == GPIO_DIR_MODE_ALT || mode == GPIO_DIR_MODE_ALT_OD ) ? -1 : PIN_MODE_0 ;
460+ int af = args [4 ].u_int ;
461+ if ((af > 0 && (mode != GPIO_DIR_MODE_ALT || mode != GPIO_DIR_MODE_ALT_OD )) || af > 15 ) {
462+ nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
463+ }
459464
460465 // configure the pin as requested
461466 pin_config (self , af , mode , pull , value , strength );
0 commit comments