@@ -320,50 +320,68 @@ static MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, MP_ROM_PTR(&pin_debug_fun_
320
320
321
321
#endif // MICROPY_PY_MACHINE_PIN_LEGACY
322
322
323
- // init(mode, pull=None, alt =-1, *, value, alt)
323
+ // init(mode, pull=None, af =-1, *, value, alt)
324
324
static mp_obj_t pin_obj_init_helper (const machine_pin_obj_t * self , size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
325
+ enum {
326
+ ARG_mode ,
327
+ ARG_pull ,
328
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
329
+ ARG_af ,
330
+ #endif
331
+ ARG_value ,
332
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
333
+ ARG_alt ,
334
+ #endif
335
+ };
325
336
static const mp_arg_t allowed_args [] = {
326
337
{ MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT },
327
338
{ MP_QSTR_pull , MP_ARG_OBJ , {.u_rom_obj = MP_ROM_NONE }},
339
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
328
340
{ MP_QSTR_af , MP_ARG_INT , {.u_int = -1 }}, // legacy
341
+ #endif
329
342
{ MP_QSTR_value , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL }},
343
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
330
344
{ MP_QSTR_alt , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 }},
345
+ #endif
331
346
};
332
347
333
348
// parse args
334
349
mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
335
350
mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
336
351
337
352
// get io mode
338
- uint mode = args [0 ].u_int ;
353
+ uint mode = args [ARG_mode ].u_int ;
339
354
if (!IS_GPIO_MODE (mode )) {
340
355
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("invalid pin mode: %d" ), mode );
341
356
}
342
357
343
358
// get pull mode
344
359
uint pull = GPIO_NOPULL ;
345
- if (args [1 ].u_obj != mp_const_none ) {
346
- pull = mp_obj_get_int (args [1 ].u_obj );
360
+ if (args [ARG_pull ].u_obj != mp_const_none ) {
361
+ pull = mp_obj_get_int (args [ARG_pull ].u_obj );
347
362
}
348
363
if (!IS_GPIO_PULL (pull )) {
349
364
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("invalid pin pull: %d" ), pull );
350
365
}
351
366
367
+ mp_int_t af = -1 ;
368
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
352
369
// get af (alternate function); alt-arg overrides af-arg
353
- mp_int_t af = args [4 ].u_int ;
370
+ af = args [ARG_alt ].u_int ;
354
371
if (af == -1 ) {
355
- af = args [2 ].u_int ;
372
+ af = args [ARG_af ].u_int ;
356
373
}
357
374
if ((mode == GPIO_MODE_AF_PP || mode == GPIO_MODE_AF_OD ) && !IS_GPIO_AF (af )) {
358
375
mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("invalid pin af: %d" ), af );
359
376
}
377
+ #endif
360
378
361
379
// enable the peripheral clock for the port of this pin
362
380
mp_hal_gpio_clock_enable (self -> gpio );
363
381
364
382
// if given, set the pin value before initialising to prevent glitches
365
- if (args [3 ].u_obj != MP_OBJ_NULL ) {
366
- mp_hal_pin_write (self , mp_obj_is_true (args [3 ].u_obj ));
383
+ if (args [ARG_value ].u_obj != MP_OBJ_NULL ) {
384
+ mp_hal_pin_write (self , mp_obj_is_true (args [ARG_value ].u_obj ));
367
385
}
368
386
369
387
// configure the GPIO as requested
@@ -566,8 +584,10 @@ static const mp_rom_map_elem_t pin_locals_dict_table[] = {
566
584
{ MP_ROM_QSTR (MP_QSTR_IN ), MP_ROM_INT (GPIO_MODE_INPUT ) },
567
585
{ MP_ROM_QSTR (MP_QSTR_OUT ), MP_ROM_INT (GPIO_MODE_OUTPUT_PP ) },
568
586
{ MP_ROM_QSTR (MP_QSTR_OPEN_DRAIN ), MP_ROM_INT (GPIO_MODE_OUTPUT_OD ) },
587
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
569
588
{ MP_ROM_QSTR (MP_QSTR_ALT ), MP_ROM_INT (GPIO_MODE_AF_PP ) },
570
589
{ MP_ROM_QSTR (MP_QSTR_ALT_OPEN_DRAIN ), MP_ROM_INT (GPIO_MODE_AF_OD ) },
590
+ #endif
571
591
{ MP_ROM_QSTR (MP_QSTR_ANALOG ), MP_ROM_INT (GPIO_MODE_ANALOG ) },
572
592
{ MP_ROM_QSTR (MP_QSTR_PULL_UP ), MP_ROM_INT (GPIO_PULLUP ) },
573
593
{ MP_ROM_QSTR (MP_QSTR_PULL_DOWN ), MP_ROM_INT (GPIO_PULLDOWN ) },
@@ -578,12 +598,16 @@ static const mp_rom_map_elem_t pin_locals_dict_table[] = {
578
598
// legacy class constants
579
599
{ MP_ROM_QSTR (MP_QSTR_OUT_PP ), MP_ROM_INT (GPIO_MODE_OUTPUT_PP ) },
580
600
{ MP_ROM_QSTR (MP_QSTR_OUT_OD ), MP_ROM_INT (GPIO_MODE_OUTPUT_OD ) },
601
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
581
602
{ MP_ROM_QSTR (MP_QSTR_AF_PP ), MP_ROM_INT (GPIO_MODE_AF_PP ) },
582
603
{ MP_ROM_QSTR (MP_QSTR_AF_OD ), MP_ROM_INT (GPIO_MODE_AF_OD ) },
604
+ #endif
583
605
{ MP_ROM_QSTR (MP_QSTR_PULL_NONE ), MP_ROM_INT (GPIO_NOPULL ) },
584
606
#endif
585
607
608
+ #if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
586
609
#include "genhdr/pins_af_const.h"
610
+ #endif
587
611
};
588
612
589
613
static MP_DEFINE_CONST_DICT (pin_locals_dict , pin_locals_dict_table ) ;
0 commit comments