modm_data.svd.stmicro

1# Copyright 2022, Niklas Hauser
2# SPDX-License-Identifier: MPL-2.0
3
4from .device import svd_file_devices, svd_device_files
5
6__all__ = ["svd_file_devices", "svd_device_files"]
def svd_file_devices() -> dict[pathlib.Path, list[modm_data.owl.DeviceIdentifier]]:
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
def svd_device_files() -> dict[modm_data.owl.DeviceIdentifier, list[pathlib.Path]]:
72def svd_device_files() -> dict[DeviceIdentifier, list[Path]]:
73    device_files = defaultdict(dict)
74
75    def _remap(files, key):
76        for file, devices in files.items():
77            for device in devices:
78                device_files[device][key] = file
79
80    hd_files, cm_files, rm_files = svd_file_devices()
81    _remap(hd_files, "hd")
82    _remap(cm_files, "cm")
83    _remap(rm_files, "rm")
84    return device_files