1
1
import argparse
2
- import collections
3
2
import fileinput
4
3
import json
5
4
import re
6
5
import subprocess
7
6
import stm32wrapper
8
7
import sys
8
+ from collections import OrderedDict
9
9
from jinja2 import Environment , FileSystemLoader
10
10
from packaging import version
11
11
from pathlib import Path
14
14
15
15
script_path = Path (__file__ ).parent .resolve ()
16
16
sys .path .append (str (script_path .parent ))
17
- from utils import copyFile , copyFolder , createFolder , deleteFolder , genSTM32List
17
+ from utils import copyFile , copyFolder , createFolder , deleteFolder , genSTM32Dict
18
18
from utils import defaultConfig , execute_cmd , getRepoBranchName
19
19
20
20
if sys .platform .startswith ("win32" ):
45
45
hal_dest_path = system_dest_path / hal_src_path
46
46
cmsis_dest_path = system_dest_path / hal_src_path / "CMSIS" / "Device" / "ST"
47
47
48
+ nx = "xx" # Default number of x in STM32 series
49
+
48
50
stm32_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
54
57
55
58
# MD to update
56
59
md_CMSIS_path = "STM32YYxx_CMSIS_version.md"
@@ -123,7 +126,7 @@ def checkConfig():
123
126
124
127
def updateStm32Def (serie ):
125
128
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 } \)" )
127
130
# Add the new STM32YY entry
128
131
added = False
129
132
serie_found = ""
@@ -142,23 +145,23 @@ def updateStm32Def(serie):
142
145
pcond = "if"
143
146
else :
144
147
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"' )
147
150
print (line .replace ("#if" , "#elif" ), end = "" )
148
151
added = True
149
152
else :
150
153
print (line , end = "" )
151
154
152
155
153
156
def 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 "
156
159
hal_conf_default = system_serie / f"{ hal_conf_base } _default.h"
157
160
158
161
regex_module = re .compile (r"#define HAL_(\w+)_MODULE_ENABLED" )
159
162
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 "
162
165
module_sel = "Module Selection"
163
166
164
167
new_include = """/**
@@ -191,16 +194,16 @@ def updateHalConfDefault(serie):
191
194
192
195
def createSystemFiles (serie ):
193
196
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 } "
195
198
createFolder (system_serie )
196
199
# Generate stm32yyxx_hal_conf_file.h
197
200
stm32_hal_conf_file = system_serie / stm32yyxx_hal_conf_file .replace (
198
201
"yy" , serie .lower ()
199
- )
202
+ ). replace ( "xx" , nx )
200
203
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 ))
202
205
# 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"
204
207
filelist = sorted (system_stm32_path .glob ("system_stm32*.c" ))
205
208
file_number = len (filelist )
206
209
if file_number :
@@ -217,8 +220,8 @@ def createSystemFiles(serie):
217
220
else :
218
221
print ("No system files found!" )
219
222
# 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 "
222
225
hal_conf_file = hal_serie_path / "Inc" / f"{ hal_conf_base } _template.h"
223
226
hal_conf_default = system_serie / f"{ hal_conf_base } _default.h"
224
227
copyFile (hal_conf_file , hal_conf_default )
@@ -307,10 +310,30 @@ def checkSTLocal():
307
310
308
311
309
312
def updateSTRepo ():
313
+ global nx
310
314
# Handle STM32Cube repo
311
315
for serie in stm32_list :
312
316
repo_name = f"{ repo_generic_name } { serie } "
313
317
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 ]
314
337
gh_STM32Cube = urljoin (gh_st , f"{ repo_name } .git" )
315
338
print (f"Updating { repo_name } ..." )
316
339
if repo_path .exists ():
@@ -428,39 +451,39 @@ def checkVersion(serie, repo_path):
428
451
HAL_file = (
429
452
repo_path
430
453
/ hal_src_path
431
- / f"STM32{ userie } xx_HAL_Driver "
454
+ / f"STM32{ userie } { nx } _HAL_Driver "
432
455
/ "Src"
433
- / f"stm32{ lserie } xx_hal .c"
456
+ / f"stm32{ lserie } { nx } _hal .c"
434
457
)
435
458
with open (HAL_file , "r" ) as fp :
436
459
data = fp .read ()
437
460
if "HAL_VERSION_MAIN" not in data :
438
461
HAL_file = (
439
462
repo_path
440
463
/ hal_src_path
441
- / f"STM32{ userie } xx_HAL_Driver "
464
+ / f"STM32{ userie } { nx } _HAL_Driver "
442
465
/ "Inc"
443
- / f"stm32{ lserie } xx_hal .h"
466
+ / f"stm32{ lserie } { nx } _hal .h"
444
467
)
445
468
cube_HAL_versions [serie ] = parseVersion (HAL_file , patterns )
446
469
if upargs .add :
447
470
core_HAL_versions [serie ] = "0.0.0"
448
471
else :
449
472
HAL_file = (
450
473
hal_dest_path
451
- / f"STM32{ userie } xx_HAL_Driver "
474
+ / f"STM32{ userie } { nx } _HAL_Driver "
452
475
/ "Src"
453
- / f"stm32{ lserie } xx_hal .c"
476
+ / f"stm32{ lserie } { nx } _hal .c"
454
477
)
455
478
with open (HAL_file , "r" ) as fp :
456
479
data = fp .read ()
457
480
if "HAL_VERSION_MAIN" not in data :
458
481
HAL_file = (
459
482
repo_path
460
483
/ hal_dest_path
461
- / f"STM32{ userie } xx_HAL_Driver "
484
+ / f"STM32{ userie } { nx } _HAL_Driver "
462
485
/ "Inc"
463
- / f"stm32{ lserie } xx_hal .h"
486
+ / f"stm32{ lserie } { nx } _hal .h"
464
487
)
465
488
core_HAL_versions [serie ] = parseVersion (HAL_file , patterns )
466
489
@@ -479,16 +502,25 @@ def checkVersion(serie, repo_path):
479
502
CMSIS_file = (
480
503
repo_path
481
504
/ cmsis_src_path
482
- / f"STM32{ userie } xx "
505
+ / f"STM32{ userie } { nx } "
483
506
/ "Include"
484
- / f"stm32{ lserie } xx .h"
507
+ / f"stm32{ lserie } { nx } .h"
485
508
)
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
+ )
486
518
cube_CMSIS_versions [serie ] = parseVersion (CMSIS_file , patterns )
487
519
if upargs .add :
488
520
core_CMSIS_versions [serie ] = "0.0.0"
489
521
else :
490
522
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"
492
524
)
493
525
core_CMSIS_versions [serie ] = parseVersion (CMSIS_file , patterns )
494
526
@@ -834,12 +866,14 @@ def updateOpenAmp():
834
866
835
867
836
868
def updateCore ():
869
+ global nx
837
870
for serie in stm32_list :
838
871
if upargs .path :
839
872
cube_path = local_cube_path
840
873
else :
841
874
cube_name = f"{ repo_generic_name } { serie } "
842
875
cube_path = repo_local_path / cube_name
876
+ nx = stm32_dict [serie .removeprefix ("STM32" )]
843
877
core_HAL_ver = core_HAL_versions [serie ]
844
878
cube_HAL_ver = cube_HAL_versions [serie ]
845
879
core_CMSIS_ver = core_CMSIS_versions [serie ]
@@ -848,23 +882,25 @@ def updateCore():
848
882
HAL_updated = False
849
883
CMSIS_updated = False
850
884
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}
852
886
853
887
Included in STM32Cube{1} FW {3}""" .format (
854
888
serie .lower (),
855
889
serie ,
856
890
cube_HAL_ver ,
857
891
cube_version ,
858
892
"add" if upargs .add else "update" ,
893
+ nx ,
859
894
)
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}
861
896
862
897
Included in STM32Cube{1} FW {3}""" .format (
863
898
serie .lower (),
864
899
serie ,
865
900
cube_CMSIS_ver ,
866
901
cube_version ,
867
902
"add" if upargs .add else "update" ,
903
+ nx ,
868
904
)
869
905
wrapper_commit_msg = (
870
906
f"core({ serie .lower ()} ): { 'add' if upargs .add else 'update' } wrapped files"
@@ -879,11 +915,11 @@ def updateCore():
879
915
f"Updating { serie } HAL from version { core_HAL_ver } to { cube_HAL_ver } ..."
880
916
)
881
917
# 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 "
883
919
deleteFolder (HAL_serie_core_path )
884
920
# Copy new one
885
921
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 "
887
923
)
888
924
copyFolder (
889
925
HAL_serie_cube_path ,
@@ -903,10 +939,21 @@ def updateCore():
903
939
f"Updating { serie } CMSIS from version { core_CMSIS_ver } to { cube_CMSIS_ver } ..."
904
940
)
905
941
# 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 } "
907
943
deleteFolder (CMSIS_serie_dest_path )
908
944
# 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
910
957
copyFolder (
911
958
CMSIS_serie_cube_path ,
912
959
CMSIS_serie_dest_path ,
@@ -919,12 +966,12 @@ def updateCore():
919
966
920
967
if upargs .add :
921
968
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"
923
970
)
924
971
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"
926
973
)
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"
928
975
# Create system files
929
976
createSystemFiles (serie )
930
977
# Commit all system files
@@ -934,7 +981,7 @@ def updateCore():
934
981
commitFiles (core_path , update_hal_conf_commit_msg )
935
982
print ("\t Please, review carefully all the system files added!" )
936
983
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" )
938
985
# Update stm32_def to add top HAL include
939
986
updateStm32Def (serie )
940
987
commitFiles (core_path , update_stm32_def_commit_msg )
@@ -1008,14 +1055,19 @@ def updateCore():
1008
1055
1009
1056
1010
1057
def main ():
1058
+ global stm32_dict
1011
1059
global stm32_list
1012
1060
# check config have to be done first
1013
1061
checkConfig ()
1014
- stm32_list = genSTM32List (hal_dest_path , upargs .serie )
1015
1062
if not upargs .local :
1016
1063
updateCoreRepo ()
1017
1064
else :
1018
1065
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 )
1019
1071
if upargs .add :
1020
1072
if upargs .add .upper () not in stm32_list :
1021
1073
stm32_list = [upargs .add .upper ()]
0 commit comments