Skip to content

Commit 1c2cdf9

Browse files
committed
stm32/pin: Add config option to exclude Pin alternate function.
Signed-off-by: Damien George <[email protected]>
1 parent bd6edf0 commit 1c2cdf9

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Diff for: ports/stm32/mpconfigboard_common.h

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
#define MICROPY_PY_MACHINE_PIN_LEGACY (!MICROPY_PREVIEW_VERSION_2)
5858
#endif
5959

60+
// Whether to include support for alternate function selection in machine.Pin (and pyb.Pin).
61+
#ifndef MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
62+
#define MICROPY_PY_MACHINE_PIN_ALT_SUPPORT (1)
63+
#endif
64+
6065
// Whether machine.bootloader() will enter the bootloader via reset, or direct jump.
6166
#ifndef MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET
6267
#define MICROPY_HW_ENTER_BOOTLOADER_VIA_RESET (1)

Diff for: ports/stm32/pin.c

+32-8
Original file line numberDiff line numberDiff line change
@@ -320,50 +320,68 @@ static MP_DEFINE_CONST_CLASSMETHOD_OBJ(pin_debug_obj, MP_ROM_PTR(&pin_debug_fun_
320320

321321
#endif // MICROPY_PY_MACHINE_PIN_LEGACY
322322

323-
// init(mode, pull=None, alt=-1, *, value, alt)
323+
// init(mode, pull=None, af=-1, *, value, alt)
324324
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+
};
325336
static const mp_arg_t allowed_args[] = {
326337
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
327338
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
339+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
328340
{ MP_QSTR_af, MP_ARG_INT, {.u_int = -1}}, // legacy
341+
#endif
329342
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
343+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
330344
{ MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1}},
345+
#endif
331346
};
332347

333348
// parse args
334349
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
335350
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
336351

337352
// get io mode
338-
uint mode = args[0].u_int;
353+
uint mode = args[ARG_mode].u_int;
339354
if (!IS_GPIO_MODE(mode)) {
340355
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin mode: %d"), mode);
341356
}
342357

343358
// get pull mode
344359
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);
347362
}
348363
if (!IS_GPIO_PULL(pull)) {
349364
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin pull: %d"), pull);
350365
}
351366

367+
mp_int_t af = -1;
368+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
352369
// 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;
354371
if (af == -1) {
355-
af = args[2].u_int;
372+
af = args[ARG_af].u_int;
356373
}
357374
if ((mode == GPIO_MODE_AF_PP || mode == GPIO_MODE_AF_OD) && !IS_GPIO_AF(af)) {
358375
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("invalid pin af: %d"), af);
359376
}
377+
#endif
360378

361379
// enable the peripheral clock for the port of this pin
362380
mp_hal_gpio_clock_enable(self->gpio);
363381

364382
// 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));
367385
}
368386

369387
// configure the GPIO as requested
@@ -566,8 +584,10 @@ static const mp_rom_map_elem_t pin_locals_dict_table[] = {
566584
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_INPUT) },
567585
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) },
568586
{ MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
587+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
569588
{ MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_AF_PP) },
570589
{ MP_ROM_QSTR(MP_QSTR_ALT_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_AF_OD) },
590+
#endif
571591
{ MP_ROM_QSTR(MP_QSTR_ANALOG), MP_ROM_INT(GPIO_MODE_ANALOG) },
572592
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULLUP) },
573593
{ 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[] = {
578598
// legacy class constants
579599
{ MP_ROM_QSTR(MP_QSTR_OUT_PP), MP_ROM_INT(GPIO_MODE_OUTPUT_PP) },
580600
{ MP_ROM_QSTR(MP_QSTR_OUT_OD), MP_ROM_INT(GPIO_MODE_OUTPUT_OD) },
601+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
581602
{ MP_ROM_QSTR(MP_QSTR_AF_PP), MP_ROM_INT(GPIO_MODE_AF_PP) },
582603
{ MP_ROM_QSTR(MP_QSTR_AF_OD), MP_ROM_INT(GPIO_MODE_AF_OD) },
604+
#endif
583605
{ MP_ROM_QSTR(MP_QSTR_PULL_NONE), MP_ROM_INT(GPIO_NOPULL) },
584606
#endif
585607

608+
#if MICROPY_PY_MACHINE_PIN_ALT_SUPPORT
586609
#include "genhdr/pins_af_const.h"
610+
#endif
587611
};
588612

589613
static MP_DEFINE_CONST_DICT(pin_locals_dict, pin_locals_dict_table);

0 commit comments

Comments
 (0)