11import json
22import sys
33from pathlib import Path
4+ from xml .dom .minidom import parse
45
56script_path = Path (__file__ ).parent .resolve ()
67sys .path .append (str (script_path .parent ))
78from utils import copyFile , copyFolder , createFolder , deleteFolder
89from utils import defaultConfig , genSTM32List
910
1011stm32_list = [] # series
12+ svd_dict = {} # 'svd file': 'name'
1113root_path = script_path .parent .parent .resolve ()
1214hal_path = root_path / "system" / "Drivers"
1315cubeclt_path = Path ("" )
16+ cubeclt_mcu_path = Path ()
1417cubeclt_svd_path = Path ("" )
1518stm32_svd_repo = Path ("" )
1619stm32_svd_dir = Path ("" )
1720
1821
1922def checkConfig ():
2023 global cubeclt_path
24+ global cubeclt_mcu_path
2125 global cubeclt_svd_path
2226 global stm32_svd_repo
2327 global stm32_svd_dir
@@ -43,6 +47,10 @@ def checkConfig():
4347 if not cubeclt_svd_path .is_dir ():
4448 print (f"{ cubeclt_svd_path } does not exist!" )
4549 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 )
4654 if "STM32_SVD_PATH" not in path_config :
4755 path_config ["STM32_SVD_PATH" ] = str ("Path to stm32_svd repository" )
4856 defaultConfig (config_file_path , path_config )
@@ -62,11 +70,56 @@ def checkConfig():
6270 )
6371
6472
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+
65109def main ():
66110 global stm32_list
67- # check config have to be done first
111+ # Check config have to be done first
68112 checkConfig ()
113+ # Get list of STM32 series from HAL driver directory
69114 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 )
70123 # Reverse order to get WBA before WB to ease svd sorting
71124 stm32_list .sort (reverse = True )
72125 # Clean up core svd folder
@@ -81,17 +134,33 @@ def main():
81134 # Create all directories
82135 for serie in stm32_list :
83136 createFolder (stm32_svd_dir / f"STM32{ serie } xx" )
84- # Get all xml files
137+ # Get all svd files
85138 svd_list = sorted (cubeclt_svd_path .glob ("STM32*.svd" ))
86139
87- # Copy all svd files per series
140+ # Copy all svd files per supported series
88141 for svd_file in svd_list :
89142 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." )
94157
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 ()
95164
96165if __name__ == "__main__" :
97166 main ()
0 commit comments