forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stm32/af_csv_gen: Add tool to generate af csv files.
Uses official online source: STM32_open_pin_data. Signed-off-by: Andrew Leech <[email protected]>
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |