Skip to content

Commit 4162271

Browse files
committed
Merge branch 'dhylands-pin-af'
2 parents a1d3ee3 + b92e753 commit 4162271

24 files changed

Lines changed: 563 additions & 100 deletions

stmhal/Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ BUILD ?= build-$(BOARD)
1111
include ../py/mkenv.mk
1212

1313
# qstr definitions (must come before including py.mk)
14-
QSTR_DEFS = qstrdefsport.h
14+
QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h
1515

1616
# include py core make definitions
1717
include ../py/py.mk
@@ -74,6 +74,7 @@ SRC_C = \
7474
timer.c \
7575
led.c \
7676
pin.c \
77+
pin_defs_stmhal.c \
7778
pin_named_pins.c \
7879
bufhelper.c \
7980
i2c.c \
@@ -223,10 +224,12 @@ $(BUILD)/firmware.elf: $(OBJ)
223224

224225
MAKE_PINS = boards/make-pins.py
225226
BOARD_PINS = boards/$(BOARD)/pins.csv
226-
AF_FILE = boards/stm32f4xx-af.csv
227-
PREFIX_FILE = boards/stm32f4xx-prefix.c
227+
AF_FILE = boards/stm32f4xx_af.csv
228+
PREFIX_FILE = boards/stm32f4xx_prefix.c
228229
GEN_PINS_SRC = $(BUILD)/pins_$(BOARD).c
229230
GEN_PINS_HDR = $(HEADER_BUILD)/pins.h
231+
GEN_PINS_QSTR = $(BUILD)/pins_qstr.h
232+
GEN_PINS_AF_CONST = $(HEADER_BUILD)/pins_af_const.h
230233

231234
INSERT_USB_IDS = ../tools/insert-usb-ids.py
232235
FILE2H = ../tools/file2h.py
@@ -247,9 +250,9 @@ $(BUILD)/main.o: $(GEN_CDCINF_HEADER)
247250

248251
# Use a pattern rule here so that make will only call make-pins.py once to make
249252
# both pins_$(BOARD).c and pins.h
250-
$(BUILD)/%_$(BOARD).c $(HEADER_BUILD)/%.h: boards/$(BOARD)/%.csv $(MAKE_PINS) $(AF_FILE) $(PREFIX_FILE)
253+
$(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)
251254
$(ECHO) "Create $@"
252-
$(Q)$(PYTHON) $(MAKE_PINS) --board $(BOARD_PINS) --af $(AF_FILE) --prefix $(PREFIX_FILE) --hdr $(GEN_PINS_HDR) > $(GEN_PINS_SRC)
255+
$(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)
253256

254257
$(BUILD)/pins_$(BOARD).o: $(BUILD)/pins_$(BOARD).c
255258
$(call compile_c)

stmhal/boards/make-pins.py

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ def ptr(self):
7272
return self.func
7373
return '{:s}{:d}'.format(self.func, self.fn_num)
7474

75+
def mux_name(self):
76+
return 'AF{:d}_{:s}'.format(self.idx, self.ptr())
77+
7578
def print(self):
7679
"""Prints the C representation of this AF."""
7780
if self.supported:
@@ -84,6 +87,9 @@ def print(self):
8487
print('({:2d}, {:8s}, {:2d}, {:10s}, {:8s}), // {:s}'.format(self.idx,
8588
self.func, fn_num, self.pin_type, self.ptr(), self.af_str))
8689

90+
def qstr_list(self):
91+
return [self.mux_name()]
92+
8793

8894
class Pin(object):
8995
"""Holds the information associated with a pin."""
@@ -170,6 +176,14 @@ def print_header(self, hdr_file):
170176
hdr_file.write('extern const pin_af_obj_t pin_{:s}_af[];\n'.
171177
format(self.cpu_pin_name()))
172178

179+
def qstr_list(self):
180+
result = []
181+
for alt_fn in self.alt_fn:
182+
if alt_fn.is_supported():
183+
result += alt_fn.qstr_list()
184+
return result
185+
186+
173187
class NamedPin(object):
174188

175189
def __init__(self, name, pin):
@@ -225,13 +239,13 @@ def parse_board_file(self, filename):
225239
self.board_pins.append(NamedPin(row[0], pin))
226240

227241
def print_named(self, label, named_pins):
228-
print('const pin_named_pin_t pin_{:s}_pins[] = {{'.format(label))
242+
print('STATIC const mp_map_elem_t pin_{:s}_pins_locals_dict_table[] = {{'.format(label))
229243
for named_pin in named_pins:
230244
pin = named_pin.pin()
231245
if pin.is_board_pin():
232-
print(' {{ "{:s}", &pin_{:s} }},'.format(named_pin.name(), pin.cpu_pin_name()))
233-
print(' { NULL, NULL }')
246+
print(' {{ MP_OBJ_NEW_QSTR(MP_QSTR_{:s}), (mp_obj_t)&pin_{:s} }},'.format(named_pin.name(), pin.cpu_pin_name()))
234247
print('};')
248+
print('MP_DEFINE_CONST_DICT(pin_{:s}_pins_locals_dict, pin_{:s}_pins_locals_dict_table);'.format(label, label));
235249

