|
| 1 | +import argparse |
| 2 | +import glob |
| 3 | +import re |
| 4 | +import os |
| 5 | +from stm32common import createFolder, deleteFolder, genSTM32List |
| 6 | + |
| 7 | +script_path = os.path.dirname(os.path.abspath(__file__)) |
| 8 | +home = os.path.expanduser("~") |
| 9 | +# Base path |
| 10 | +core_path = os.path.abspath(os.path.join(script_path, "..", "..")) |
| 11 | +SrcWrapper_path = "" |
| 12 | +HALDrivers_path = "" |
| 13 | +CMSIS_Device_ST_path = "" |
| 14 | +CMSIS_DSP_lib_path = "" |
| 15 | + |
| 16 | +# CMSIS outside of the core. Can be updated by arg |
| 17 | +CMSIS_path = os.path.abspath( |
| 18 | + os.path.join(core_path, "..", "ArduinoModule-CMSIS", "CMSIS_5") |
| 19 | +) |
| 20 | +CMSIS_DSPSrc_path = "" |
| 21 | + |
| 22 | +# Out sources files |
| 23 | +HALoutSrc_path = "" |
| 24 | +LLoutSrc_path = "" |
| 25 | +CMSIS_DSP_outSrc_path = "" |
| 26 | + |
| 27 | +# Out include files |
| 28 | +LLoutInc_path = "" |
| 29 | + |
| 30 | +# Out startup files |
| 31 | +CMSIS_Startupfile = "" |
| 32 | + |
| 33 | +all_LL_file = "stm32yyxx_ll.h" |
| 34 | + |
| 35 | +stm32_series = [] |
| 36 | + |
| 37 | + |
| 38 | +def checkConfig(arg_core, arg_cmsis): |
| 39 | + global core_path |
| 40 | + global CMSIS_path |
| 41 | + global CMSIS_DSPSrc_path |
| 42 | + global SrcWrapper_path |
| 43 | + global HALDrivers_path |
| 44 | + global CMSIS_Device_ST_path |
| 45 | + global CMSIS_DSP_lib_path |
| 46 | + global CMSIS_DSP_outSrc_path |
| 47 | + global CMSIS_Startupfile |
| 48 | + global HALoutSrc_path |
| 49 | + global LLoutSrc_path |
| 50 | + global LLoutInc_path |
| 51 | + |
| 52 | + if arg_core is not None: |
| 53 | + core_path = arg_core |
| 54 | + CMSIS_path = os.path.abspath( |
| 55 | + os.path.join(core_path, "..", "ArduinoModule-CMSIS", "CMSIS_5") |
| 56 | + ) |
| 57 | + |
| 58 | + if not os.path.isdir(core_path): |
| 59 | + print("Could not find " + core_path) |
| 60 | + exit(1) |
| 61 | + |
| 62 | + SrcWrapper_path = os.path.join(core_path, "libraries", "SrcWrapper") |
| 63 | + HALDrivers_path = os.path.join(core_path, "system", "Drivers") |
| 64 | + CMSIS_Device_ST_path = os.path.join( |
| 65 | + core_path, "system", "Drivers", "CMSIS", "Device", "ST" |
| 66 | + ) |
| 67 | + CMSIS_DSP_lib_path = os.path.join(core_path, "libraries", "CMSIS_DSP") |
| 68 | + CMSIS_DSP_outSrc_path = os.path.join(CMSIS_DSP_lib_path, "src") |
| 69 | + CMSIS_Startupfile = os.path.join( |
| 70 | + core_path, "cores", "arduino", "stm32", "stm32_def_build.h" |
| 71 | + ) |
| 72 | + |
| 73 | + HALoutSrc_path = os.path.join(SrcWrapper_path, "src", "HAL") |
| 74 | + LLoutSrc_path = os.path.join(SrcWrapper_path, "src", "LL") |
| 75 | + LLoutInc_path = os.path.join(core_path, "cores", "arduino", "stm32", "LL") |
| 76 | + |
| 77 | + if arg_cmsis is not None: |
| 78 | + CMSIS_path = arg_cmsis |
| 79 | + CMSIS_DSPSrc_path = os.path.join(CMSIS_path, "CMSIS", "DSP", "Source") |
| 80 | + |
| 81 | + |
| 82 | +# Add some pragma to ll header files to avoid several warnings |
| 83 | +# which will be corrected along Cube update |
| 84 | +def print_LL_header(open_file, name): |
| 85 | + upper = name.upper().replace(".", "_") |
| 86 | + open_file.write( |
| 87 | + """#ifndef _{0}_ |
| 88 | +#define _{0}_ |
| 89 | +/* LL raised several warnings, ignore them */ |
| 90 | +#pragma GCC diagnostic push |
| 91 | +#pragma GCC diagnostic ignored \"-Wunused-parameter\" |
| 92 | +#pragma GCC diagnostic ignored \"-Wstrict-aliasing\" |
| 93 | +
|
| 94 | +""".format( |
| 95 | + upper |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def printCMSISStartup(log): |
| 101 | + filelist = glob.glob( |
| 102 | + os.path.join( |
| 103 | + CMSIS_Device_ST_path, "STM32*", "Source", "Templates", "gcc", "startup_*.s", |
| 104 | + ) |
| 105 | + ) |
| 106 | + if len(filelist): |
| 107 | + if log: |
| 108 | + print("Number of startup files: %i" % len(filelist)) |
| 109 | + out_file = open(CMSIS_Startupfile, "w", newline="\n") |
| 110 | + # Header |
| 111 | + out_file.write( |
| 112 | + """#ifndef _STM32_DEF_BUILD_ |
| 113 | +#define _STM32_DEF_BUILD_ |
| 114 | +
|
| 115 | +#if !defined(CMSIS_STARTUP_FILE) && !defined(CUSTOM_STARTUP_FILE) |
| 116 | +""" |
| 117 | + ) |
| 118 | + # File name |
| 119 | + fn = os.path.basename(filelist.pop(0)) |
| 120 | + valueline = re.split("_|\\.", fn) |
| 121 | + upper = valueline[1].upper().replace("X", "x") |
| 122 | + out_file.write( |
| 123 | + """ #if defined({}) |
| 124 | + #define CMSIS_STARTUP_FILE \"{}\" |
| 125 | +""".format( |
| 126 | + upper, fn |
| 127 | + ) |
| 128 | + ) |
| 129 | + if len(filelist): |
| 130 | + for fp in filelist: |
| 131 | + # File name |
| 132 | + fn = os.path.basename(fp) |
| 133 | + valueline = re.split("_|\\.", fn) |
| 134 | + upper = ( |
| 135 | + valueline[1].upper().replace("X", "x").replace("MP15xx", "MP1xx") |
| 136 | + ) |
| 137 | + out_file.write( |
| 138 | + """ #elif defined({}) |
| 139 | + #define CMSIS_STARTUP_FILE \"{}\" |
| 140 | +""".format( |
| 141 | + upper, fn |
| 142 | + ) |
| 143 | + ) |
| 144 | + # footer |
| 145 | + out_file.write( |
| 146 | + """ #else |
| 147 | + #error UNKNOWN CHIP |
| 148 | + #endif |
| 149 | +#else |
| 150 | + #warning \"No CMSIS startup file defined, custom one should be used\" |
| 151 | +#endif /* !CMSIS_STARTUP_FILE && !CUSTOM_STARTUP_FILE */ |
| 152 | +#endif /* _STM32_DEF_BUILD_ */ |
| 153 | +""" |
| 154 | + ) |
| 155 | + out_file.close() |
| 156 | + else: |
| 157 | + if log: |
| 158 | + print("No startup files found!") |
| 159 | + |
| 160 | + |
| 161 | +def wrap(arg_core, arg_cmsis, log): |
| 162 | + global stm32_series |
| 163 | + # check config have to be done first |
| 164 | + checkConfig(arg_core, arg_cmsis) |
| 165 | + stm32_series = genSTM32List(HALDrivers_path, "") |
| 166 | + |
| 167 | + # Remove old file |
| 168 | + deleteFolder(HALoutSrc_path) |
| 169 | + createFolder(HALoutSrc_path) |
| 170 | + deleteFolder(LLoutSrc_path) |
| 171 | + createFolder(LLoutSrc_path) |
| 172 | + deleteFolder(LLoutInc_path) |
| 173 | + createFolder(LLoutInc_path) |
| 174 | + if os.path.isfile(CMSIS_Startupfile): |
| 175 | + os.remove(CMSIS_Startupfile) |
| 176 | + full_ll_list = [] |
| 177 | + # Search all files for each series |
| 178 | + for serie in stm32_series: |
| 179 | + src = os.path.join(HALDrivers_path, "STM32" + serie + "xx_HAL_Driver", "Src") |
| 180 | + inc = os.path.join(HALDrivers_path, "STM32" + serie + "xx_HAL_Driver", "Inc") |
| 181 | + |
| 182 | + if os.path.exists(src): |
| 183 | + if log: |
| 184 | + print("Generating for " + serie + "...") |
| 185 | + lower = serie.lower() |
| 186 | + # Generate stm32yyxx_[hal|ll]*.c file |
| 187 | + filelist = glob.glob(os.path.join(src, "stm32" + lower + "xx_*.c")) |
| 188 | + for fp in filelist: |
| 189 | + if "_template" in fp: |
| 190 | + continue |
| 191 | + outp = HALoutSrc_path |
| 192 | + # File name |
| 193 | + fn = os.path.basename(fp) |
| 194 | + if "_ll_" in fn: |
| 195 | + outp = LLoutSrc_path |
| 196 | + # Compute generic file name with path |
| 197 | + gp = os.path.join(outp, fn.replace(lower, "yy")) |
| 198 | + out_file = open(gp, "a", newline="\n") |
| 199 | + # Amend file name under serie switch |
| 200 | + out_file.write("#ifdef STM32" + serie + "xx\n") |
| 201 | + out_file.write(' #include "' + fn + '"\n') |
| 202 | + out_file.write("#endif\n") |
| 203 | + out_file.close() |
| 204 | + # Generate stm32yyxx_ll_*.h file |
| 205 | + filelist = glob.glob(os.path.join(inc, "stm32" + lower + "xx_ll_*.h")) |
| 206 | + for fp in filelist: |
| 207 | + outp = LLoutInc_path |
| 208 | + # File name |
| 209 | + fn = os.path.basename(fp) |
| 210 | + # Compute generic file name |
| 211 | + gn = fn.replace(lower, "yy") |
| 212 | + # with path |
| 213 | + gp = os.path.join(outp, gn) |
| 214 | + out_file = open(gp, "a", newline="\n") |
| 215 | + if os.path.getsize(gp) == 0: |
| 216 | + print_LL_header(out_file, gn) |
| 217 | + # Amend full LL header file |
| 218 | + full_ll_list.append(gn) |
| 219 | + # Amend file name under serie switch |
| 220 | + out_file.write("#ifdef STM32" + serie + "xx\n") |
| 221 | + out_file.write(' #include "' + fn + '"\n') |
| 222 | + out_file.write("#endif\n") |
| 223 | + out_file.close() |
| 224 | + if log: |
| 225 | + print("done") |
| 226 | + |
| 227 | + # Filter full LL header file |
| 228 | + full_ll_file = open(os.path.join(LLoutInc_path, all_LL_file), "w", newline="\n") |
| 229 | + print_LL_header(full_ll_file, all_LL_file) |
| 230 | + full_ll_file.write("/* Include Low Layers drivers */\n") |
| 231 | + full_ll_list = sorted(set(full_ll_list)) |
| 232 | + for hn in full_ll_list: |
| 233 | + full_ll_file.write('#include "' + hn + '"\n') |
| 234 | + full_ll_file.close() |
| 235 | + |
| 236 | + # Search all LL header files to end guard |
| 237 | + filelist = glob.glob(os.path.join(LLoutInc_path, "stm32yyxx_ll*.h")) |
| 238 | + for fp in filelist: |
| 239 | + out_file = open(fp, "a", newline="\n") |
| 240 | + upper = os.path.basename(fp).upper().replace(".", "_") |
| 241 | + out_file.write("#pragma GCC diagnostic pop\n") |
| 242 | + out_file.write("#endif /* _" + upper + "_ */\n") |
| 243 | + out_file.close() |
| 244 | + |
| 245 | + # CMSIS startup files |
| 246 | + printCMSISStartup(log) |
| 247 | + |
| 248 | + # CMSIS DSP C source file |
| 249 | + if not os.path.isdir(CMSIS_path): |
| 250 | + print("Could not find " + CMSIS_path) |
| 251 | + print("CMSIS DSP generation skipped.") |
| 252 | + else: |
| 253 | + # Delete all subfolders |
| 254 | + deleteFolder(os.path.join(CMSIS_DSP_outSrc_path, "*")) |
| 255 | + dirlist = [] |
| 256 | + for root, dirs, files in os.walk(CMSIS_DSPSrc_path): |
| 257 | + for file in files: |
| 258 | + if file.endswith(".c"): |
| 259 | + dirlist.append(root.replace(CMSIS_DSPSrc_path, "")[1:]) |
| 260 | + dirlist = sorted(set(dirlist)) |
| 261 | + for dn in dirlist: |
| 262 | + fdn = os.path.join(CMSIS_DSP_outSrc_path, dn) |
| 263 | + if not os.path.isdir(fdn): |
| 264 | + createFolder(fdn) |
| 265 | + out_file = open(os.path.join(fdn, dn + ".c"), "w", newline="\n") |
| 266 | + out_file.write('#include "../Source/{0}/{0}.c"\n'.format(dn)) |
| 267 | + out_file.close() |
| 268 | + return 0 |
| 269 | + |
| 270 | + |
| 271 | +if __name__ == "__main__": |
| 272 | + # Parser |
| 273 | + wrapparser = argparse.ArgumentParser( |
| 274 | + description="Generate all wrappers files need by the STM32 core (HAL, LL, CMSIS, ...)" |
| 275 | + ) |
| 276 | + wrapparser.add_argument( |
| 277 | + "-c", |
| 278 | + "--core", |
| 279 | + metavar="core_path", |
| 280 | + help="Root path of the STM32 core. Default: " + core_path, |
| 281 | + ) |
| 282 | + wrapparser.add_argument( |
| 283 | + "-s", |
| 284 | + "--cmsis", |
| 285 | + metavar="cmsis_path", |
| 286 | + help="Root path of the CMSIS. Default: " + CMSIS_path, |
| 287 | + ) |
| 288 | + |
| 289 | + wrapargs = wrapparser.parse_args() |
| 290 | + |
| 291 | + wrap(wrapargs.core, wrapargs.cmsis, True) |
0 commit comments