11import argparse
2- import collections
32import fileinput
43import json
54import re
65import subprocess
76import stm32wrapper
87import sys
8+ from collections import OrderedDict
99from jinja2 import Environment , FileSystemLoader
1010from packaging import version
1111from pathlib import Path
1414
1515script_path = Path (__file__ ).parent .resolve ()
1616sys .path .append (str (script_path .parent ))
17- from utils import copyFile , copyFolder , createFolder , deleteFolder , genSTM32List
17+ from utils import copyFile , copyFolder , createFolder , deleteFolder , genSTM32Dict
1818from utils import defaultConfig , execute_cmd , getRepoBranchName
1919
2020if sys .platform .startswith ("win32" ):
4545hal_dest_path = system_dest_path / hal_src_path
4646cmsis_dest_path = system_dest_path / hal_src_path / "CMSIS" / "Device" / "ST"
4747
48+ nx = "xx" # Default number of x in STM32 series
49+
4850stm32_list = [] # series
49- cube_versions = collections .OrderedDict () # key: serie name, value: cube version
50- cube_HAL_versions = collections .OrderedDict () # key: serie name, value: HAL version
51- cube_CMSIS_versions = collections .OrderedDict () # key: serie name, value: CMSIS version
52- core_HAL_versions = collections .OrderedDict () # key: serie name, value: HAL version
53- core_CMSIS_versions = collections .OrderedDict () # key: serie name, value: CMSIS version
51+ stm32_dict = OrderedDict () # key: serie, value: nx
52+ cube_versions = OrderedDict () # key: serie name, value: cube version
53+ cube_HAL_versions = OrderedDict () # key: serie name, value: HAL version
54+ cube_CMSIS_versions = OrderedDict () # key: serie name, value: CMSIS version
55+ core_HAL_versions = OrderedDict () # key: serie name, value: HAL version
56+ core_CMSIS_versions = OrderedDict () # key: serie name, value: CMSIS version
5457
5558# MD to update
5659md_CMSIS_path = "STM32YYxx_CMSIS_version.md"
@@ -123,7 +126,7 @@ def checkConfig():
123126
124127def updateStm32Def (serie ):
125128 print (f"Adding top HAL include for { serie } ..." )
126- regex_serie = re .compile (r "defined\(STM32(\w+)xx \)" )
129+ regex_serie = re .compile (rf "defined\(STM32(\w+){ nx } \)" )
127130 # Add the new STM32YY entry
128131 added = False
129132 serie_found = ""
@@ -142,23 +145,23 @@ def updateStm32Def(serie):
142145 pcond = "if"
143146 else :
144147 pcond = "elif"
145- print (f"#{ pcond } defined(STM32{ serie } xx )" )
146- print (f' #include "stm32{ serie .lower ()} xx .h"' )
148+ print (f"#{ pcond } defined(STM32{ serie } { nx } )" )
149+ print (f' #include "stm32{ serie .lower ()} { nx } .h"' )
147150 print (line .replace ("#if" , "#elif" ), end = "" )
148151 added = True
149152 else :
150153 print (line , end = "" )
151154
152155
153156def updateHalConfDefault (serie ):
154- system_serie = system_dest_path / f"STM32{ serie } xx "
155- hal_conf_base = f"stm32{ serie .lower ()} xx_hal_conf "
157+ system_serie = system_dest_path / f"STM32{ serie } { nx } "
158+ hal_conf_base = f"stm32{ serie .lower ()} { nx } _hal_conf "
156159 hal_conf_default = system_serie / f"{ hal_conf_base } _default.h"
157160
158161 regex_module = re .compile (r"#define HAL_(\w+)_MODULE_ENABLED" )
159162
160- old_guard = f"STM32{ serie } xx_HAL_CONF_H "
161- new_guard = f"STM32{ serie } xx_HAL_CONF_DEFAULT_H "
163+ old_guard = f"STM32{ serie } { nx } _HAL_CONF_H "
164+ new_guard = f"STM32{ serie } { nx } _HAL_CONF_DEFAULT_H "
162165 module_sel = "Module Selection"
163166
164167 new_include = """/**
@@ -191,16 +194,16 @@ def updateHalConfDefault(serie):
191194
192195def createSystemFiles (serie ):
193196 print (f"Creating system files for { serie } ..." )
194- system_serie = system_dest_path / f"STM32{ serie } xx "
197+ system_serie = system_dest_path / f"STM32{ serie } { nx } "
195198 createFolder (system_serie )
196199 # Generate stm32yyxx_hal_conf_file.h
197200 stm32_hal_conf_file = system_serie / stm32yyxx_hal_conf_file .replace (
198201 "yy" , serie .lower ()
199- )
202+ ). replace ( "xx" , nx )
200203 with open (stm32_hal_conf_file , "w" , newline = "\n " ) as out_file :
201- out_file .write (stm32yyxx_hal_conf_file_template .render (serie = serie ))
204+ out_file .write (stm32yyxx_hal_conf_file_template .render (serie = serie , nx = nx ))
202205 # Copy system_stm32*.c file from CMSIS device template
203- system_stm32_path = cmsis_dest_path / f"STM32{ serie } xx " / "Source" / "Templates"
206+ system_stm32_path = cmsis_dest_path / f"STM32{ serie } { nx } " / "Source" / "Templates"
204207 filelist = sorted (system_stm32_path .glob ("system_stm32*.c" ))
205208 file_number = len (filelist )
206209 if file_number :
@@ -217,8 +220,8 @@ def createSystemFiles(serie):
217220 else :
218221 print ("No system files found!" )
219222 # Copy stm32yyxx_hal_conf_default.h file
220- hal_conf_base = f"stm32{ serie .lower ()} xx_hal_conf "
221- hal_serie_path = hal_dest_path / f"STM32{ serie } xx_HAL_Driver "
223+ hal_conf_base = f"stm32{ serie .lower ()} { nx } _hal_conf "
224+ hal_serie_path = hal_dest_path / f"STM32{ serie } { nx } _HAL_Driver "
222225 hal_conf_file = hal_serie_path / "Inc" / f"{ hal_conf_base } _template.h"
223226 hal_conf_default = system_serie / f"{ hal_conf_base } _default.h"
224227 copyFile (hal_conf_file , hal_conf_default )
@@ -307,10 +310,30 @@ def checkSTLocal():
307310
308311
309312def updateSTRepo ():
313+ global nx
310314 # Handle STM32Cube repo
311315 for serie in stm32_list :
312316 repo_name = f"{ repo_generic_name } { serie } "
313317 repo_path = repo_local_path / repo_name
318+ if upargs .add :
319+ # Series can have only one x,
320+ # find directory starting with STM32 and ending with HAL_Driver
321+ # in hal_src_path folder then check number of x
322+ for f in (repo_path / hal_src_path ).iterdir ():
323+ if f .is_dir ():
324+ if f .name .startswith (f"STM32{ serie } xx_HAL_Driver" ):
325+ nx = "xx"
326+ break
327+ elif f .name .startswith (f"STM32{ serie } x_HAL_Driver" ):
328+ nx = "x"
329+ break
330+ else :
331+ print (
332+ f"Could not find HAL_Driver for { serie } in { repo_path / hal_src_path } "
333+ )
334+ exit (1 )
335+ stm32_dict [serie ] = nx
336+ nx = stm32_dict [serie ]
314337 gh_STM32Cube = urljoin (gh_st , f"{ repo_name } .git" )
315338 print (f"Updating { repo_name } ..." )
316339 if repo_path .exists ():
@@ -428,39 +451,39 @@ def checkVersion(serie, repo_path):
428451 HAL_file = (
429452 repo_path
430453 / hal_src_path
431- / f"STM32{ userie } xx_HAL_Driver "
454+ / f"STM32{ userie } { nx } _HAL_Driver "
432455 / "Src"
433- / f"stm32{ lserie } xx_hal .c"
456+ / f"stm32{ lserie } { nx } _hal .c"
434457 )
435458 with open (HAL_file , "r" ) as fp :
436459 data = fp .read ()
437460 if "HAL_VERSION_MAIN" not in data :
438461 HAL_file = (
439462 repo_path
440463 / hal_src_path
441- / f"STM32{ userie } xx_HAL_Driver "
464+ / f"STM32{ userie } { nx } _HAL_Driver "
442465 / "Inc"
443- / f"stm32{ lserie } xx_hal .h"
466+ / f"stm32{ lserie } { nx } _hal .h"
444467 )
445468 cube_HAL_versions [serie ] = parseVersion (HAL_file , patterns )
446469 if upargs .add :
447470 core_HAL_versions [serie ] = "0.0.0"
448471 else :
449472 HAL_file = (
450473 hal_dest_path
451- / f"STM32{ userie } xx_HAL_Driver "
474+ / f"STM32{ userie } { nx } _HAL_Driver "
452475 / "Src"
453- / f"stm32{ lserie } xx_hal .c"
476+ / f"stm32{ lserie } { nx } _hal .c"
454477 )
455478 with open (HAL_file , "r" ) as fp :
456479 data = fp .read ()
457480 if "HAL_VERSION_MAIN" not in data :
458481 HAL_file = (
459482 repo_path
460483 / hal_dest_path
461- / f"STM32{ userie } xx_HAL_Driver "
484+ / f"STM32{ userie } { nx } _HAL_Driver "
462485 / "Inc"
463- / f"stm32{ lserie } xx_hal .h"
486+ / f"stm32{ lserie } { nx } _hal .h"
464487 )
465488 core_HAL_versions [serie ] = parseVersion (HAL_file , patterns )
466489
@@ -479,16 +502,25 @@ def checkVersion(serie, repo_path):
479502 CMSIS_file = (
480503 repo_path
481504 / cmsis_src_path
482- / f"STM32{ userie } xx "
505+ / f"STM32{ userie } { nx } "
483506 / "Include"
484- / f"stm32{ lserie } xx .h"
507+ / f"stm32{ lserie } { nx } .h"
485508 )
509+ # Some CMSIS folder have a uppercase x
510+ if not CMSIS_file .is_file ():
511+ CMSIS_file = (
512+ repo_path
513+ / cmsis_src_path
514+ / f"STM32{ userie } { nx .upper ()} "
515+ / "Include"
516+ / f"stm32{ lserie } { nx } .h"
517+ )
486518 cube_CMSIS_versions [serie ] = parseVersion (CMSIS_file , patterns )
487519 if upargs .add :
488520 core_CMSIS_versions [serie ] = "0.0.0"
489521 else :
490522 CMSIS_file = (
491- cmsis_dest_path / f"STM32{ userie } xx " / "Include" / f"stm32{ lserie } xx .h"
523+ cmsis_dest_path / f"STM32{ userie } { nx } " / "Include" / f"stm32{ lserie } { nx } .h"
492524 )
493525 core_CMSIS_versions [serie ] = parseVersion (CMSIS_file , patterns )
494526
@@ -834,12 +866,14 @@ def updateOpenAmp():
834866
835867
836868def updateCore ():
869+ global nx
837870 for serie in stm32_list :
838871 if upargs .path :
839872 cube_path = local_cube_path
840873 else :
841874 cube_name = f"{ repo_generic_name } { serie } "
842875 cube_path = repo_local_path / cube_name
876+ nx = stm32_dict [serie .removeprefix ("STM32" )]
843877 core_HAL_ver = core_HAL_versions [serie ]
844878 cube_HAL_ver = cube_HAL_versions [serie ]
845879 core_CMSIS_ver = core_CMSIS_versions [serie ]
@@ -848,23 +882,25 @@ def updateCore():
848882 HAL_updated = False
849883 CMSIS_updated = False
850884 openamp_updated = False
851- hal_commit_msg = """system({0}) {4} STM32{1}xx HAL Drivers to v{2}
885+ hal_commit_msg = """system({0}) {4} STM32{1}{5} HAL Drivers to v{2}
852886
853887Included in STM32Cube{1} FW {3}""" .format (
854888 serie .lower (),
855889 serie ,
856890 cube_HAL_ver ,
857891 cube_version ,
858892 "add" if upargs .add else "update" ,
893+ nx ,
859894 )
860- cmsis_commit_msg = """system({0}): {4} STM32{1}xx CMSIS Drivers to v{2}
895+ cmsis_commit_msg = """system({0}): {4} STM32{1}{5} CMSIS Drivers to v{2}
861896
862897Included in STM32Cube{1} FW {3}""" .format (
863898 serie .lower (),
864899 serie ,
865900 cube_CMSIS_ver ,
866901 cube_version ,
867902 "add" if upargs .add else "update" ,
903+ nx ,
868904 )
869905 wrapper_commit_msg = (
870906 f"core({ serie .lower ()} ): { 'add' if upargs .add else 'update' } wrapped files"
@@ -879,11 +915,11 @@ def updateCore():
879915 f"Updating { serie } HAL from version { core_HAL_ver } to { cube_HAL_ver } ..."
880916 )
881917 # First delete old HAL version
882- HAL_serie_core_path = hal_dest_path / f"STM32{ serie } xx_HAL_Driver "
918+ HAL_serie_core_path = hal_dest_path / f"STM32{ serie } { nx } _HAL_Driver "
883919 deleteFolder (HAL_serie_core_path )
884920 # Copy new one
885921 HAL_serie_cube_path = (
886- cube_path / hal_src_path / f"STM32{ serie } xx_HAL_Driver "
922+ cube_path / hal_src_path / f"STM32{ serie } { nx } _HAL_Driver "
887923 )
888924 copyFolder (
889925 HAL_serie_cube_path ,
@@ -903,10 +939,21 @@ def updateCore():
903939 f"Updating { serie } CMSIS from version { core_CMSIS_ver } to { cube_CMSIS_ver } ..."
904940 )
905941 # First delete CMSIS folder
906- CMSIS_serie_dest_path = cmsis_dest_path / f"STM32{ serie } xx "
942+ CMSIS_serie_dest_path = cmsis_dest_path / f"STM32{ serie } { nx } "
907943 deleteFolder (CMSIS_serie_dest_path )
908944 # Copy new one
909- CMSIS_serie_cube_path = cube_path / cmsis_src_path / f"STM32{ serie } xx"
945+ CMSIS_serie_cube_path = cube_path / cmsis_src_path / f"STM32{ serie } { nx } "
946+ # Check if path exists
947+ if not CMSIS_serie_cube_path .exists ():
948+ # Try to find the upper case version, ex: WB0X
949+ CMSIS_serie_cube_path = (
950+ cube_path / cmsis_src_path / f"STM32{ serie } { nx .upper ()} "
951+ )
952+ if not CMSIS_serie_cube_path .exists ():
953+ print (f"Could not find CMSIS serie { serie } in { CMSIS_serie_cube_path } !" )
954+ exit (1 )
955+ # Copy CMSIS files
956+ # note: that dest path uses lower x case
910957 copyFolder (
911958 CMSIS_serie_cube_path ,
912959 CMSIS_serie_dest_path ,
@@ -919,12 +966,12 @@ def updateCore():
919966
920967 if upargs .add :
921968 system_commit_msg = (
922- f"system({ serie .lower ()} ): add STM32{ serie } xx system source files"
969+ f"system({ serie .lower ()} ): add STM32{ serie } { nx } system source files"
923970 )
924971 update_hal_conf_commit_msg = (
925- f"system({ serie .lower ()} ): update STM32{ serie } xx hal default config"
972+ f"system({ serie .lower ()} ): update STM32{ serie } { nx } hal default config"
926973 )
927- update_stm32_def_commit_msg = f"core({ serie } ): add top HAL include"
974+ update_stm32_def_commit_msg = f"core({ serie . lower () } ): add top HAL include"
928975 # Create system files
929976 createSystemFiles (serie )
930977 # Commit all system files
@@ -934,7 +981,7 @@ def updateCore():
934981 commitFiles (core_path , update_hal_conf_commit_msg )
935982 print ("\t Please, review carefully all the system files added!" )
936983 print ("\t Add #ifndef/#endif to all definitions which should be" )
937- print (f"\t redefinable in the stm32{ serie .lower ()} xx_hal_conf_default .h" )
984+ print (f"\t redefinable in the stm32{ serie .lower ()} { nx } _hal_conf_default .h" )
938985 # Update stm32_def to add top HAL include
939986 updateStm32Def (serie )
940987 commitFiles (core_path , update_stm32_def_commit_msg )
@@ -1008,14 +1055,19 @@ def updateCore():
10081055
10091056
10101057def main ():
1058+ global stm32_dict
10111059 global stm32_list
10121060 # check config have to be done first
10131061 checkConfig ()
1014- stm32_list = genSTM32List (hal_dest_path , upargs .serie )
10151062 if not upargs .local :
10161063 updateCoreRepo ()
10171064 else :
10181065 checkCoreRepo ()
1066+ stm32_dict = genSTM32Dict (hal_dest_path , upargs .serie )
1067+ stm32_list = sorted (list (stm32_dict .keys ()))
1068+ if not stm32_list :
1069+ print (f"{ upargs .serie } is not supported yet. Consider using -a instead of -s" )
1070+ exit (1 )
10191071 if upargs .add :
10201072 if upargs .add .upper () not in stm32_list :
10211073 stm32_list = [upargs .add .upper ()]
0 commit comments