Skip to content

Commit 3d94555

Browse files
dhylandsdpgeorge
authored andcommitted
Added python script to map AF to a pin name
Added some functions to Pin class to query mode, pull, and af
1 parent c668d51 commit 3d94555

3 files changed

Lines changed: 50 additions & 1 deletion

File tree

stmhal/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
238238
GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
239239
GEN_PINS_QSTR = $(BUILD)/pins_qstr.h
240240
GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
241+
GEN_PINS_AF_PY = $(BUILD)/pins_af.py
241242

242243
INSERT_USB_IDS = ../tools/insert-usb-ids.py
243244
FILE2H = ../tools/file2h.py
@@ -260,7 +261,7 @@ $(BUILD)/main.o: $(GEN_CDCINF_HEADER)
260261
# both pins_$(BOARD).c and pins.h
261262
$(BUILD)/%_$(BOARD).c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qstr.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD)
262263
$(ECHO) "Create $@"
263-
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) --af-const $(GEN_PINS_AF_CONST) > $(GEN_PINS_SRC)
264+
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) --qstr $(GEN_PINS_QSTR) --af-const $(GEN_PINS_AF_CONST) --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC)
264265

265266
$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c
266267
$(call compile_c)

stmhal/boards/make-pins.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,17 @@ def print_af_hdr(self, af_const_filename):
315315
print(' { %-*s %s },' % (mux_name_width + 26, key, val),
316316
file=af_const_file)
317317

318+
def print_af_py(self, af_py_filename):
319+
with open(af_py_filename, 'wt') as af_py_file:
320+
print('PINS_AF = (', file=af_py_file);
321+
for named_pin in self.board_pins:
322+
print(" ('%s', " % named_pin.name(), end='', file=af_py_file)
323+
for af in named_pin.pin().alt_fn:
324+
if af.is_supported():
325+
print("(%d, '%s'), " % (af.idx, af.af_str), end='', file=af_py_file)
326+
print('),', file=af_py_file)
327+
print(')', file=af_py_file)
328+
318329

319330
def main():
320331
parser = argparse.ArgumentParser(
@@ -334,6 +345,12 @@ def main():
334345
help="Specifies header file for alternate function constants.",
335346
default="build/pins_af_const.h"
336347
)
348+
parser.add_argument(
349+
"--af-py",
350+
dest="af_py_filename",
351+
help="Specifies the filename for the python alternate function mappings.",
352+
default="build/pins_af.py"
353+
)
337354
parser.add_argument(
338355
"-b", "--board",
339356
dest="board_filename",
@@ -383,6 +400,7 @@ def main():
383400
pins.print_header(args.hdr_filename)
384401
pins.print_qstr(args.qstr_filename)
385402
pins.print_af_hdr(args.af_const_filename)
403+
pins.print_af_py(args.af_py_filename)
386404

387405

388406
if __name__ == "__main__":

stmhal/pin.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,33 @@ STATIC mp_obj_t pin_gpio(mp_obj_t self_in) {
508508
}
509509
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_gpio_obj, pin_gpio);
510510

511+
/// \method mode()
512+
/// Returns the currently configured mode of the pin. The integer returned
513+
/// will match one of the allowed constants for the mode argument to the init
514+
/// function.
515+
STATIC mp_obj_t pin_mode(mp_obj_t self_in) {
516+
return MP_OBJ_NEW_SMALL_INT(pin_get_mode(self_in));
517+
}
518+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_mode_obj, pin_mode);
519+
520+
/// \method pull()
521+
/// Returns the currently configured pull of the pin. The integer returned
522+
/// will match one of the allowed constants for the pull argument to the init
523+
/// function.
524+
STATIC mp_obj_t pin_pull(mp_obj_t self_in) {
525+
return MP_OBJ_NEW_SMALL_INT(pin_get_pull(self_in));
526+
}
527+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_pull_obj, pin_pull);
528+
529+
/// \method af()
530+
/// Returns the currently configured af of the pin. The integer returned
531+
/// will match one of the allowed constants for the af argument to the init
532+
/// function.
533+
STATIC mp_obj_t pin_af(mp_obj_t self_in) {
534+
return MP_OBJ_NEW_SMALL_INT(pin_get_af(self_in));
535+
}
536+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pin_af_obj, pin_af);
537+
511538
STATIC const mp_map_elem_t pin_locals_dict_table[] = {
512539
// instance methods
513540
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pin_init_obj },
@@ -520,6 +547,9 @@ STATIC const mp_map_elem_t pin_locals_dict_table[] = {
520547
{ MP_OBJ_NEW_QSTR(MP_QSTR_port), (mp_obj_t)&pin_port_obj },
521548
{ MP_OBJ_NEW_QSTR(MP_QSTR_pin), (mp_obj_t)&pin_pin_obj },
522549
{ MP_OBJ_NEW_QSTR(MP_QSTR_gpio), (mp_obj_t)&pin_gpio_obj },
550+
{ MP_OBJ_NEW_QSTR(MP_QSTR_mode), (mp_obj_t)&pin_mode_obj },
551+
{ MP_OBJ_NEW_QSTR(MP_QSTR_pull), (mp_obj_t)&pin_pull_obj },
552+
{ MP_OBJ_NEW_QSTR(MP_QSTR_af), (mp_obj_t)&pin_af_obj },
523553

524554
// class methods
525555
{ MP_OBJ_NEW_QSTR(MP_QSTR_mapper), (mp_obj_t)&pin_mapper_obj },

0 commit comments

Comments
 (0)