Skip to content

Commit ee6a512

Browse files
andryblacksalkinium
authored andcommitted
[build] Add ELF2UF2 convertion tool
1 parent c468fa5 commit ee6a512

File tree

11 files changed

+384
-2
lines changed

11 files changed

+384
-2
lines changed

tools/build_script_generator/make/module.lb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ def post_build(env):
5252
if subs["core"].startswith("cortex-m"):
5353
# get memory information
5454
subs["memories"] = env.query("::memories")
55+
subs["uf2mem"] = ["0x{}:0x{}:{}".format(hex(m["start"]), hex(m["start"] + m["size"]),
56+
"CONTENTS" if "flash" in m["name"] else "NO_CONTENTS")
57+
for m in subs["memories"]]
5558
else:
5659
subs["memories"] = []
5760
# Add SCons specific data

tools/build_script_generator/make/module.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,23 @@ Hex File······· {debug|release}/blink.hex
485485
```
486486

487487

488+
#### make uf2
489+
490+
```
491+
make uf2 profile={debug|release} [firmware={hash or file}]
492+
```
493+
494+
Creates a UF2 compatible file of your executable. UF2 is a
495+
[bootloader by Microsoft](https://github.com/microsoft/uf2).
496+
497+
```
498+
$ make uf2
499+
UF2 File······· {debug|release}/blink.uf2
500+
```
501+
502+
(\* *only ARM Cortex-M targets*)
503+
504+
488505
## Information Tool
489506

490507
This tool generates a set of header files containing information about the

tools/build_script_generator/make/resources/Makefile.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ delay?=5
108108
program-dfu: bin
109109
@dfu-util -v -E$(delay) -R -i 0 -a 0 -s 0x08000000:leave -D $(BIN_FILE)
110110

111+
.PHONY: uf2
112+
uf2: build
113+
@echo "UF2 File·······" $(UF2_FILE)
114+
@python3 $(MODM_PATH)/modm_tools/elf2uf2.py $(ELF_FILE) -o $(UF2_FILE) \
115+
--target $(CONFIG_DEVICE_NAME) \
116+
%% for range in uf2mem
117+
--range {{range}}{% if not loop.last %} \{% endif %}
118+
%% endfor
119+
%#
111120
%% if platform in ["sam"]
112121
.PHONY: program-bossac
113122
program-bossac: bin

tools/build_script_generator/make/resources/config.mk.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ BIN_FILE := $(BUILDPATH)/$(MODM_PROJECT_NAME).bin
1616
HEX_FILE := $(BUILDPATH)/$(MODM_PROJECT_NAME).hex
1717
LSS_FILE := $(BUILDPATH)/$(MODM_PROJECT_NAME).lss
1818
MAP_FILE := $(BUILDPATH)/$(MODM_PROJECT_NAME).map
19+
UF2_FILE := $(BUILDPATH)/$(MODM_PROJECT_NAME).uf2
1920
CLEAN_FILES += $(BUILDPATH)
2021

2122
# Device configuration

tools/build_script_generator/module.lb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def build(env):
206206
tools.add("unit_test")
207207
if is_cortex_m:
208208
tools.update({"bmp", "openocd", "crashdebug", "gdb", "backend",
209-
"log", "build_id", "size"})
209+
"log", "build_id", "size", "elf2uf2"})
210210
if platform in ["sam"]:
211211
tools.update({"bossac"})
212212
elif platform in ["avr"]:

tools/build_script_generator/scons/module.lb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def build(env):
102102
device = env.query("::device")
103103
if device["core"].startswith("cortex-m"):
104104
tools.update({"size", "log_cortex", "artifact", "openocd",
105-
"openocd_remote", "bmp", "crashdebug", "dfu"})
105+
"elf2uf2", "openocd_remote", "bmp", "crashdebug", "dfu"})
106106
if device["platform"] in ["sam"]:
107107
tools.update({"bossac"})
108108
elif device["core"].startswith("avr"):

tools/build_script_generator/scons/module.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,23 @@ Hex File······· {debug|release}/blink.hex
557557
```
558558

