2222https://github.com/stm32duino/Arduino_Core_STM32
2323"""
2424
25-
25+ import json
2626from os .path import isfile , isdir , join
2727
2828from SCons .Script import DefaultEnvironment
2929
3030env = DefaultEnvironment ()
3131platform = env .PioPlatform ()
32- board = env .BoardConfig ()
32+ board_config = env .BoardConfig ()
3333
3434FRAMEWORK_DIR = platform .get_package_dir ("framework-arduinoststm32" )
3535CMSIS_DIR = join (platform .get_package_dir ("framework-cmsis" ), "CMSIS" )
3636assert isdir (FRAMEWORK_DIR )
3737assert isdir (CMSIS_DIR )
3838
3939
40- mcu = env .BoardConfig ().get ("build.mcu" , "" )
41- board_name = env .subst ("$BOARD" )
40+ mcu = board_config .get ("build.mcu" , "" )
4241mcu_type = mcu [:- 2 ]
43- variant = board .get ("build.variant" )
42+ variant = board_config .get (
43+ "build.variant" , board_config .get ("build.arduino.variant" , "generic" ))
4444series = mcu_type [:7 ].upper () + "xx"
4545variants_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" , "" )
4848 else join (FRAMEWORK_DIR , "variants" )
4949)
5050variant_dir = join (variants_dir , variant )
@@ -97,8 +97,8 @@ def process_usb_configuration(cpp_defines):
9797 env .Append (
9898 CPPDEFINES = [
9999 "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 ]),
102102 ]
103103 )
104104
@@ -107,13 +107,15 @@ def process_usb_configuration(cpp_defines):
107107
108108
109109def 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 :
112114 return "arm_cortexM4lf_math"
113- elif core == "m7" :
115+ elif "m7" in core :
114116 return "arm_cortexM7lfsp_math"
115117
116- return "arm_cortex%sl_math" % core .upper ()
118+ return "arm_cortex%sl_math" % core [ 7 : 9 ] .upper ()
117119
118120
119121def configure_application_offset (mcu , upload_protocol ):
@@ -131,7 +133,7 @@ def configure_application_offset(mcu, upload_protocol):
131133 # STM32F103 series doesn't have embedded DFU over USB
132134 # stm32duino bootloader (v1, v2) is used instead
133135 if mcu .startswith ("stm32f103" ):
134- if board .get ("upload.boot_version" , 2 ) == 1 :
136+ if board_config .get ("upload.boot_version" , 2 ) == 1 :
135137 offset = 0x5000
136138 else :
137139 offset = 0x2000
@@ -144,12 +146,61 @@ def configure_application_offset(mcu, upload_protocol):
144146 env .Append (LINKFLAGS = ["-Wl,--defsym=LD_FLASH_OFFSET=%s" % hex (offset )])
145147
146148
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" )):
148150 env .Append (
149151 CCFLAGS = ["-mfpu=fpv4-sp-d16" , "-mfloat-abi=hard" ],
150152 LINKFLAGS = ["-mfpu=fpv4-sp-d16" , "-mfloat-abi=hard" ],
151153 )
152154
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+
153204env .Append (
154205 ASFLAGS = ["-x" , "assembler-with-cpp" ],
155206 CFLAGS = ["-std=gnu11" ],
@@ -162,11 +213,10 @@ def configure_application_offset(mcu, upload_protocol):
162213 ],
163214 CCFLAGS = [
164215 "-Os" , # optimize for size
165- "-mcpu=%s" % env . BoardConfig () .get ("build.cpu" ),
216+ "-mcpu=%s" % board_config .get ("build.cpu" ),
166217 "-mthumb" ,
167218 "-ffunction-sections" , # place each function in its own section
168219 "-fdata-sections" ,
169- "-Wall" ,
170220 "-nostdlib" ,
171221 "--param" ,
172222 "max-inline-insns-single=500" ,
@@ -175,9 +225,23 @@ def configure_application_offset(mcu, upload_protocol):
175225 series ,
176226 ("ARDUINO" , 10808 ),
177227 "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 ),
180230 "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+ ),
181245 ],
182246 CPPPATH = [
183247 join (FRAMEWORK_DIR , "cores" , "arduino" , "avr" ),
@@ -263,45 +327,55 @@ def configure_application_offset(mcu, upload_protocol):
263327 "gcc" ,
264328 ),
265329 join (CMSIS_DIR , "DSP" , "Include" ),
330+ join (CMSIS_DIR , "DSP" , "PrivateInclude" ),
266331 join (FRAMEWORK_DIR , "cores" , "arduino" ),
267- variant_dir ,
268332 ],
269333 LINKFLAGS = [
270334 "-Os" ,
271335 "-mthumb" ,
272- "-mcpu=%s" % env . BoardConfig () .get ("build.cpu" ),
336+ "-mcpu=%s" % board_config .get ("build.cpu" ),
273337 "--specs=nano.specs" ,
274338 "-Wl,--gc-sections,--relax" ,
275339 "-Wl,--check-sections" ,
276340 "-Wl,--entry=Reset_Handler" ,
277341 "-Wl,--unresolved-symbols=report-all" ,
278342 "-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" ),
281345 ],
282346 LIBS = [
283- get_arm_math_lib (env . BoardConfig () .get ("build.cpu" )),
347+ get_arm_math_lib (board_config .get ("build.cpu" )),
284348 "c" ,
285349 "m" ,
286350 "gcc" ,
287351 "stdc++" ,
288352 ],
289- LIBPATH = [variant_dir , join (CMSIS_DIR , "DSP" , "Lib" , "GCC" )],
353+ LIBPATH = [join (CMSIS_DIR , "DSP" , "Lib" , "GCC" )],
290354)
291355
292- env .ProcessFlags (board .get ("build.framework_extra_flags.arduino" , "" ))
356+ env .ProcessFlags (board_config .get ("build.framework_extra_flags.arduino" , "" ))
293357
294358configure_application_offset (mcu , upload_protocol )
295359
296360#
297361# Linker requires preprocessing with correct RAM|ROM sizes
298362#
299363
300- if not board .get ("build.ldscript" , "" ):
364+ if not board_config .get ("build.ldscript" , "" ):
301365 env .Replace (LDSCRIPT_PATH = join (FRAMEWORK_DIR , "system" , "ldscript.ld" ))
302366 if not isfile (join (env .subst (variant_dir ), "ldscript.ld" )):
303367 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+ )
305379
306380#
307381# Process configuration flags
@@ -330,13 +404,16 @@ def configure_application_offset(mcu, upload_protocol):
330404
331405libs = []
332406
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+ )
335412 env .BuildSources (join ("$BUILD_DIR" , "FrameworkArduinoVariant" ), variant_dir )
336413
337- env .BuildSources (
414+ libs . append ( env .BuildLibrary (
338415 join ("$BUILD_DIR" , "FrameworkArduino" ), join (FRAMEWORK_DIR , "cores" , "arduino" )
339- )
416+ ))
340417
341418env .BuildSources (
342419 join ("$BUILD_DIR" , "SrcWrapper" ), join (FRAMEWORK_DIR , "libraries" , "SrcWrapper" )
0 commit comments