|
| 1 | +importargparse |
| 2 | +importglob |
| 3 | +importre |
| 4 | +importos |
| 5 | +fromstm32commonimportcreateFolder,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 | +defcheckConfig(arg_core,arg_cmsis): |
| 39 | +globalcore_path |
| 40 | +globalCMSIS_path |
| 41 | +globalCMSIS_DSPSrc_path |
| 42 | +globalSrcWrapper_path |
| 43 | +globalHALDrivers_path |
| 44 | +globalCMSIS_Device_ST_path |
| 45 | +globalCMSIS_DSP_lib_path |
| 46 | +globalCMSIS_DSP_outSrc_path |
| 47 | +globalCMSIS_Startupfile |
| 48 | +globalHALoutSrc_path |
| 49 | +globalLLoutSrc_path |
| 50 | +globalLLoutInc_path |
| 51 | + |
| 52 | +ifarg_coreisnotNone: |
| 53 | +core_path=arg_core |
| 54 | +CMSIS_path=os.path.abspath( |
| 55 | +os.path.join(core_path,"..","ArduinoModule-CMSIS","CMSIS_5") |
| 56 | + ) |
| 57 | + |
| 58 | +ifnotos.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 | +ifarg_cmsisisnotNone: |
| 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 | +defprint_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 | +defprintCMSISStartup(log): |
| 101 | +filelist=glob.glob( |
| 102 | +os.path.join( |
| 103 | +CMSIS_Device_ST_path,"STM32*","Source","Templates","gcc","startup_*.s", |
| 104 | + ) |
| 105 | + ) |
| 106 | +iflen(filelist): |
| 107 | +iflog: |
| 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 | +iflen(filelist): |
| 130 | +forfpinfilelist: |
| 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 | +iflog: |
| 158 | +print("No startup files found!") |
| 159 | + |
| 160 | + |
| 161 | +defwrap(arg_core,arg_cmsis,log): |
| 162 | +globalstm32_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 | +ifos.path.isfile(CMSIS_Startupfile): |
| 175 | +os.remove(CMSIS_Startupfile) |
| 176 | +full_ll_list= [] |
| 177 | +# Search all files for each series |
| 178 | +forserieinstm32_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 | +ifos.path.exists(src): |
| 183 | +iflog: |
| 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 | +forfpinfilelist: |
| 189 | +if"_template"infp: |
| 190 | +continue |
| 191 | +outp=HALoutSrc_path |
| 192 | +# File name |
| 193 | +fn=os.path.basename(fp) |
| 194 | +if"_ll_"infn: |
| 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 | +forfpinfilelist: |
| 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 | +ifos.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 | +iflog: |
| 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 | +forhninfull_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 | +forfpinfilelist: |
| 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 | +ifnotos.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 | +forroot,dirs,filesinos.walk(CMSIS_DSPSrc_path): |
| 257 | +forfileinfiles: |
| 258 | +iffile.endswith(".c"): |
| 259 | +dirlist.append(root.replace(CMSIS_DSPSrc_path,"")[1:]) |
| 260 | +dirlist=sorted(set(dirlist)) |
| 261 | +fordnindirlist: |
| 262 | +fdn=os.path.join(CMSIS_DSP_outSrc_path,dn) |
| 263 | +ifnotos.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 | +return0 |
| 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) |