Skip to content

Commit 6e31bb8

Browse files
committed
Improve logic for finding STM32Cube linker scripts
1 parent 0829e59 commit 6e31bb8

File tree

5 files changed

+44
-18
lines changed

5 files changed

+44
-18
lines changed

boards/disco_h735ig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"cpu": "cortex-m7",
44
"extra_flags": "-DSTM32H7xx -DSTM32H735xx",
55
"f_cpu": "550000000L",
6-
"mcu": "stm32h735igk6u",
6+
"mcu": "stm32h735igk6",
77
"product_line": "STM32H735xx",
88
"zephyr": {
99
"variant": "stm32h735g_disco"

boards/olimexino_stm32f3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cpu": "cortex-m3",
55
"extra_flags": "-DSTM32F303xC",
66
"f_cpu": "72000000L",
7-
"mcu": "stm32f303rct6tr",
7+
"mcu": "stm32f303rct6",
88
"variant": "STM32F3xx/F303R(B-C)T",
99
"product_line": "STM32F303xC"
1010
},

boards/prntr_v2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"framework_extra_flags": {
88
"arduino": "-DVECT_TAB_OFFSET=0x8000 -DCUSTOM_PERIPHERAL_PINS"
99
},
10-
"mcu": "stm32f407re",
10+
"mcu": "stm32f407vet6",
1111
"product_line": "STM32F407xx",
1212
"variant": "STM32F4xx/F407V(E-G)T_F417V(E-G)T"
1313
},

boards/st3dp001_eval.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"framework_extra_flags": {
88
"arduino": "-DCUSTOM_PERIPHERAL_PINS"
99
},
10-
"mcu": "stm32f401vgt6",
10+
"mcu": "stm32f401vet6",
1111
"product_line": "STM32F401xE",
1212
"variant": "STM32F4xx/F401V(B-C-D-E)T"
1313
},

builder/frameworks/stm32cube.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
http://www.st.com/en/embedded-software/stm32cube-embedded-software.html?querycriteria=productId=LN1897
2323
"""
2424

25-
import glob
2625
import os
2726
import shutil
2827
import string
2928
import sys
29+
import re
3030

31-
from SCons.Script import DefaultEnvironment
31+
from SCons.Script import ARGUMENTS, DefaultEnvironment
3232

3333
from platformio.builder.tools.piolib import PlatformIOLibBuilder
3434

@@ -72,25 +72,50 @@ def generate_ldscript(default_ldscript_path):
7272
fp.write(content)
7373

7474

75-
def get_linker_script(board_mcu):
76-
ldscript_match = glob.glob(
77-
os.path.join(
78-
LDSCRIPTS_DIR, board_mcu[0:7], board_mcu[0:11].upper() + "*_FLASH.ld"
79-
)
80-
)
75+
def get_linker_script(board_mcu, board_cpu):
76+
def _glob_re(pattern, ldscripts):
77+
re_c = re.compile(pattern)
78+
return list(filter(re_c.match, ldscripts))
8179

82-
if ldscript_match and os.path.isfile(ldscript_match[0]):
83-
return ldscript_match[0]
80+
if len(board_mcu) > 12:
81+
board_mcu = board_mcu[:12] + "X" + board_mcu[13:]
8482

85-
default_ldscript = os.path.join(
86-
LDSCRIPTS_DIR, board_mcu[0:7], board_mcu[0:11].upper() + "_DEFAULT.ld"
83+
family_ldscripts_dir = os.path.join(LDSCRIPTS_DIR, board_mcu[0:7])
84+
85+
ldscript_matches = _glob_re(
86+
"^%s.*_FLASH\\.ld$" % board_mcu.upper(),
87+
os.listdir(family_ldscripts_dir),
8788
)
8889

90+
if ldscript_matches:
91+
ldscript_file = ldscript_matches[0]
92+
if len(ldscript_matches) > 1:
93+
# Precise match with the CPU in filename has the highest priority
94+
board_cpu = board_cpu.replace("cortex-", "").upper()
95+
for match in ldscript_matches:
96+
if board_cpu in match:
97+
ldscript_file = match
98+
break
99+
100+
if int(ARGUMENTS.get("PIOVERBOSE", 0)):
101+
print(
102+
"Found suitable linker scripts: ["
103+
+ ", ".join(ldscript_matches)
104+
+ "], %s will be used!" % ldscript_file
105+
)
106+
107+
return os.path.join(family_ldscripts_dir, ldscript_file)
108+
109+
# Fall back to an auto-generated linker script
89110
print(
90111
"Warning! Cannot find a linker script for the required board! "
91112
"An auto-generated script will be used to link firmware!"
92113
)
93114

115+
default_ldscript = os.path.join(
116+
LDSCRIPTS_DIR, board_mcu[0:7], board_mcu[0:11].upper() + "_DEFAULT.ld"
117+
)
118+
94119
if not os.path.isfile(default_ldscript):
95120
generate_ldscript(default_ldscript)
96121

@@ -252,7 +277,7 @@ def process_dsp_lib():
252277
],
253278

254279
LIBPATH=[
255-
os.path.join(FRAMEWORK_DIR, "platformio", "ldscripts"),
280+
os.path.join(LDSCRIPTS_DIR, MCU_FAMILY),
256281
],
257282

258283
LIBS=["c", "gcc", "m", "stdc++", "nosys"],
@@ -262,7 +287,8 @@ def process_dsp_lib():
262287
env.Append(ASFLAGS=env.get("CCFLAGS", [])[:])
263288

264289
if not board.get("build.ldscript", ""):
265-
env.Replace(LDSCRIPT_PATH=get_linker_script(board.get("build.mcu", "")))
290+
env.Replace(LDSCRIPT_PATH=get_linker_script(
291+
board.get("build.mcu", ""), board.get("build.cpu", "")))
266292

267293
#
268294
# Process BSP components

0 commit comments

Comments
 (0)