Skip to content

Commit 5e40b65

Browse files
gautierg-stkartben
authored andcommitted
scripts: stm32cubeprogrammer: Add possibility to flash a bin file
Add the option to flash a bin file if one is available from the board's cmake configuration. Use of elf has prio over this, and bin has prio over hex which remains the default option. Also add a parameter to stm32cubeprogrammer runner to specify the download address. Signed-off-by: Guillaume Gautier <[email protected]>
1 parent 25dea79 commit 5e40b65

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

scripts/west_commands/runners/stm32cubeprogrammer.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(
3434
port: str,
3535
frequency: int | None,
3636
reset_mode: str | None,
37+
download_address: int | None,
3738
start_address: int | None,
3839
conn_modifiers: str | None,
3940
cli: Path | None,
@@ -46,6 +47,7 @@ def __init__(
4647

4748
self._port = port
4849
self._frequency = frequency
50+
self._download_address = download_address
4951
self._start_address = start_address
5052
self._reset_mode = reset_mode
5153
self._conn_modifiers = conn_modifiers
@@ -149,6 +151,15 @@ def do_add_parser(cls, parser):
149151
choices=["sw", "hw", "core"],
150152
help="Reset mode",
151153
)
154+
parser.add_argument(
155+
"--download-address",
156+
# To accept arguments in hex format, a wrapper lambda around int() must be used.
157+
# Wrapping the lambda with functools.wraps() makes it so that 'invalid int value'
158+
# is displayed when an invalid value is provided for this argument.
159+
type=functools.wraps(int)(lambda s: int(s, base=0)),
160+
required=False,
161+
help="Address where flashing should be done"
162+
)
152163
parser.add_argument(
153164
"--start-address",
154165
# To accept arguments in hex format, a wrapper lambda around int() must be used.
@@ -194,6 +205,7 @@ def do_create(
194205
port=args.port,
195206
frequency=args.frequency,
196207
reset_mode=args.reset_mode,
208+
download_address=args.download_address,
197209
start_address=args.start_address,
198210
conn_modifiers=args.conn_modifiers,
199211
cli=args.cli,
@@ -233,13 +245,22 @@ def flash(self, **kwargs) -> None:
233245
self.check_call(cmd + ["--erase", "all"])
234246

235247
# flash image and run application
236-
dl_file = self.cfg.elf_file if self._use_elf else self.cfg.hex_file
248+
if self._use_elf:
249+
dl_file = self.cfg.elf_file
250+
elif self.cfg.bin_file is not None and os.path.isfile(self.cfg.bin_file) and \
251+
"zephyr.signed" in self.cfg.bin_file:
252+
dl_file = self.cfg.bin_file
253+
elif self.cfg.hex_file is not None and os.path.isfile(self.cfg.hex_file):
254+
# --user-elf not used and no bin file given, default to hex
255+
dl_file = self.cfg.hex_file
237256
if dl_file is None:
238257
raise RuntimeError('cannot flash; no download file was specified')
239258
elif not os.path.isfile(dl_file):
240259
raise RuntimeError(f'download file {dl_file} does not exist')
241260

242261
flash_and_run_args = ["--download", dl_file]
262+
if self._download_address is not None:
263+
flash_and_run_args.append(f"0x{self._download_address:X}")
243264

244265
# '--start' is needed to start execution after flash.
245266
# The default start address is the beggining of the flash,

scripts/west_commands/tests/test_stm32cubeprogrammer.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
"port": "swd",
6565
"frequency": None,
6666
"reset_mode": None,
67+
"download_address": None,
6768
"start_address": None,
6869
"conn_modifiers": None,
6970
"cli": CLI_PATH,
@@ -88,6 +89,7 @@
8889
"port": "swd",
8990
"frequency": None,
9091
"reset_mode": None,
92+
"download_address": None,
9193
"start_address": 0x8001000,
9294
"conn_modifiers": None,
9395
"cli": CLI_PATH,
@@ -113,6 +115,7 @@
113115
"port": "swd",
114116
"frequency": "4000",
115117
"reset_mode": None,
118+
"download_address": None,
116119
"start_address": None,
117120
"conn_modifiers": None,
118121
"cli": CLI_PATH,
@@ -137,6 +140,7 @@
137140
"port": "swd",
138141
"frequency": None,
139142
"reset_mode": "hw",
143+
"download_address": None,
140144
"start_address": None,
141145
"conn_modifiers": None,
142146
"cli": CLI_PATH,
@@ -161,6 +165,7 @@
161165
"port": "swd",
162166
"frequency": None,
163167
"reset_mode": "sw",
168+
"download_address": None,
164169
"start_address": None,
165170
"conn_modifiers": None,
166171
"cli": CLI_PATH,
@@ -185,6 +190,7 @@
185190
"port": "swd",
186191
"frequency": None,
187192
"reset_mode": "core",
193+
"download_address": None,
188194
"start_address": None,
189195
"conn_modifiers": None,
190196
"cli": CLI_PATH,
@@ -209,6 +215,7 @@
209215
"port": "swd",
210216
"frequency": None,
211217
"reset_mode": None,
218+
"download_address": None,
212219
"start_address": None,
213220
"conn_modifiers": "br=115200 sn=TEST",
214221
"cli": CLI_PATH,
@@ -233,6 +240,7 @@
233240
"port": "swd",
234241
"frequency": None,
235242
"reset_mode": None,
243+
"download_address": None,
236244
"start_address": None,
237245
"conn_modifiers": None,
238246
"cli": CLI_PATH,
@@ -257,6 +265,7 @@
257265
"port": "swd",
258266
"frequency": None,
259267
"reset_mode": None,
268+
"download_address": None,
260269
"start_address": None,
261270
"conn_modifiers": None,
262271
"cli": CLI_PATH,
@@ -282,6 +291,7 @@
282291
"port": "swd",
283292
"frequency": None,
284293
"reset_mode": None,
294+
"download_address": None,
285295
"start_address": None,
286296
"conn_modifiers": None,
287297
"cli": CLI_PATH,
@@ -307,6 +317,7 @@
307317
"port": "swd",
308318
"frequency": None,
309319
"reset_mode": None,
320+
"download_address": None,
310321
"start_address": None,
311322
"conn_modifiers": None,
312323
"cli": None,
@@ -331,6 +342,7 @@
331342
"port": "swd",
332343
"frequency": None,
333344
"reset_mode": None,
345+
"download_address": None,
334346
"start_address": None,
335347
"conn_modifiers": None,
336348
"cli": None,
@@ -355,6 +367,7 @@
355367
"port": "swd",
356368
"frequency": None,
357369
"reset_mode": None,
370+
"download_address": None,
358371
"start_address": None,
359372
"conn_modifiers": None,
360373
"cli": None,
@@ -375,6 +388,32 @@
375388
],
376389
],
377390
},
391+
{
392+
"port": "swd",
393+
"frequency": None,
394+
"reset_mode": None,
395+
"download_address": 0x80000000,
396+
"start_address": None,
397+
"conn_modifiers": None,
398+
"cli": CLI_PATH,
399+
"use_elf": False,
400+
"erase": False,
401+
"extload": None,
402+
"tool_opt": [],
403+
"system": "",
404+
"cli_path": str(CLI_PATH),
405+
"calls": [
406+
[
407+
str(CLI_PATH),
408+
"--connect",
409+
"port=swd",
410+
"--download",
411+
RC_KERNEL_HEX,
412+
"0x80000000",
413+
"--start",
414+
],
415+
],
416+
},
378417
)
379418
"""Test cases."""
380419

@@ -400,6 +439,7 @@ def test_stm32cubeprogrammer_init(
400439
port=tc["port"],
401440
frequency=tc["frequency"],
402441
reset_mode=tc["reset_mode"],
442+
download_address=tc["download_address"],
403443
start_address=tc["start_address"],
404444
conn_modifiers=tc["conn_modifiers"],
405445
cli=tc["cli"],
@@ -436,6 +476,8 @@ def test_stm32cubeprogrammer_create(
436476
args.extend(["--frequency", tc["frequency"]])
437477
if tc["reset_mode"]:
438478
args.extend(["--reset-mode", tc["reset_mode"]])
479+
if tc["download_address"]:
480+
args.extend(["--download-address", str(tc["download_address"])])
439481
if tc["start_address"]:
440482
args.extend(["--start-address", str(tc["start_address"])])
441483
if tc["conn_modifiers"]:

0 commit comments

Comments
 (0)