20def svd_file_devices() -> dict[Path, list[DeviceIdentifier]]:
21 global _SVD_FILES, _SVD_MAP_FILE
22 if _SVD_FILES is None:
23 if not _SVD_MAP_FILE.exists():
24 rm_files, hd_files, cm_files = {}, {}, {}
25 for file in cache_path("stmicro-svd/").glob("*.svd"):
26 match = re.search(r"<description>(.*?)</description>", file.read_text())
27 devices = [did_from_string(n) for n in match.group(1).split(",")]
28 if file.stem.startswith("rm_"):
29 rm_files[file] = devices
30 else:
31 hd_files[file] = devices
32
33 files = list(root_path("ext/cmsis/svd/data/STMicro/").glob("*.svd"))
34
35 def _sortkey(file):
36 name = re.sub(r"x+$", "", file.stem)
37 return (-len(name), name.count("x"), name.index("x") if "x" in name else 0, name)
38
39 files.sort(key=_sortkey)
40
41 def _match(file, devices):
42 pattern = re.sub(r"x+$", "", file.stem)
43 pattern = pattern.replace("x", ".").replace("_CM", ".*?@m")
44 pattern = pattern.replace("...A", ".....A")
45 return [d for d in devices if re.match(pattern, d.string, flags=re.IGNORECASE)]
46
47 mdevices = set(cubemx_device_list())
48 for file in files:
49 devices = _match(file, mdevices)
50 mdevices -= set(devices)
51 cm_files[file] = devices
52
53 filefmt = {
54 "cm": {str(p): [str(d) for d in v] for p, v in cm_files.items()},
55 "hd": {str(p): [str(d) for d in v] for p, v in hd_files.items()},
56 "rm": {str(p): [str(d) for d in v] for p, v in rm_files.items()},
57 }
58 with _SVD_MAP_FILE.open("w", encoding="utf-8") as fh:
59 json.dump(filefmt, fh, indent=4)
60 else:
61 with _SVD_MAP_FILE.open("r", encoding="utf-8") as fh:
62 cache = json.load(fh)
63 rm_files = {Path(p): [did_from_string(d) for d in v] for p, v in cache["rm"].items()}
64 hd_files = {Path(p): [did_from_string(d) for d in v] for p, v in cache["hd"].items()}
65 cm_files = {Path(p): [did_from_string(d) for d in v] for p, v in cache["cm"].items()}
66
67 _SVD_FILES = (hd_files, cm_files, rm_files)
68
69 return _SVD_FILES