Skip to content

Commit 77dc514

Browse files
committed
[genpinmap] Use GitHub repository STM32_open_pin_data
https://github.com/STMicroelectronics/STM32_open_pin_data Signed-off-by: Frederic Pillon <[email protected]>
1 parent 29d4a0d commit 77dc514

File tree

2 files changed

+207
-77
lines changed

2 files changed

+207
-77
lines changed

src/genpinmap/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
config.json
2+
repo/

src/genpinmap/genpinmap_arduino.py

Lines changed: 206 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import re
55
import string
6+
import subprocess
67
import sys
78
import textwrap
89
from argparse import RawTextHelpFormatter
@@ -303,7 +304,7 @@ def print_periph_header():
303304
s = """{}
304305
/*
305306
* Automatically generated from {}
306-
* CubeMX DB version {} release {}
307+
* CubeMX DB release {}
307308
*/
308309
#include "Arduino.h"
309310
#include "{}.h"
@@ -322,8 +323,7 @@ def print_periph_header():
322323
""".format(
323324
license_header,
324325
mcu_file.name,
325-
cubemx_db_version,
326-
cubemx_db_release,
326+
db_release,
327327
re.sub("\\.c$", "", periph_c_filename),
328328
)
329329
periph_c_file.write(s)
@@ -1310,59 +1310,160 @@ def parse_pins():
13101310
store_sd(pin, name, sig)
13111311

13121312

1313+
# Config management
1314+
def create_config():
1315+
# Create a Json file for a better path management
1316+
try:
1317+
print("Please set your configuration in '{}' file".format(config_filename))
1318+
config_file = open(config_filename, "w", newline="\n")
1319+
if sys.platform.startswith("win32"):
1320+
print("Platform is Windows")
1321+
cubemxdir = Path(
1322+
r"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX"
1323+
)
1324+
elif sys.platform.startswith("linux"):
1325+
print("Platform is Linux")
1326+
cubemxdir = Path.home() / "STM32CubeMX"
1327+
elif sys.platform.startswith("darwin"):
1328+
print("Platform is Mac OSX")
1329+
cubemxdir = Path(
1330+
"/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources"
1331+
)
1332+
else:
1333+
print("Platform unknown")
1334+
cubemxdir = "<Set CubeMX install directory>"
1335+
config_file.write(
1336+
json.dumps(
1337+
{
1338+
"CUBEMX_DIRECTORY": str(cubemxdir),
1339+
"REPO_LOCAL_PATH": str(repo_local_path),
1340+
},
1341+
indent=2,
1342+
)
1343+
)
1344+
config_file.close()
1345+
except IOError:
1346+
print("Failed to open " + config_filename)
1347+
exit(1)
1348+
1349+
1350+
def check_config():
1351+
global cubemxdir
1352+
global repo_local_path
1353+
global repo_path
1354+
1355+
if config_filename.is_file():
1356+
try:
1357+
config_file = open(config_filename, "r")
1358+
config = json.load(config_file)
1359+
config_file.close()
1360+
1361+
conf = config["REPO_LOCAL_PATH"]
1362+
if conf:
1363+
if conf != "":
1364+
repo_local_path = Path(conf)
1365+
repo_path = repo_local_path / repo_name
1366+
conf = config["CUBEMX_DIRECTORY"]
1367+
if conf:
1368+
cubemxdir = Path(conf)
1369+
except IOError:
1370+
print("Failed to open " + config_filename)
1371+
else:
1372+
create_config()
1373+
1374+
1375+
def manage_repo():
1376+
global db_release
1377+
repo_local_path.mkdir(parents=True, exist_ok=True)
1378+
1379+
print("Updating " + repo_name + "...")
1380+
try:
1381+
if not args.skip:
1382+
if repo_path.is_dir():
1383+
# Get new tags from the remote
1384+
git_cmds = [
1385+
["git", "-C", repo_path, "clean", "-fdx"],
1386+
["git", "-C", repo_path, "fetch"],
1387+
["git", "-C", repo_path, "reset", "--hard", "origin/master"],
1388+
]
1389+
else:
1390+
# Clone it as it does not exists yet
1391+
git_cmds = [["git", "-C", repo_local_path, "clone", gh_url]]
1392+
1393+
for cmd in git_cmds:
1394+
subprocess.check_output(cmd).decode("utf-8")
1395+
if repo_path.is_dir():
1396+
# Get tag
1397+
sha1_id = (
1398+
subprocess.check_output(
1399+
["git", "-C", repo_path, "rev-list", "--tags", "--max-count=1"]
1400+
)
1401+
.decode("utf-8")
1402+
.strip()
1403+
)
1404+
version_tag = (
1405+
subprocess.check_output(
1406+
["git", "-C", repo_path, "describe", "--tags", sha1_id]
1407+
)
1408+
.decode("utf-8")
1409+
.strip()
1410+
)
1411+
subprocess.check_output(
1412+
["git", "-C", repo_path, "checkout", version_tag],
1413+
stderr=subprocess.DEVNULL,
1414+
)
1415+
db_release = version_tag
1416+
return True
1417+
except subprocess.CalledProcessError as e:
1418+
print("Command {} failed with error code {}".format(e.cmd, e.returncode))
1419+
return False
1420+
1421+
13131422
# main
13141423
cur_dir = Path.cwd()
13151424
templates_dir = cur_dir / "templates"
13161425
periph_c_filename = "PeripheralPins.c"
13171426
pinvar_h_filename = "PinNamesVar.h"
1318-
config_filename = "config.json"
1427+
config_filename = Path("config.json")
13191428
variant_h_filename = "variant.h"
13201429
variant_cpp_filename = "variant.cpp"
1430+
repo_local_path = cur_dir / "repo"
1431+
gh_url = "https://github.com/STMicroelectronics/STM32_open_pin_data"
1432+
repo_name = gh_url.rsplit("/", 1)[-1]
1433+
repo_path = repo_local_path / repo_name
1434+
db_release = "Unknown"
13211435