236250
def print(self):
237251
for named_pin in self.cpu_pins:
@@ -269,6 +283,38 @@ def print_header(self, hdr_filename):
269283
hdr_file.write('extern const pin_obj_t * const pin_adc2[];\n')
270284
hdr_file.write('extern const pin_obj_t * const pin_adc3[];\n')
271285

286+
def print_qstr(self, qstr_filename):
287+
with open(qstr_filename, 'wt') as qstr_file:
288+
qstr_set = set([])
289+
for named_pin in self.cpu_pins:
290+
pin = named_pin.pin()
291+
if pin.is_board_pin():
292+
qstr_set |= set(pin.qstr_list())
293+
qstr_set |= set([named_pin.name()])
294+
for named_pin in self.board_pins:
295+
qstr_set |= set([named_pin.name()])
296+
for qstr in sorted(qstr_set):
297+
print('Q({})'.format(qstr), file=qstr_file)
298+
299+
def print_af_hdr(self, af_const_filename):
300+
with open(af_const_filename, 'wt') as af_const_file:
301+
af_hdr_set = set([])
302+
mux_name_width = 0
303+
for named_pin in self.cpu_pins:
304+
pin = named_pin.pin()
305+
if pin.is_board_pin():
306+
for af in pin.alt_fn:
307+
if af.is_supported():
308+
mux_name = af.mux_name()
309+
af_hdr_set |= set([mux_name])
310+
if len(mux_name) > mux_name_width:
311+
mux_name_width = len(mux_name)
312+
for mux_name in sorted(af_hdr_set):
313+
key = 'MP_OBJ_NEW_QSTR(MP_QSTR_{}),'.format(mux_name)
314+
val = 'MP_OBJ_NEW_SMALL_INT(GPIO_{})'.format(mux_name)
315+
print(' { %-*s %s },' % (mux_name_width + 26, key, val),
316+
file=af_const_file)
317+
272318

273319
def main():
274320
parser = argparse.ArgumentParser(
@@ -280,7 +326,13 @@ def main():
280326
"-a", "--af",
281327
dest="af_filename",
282328
help="Specifies the alternate function file for the chip",
283-
default="stm32f4xx-af.csv"
329+
default="stm32f4xx_af.csv"
330+
)
331+
parser.add_argument(
332+
"--af-const",
333+
dest="af_const_filename",
334+
help="Specifies header file for alternate function constants.",
335+
default="build/pins_af_const.h"
284336
)
285337
parser.add_argument(
286338
"-b", "--board",
@@ -291,7 +343,13 @@ def main():
291343
"-p", "--prefix",
292344
dest="prefix_filename",
293345
help="Specifies beginning portion of generated pins file",
294-
default="stm32f4xx-prefix.c"
346+
default="stm32f4xx_prefix.c"
347+
)
348+
parser.add_argument(
349+
"-q", "--qstr",
350+
dest="qstr_filename",
351+
help="Specifies name of generated qstr header file",
352+
default="build/pins_qstr.h"
295353
)
296354
parser.add_argument(
297355
"-r", "--hdr",
@@ -323,6 +381,8 @@ def main():
323381
pins.print_adc(2)
324382
pins.print_adc(3)
325383
pins.print_header(args.hdr_filename)
384+
pins.print_qstr(args.qstr_filename)
385+
pins.print_af_hdr(args.af_const_filename)
326386

327387

328388
if __name__ == "__main__":
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// stm32fxx-prefix.c becomes the initial portion of the generated pins file.
1+
// stm32f4xx_prefix.c becomes the initial portion of the generated pins file.
22

33
#include <stdio.h>
44
#include <stdint.h>
@@ -14,6 +14,7 @@
1414
#define AF(af_idx, af_fn, af_unit, af_type, af_ptr) \
1515
{ \
1616
{ &pin_af_type }, \
17+
.name = MP_QSTR_AF ## af_idx ## _ ## af_fn ## af_unit, \
1718
.idx = (af_idx), \
1819
.fn = AF_FN_ ## af_fn, \
1920
.unit = (af_unit), \
@@ -24,7 +25,7 @@
2425
#define PIN(p_port, p_pin, p_num_af, p_af, p_adc_num, p_adc_channel) \
2526
{ \
2627
{ &pin_type }, \
27-
.name = #p_port #p_pin, \
28+
.name = MP_QSTR_ ## p_port ## p_pin, \
2829
.port = PORT_ ## p_port, \
2930
.pin = (p_pin), \
3031
.num_af = (p_num_af), \

stmhal/mphal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
#define GPIO_read_pin(gpio, pin) (((gpio)->IDR >> (pin)) & 1)
66
#define GPIO_set_pin(gpio, pin_mask) (((gpio)->BSRRL) = (pin_mask))
77
#define GPIO_clear_pin(gpio, pin_mask) (((gpio)->BSRRH) = (pin_mask))
8+
#define GPIO_read_output_pin(gpio, pin) (((gpio)->ODR >> (pin)) & 1)

0 commit comments

Comments
 (0)