1
1
import argparse
2
- import glob
3
2
import re
4
- import os
3
+ from pathlib import Path
5
4
from stm32common import createFolder , deleteFolder , genSTM32List
6
5
7
- script_path = os .path .dirname (os .path .abspath (__file__ ))
8
- home = os .path .expanduser ("~" )
6
+ script_path = Path (__file__ ).parent .resolve ()
9
7
# Base path
10
- core_path = os . path . abspath ( os . path . join ( script_path , ".." , ".." ))
8
+ core_path = script_path . parent . parent
11
9
SrcWrapper_path = ""
12
10
HALDrivers_path = ""
13
11
CMSIS_Device_ST_path = ""
14
12
CMSIS_DSP_lib_path = ""
15
13
16
14
# 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
- )
15
+ CMSIS_path = core_path .parent / "ArduinoModule-CMSIS" / "CMSIS_5"
20
16
CMSIS_DSPSrc_path = ""
21
17
22
18
# Out sources files
@@ -50,33 +46,27 @@ def checkConfig(arg_core, arg_cmsis):
50
46
global LLoutInc_path
51
47
52
48
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
- )
49
+ core_path = Path (arg_core ).resolve ()
50
+ CMSIS_path = core_path .parent / "ArduinoModule-CMSIS" / "CMSIS_5"
57
51
58
- if not os . path . isdir ( core_path ):
52
+ if not core_path . is_dir ( ):
59
53
print ("Could not find " + core_path )
60
54
exit (1 )
61
55
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
- )
56
+ SrcWrapper_path = core_path / "libraries" / "SrcWrapper"
57
+ HALDrivers_path = core_path / "system" / "Drivers"
58
+ CMSIS_Device_ST_path = core_path / "system" / "Drivers" / "CMSIS" / "Device" / "ST"
59
+ CMSIS_DSP_lib_path = core_path / "libraries" / "CMSIS_DSP"
60
+ CMSIS_DSP_outSrc_path = CMSIS_DSP_lib_path / "src"
61
+ CMSIS_Startupfile = core_path / "cores" / "arduino" / "stm32" / "stm32_def_build.h"
72
62
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" )
63
+ HALoutSrc_path = SrcWrapper_path / "src" / "HAL"
64
+ LLoutSrc_path = SrcWrapper_path / "src" / "LL"
65
+ LLoutInc_path = core_path / "cores" / "arduino" / "stm32" / "LL"
76
66
77
67
if arg_cmsis is not None :
78
- CMSIS_path = arg_cmsis
79
- CMSIS_DSPSrc_path = os . path . join ( CMSIS_path , "CMSIS" , "DSP" , "Source" )
68
+ CMSIS_path = Path ( arg_cmsis ). resolve ()
69
+ CMSIS_DSPSrc_path = CMSIS_path / "CMSIS" / "DSP" / "Source"
80
70
81
71
82
72
# Add some pragma to ll header files to avoid several warnings
@@ -98,18 +88,7 @@ def print_LL_header(open_file, name):
98
88
99
89
100
90
def printCMSISStartup (log ):
101
- filelist = sorted (
102
- glob .glob (
103
- os .path .join (
104
- CMSIS_Device_ST_path ,
105
- "STM32*" ,
106
- "Source" ,
107
- "Templates" ,
108
- "gcc" ,
109
- "startup_*.s" ,
110
- )
111
- )
112
- )
91
+ filelist = sorted (CMSIS_Device_ST_path .glob ("**/startup_*.s" ))
113
92
if len (filelist ):
114
93
if log :
115
94
print ("Number of startup files: %i" % len (filelist ))
@@ -123,7 +102,7 @@ def printCMSISStartup(log):
123
102
"""
124
103
)
125
104
# File name
126
- fn = os . path . basename (filelist .pop (0 ))
105
+ fn = (filelist .pop (0 )). name
127
106
valueline = re .split ("_|\\ ." , fn )
128
107
upper = valueline [1 ].upper ().replace ("X" , "x" )
129
108
out_file .write (
@@ -136,7 +115,7 @@ def printCMSISStartup(log):
136
115
if len (filelist ):
137
116
for fp in filelist :
138
117
# File name
139
- fn = os . path . basename ( fp )
118
+ fn = fp . name
140
119
valueline = re .split ("_|\\ ." , fn )
141
120
if "stm32mp15" in valueline [1 ] and not valueline [1 ].endswith ("xx" ):
142
121
valueline [1 ] += "xx"
@@ -178,48 +157,48 @@ def wrap(arg_core, arg_cmsis, log):
178
157
createFolder (LLoutSrc_path )
179
158
deleteFolder (LLoutInc_path )
180
159
createFolder (LLoutInc_path )
181
- if os . path . isfile ( CMSIS_Startupfile ):
182
- os . remove ( CMSIS_Startupfile )
160
+ if CMSIS_Startupfile . is_file ( ):
161
+ CMSIS_Startupfile . unlink ( )
183
162
full_ll_list = []
184
163
# Search all files for each series
185
164
for serie in stm32_series :
186
- src = os . path . join ( HALDrivers_path , "STM32" + serie + "xx_HAL_Driver" , "Src" )
187
- inc = os . path . join ( HALDrivers_path , "STM32" + serie + "xx_HAL_Driver" , "Inc" )
165
+ src = HALDrivers_path / ( "STM32" + serie + "xx_HAL_Driver" ) / "Src"
166
+ inc = HALDrivers_path / ( "STM32" + serie + "xx_HAL_Driver" ) / "Inc"
188
167
189
- if os . path . exists (src ):
168
+ if src . exists ():
190
169
if log :
191
170
print ("Generating for " + serie + "..." )
192
171
lower = serie .lower ()
193
172
# Generate stm32yyxx_[hal|ll]*.c file
194
- filelist = glob .glob (os . path . join ( src , "stm32" + lower + "xx_*.c" ) )
173
+ filelist = src .glob ("stm32" + lower + "xx_*.c" )
195
174
for fp in filelist :
196
- if "_template" in fp :
175
+ # File name
176
+ fn = fp .name
177
+ if "_template" in fn :
197
178
continue
198
179
outp = HALoutSrc_path
199
- # File name
200
- fn = os .path .basename (fp )
201
180
if "_ll_" in fn :
202
181
outp = LLoutSrc_path
203
182
# Compute generic file name with path
204
- gp = os . path . join ( outp , fn .replace (lower , "yy" ) )
183
+ gp = outp / fn .replace (lower , "yy" )
205
184
out_file = open (gp , "a" , newline = "\n " )
206
185
# Amend file name under serie switch
207
186
out_file .write ("#ifdef STM32" + serie + "xx\n " )
208
187
out_file .write (' #include "' + fn + '"\n ' )
209
188
out_file .write ("#endif\n " )
210
189
out_file .close ()
211
190
# Generate stm32yyxx_ll_*.h file
212
- filelist = glob .glob (os . path . join ( inc , "stm32" + lower + "xx_ll_*.h" ) )
191
+ filelist = inc .glob ("stm32" + lower + "xx_ll_*.h" )
213
192
for fp in filelist :
214
193
outp = LLoutInc_path
215
194
# File name
216
- fn = os . path . basename ( fp )
195
+ fn = fp . name
217
196
# Compute generic file name
218
197
gn = fn .replace (lower , "yy" )
219
198
# with path
220
- gp = os . path . join ( outp , gn )
199
+ gp = outp / gn
221
200
out_file = open (gp , "a" , newline = "\n " )
222
- if os . path . getsize ( gp ) == 0 :
201
+ if gp . stat (). st_size == 0 :
223
202
print_LL_header (out_file , gn )
224
203
# Amend full LL header file
225
204
full_ll_list .append (gn )
@@ -232,7 +211,7 @@ def wrap(arg_core, arg_cmsis, log):
232
211
print ("done" )
233
212
234
213
# Filter full LL header file
235
- full_ll_file = open (os . path . join ( LLoutInc_path , all_LL_file ) , "w" , newline = "\n " )
214
+ full_ll_file = open (LLoutInc_path / all_LL_file , "w" , newline = "\n " )
236
215
print_LL_header (full_ll_file , all_LL_file )
237
216
full_ll_file .write ("/* Include Low Layers drivers */\n " )
238
217
full_ll_list = sorted (set (full_ll_list ))
@@ -241,10 +220,10 @@ def wrap(arg_core, arg_cmsis, log):
241
220
full_ll_file .close ()
242
221
243
222
# Search all LL header files to end guard
244
- filelist = glob .glob (os . path . join ( LLoutInc_path , "stm32yyxx_ll*.h" ) )
223
+ filelist = LLoutInc_path .glob ("stm32yyxx_ll*.h" )
245
224
for fp in filelist :
246
225
out_file = open (fp , "a" , newline = "\n " )
247
- upper = os . path . basename ( fp ) .upper ().replace ("." , "_" )
226
+ upper = fp . name .upper ().replace ("." , "_" )
248
227
out_file .write ("#pragma GCC diagnostic pop\n " )
249
228
out_file .write ("#endif /* _" + upper + "_ */\n " )
250
229
out_file .close ()
@@ -253,23 +232,23 @@ def wrap(arg_core, arg_cmsis, log):
253
232
printCMSISStartup (log )
254
233
255
234
# CMSIS DSP C source file
256
- if not os . path . isdir ( CMSIS_path ):
257
- print ("Could not find " + CMSIS_path )
235
+ if not CMSIS_path . is_dir ( ):
236
+ print ("Could not find {}" ). format ( CMSIS_path )
258
237
print ("CMSIS DSP generation skipped." )
259
238
else :
260
239
# Delete all subfolders
261
- deleteFolder (os . path . join ( CMSIS_DSP_outSrc_path , "*" ) )
240
+ deleteFolder (CMSIS_DSP_outSrc_path / "*" )
262
241
dirlist = []
263
- for root , dirs , files in os . walk ( CMSIS_DSPSrc_path ):
264
- for file in files :
265
- if file .endswith (".c" ):
266
- dirlist .append (root . replace ( CMSIS_DSPSrc_path , "" )[ 1 :] )
242
+ for path_object in CMSIS_DSPSrc_path . glob ( "**/*" ):
243
+ if path_object . is_file () :
244
+ if path_object . name .endswith (".c" ):
245
+ dirlist .append (path_object . parent . name )
267
246
dirlist = sorted (set (dirlist ))
268
247
for dn in dirlist :
269
- fdn = os . path . join ( CMSIS_DSP_outSrc_path , dn )
270
- if not os . path . isdir ( fdn ):
248
+ fdn = CMSIS_DSP_outSrc_path / dn
249
+ if not fdn . is_dir ( ):
271
250
createFolder (fdn )
272
- out_file = open (os . path . join ( fdn , dn + ".c" ), "w" , newline = "\n " )
251
+ out_file = open (fdn / ( dn + ".c" ), "w" , newline = "\n " )
273
252
out_file .write ('#include "../Source/{0}/{0}.c"\n ' .format (dn ))
274
253
out_file .close ()
275
254
return 0
@@ -284,13 +263,13 @@ def wrap(arg_core, arg_cmsis, log):
284
263
"-c" ,
285
264
"--core" ,
286
265
metavar = "core_path" ,
287
- help = "Root path of the STM32 core. Default: " + core_path ,
266
+ help = "Root path of the STM32 core. Default: {}" . format ( core_path ) ,
288
267
)
289
268
wrapparser .add_argument (
290
269
"-s" ,
291
270
"--cmsis" ,
292
271
metavar = "cmsis_path" ,
293
- help = "Root path of the CMSIS. Default: " + CMSIS_path ,
272
+ help = "Root path of the CMSIS. Default: {}" . format ( CMSIS_path ) ,
294
273
)
295
274
296
275
wrapargs = wrapparser .parse_args ()
0 commit comments