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