Skip to content

Commit

Permalink
stm32/af_csv_gen: Add tool to generate af csv files.
Browse files Browse the repository at this point in the history
Uses official online source: STM32_open_pin_data.

Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
pi-anl committed Oct 13, 2023
1 parent 480659b commit 42a7d23
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions ports/stm32/boards/af_csv_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import re
import sys
import urllib.request
import xml.etree.ElementTree as ET
from io import StringIO
from pathlib import Path


def generate_af_csv(target: str):
cpu = target.rstrip("x")
listing_url = "https://github.com/STMicroelectronics/STM32_open_pin_data/tree/master/mcu/IP"
response = urllib.request.urlopen(listing_url)
listings_data = response.read().decode("utf-8")
matches = set(re.findall(r"GPIO-%s\S*?_gpio_v1_0_Modes.xml" % cpu.upper(), listings_data))
if not matches:
raise ValueError('ERROR: Could not find "%s" on: %s' % (cpu.upper(), listing_url))
if len(matches) > 1:
raise ValueError(
'ERROR: Multiple definitions for "%s", select one: %s' % (cpu.upper(), matches)
)
filename = matches.pop()
xml_file_url = (
"https://raw.githubusercontent.com/STMicroelectronics/STM32_open_pin_data/master/mcu/IP/"
+ filename
)
response = urllib.request.urlopen(xml_file_url)
xml_data = response.read().decode("utf-8")
print("Downloaded: %s" % xml_file_url)

it = ET.iterparse(StringIO(xml_data))
for _, el in it:
_, _, el.tag = el.tag.rpartition("}")
root = it.root

af_domains = set()

mapping = {}
for pin in root.findall("./GPIO_Pin"):
for pin_signal in pin.findall("./PinSignal"):
for specific_param in pin_signal.findall("SpecificParameter"):
if specific_param.get("Name") != "GPIO_AF":
continue
pin_name = pin.get("Name")
signal_name = pin_signal.get("Name")
af_fn = specific_param.find("./PossibleValue").text
if pin_name not in mapping:
mapping[pin_name] = []
mapping[pin_name].append((signal_name, af_fn))
af_domains.add(af_fn.split("_")[1])

def pin_int(pin_name):
return int(re.search("[PA][A-Z](\d+)", pin_name).group(1))

af_domains = sorted(list(af_domains), key=pin_int)
heading = ["Port", ""] + af_domains
category_sets = [set() for _ in range(len(heading))]
rows = []
for pin in sorted(mapping, key=pin_int):
functions = mapping[pin]
row = [""] * len(heading)
row[0] = "Port%s" % pin[1]
row[1] = pin
for signal, af in functions:
column = pin_int(af) + 2
category_sets[column].add(signal.split("_")[0])
row[column] = signal
rows.append(row)

output_file = Path(__file__).parent / ("%s_af.csv" % target.lower())
with output_file.open("w") as out:
print(",".join(heading), file=out)
categories = ["/".join(sorted(c)) for c in category_sets]
print(",".join(categories), file=out)
for row in rows:
print(",".join(row), file=out)

print("Written:", output_file.resolve())


if __name__ == "__main__":
generate_af_csv(sys.argv[1])

0 comments on commit 42a7d23

Please sign in to comment.