forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstm32svd.py
166 lines (153 loc) · 6.21 KB
/
stm32svd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import json
import sys
from pathlib import Path
from xml.dom.minidom import parse
script_path = Path(__file__).parent.resolve()
sys.path.append(str(script_path.parent))
from utils import copyFile, copyFolder, createFolder, deleteFolder
from utils import defaultConfig, genSTM32List
stm32_list = [] # series
svd_dict = {} # 'svd file': 'name'
root_path = script_path.parent.parent.resolve()
hal_path = root_path / "system" / "Drivers"
cubeclt_path = Path("")
cubeclt_mcu_path = Path()
cubeclt_svd_path = Path("")
stm32_svd_repo = Path("")
stm32_svd_dir = Path("")
def checkConfig():
global cubeclt_path
global cubeclt_mcu_path
global cubeclt_svd_path
global stm32_svd_repo
global stm32_svd_dir
config_file_path = script_path / "update_config.json"
if config_file_path.is_file():
try:
config_file = open(config_file_path, "r")
path_config = json.load(config_file)
config_file.close()
if "STM32CUBECLT_PATH" not in path_config:
path_config["STM32CUBECLT_PATH"] = str(
"Path to STM32CubeCLT installation directory"
)
defaultConfig(config_file_path, path_config)
else:
cubeclt_path = Path(path_config["STM32CUBECLT_PATH"])
if not cubeclt_path.is_dir():
print(f"{cubeclt_path} does not exist!")
exit(1)
else:
cubeclt_svd_path = cubeclt_path / "STMicroelectronics_CMSIS_SVD"
if not cubeclt_svd_path.is_dir():
print(f"{cubeclt_svd_path} does not exist!")
exit(1)
cubeclt_mcu_path = cubeclt_path / "STM32target-mcu"
if not cubeclt_mcu_path.is_dir():
print(f"{cubeclt_mcu_path} does not exist!")
exit(1)
if "STM32_SVD_PATH" not in path_config:
path_config["STM32_SVD_PATH"] = str("Path to stm32_svd repository")
defaultConfig(config_file_path, path_config)
else:
stm32_svd_repo = Path(path_config["STM32_SVD_PATH"])
if not stm32_svd_repo.is_dir():
print(f"{stm32_svd_repo} does not exist!")
exit(1)
else:
stm32_svd_dir = stm32_svd_repo / "svd"
except IOError:
print(f"Failed to open {config_file}!")
else:
defaultConfig(
config_file_path,
{"STM32CUBECLT_PATH": "Path to STM32CubeCLT installation directory"},
)
def parse_stm32targets(stm32targets_file: Path):
global stm32_list
global svd_dict
xml_stm32targets = parse(str(stm32targets_file))
mcu_nodes = xml_stm32targets.getElementsByTagName("mcu")
for mcu_node in mcu_nodes:
parent_node_name = mcu_node.getElementsByTagName("parent")[0].firstChild.data
mcu_node_name = mcu_node.getElementsByTagName("name")[0].firstChild.data
cpus_node_name = mcu_node.getElementsByTagName("cpus")
cpu_node_name = cpus_node_name[0].getElementsByTagName("cpu")
svd_node = cpu_node_name[0].getElementsByTagName("svd")
svd_file = svd_node[0].getElementsByTagName("name")[0].firstChild.data
serie = (
parent_node_name.upper()
.removeprefix("STM32")
.removesuffix("SINGLE")
.removesuffix("DUAL")
)
if serie == "L4PLUS":
serie = "L4"
else:
if mcu_node_name.startswith("STM32H7R") or mcu_node_name.startswith(
"STM32H7S"
):
serie = "H7RS"
svd_dict[svd_file] = serie
# Check if a second cpu is defined
if cpu_node_name.length > 1:
svd_node = cpu_node_name[1].getElementsByTagName("svd")
svd_file = svd_node[0].getElementsByTagName("name")[0].firstChild.data
serie = parent_node_name.upper().removeprefix("STM32").removesuffix("DUAL")
svd_dict[svd_file] = serie
xml_stm32targets.unlink()
def main():
global stm32_list
# Check config have to be done first
checkConfig()
# Get list of STM32 series from HAL driver directory
stm32_list = genSTM32List(hal_path, None)
# Parse STM32Targets.xml to get list of STM32 series and svd file
# one per Cube reference
stm32targets_file = cubeclt_mcu_path / "stm32targets.xml"
if stm32targets_file.is_file():
parse_stm32targets(stm32targets_file)
else:
print(f"{stm32targets_file} does not exits!")
exit(1)
# Reverse order to get WBA before WB to ease svd sorting
stm32_list.sort(reverse=True)
# Clean up core svd folder
deleteFolder(stm32_svd_dir)
createFolder(stm32_svd_dir)
# Update the Core folder
copyFolder(cubeclt_svd_path / "Core", stm32_svd_dir / "Core")
# Update the license
copyFile(cubeclt_svd_path / "about.html", stm32_svd_dir)
# Copy the version
copyFile(cubeclt_path / ".version", stm32_svd_dir / "STM32CubeCLT.version")
# Create all directories
for serie in stm32_list:
createFolder(stm32_svd_dir / f"STM32{serie}xx")
# Get all svd files
svd_list = sorted(cubeclt_svd_path.glob("STM32*.svd"))
# Copy all svd files per supported series
for svd_file in svd_list:
svd_name = svd_file.name
if svd_name in svd_dict:
if svd_dict[svd_name] in stm32_list:
copyFile(svd_file, stm32_svd_dir / f"STM32{svd_dict[svd_name]}xx")
else:
# File not copied as not referenced in stm32targets.xml
if svd_name.startswith("STM32GBK"):
copyFile(svd_file, stm32_svd_dir / "STM32G4xx")
else:
for serie in stm32_list:
if svd_name.startswith(f"STM32{serie}"):
copyFile(svd_file, stm32_svd_dir / f"STM32{serie}xx")
break
else:
print(f"File {svd_name} not copied.")
# Check if created folder is empty and delete it
for serie in stm32_list:
serie_dir = stm32_svd_dir / f"STM32{serie}xx"
if not any(serie_dir.glob("*")):
print (f"Folder {serie_dir} is empty.")
serie_dir.rmdir()
if __name__ == "__main__":
main()