66import subprocess
77import stm32wrapper
88import sys
9+ from jinja2 import Environment , FileSystemLoader
910from packaging import version
1011from pathlib import Path
11- from stm32common import createFolder , deleteFolder , copyFolder , genSTM32List
12+ from stm32common import createFolder , deleteFolder , copyFolder , copyFile , genSTM32List
1213from urllib .parse import urljoin
1314
1415if sys .platform .startswith ("win32" ):
3031# From
3132# Relative to repo path
3233hal_src_path = "Drivers"
33- cmsis_src_path = Path ("Drivers" , "CMSIS" , "Device" , "ST" )
34+ system_path = "system"
35+ cmsis_src_path = Path (hal_src_path , "CMSIS" , "Device" , "ST" )
3436# To
35- hal_dest_path = Path ("system" , "Drivers" )
36- cmsis_dest_path = Path ("system" , "Drivers" , "CMSIS" , "Device" , "ST" )
37+ system_dest_path = Path (system_path )
38+ hal_dest_path = system_dest_path / hal_src_path
39+ cmsis_dest_path = system_dest_path / hal_src_path / "CMSIS" / "Device" / "ST"
3740
3841stm32_list = [] # Serie
3942cube_versions = collections .OrderedDict () # key: serie name, value: cube version
4649md_CMSIS_path = "STM32YYxx_CMSIS_version.md"
4750md_HAL_path = "STM32YYxx_HAL_Driver_version.md"
4851
52+ # Templating
53+ templates_dir = script_path / "templates"
54+ stm32yyxx_hal_conf_file = "stm32yyxx_hal_conf.h"
55+ # Create the jinja2 environment.
56+ j2_env = Environment (
57+ loader = FileSystemLoader (str (templates_dir )), trim_blocks = True , lstrip_blocks = True
58+ )
59+ stm32yyxx_hal_conf_file_template = j2_env .get_template (stm32yyxx_hal_conf_file )
60+
4961# Format
5062out_format_Header = "| {:^22} | {:^31} | {:^31} |"
5163out_subheader = "| {:^4} | {:^7} | {:^8} | {:^8} | {:^1} | {:^8} | {:^8} | {:^1} |"
@@ -84,6 +96,7 @@ def checkConfig():
8496 global repo_local_path
8597 global hal_dest_path
8698 global cmsis_dest_path
99+ global system_dest_path
87100 global md_HAL_path
88101 global md_CMSIS_path
89102 config_file_path = script_path / path_config_filename
@@ -97,6 +110,7 @@ def checkConfig():
97110 hal_dest_path = repo_local_path / repo_core_name / hal_dest_path
98111 md_HAL_path = hal_dest_path / md_HAL_path
99112 cmsis_dest_path = repo_local_path / repo_core_name / cmsis_dest_path
113+ system_dest_path = repo_local_path / repo_core_name / system_dest_path
100114 md_CMSIS_path = cmsis_dest_path / md_CMSIS_path
101115 except IOError :
102116 print ("Failed to open {}!" .format (config_file ))
@@ -105,6 +119,83 @@ def checkConfig():
105119 createFolder (repo_local_path )
106120
107121
122+ def updateHalConfDefault (serie ):
123+ system_serie = system_dest_path / ("STM32{}xx" .format (serie ))
124+ hal_conf_base = "stm32{}xx_hal_conf" .format (serie .lower ())
125+ hal_conf_default = system_serie / (hal_conf_base + "_default.h" )
126+
127+ regex_module = re .compile (r"#define HAL_(\w+)_MODULE_ENABLED" )
128+
129+ old_guard = "STM32{}xx_HAL_CONF_H" .format (serie )
130+ new_guard = "STM32{}xx_HAL_CONF_DEFAULT_H" .format (serie )
131+ module_sel = "Module Selection"
132+
133+ new_include = """/**
134+ * @brief Include the default list of modules to be used in the HAL driver
135+ * and manage module deactivation
136+ */
137+ #include "stm32yyxx_hal_conf.h"
138+ #if 0
139+ """
140+ module_found = False
141+ end_guard_added = False
142+ for line in fileinput .input (hal_conf_default , inplace = True ):
143+ if old_guard in line :
144+ print (line .replace (old_guard , new_guard ), end = "" )
145+ elif "@file" in line :
146+ print (line .replace ("conf.h" , "conf_default.h" ), end = "" )
147+ elif "@brief" in line :
148+ print (line .replace ("HAL config" , "HAL default config" ), end = "" )
149+ elif "@author" not in line :
150+ m = regex_module .search (line )
151+ if m :
152+ module_found = True
153+ elif module_found and not end_guard_added :
154+ end_guard_added = True
155+ print ("#endif" )
156+ print (line , end = "" )
157+ if module_sel in line :
158+ print (new_include , end = "" )
159+
160+
161+ def createSystemFiles (serie ):
162+ print ("Creating system files for {}..." .format (serie ))
163+ system_serie = system_dest_path / ("STM32{}xx" .format (serie ))
164+ createFolder (system_serie )
165+ # Generate stm32yyxx_hal_conf_file.h
166+ stm32_hal_conf_file = system_serie / stm32yyxx_hal_conf_file .replace (
167+ "yy" , serie .lower ()
168+ )
169+ out_file = open (stm32_hal_conf_file , "w" , newline = "\n " )
170+ out_file .write (stm32yyxx_hal_conf_file_template .render (serie = serie ))
171+ out_file .close ()
172+ # Copy system_stm32*.c file from CMSIS device template
173+ system_stm32_path = (
174+ cmsis_dest_path / "STM32{}xx" .format (serie ) / "Source" / "Templates"
175+ )
176+ filelist = sorted (system_stm32_path .glob ("system_stm32*.c" ))
177+ file_number = len (filelist )
178+ if file_number :
179+ if file_number == 1 :
180+ file_number = 0
181+ else :
182+ menu_list = "Several system stm32 files exist:\n "
183+ for index , fp in enumerate (filelist ):
184+ menu_list += "{}. {}\n " .format (index , fp .name )
185+ menu_list += "Your choice: "
186+ while file_number >= len (filelist ):
187+ file_number = int (input (menu_list ))
188+ copyFile (filelist [file_number ], system_serie )
189+ else :
190+ print ("No system files found!" )
191+ # Copy stm32yyxx_hal_conf_default.h file
192+ hal_conf_base = "stm32{}xx_hal_conf" .format (serie .lower ())
193+ hal_serie_path = hal_dest_path / "STM32{}xx_HAL_Driver" .format (serie )
194+ hal_conf_file = hal_serie_path / "Inc" / (hal_conf_base + "_template.h" )
195+ hal_conf_default = system_serie / (hal_conf_base + "_default.h" )
196+ copyFile (hal_conf_file , hal_conf_default )
197+
198+
108199def getRepoBranchName (repo_path ):
109200 bname = ""
110201 rname = ""
@@ -425,6 +516,30 @@ def applyPatch(serie, HAL_updated, CMSIS_updated, repo_path):
425516 )
426517
427518
519+ def updateMDFile (md_file , serie , version ):
520+ regexmd_up = re .compile (rf"(STM32{ serie } :\s+)\d+.\d+.\d+" )
521+ regexmd_serie = re .compile (r"STM32(\w+):\s+\d+.\d+.\d+" )
522+ regexmd_add = re .compile (r"(STM32)\w+(:\s+)\d+.\d+.\d+" )
523+ # Update MD file
524+ if upargs .add : # Add the new STM32YY entry
525+ added = False
526+ new_line = ""
527+ serie_found = ""
528+ for line in fileinput .input (md_file , inplace = True ):
529+ m = regexmd_serie .search (line )
530+ if m :
531+ serie_found = m .group (1 )
532+ if not new_line :
533+ new_line = regexmd_add .sub (rf"\g<1>{ serie } \g<2>{ version } " , line )
534+ if not added and serie_found and (serie_found > serie .upper () or not m ):
535+ print (new_line , end = "" )
536+ added = True
537+ print (line , end = "" )
538+ else : # Update the version
539+ for line in fileinput .input (md_file , inplace = True ):
540+ print (regexmd_up .sub (rf"\g<1>{ version } " , line ), end = "" )
541+
542+
428543def updateCore ():
429544 for serie in stm32_list :
430545 cube_name = repo_generic_name + serie
@@ -435,109 +550,97 @@ def updateCore():
435550 core_CMSIS_version = core_CMSIS_versions [serie ]
436551 cube_CMSIS_version = cube_CMSIS_versions [serie ]
437552 cube_version = cube_versions [serie ]
438- regexmd_up = re .compile (rf"(STM32{ serie } :\s+)\d+.\d+.\d+" )
439- regexmd_serie = re .compile (r"STM32(\w+):\s+\d+.\d+.\d+" )
440- regexmd_add = re .compile (r"(STM32)\w+(:\s+)\d+.\d+.\d+" )
441553 HAL_updated = False
442554 CMSIS_updated = False
443- hal_commit_msg = """system: {0}: update STM32{0}xx HAL Drivers to v{1}
555+ hal_commit_msg = """system: {0}: {3} STM32{0}xx HAL Drivers to v{1}
444556
445557Included in STM32Cube{0} FW {2}""" .format (
446- serie , cube_HAL_version , cube_version
558+ serie , cube_HAL_version , cube_version , "add" if upargs . add else "update"
447559 )
448- cmsis_commit_msg = """system: {0}: update STM32{0}xx CMSIS Drivers to v{1}
560+ cmsis_commit_msg = """system: {0}: {3} STM32{0}xx CMSIS Drivers to v{1}
449561
450562Included in STM32Cube{0} FW {2}""" .format (
451- serie , cube_CMSIS_version , cube_version
563+ serie , cube_CMSIS_version , cube_version , "add" if upargs .add else "update"
564+ )
565+ wrapper_commit_msg = "core: {}: {} wrapped files" .format (
566+ serie , "add" if upargs .add else "update"
452567 )
453- wrapper_commit_msg = "core: {}: update wrapped files" .format (serie )
454568
455569 # Update HAL part if needed
456570 if version .parse (core_HAL_version ) < version .parse (cube_HAL_version ):
457- print (
458- "Update "
459- + serie
460- + " HAL from version "
461- + core_HAL_version
462- + " to "
463- + cube_HAL_version
464- )
571+ if upargs . add :
572+ print ( "Adding {} HAL version {}..." . format ( serie , cube_HAL_version ))
573+ else :
574+ print (
575+ "Updating {} HAL from version {} to {}..." . format (
576+ serie , core_HAL_version , cube_HAL_version
577+ )
578+ )
465579 # First delete old HAL version
466- HAL_serie_core_path = (
467- core_path / hal_dest_path / "STM32{}xx_HAL_Driver" .format (serie )
468- )
580+ HAL_serie_core_path = hal_dest_path / "STM32{}xx_HAL_Driver" .format (serie )
469581 deleteFolder (HAL_serie_core_path )
470582 # Copy new one
471583 HAL_serie_cube_path = (
472584 cube_path / hal_src_path / "STM32{}xx_HAL_Driver" .format (serie )
473585 )
474586 copyFolder (HAL_serie_cube_path , HAL_serie_core_path , {"*.chm" })
475587 # Update MD file
476- if upargs .add : # Add the new STM32YY entry
477- added = False
478- for line in fileinput .input (md_HAL_path , inplace = True ):
479- m = regexmd_serie .search (line )
480- if not added and m and m .group (1 ) > serie .upper ():
481- print (
482- regexmd_add .sub (
483- rf"\g<1>{ serie } \g<2>{ cube_HAL_version } " , line
484- ),
485- end = "" ,
486- )
487- added = True
488- print (line , end = "" )
489- else : # Update the version
490- for line in fileinput .input (md_HAL_path , inplace = True ):
491- print (regexmd_up .sub (rf"\g<1>{ cube_HAL_version } " , line ), end = "" )
588+ updateMDFile (md_HAL_path , serie , cube_HAL_version )
492589 # Commit all HAL files
493590 commitFiles (core_path , hal_commit_msg )
494591 HAL_updated = True
495592
496593 if version .parse (core_CMSIS_version ) < version .parse (cube_CMSIS_version ):
497- print (
498- "Update "
499- + serie
500- + " CMSIS from version "
501- + core_CMSIS_version
502- + " to "
503- + cube_CMSIS_version
504- )
594+ if upargs . add :
595+ print ( "Adding {} CMSIS version {}..." . format ( serie , cube_CMSIS_version ))
596+ else :
597+ print (
598+ "Updating {} CMSIS from version {} to {}..." . format (
599+ serie , core_CMSIS_version , cube_CMSIS_version
600+ )
601+ )
505602 # First delete CMSIS folder
506- CMSIS_serie_dest_path = (
507- core_path / cmsis_dest_path / "STM32{}xx" .format (serie .upper ())
508- )
603+ CMSIS_serie_dest_path = cmsis_dest_path / "STM32{}xx" .format (serie )
509604 deleteFolder (CMSIS_serie_dest_path )
510605 # Copy new one
511606 CMSIS_serie_cube_path = (
512- cube_path / cmsis_src_path / "STM32{}xx" .format (serie . upper () )
607+ cube_path / cmsis_src_path / "STM32{}xx" .format (serie )
513608 )
514609 copyFolder (CMSIS_serie_cube_path , CMSIS_serie_dest_path , {"iar" , "arm" })
515610 # Update MD file
516- if upargs .add : # Add the new STM32YY entry
517- added = False
518- for line in fileinput .input (md_CMSIS_path , inplace = True ):
519- m = regexmd_serie .search (line )
520- if not added and m and m .group (1 ) > serie .upper ():
521- print (
522- regexmd_add .sub (
523- rf"\g<1>{ serie } \g<2>{ cube_CMSIS_version } " , line
524- ),
525- end = "" ,
526- )
527- added = True
528- print (line , end = "" )
529- else : # Update the version
530- for line in fileinput .input (md_CMSIS_path , inplace = True ):
531- print (regexmd_up .sub (rf"\g<1>{ cube_CMSIS_version } " , line ), end = "" )
611+ updateMDFile (md_CMSIS_path , serie , cube_CMSIS_version )
532612 # Commit all CMSIS files
533613 commitFiles (core_path , cmsis_commit_msg )
534614 CMSIS_updated = True
535615
616+ if upargs .add :
617+ system_commit_msg = (
618+ "system: {0}: add STM32{0}xx system source files" .format (serie )
619+ )
620+ update_hal_conf_commit_msg = (
621+ "system: {0}: update STM32{0}xx hal default config" .format (serie )
622+ )
623+ # Create system files
624+ createSystemFiles (serie )
625+ # Commit all sytem files
626+ commitFiles (core_path , system_commit_msg )
627+ updateHalConfDefault (serie )
628+ commitFiles (core_path , update_hal_conf_commit_msg )
629+ print ("Please, review carefully all the system files added!" )
630+ print ("Add #ifndef/#endif to all definitions which should be" )
631+ print (
632+ "redefinable in the stm32{}xx_hal_conf_default.h" .format (serie .lower ())
633+ )
634+
536635 if HAL_updated or CMSIS_updated :
537636 # Generate all wrapper files
538637 # Assuming the ArduinoModule-CMSIS repo available
539638 # at the same root level than the core
540- print ("Update {} wrapped files" .format (serie ))
639+ print (
640+ "{} {} wrapped files..." .format (
641+ "Adding" if upargs .add else "Updating" , serie
642+ )
643+ )
541644 if stm32wrapper .wrap (core_path , None , False ) == 0 :
542645 commitFiles (core_path , wrapper_commit_msg )
543646 # Apply all related patch if any
0 commit comments