Skip to content

Commit c5aabae

Browse files
committed
Synchronize PlatformIO build script according to changes in v2.0
1 parent 75d6531 commit c5aabae

File tree

2 files changed

+140
-32
lines changed

2 files changed

+140
-32
lines changed

tools/platformio/boards_remap.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"adafruit_feather_f405": "FEATHER_F405",
3+
"blackpill_f103c8_128": "BLACKPILL_F103C8",
4+
"blackpill_f401ce": "BLACKPILL_F401CC",
5+
"coreboard_f401rc": "CoreBoard_F401RC",
6+
"disco_g031j6": "DISCO_G0316",
7+
"disco_l072cz_lrwan1": "B_L072Z_LRWAN1",
8+
"disco_l475vg_iot01a": "B_L475E_IOT01A",
9+
"disco_l4s5i_iot01a": "B_L4S5I_IOT01A",
10+
"electrosmith_daisy": "DAISY_SEED",
11+
"maple_mini_origin": "MAPLEMINI_F103CB",
12+
"microduino32_flash": "GENERIC_F103C8TX",
13+
"netduino2plus": "GENERIC_F405RGTX",
14+
"nucleo_wb55rg_p": "P_NUCLEO_WB55RG",
15+
"olimex_e407": "GENERIC_F407ZGTX",
16+
"olimex_f103": "GENERIC_F103R8TX",
17+
"olimex_h407": "GENERIC_F407ZGTX",
18+
"olimex_p405": "GENERIC_F405RGTX",
19+
"olimexino": "GENERIC_F103RBTX",
20+
"piconomix_px_her0": "PX_HER0",
21+
"rak811_tracker_32": "RAK811_TRACKERA",
22+
"robotdyn_blackpill_f303cc": "BLACKPILL_F303CC",
23+
"rumba32_f446ve": "RUMBA32",
24+
"sparky_v1": "SPARKY_F303CC",
25+
"steval_mksboxv1": "STEVAL_MKSBOX1V1",
26+
"stm32f4stamp": "GENERIC_F405RGTX",
27+
"thunder_pack": "THUNDERPACK_L072",
28+
"thunder_pack_f411": "THUNDERPACK_F411",
29+
"vccgnd_f103zet6": "VCCGND_F103ZET6_MINI",
30+
"waveshare_open103z": "GENERIC_F103ZEHX"
31+
}

tools/platformio-build.py renamed to tools/platformio/platformio-build.py

+109-32
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,29 @@
2222
https://github.com/stm32duino/Arduino_Core_STM32
2323
"""
2424

25-
25+
import json
2626
from os.path import isfile, isdir, join
2727

2828
from SCons.Script import DefaultEnvironment
2929

3030
env = DefaultEnvironment()
3131
platform = env.PioPlatform()
32-
board = env.BoardConfig()
32+
board_config = env.BoardConfig()
3333

3434
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoststm32")
3535
CMSIS_DIR = join(platform.get_package_dir("framework-cmsis"), "CMSIS")
3636
assert isdir(FRAMEWORK_DIR)
3737
assert 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", "")
4241
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"))
4444
series = mcu_type[:7].upper() + "xx"
4545
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", "")
4848
else join(FRAMEWORK_DIR, "variants")
4949
)
5050
variant_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

109109
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:
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

119121
def 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+
153204
env.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

294358
configure_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

331405
libs = []
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

341418
env.BuildSources(
342419
join("$BUILD_DIR", "SrcWrapper"), join(FRAMEWORK_DIR, "libraries", "SrcWrapper")

0 commit comments

Comments
 (0)