22
22
https://github.com/stm32duino/Arduino_Core_STM32
23
23
"""
24
24
25
-
25
+ import json
26
26
from os .path import isfile , isdir , join
27
27
28
28
from SCons .Script import DefaultEnvironment
29
29
30
30
env = DefaultEnvironment ()
31
31
platform = env .PioPlatform ()
32
- board = env .BoardConfig ()
32
+ board_config = env .BoardConfig ()
33
33
34
34
FRAMEWORK_DIR = platform .get_package_dir ("framework-arduinoststm32" )
35
35
CMSIS_DIR = join (platform .get_package_dir ("framework-cmsis" ), "CMSIS" )
36
36
assert isdir (FRAMEWORK_DIR )
37
37
assert isdir (CMSIS_DIR )
38
38
39
39
40
- mcu = env .BoardConfig ().get ("build.mcu" , "" )
41
- board_name = env .subst ("$BOARD" )
40
+ mcu = board_config .get ("build.mcu" , "" )
42
41
mcu_type = mcu [:- 2 ]
43
- variant = board .get ("build.variant" )
42
+ variant = board_config .get (
43
+ "build.variant" , board_config .get ("build.arduino.variant" , "generic" ))
44
44
series = mcu_type [:7 ].upper () + "xx"
45
45
variants_dir = (
46
- join ("$PROJECT_DIR" , board .get ("build.variants_dir" ))
47
- if board .get ("build.variants_dir" , "" )
46
+ join ("$PROJECT_DIR" , board_config .get ("build.variants_dir" ))
47
+ if board_config .get ("build.variants_dir" , "" )
48
48
else join (FRAMEWORK_DIR , "variants" )
49
49
)
50
50
variant_dir = join (variants_dir , variant )
@@ -97,8 +97,8 @@ def process_usb_configuration(cpp_defines):
97
97
env .Append (
98
98
CPPDEFINES = [
99
99
"USBCON" ,
100
- ("USB_VID" , board .get ("build.hwids" , [[0 , 0 ]])[0 ][0 ]),
101
- ("USB_PID" , board .get ("build.hwids" , [[0 , 0 ]])[0 ][1 ]),
100
+ ("USB_VID" , board_config .get ("build.hwids" , [[0 , 0 ]])[0 ][0 ]),
101
+ ("USB_PID" , board_config .get ("build.hwids" , [[0 , 0 ]])[0 ][1 ]),
102
102
]
103
103
)
104
104
@@ -107,13 +107,15 @@ def process_usb_configuration(cpp_defines):
107
107
108
108
109
109
def get_arm_math_lib (cpu ):
110
- core = board .get ("build.cpu" )[7 :9 ]
111
- if core == "m4" :
110
+ core = board_config .get ("build.cpu" )
111
+ if "m33" in core :
112
+ return "arm_ARMv8MMLlfsp_math"
113
+ elif "m4" in core :
112
114
return "arm_cortexM4lf_math"
113
- elif core == "m7" :
115
+ elif "m7" in core :
114
116
return "arm_cortexM7lfsp_math"
115
117
116
- return "arm_cortex%sl_math" % core .upper ()
118
+ return "arm_cortex%sl_math" % core [ 7 : 9 ] .upper ()
117
119
118
120
119
121
def configure_application_offset (mcu , upload_protocol ):
@@ -131,7 +133,7 @@ def configure_application_offset(mcu, upload_protocol):
131
133
# STM32F103 series doesn't have embedded DFU over USB
132
134
# stm32duino bootloader (v1, v2) is used instead
133
135
if mcu .startswith ("stm32f103" ):
134
- if board .get ("upload.boot_version" , 2 ) == 1 :
136
+ if board_config .get ("upload.boot_version" , 2 ) == 1 :
135
137
offset = 0x5000
136
138
else :
137
139
offset = 0x2000
@@ -144,12 +146,61 @@ def configure_application_offset(mcu, upload_protocol):
144
146
env .Append (LINKFLAGS = ["-Wl,--defsym=LD_FLASH_OFFSET=%s" % hex (offset )])
145
147
146
148
147
- if any (mcu in board .get ("build.cpu" ) for mcu in ("cortex-m4" , "cortex-m7" )):
149
+ if any (mcu in board_config .get ("build.cpu" ) for mcu in ("cortex-m4" , "cortex-m7" )):
148
150
env .Append (
149
151
CCFLAGS = ["-mfpu=fpv4-sp-d16" , "-mfloat-abi=hard" ],
150
152
LINKFLAGS = ["-mfpu=fpv4-sp-d16" , "-mfloat-abi=hard" ],
151
153
)
152
154
155
+
156
+ def load_boards_remap ():
157
+ remap_file = join (FRAMEWORK_DIR , "tools" , "platformio" , "boards_remap.json" )
158
+ if not isfile (remap_file ):
159
+ print ("Warning! Couldn't find board remap file!" )
160
+ return {}
161
+
162
+ with open (remap_file , "r" ) as fp :
163
+ try :
164
+ return json .load (fp )
165
+ except :
166
+ print ("Warning! Failed to parse board remap file!" )
167
+ return {}
168
+
169
+
170
+ def get_arduino_board_id (board_config , mcu ):
171
+ # User-specified value
172
+ if board_config .get ("build.arduino.board" , "" ):
173
+ return board_config .get ("build.arduino.board" )
174
+
175
+ # Default boards
176
+ boards_remap = load_boards_remap ()
177
+ board_id = env .subst ("$BOARD" )
178
+ if board_id in boards_remap :
179
+ return boards_remap [board_id ]
180
+
181
+ # Fall back to default cases according to MCU value for generic boards
182
+ if board_id .lower ().startswith ("generic" ):
183
+ board_id = "GENERIC_"
184
+ mcu = mcu .upper ()
185
+ if len (mcu ) > 12 :
186
+ board_id += mcu [5 :12 ] + "X"
187
+ else :
188
+ if len (mcu ) > 10 :
189
+ board_id += mcu [5 :11 ] + "TX"
190
+ else :
191
+ board_id += mcu
192
+ print (
193
+ "Warning! Couldn't generate proper internal board id from the `%s` MCU "
194
+ "field! At least 12 symbols are required!" % mcu
195
+ )
196
+
197
+ print ("Falling back to `%s`." % board_id )
198
+
199
+ return board_id .upper ()
200
+
201
+
202
+ board_id = get_arduino_board_id (board_config , mcu )
203
+
153
204
env .Append (
154
205
ASFLAGS = ["-x" , "assembler-with-cpp" ],
155
206
CFLAGS = ["-std=gnu11" ],
@@ -162,11 +213,10 @@ def configure_application_offset(mcu, upload_protocol):
162
213
],
163
214
CCFLAGS = [
164
215
"-Os" , # optimize for size
165
- "-mcpu=%s" % env . BoardConfig () .get ("build.cpu" ),
216
+ "-mcpu=%s" % board_config .get ("build.cpu" ),
166
217
"-mthumb" ,
167
218
"-ffunction-sections" , # place each function in its own section
168
219
"-fdata-sections" ,
169
- "-Wall" ,
170
220
"-nostdlib" ,
171
221
"--param" ,
172
222
"max-inline-insns-single=500" ,
@@ -175,9 +225,23 @@ def configure_application_offset(mcu, upload_protocol):
175
225
series ,
176
226
("ARDUINO" , 10808 ),
177
227
"ARDUINO_ARCH_STM32" ,
178
- "ARDUINO_%s" % board_name . upper () ,
179
- ("BOARD_NAME" , '\\ "%s\\ "' % board_name . upper () ),
228
+ "ARDUINO_%s" % board_id ,
229
+ ("BOARD_NAME" , '\\ "%s\\ "' % board_id ),
180
230
"HAL_UART_MODULE_ENABLED" ,
231
+ "USE_FULL_LL_DRIVER" ,
232
+ (
233
+ "VARIANT_H" ,
234
+ '\\ "%s\\ "'
235
+ % board_config .get (
236
+ "build.arduino.variant_h" ,
237
+ "variant_%s.h"
238
+ % (
239
+ "generic"
240
+ if board_id .lower ().startswith ("generic" )
241
+ else board_id
242
+ ),
243
+ ),
244
+ ),
181
245
],
182
246
CPPPATH = [
183
247
join (FRAMEWORK_DIR , "cores" , "arduino" , "avr" ),
@@ -263,45 +327,55 @@ def configure_application_offset(mcu, upload_protocol):
263
327
"gcc" ,
264
328
),
265
329
join (CMSIS_DIR , "DSP" , "Include" ),
330
+ join (CMSIS_DIR , "DSP" , "PrivateInclude" ),
266
331
join (FRAMEWORK_DIR , "cores" , "arduino" ),
267
- variant_dir ,
268
332
],
269
333
LINKFLAGS = [
270
334
"-Os" ,
271
335
"-mthumb" ,
272
- "-mcpu=%s" % env . BoardConfig () .get ("build.cpu" ),
336
+ "-mcpu=%s" % board_config .get ("build.cpu" ),
273
337
"--specs=nano.specs" ,
274
338
"-Wl,--gc-sections,--relax" ,
275
339
"-Wl,--check-sections" ,
276
340
"-Wl,--entry=Reset_Handler" ,
277
341
"-Wl,--unresolved-symbols=report-all" ,
278
342
"-Wl,--warn-common" ,
279
- "-Wl,--defsym=LD_MAX_SIZE=%d" % board .get ("upload.maximum_size" ),
280
- "-Wl,--defsym=LD_MAX_DATA_SIZE=%d" % board .get ("upload.maximum_ram_size" ),
343
+ "-Wl,--defsym=LD_MAX_SIZE=%d" % board_config .get ("upload.maximum_size" ),
344
+ "-Wl,--defsym=LD_MAX_DATA_SIZE=%d" % board_config .get ("upload.maximum_ram_size" ),
281
345
],
282
346
LIBS = [
283
- get_arm_math_lib (env . BoardConfig () .get ("build.cpu" )),
347
+ get_arm_math_lib (board_config .get ("build.cpu" )),
284
348
"c" ,
285
349
"m" ,
286
350
"gcc" ,
287
351
"stdc++" ,
288
352
],
289
- LIBPATH = [variant_dir , join (CMSIS_DIR , "DSP" , "Lib" , "GCC" )],
353
+ LIBPATH = [join (CMSIS_DIR , "DSP" , "Lib" , "GCC" )],
290
354
)
291
355
292
- env .ProcessFlags (board .get ("build.framework_extra_flags.arduino" , "" ))
356
+ env .ProcessFlags (board_config .get ("build.framework_extra_flags.arduino" , "" ))
293
357
294
358
configure_application_offset (mcu , upload_protocol )
295
359
296
360
#
297
361
# Linker requires preprocessing with correct RAM|ROM sizes
298
362
#
299
363
300
- if not board .get ("build.ldscript" , "" ):
364
+ if not board_config .get ("build.ldscript" , "" ):
301
365
env .Replace (LDSCRIPT_PATH = join (FRAMEWORK_DIR , "system" , "ldscript.ld" ))
302
366
if not isfile (join (env .subst (variant_dir ), "ldscript.ld" )):
303
367
print ("Warning! Cannot find linker script for the current target!\n " )
304
- env .Append (LINKFLAGS = [("-Wl,--default-script" , join (variant_dir , "ldscript.ld" ))])
368
+ env .Append (
369
+ LINKFLAGS = [
370
+ (
371
+ "-Wl,--default-script" ,
372
+ '\" %s\" ' % join (
373
+ variant_dir ,
374
+ board_config .get ("build.arduino.ldscript" , "ldscript.ld" ),
375
+ ),
376
+ )
377
+ ]
378
+ )
305
379
306
380
#
307
381
# Process configuration flags
@@ -330,13 +404,16 @@ def configure_application_offset(mcu, upload_protocol):
330
404
331
405
libs = []
332
406
333
- if "build.variant" in env .BoardConfig ():
334
- env .Append (CPPPATH = [variant_dir ])
407
+ if "build.variant" in board_config :
408
+ env .Append (
409
+ CCFLAGS = '"-I%s"' % variant_dir ,
410
+ LINKFLAGS = ['"-L%s"' % variant_dir ]
411
+ )
335
412
env .BuildSources (join ("$BUILD_DIR" , "FrameworkArduinoVariant" ), variant_dir )
336
413
337
- env .BuildSources (
414
+ libs . append ( env .BuildLibrary (
338
415
join ("$BUILD_DIR" , "FrameworkArduino" ), join (FRAMEWORK_DIR , "cores" , "arduino" )
339
- )
416
+ ))
340
417
341
418
env .BuildSources (
342
419
join ("$BUILD_DIR" , "SrcWrapper" ), join (FRAMEWORK_DIR , "libraries" , "SrcWrapper" )
0 commit comments