Skip to content

Commit a48a18e

Browse files
committed
stm32: Add method for statically configuring pin alternate function
Works with pins declared normally in mpconfigboard.h, eg. (pin_XX) Provides new mp_hal_pin_config_alt_static(pin_obj, mode, pull, fn, type) function declared in pin_static_af.h to allow configuring pin alternate functions by name
1 parent 46091b8 commit a48a18e

File tree

4 files changed

+83
-5
lines changed

4 files changed

+83
-5
lines changed

ports/stm32/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
519519
GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
520520
GEN_PINS_QSTR = $(BUILD)/pins_qstr.h
521521
GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
522+
GEN_PINS_AF_DEFS = $(HEADER_BUILD)/pins_af_defs.h
522523
GEN_PINS_AF_PY = $(BUILD)/pins_af.py
523524

524525
INSERT_USB_IDS = $(TOP)/tools/insert-usb-ids.py
@@ -551,9 +552,9 @@ main.c: $(GEN_CDCINF_HEADER)
551552

552553
# Use a pattern rule here so that make will only call make-pins.py once to make
553554
# both pins_$(BOARD).c and pins.h
554-
$(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)
555+
$(BUILD)/%_$(BOARD).c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(HEADER_BUILD)/%_af_defs.h $(BUILD)/%_qstr.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE) | $(HEADER_BUILD)
555556
$(ECHO) "GEN $@"
556-
$(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)
557+
$(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-defs $(GEN_PINS_AF_DEFS) --af-py $(GEN_PINS_AF_PY) > $(GEN_PINS_SRC)
557558

558559
$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c
559560
$(call compile_c)

ports/stm32/boards/make-pins.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import csv
99

10+
# Must have matching entries in AF_FN_* enum in ..\pin_defs_stm32.h
1011
SUPPORTED_FN = {
1112
'TIM' : ['CH1', 'CH2', 'CH3', 'CH4',
1213
'CH1N', 'CH2N', 'CH3N', 'CH1_ETR', 'ETR', 'BKIN'],
@@ -291,7 +292,7 @@ def print_named(self, label, named_pins):
291292
if pin.is_board_pin():
292293
print(' {{ MP_ROM_QSTR(MP_QSTR_{:s}), MP_ROM_PTR(&pin_{:s}_obj) }},'.format(named_pin.name(), pin.cpu_pin_name()))
293294
print('};')
294-
print('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);'.format(label, label));
295+
print('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);'.format(label, label))
295296

296297
def print(self):
297298
for named_pin in self.cpu_pins:
@@ -303,7 +304,7 @@ def print(self):
303304
self.print_named('board', self.board_pins)
304305

305306
def print_adc(self, adc_num):
306-
print('');
307+
print('')
307308
print('const pin_obj_t * const pin_adc{:d}[] = {{'.format(adc_num))
308309
for channel in range(17):
309310
if channel == 16:
@@ -378,9 +379,31 @@ def print_af_hdr(self, af_const_filename):
378379
file=af_const_file)
379380
print_conditional_endif(cond_var, file=af_const_file)
380381

382+
def print_af_defs(self, af_defs_filename):
383+
with open(af_defs_filename, 'wt') as af_defs_file:
384+
385+
STATIC_AF_TOKENS = {}
386+
for named_pin in self.board_pins:
387+
for af in named_pin.pin().alt_fn:
388+
func = "%s%d" % (af.func, af.fn_num) if af.fn_num else af.func
389+
pin_type = (af.pin_type or "NULL").split('(')[0]
390+
tok = "#define STATIC_AF_%s_%s(pin_obj) ( \\" % (func, pin_type)
391+
if tok not in STATIC_AF_TOKENS:
392+
STATIC_AF_TOKENS[tok] = []
393+
STATIC_AF_TOKENS[tok].append(
394+
' ((strcmp( #pin_obj , "(&pin_%s_obj)") & strcmp( #pin_obj , "((&pin_%s_obj))")) == 0) ? (%d) : \\' % (
395+
named_pin.pin().cpu_pin_name(), named_pin.pin().cpu_pin_name(), af.idx
396+
)
397+
)
398+
399+
for tok, pins in STATIC_AF_TOKENS.items():
400+
print(tok, file=af_defs_file)
401+
print("\n".join(sorted(pins)), file=af_defs_file)
402+
print(" (0xffffffffffffffffULL))\n", file=af_defs_file)
403+
381404
def print_af_py(self, af_py_filename):
382405
with open(af_py_filename, 'wt') as af_py_file:
383-
print('PINS_AF = (', file=af_py_file);
406+
print('PINS_AF = (', file=af_py_file)
384407
for named_pin in self.board_pins:
385408
print(" ('%s', " % named_pin.name(), end='', file=af_py_file)
386409
for af in named_pin.pin().alt_fn:
@@ -414,6 +437,12 @@ def main():
414437
help="Specifies the filename for the python alternate function mappings.",
415438
default="build/pins_af.py"
416439
)
440+
parser.add_argument(
441+
"--af-defs",
442+
dest="af_defs_filename",
443+
help="Specifies the filename for the alternate function defines.",
444+
default="build/pins_af_defs.h"
445+
)
417446
parser.add_argument(
418447
"-b", "--board",
419448
dest="board_filename",
@@ -464,6 +493,7 @@ def main():
464493
pins.print_qstr(args.qstr_filename)
465494
pins.print_af_hdr(args.af_const_filename)
466495
pins.print_af_py(args.af_py_filename)
496+
pins.print_af_defs(args.af_defs_filename)
467497

468498

469499
if __name__ == "__main__":

ports/stm32/pin_defs_stm32.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ enum {
4141
PORT_K,
4242
};
4343

44+
// Must have matching entries in SUPPORTED_FN in boards\make-pins.py
4445
enum {
4546
AF_FN_TIM,
4647
AF_FN_I2C,

ports/stm32/pin_static_af.h

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013, 2014 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_STM32_PIN_STATIC_AF_H
27+
#define MICROPY_INCLUDED_STM32_PIN_STATIC_AF_H
28+
29+
#include "py/mphal.h"
30+
#include "genhdr/pins.h"
31+
#include "genhdr/pins_af_defs.h"
32+
33+
#if 0 // Enable to test if AF's are statically compiled
34+
#define mp_hal_pin_config_alt_static(pin_obj, mode, pull, fn_type) \
35+
mp_hal_pin_config(pin_obj, mode, pull, fn_type(pin_obj)); \
36+
_Static_assert(fn_type(pin_obj) != -1, ""); \
37+
_Static_assert(__builtin_constant_p(fn_type(pin_obj)) == 1, "")
38+
39+
#else
40+
41+
#define mp_hal_pin_config_alt_static(pin_obj, mode, pull, fn_type) \
42+
mp_hal_pin_config(pin_obj, mode, pull, fn_type(pin_obj)) /* Overflow Error => alt func not found */
43+
44+
#endif
45+
46+
#endif // MICROPY_INCLUDED_STM32_PIN_STATIC_AF_H

0 commit comments

Comments
 (0)