559559

560+
#### scons uf2
561+
562+
```
563+
scons uf2 profile={debug|release} [firmware={hash or file}]
564+
```
565+
566+
Creates a UF2 compatible file of your executable. UF2 is a
567+
[bootloader by Microsoft](https://github.com/microsoft/uf2).
568+
569+
```
570+
$ scons uf2
571+
UF2 File······· {debug|release}/blink.uf2
572+
```
573+
574+
(\* *only ARM Cortex-M targets*)
575+
576+
560577
#### scons artifact
561578

562579
```

tools/build_script_generator/scons/resources/build_target.py.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def build_target(env, sources):
2020
env.Clean(program, join(env["BUILDPATH"], env["CONFIG_PROJECT_NAME"]+".bin"))
2121
env.Clean(program, join(env["BUILDPATH"], env["CONFIG_PROJECT_NAME"]+".hex"))
2222
env.Clean(program, join(env["BUILDPATH"], env["CONFIG_PROJECT_NAME"]+".lss"))
23+
env.Clean(program, join(env["BUILDPATH"], env["CONFIG_PROJECT_NAME"]+".uf2"))
2324

2425
%% if with_compilation_db
2526
env.Command("compile_commands.json", sources,
@@ -42,6 +43,8 @@ def build_target(env, sources):
4243
env.Depends(target=program, dependency="{{ linkerscript }}")
4344
env.Alias("size", env.Size(chosen_program))
4445
%% if core.startswith("cortex-m")
46+
env.Alias("uf2", env.UF2(chosen_program))
47+
4548
env.Alias("log-itm", env.LogItmOpenOcd())
4649
env.Alias("log-rtt", env.LogRttOpenOcd())
4750

tools/build_script_generator/scons/site_tools/comstr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def generate(env, **kw):
7070
env['HEXCOMSTR'] = "%sHex File······· %s$TARGET%s" % default
7171
env['BINCOMSTR'] = "%sBinary File···· %s$TARGET%s" % default
7272
env['LSSCOMSTR'] = "%sListing········ %s$TARGET%s" % default
73+
env['UF2COMSTR'] = "%sUF2 File······· %s$TARGET%s" % default
7374

7475
# modm tools format strings
7576
env['BITMAPCOMSTR'] = "%sBitmap········· %s${str(TARGET).replace(BUILDPATH,CONFIG_PROFILE)}%s" % default
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2016-2017, German Aerospace Center (DLR)
5+
# Copyright (c) 2018, Niklas Hauser
6+
#
7+
# This Source Code Form is subject to the terms of the Mozilla Public
8+
# License, v. 2.0. If a copy of the MPL was not distributed with this
9+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
10+
#
11+
# Authors:
12+
# - 2022, Andrey Kunitsyn
13+
14+
import os
15+
16+
from SCons.Script import *
17+
from modm_tools import elf2uf2
18+
19+
def elf2uf2_action(target, source, env):
20+
memranges = [{
21+
"start": m["start"],
22+
"end": m["start"] + m["size"],
23+
"type": "CONTENTS" if "flash" in m["name"] else "NO_CONTENTS"
24+
}
25+
for m in env["CONFIG_DEVICE_MEMORY"]]
26+
elf2uf2.convert(str(source[0]),
27+
str(target[0]),
28+
env["CONFIG_DEVICE_NAME"],
29+
memranges)
30+
31+
def generate(env, **kw):
32+
builder_elf2uf2 = Builder(
33+
action=Action(elf2uf2_action, cmdstr="$UF2COMSTR"),
34+
suffix=".uf2",
35+
src_suffix=".elf")
36+
37+
env.Append(BUILDERS={
38+
"UF2": builder_elf2uf2,
39+
})
40+
41+
42+
def exists(env):
43+
return True
44+

0 commit comments

Comments
 (0)