1322-
try:
1323-
config_file = open(config_filename, "r")
1324-
except IOError:
1325-
print("Please set your configuration in '{}' file".format(config_filename))
1326-
config_file = open(config_filename, "w", newline="\n")
1327-
if sys.platform.startswith("win32"):
1328-
print("Platform is Windows")
1329-
cubemxdir = Path(r"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeMX")
1330-
elif sys.platform.startswith("linux"):
1331-
print("Platform is Linux")
1332-
cubemxdir = Path.home() / "STM32CubeMX"
1333-
elif sys.platform.startswith("darwin"):
1334-
print("Platform is Mac OSX")
1335-
cubemxdir = Path(
1336-
"/Applications/STMicroelectronics/STM32CubeMX.app/Contents/Resources"
1337-
)
1338-
else:
1339-
print("Platform unknown")
1340-
cubemxdir = "<Set CubeMX install directory>"
1341-
config_file.write(json.dumps({"CUBEMX_DIRECTORY": str(cubemxdir)}))
1342-
config_file.close()
1343-
exit(1)
1344-
1345-
config = json.load(config_file)
1346-
config_file.close()
1347-
cubemxdir = Path(config["CUBEMX_DIRECTORY"])
1436+
check_config()
13481437

13491438
# By default, generate for all mcu xml files description
13501439
parser = argparse.ArgumentParser(
13511440
description=textwrap.dedent(
13521441
"""\
1353-
By default, generate {} and {} for all xml files description available in
1354-
STM32CubeMX directory defined in '{}':
1355-
\t{}""".format(
1356-
periph_c_filename, pinvar_h_filename, config_filename, cubemxdir
1442+
By default, generate {}, {}, {} and {}
1443+
for all xml files description available in STM32CubeMX internal database.
1444+
Internal database path must be defined in {}.
1445+
It can be the one from STM32CubeMX directory if defined:
1446+
\t{}
1447+
or the one from GitHub:
1448+
\t{}
1449+
1450+
""".format(
1451+
periph_c_filename,
1452+
pinvar_h_filename,
1453+
variant_cpp_filename,
1454+
variant_h_filename,
1455+
config_filename,
1456+
cubemxdir,
1457+
gh_url,
13571458
)
13581459
),
13591460
epilog=textwrap.dedent(
13601461
"""\
1361-
After files generation, review them carefully and please report any issue to github:
1462+
After files generation, review them carefully and please report any issue to GitHub:
13621463
\thttps://github.com/stm32duino/Arduino_Tools/issues\n
1363-
Once generated, you have to comment a line if the pin is generated several times
1364-
for the same IP or if the pin should not be used (overlaid with some HW on the board,
1365-
for instance)"""
1464+
Once generated, you can comment a line if the pin should not be used
1465+
(overlaid with some HW on the board, for instance) and update all undefined pins
1466+
in variant."""
13661467
),
13671468
formatter_class=RawTextHelpFormatter,
13681469
)
@@ -1379,48 +1480,84 @@ def parse_pins():
13791480
metavar="xml",
13801481
help=textwrap.dedent(
13811482
"""\
1382-
Generate {} and {} for specified mcu xml file description
1383-
in STM32CubeMX. This xml file contains non alpha characters in
1384-
its name, you should call it with double quotes""".format(
1385-
periph_c_filename, pinvar_h_filename
1483+
Generate {}, {}, {} and {}
1484+
for specified mcu xml file description in STM32CubeMX.
1485+
This xml file can contain non alpha characters in its name,
1486+
you should call it with double quotes""".format(
1487+
periph_c_filename,
1488+
pinvar_h_filename,
1489+
variant_cpp_filename,
1490+
variant_h_filename,
1491+
)
1492+
),
1493+
)
1494+
1495+
parser.add_argument(
1496+
"-c",
1497+
"--cube",
1498+
help=textwrap.dedent(
1499+
"""\
1500+
Use STM32CubeMX internal database. Default use GitHub {} repository.
1501+
""".format(
1502+
repo_name
13861503
)
13871504
),
1505+
action="store_true",
1506+
)
1507+
parser.add_argument(
1508+
"-s",
1509+
"--skip",
1510+
help="Skip {} clone/fetch".format(repo_name),
1511+
action="store_true",
13881512
)
13891513
args = parser.parse_args()
13901514

1391-
if not (cubemxdir.is_dir()):
1392-
print("\nCube Mx seems not to be installed or not at the requested location.")
1393-
print(
1394-
"\nPlease check the value you set for 'CUBEMX_DIRECTORY' in '{}' file.".format(
1395-
config_filename
1515+
# Using GitHub repo is the preferred way, CubeMX used as a fallback
1516+
fallback = False
1517+
if not args.cube:
1518+
if manage_repo():
1519+
dirMCU = repo_path / "mcu"
1520+
dirIP = dirMCU / "IP"
1521+
else:
1522+
fallback = True
1523+
if fallback or args.cube:
1524+
if not (cubemxdir.is_dir()):
1525+
print(
1526+
"""
1527+
Cube Mx seems not to be installed or not at the specified location.
1528+
1529+
Please check the value set for 'CUBEMX_DIRECTORY' in '{}' file.""".format(
1530+
config_filename
1531+
)
13961532
)
1397-
)
1398-
quit()
1533+
quit()
13991534

1400-
cubemxdirMCU = cubemxdir / "db" / "mcu"
1401-
cubemxdirIP = cubemxdirMCU / "IP"
1402-
version_file = cubemxdir / "db" / "package.xml"
1403-
cubemx_db_version = "Unknown"
1404-
cubemx_db_release = "Unknown"
1405-
xml_file = parse(str(version_file))
1406-
Package_item = xml_file.getElementsByTagName("Package")
1407-
for item in Package_item:
1408-
cubemx_db_version = item.attributes["DBVersion"].value
1409-
PackDescription_item = xml_file.getElementsByTagName("PackDescription")
1410-
for item in PackDescription_item:
1411-
cubemx_db_release = item.attributes["Release"].value
1412-
print("CubeMX DB version {} release {}\n".format(cubemx_db_version, cubemx_db_release))
1535+
dirMCU = cubemxdir / "db" / "mcu"
1536+
dirIP = dirMCU / "IP"
1537+
version_file = cubemxdir / "db" / "package.xml"
1538+
if version_file.is_file():
1539+
xml_file = parse(str(version_file))
1540+
PackDescription_item = xml_file.getElementsByTagName("PackDescription")
1541+
for item in PackDescription_item:
1542+
db_release = item.attributes["Release"].value
1543+
1544+
# Process DB release
1545+
release_regex = r".*(\d+.\d+.\d+)$"
1546+
release_match = re.match(release_regex, db_release)
1547+
if release_match:
1548+
db_release = release_match.group(1)
1549+
print("CubeMX DB release {}\n".format(db_release))
14131550

14141551
if args.mcu:
1415-
# check input file exists
1416-
if not ((cubemxdirMCU / args.mcu).is_file()):
1552+
# Check input file exists
1553+
if not ((dirMCU / args.mcu).is_file()):
14171554
print("\n" + args.mcu + " file not found")
1418-
print("\nCheck in " + cubemxdirMCU + " the correct name of this file")
1555+
print("\nCheck in " + dirMCU + " the correct name of this file")
14191556
print("\nYou may use double quotes for file containing special characters")
14201557
quit()
1421-
mcu_list.append(cubemxdirMCU / args.mcu)
1558+
mcu_list.append(dirMCU / args.mcu)
14221559
else:
1423-
mcu_list = cubemxdirMCU.glob("STM32*.xml")
1560+
mcu_list = dirMCU.glob("STM32*.xml")
14241561

14251562
if args.list:
14261563
print("Available xml files description:")
@@ -1435,15 +1572,7 @@ def parse_pins():
14351572
)
14361573

14371574
for mcu_file in mcu_list:
1438-
print(
1439-
"Generating {}, {}, {} and {} for '{}'...".format(
1440-
periph_c_filename,
1441-
pinvar_h_filename,
1442-
variant_cpp_filename,
1443-
variant_h_filename,
1444-
mcu_file.name,
1445-
)
1446-
)
1575+
print("Generating files for '{}'...".format(mcu_file.name))
14471576
out_path = cur_dir / "Arduino" / mcu_file.name[:7] / mcu_file.stem
14481577
periph_c_filepath = out_path / periph_c_filename
14491578
periph_h_filepath = out_path / pinvar_h_filename
@@ -1467,7 +1596,7 @@ def parse_pins():
14671596
if gpiofile == "ERROR":
14681597
print("Could not find GPIO file")
14691598
quit()
1470-
xml_gpio = parse(str(cubemxdirIP / ("GPIO-" + gpiofile + "_Modes.xml")))
1599+
xml_gpio = parse(str(dirIP / ("GPIO-" + gpiofile + "_Modes.xml")))
14711600

14721601
parse_pins()
14731602
sort_my_lists()

0 commit comments

Comments
 (0)