[core] Build UF2 OTA image after linking

This commit is contained in:
Kuba Szczodrzyński
2022-06-02 14:52:03 +02:00
parent 5b4cf53d8a
commit 9c7ea46ec3
4 changed files with 90 additions and 44 deletions

View File

@@ -10,31 +10,6 @@ board = env.BoardConfig()
env.AddDefaults("realtek-ambz", "framework-realtek-amb1")
flash_addr = board.get("build.amb_flash_addr")
flash_ota1_offset = env.subst("$FLASH_OTA1_OFFSET")
flash_ota2_offset = env.subst("$FLASH_OTA2_OFFSET")
boot_all = board.get("build.amb_boot_all")
ota1_offset = hex(int(flash_addr, 16) + int(flash_ota1_offset, 16))
ota2_offset = hex(int(flash_addr, 16) + int(flash_ota2_offset, 16))
# Outputs
env.Replace(
IMG_FW="image2_all_ota1.bin",
IMG_OTA="ota_all.bin",
LINK="python ${LT_DIR}/tools/link2bin.py AMBZ xip1 xip2",
)
# Tools
# fmt: off
TOOL_DIR = join("$SDK_DIR", "component", "soc", "realtek", "8711b", "misc", "iar_utility", "common", "tools")
# fmt: on
env.Replace(
PICK=join(TOOL_DIR, "pick"),
PAD=join(TOOL_DIR, "pad"),
CHECKSUM=join(TOOL_DIR, "checksum"),
OTA=join(TOOL_DIR, "ota"),
)
# Flags
env.Append(
CFLAGS=[
@@ -295,11 +270,6 @@ env.Append(
)
),
)
actions = [
# env.VerboseAction(package_ota, "Packaging OTA image - $IMG_OTA"),
env.VerboseAction("true", f"- OTA1 flash offset: $FLASH_OTA1_OFFSET"),
env.VerboseAction("true", f"- OTA2 flash offset: $FLASH_OTA2_OFFSET"),
]
# Uploader
upload_protocol = env.subst("$UPLOAD_PROTOCOL")
@@ -327,6 +297,7 @@ else:
sys.stderr.write("Warning! Unknown upload protocol %s\n" % upload_protocol)
# Bootloader library
boot_all = board.get("build.amb_boot_all")
target_boot = env.StaticLibrary(
join("$BUILD_DIR", "boot_all"),
env.BinToObj(
@@ -339,10 +310,21 @@ env.Prepend(LIBS=[target_boot])
# Build all libraries
env.BuildLibraries()
# Main firmware binary builder
env.Append(
BUILDERS=dict(
DumpFirmwareBinary=Builder(action=actions),
),
# Main firmware outputs and actions
env.Replace(
# linker command (dual .bin outputs)
LINK="${LINK2BIN} AMBZ xip1 xip2",
# default output .bin name
IMG_FW="image_${FLASH_OTA1_OFFSET}.ota1.bin",
# UF2OTA input list
UF2OTA=[
(
"ota1",
"${BUILD_DIR}/image_${FLASH_OTA1_OFFSET}.ota1.bin",
"ota2",
"${BUILD_DIR}/image_${FLASH_OTA2_OFFSET}.ota2.bin",
),
],
# uploader
UPLOAD_ACTIONS=upload_actions,
)

View File

@@ -1,5 +1,7 @@
# Copyright (c) Kuba Szczodrzyński 2022-04-20.
import sys
from SCons.Script import Default, DefaultEnvironment
env = DefaultEnvironment()
@@ -7,6 +9,7 @@ board = env.BoardConfig()
# Utilities
env.SConscript("utils.py", exports="env")
env.SConscript("uf2.py", exports="env")
# Vendor-specific library ports
env.SConscript("libs/lwip.py", exports="env")
env.SConscript("libs/flashdb.py", exports="env")
@@ -25,7 +28,6 @@ env.Replace(
GDB="arm-none-eabi-gdb",
NM="arm-none-eabi-gcc-nm",
LINK="arm-none-eabi-gcc",
LD="arm-none-eabi-gcc",
OBJCOPY="arm-none-eabi-objcopy",
OBJDUMP="arm-none-eabi-objdump",
# RANLIB="arm-none-eabi-gcc-ranlib",
@@ -53,23 +55,29 @@ if flash_layout:
# Platform builders details:
# - call env.AddDefaults("platform name", "sdk name") to add dir paths
# - call env.AddLibrary("lib name", "base dir", [sources]) to add lib sources
# - output main firmware image binary as $IMG_FW
# - call env.BuildLibraries() to build lib targets with safe envs
# - configure LINK, UF2OTA and UPLOAD_ACTIONS
# - script code ordering:
# - global vars
# - # Outputs
# - # Tools
# - # Flags (C(XX)FLAGS / CPPDEFINES / LINKFLAGS)
# - sources (env.AddLibrary)
# - # Libs & linker config (LIBPATH / LIBS / LDSCRIPT_PATH)
# - # Misc options
# - # Image conversion (tools, functions, builders, actions, etc.)
# - # Uploader
# - # Library targets
# - # Bootloader library
# - env.BuildLibraries()
# - # Main firmware binary builder
# - # Main firmware outputs and actions
target_elf = env.BuildProgram()
target_fw = env.DumpFirmwareBinary("$IMG_FW", target_elf)
env.AddPlatformTarget("upload", target_fw, env["UPLOAD_ACTIONS"], "Upload")
Default(target_fw)
targets = [target_elf]
if "UF2OTA" in env:
targets.append(env.BuildUF2OTA(target_elf))
elif "IMG_FW" in env:
target_fw = env.subst("$IMG_FW")
env.AddPlatformTarget("upload", target_fw, env["UPLOAD_ACTIONS"], "Upload")
else:
sys.stderr.write("Warning! Firmware outputs not specified.\n")
Default(targets)

53
builder/uf2.py Normal file
View File

@@ -0,0 +1,53 @@
# Copyright (c) Kuba Szczodrzyński 2022-06-02.
from datetime import datetime
from os.path import basename, join, normpath
from SCons.Script import Builder, DefaultEnvironment
env = DefaultEnvironment()
platform = env.PioPlatform()
def env_uf2ota(env, *args, **kwargs):
now = datetime.now()
project_dir = env.subst("$PROJECT_DIR")
project_name = basename(normpath(project_dir))
# TODO support specifying custom version
project_version = now.strftime("%y.%m.%d")
lt_version = platform.version
inputs = " ".join(f'"{";".join(input)}"' for input in env["UF2OTA"])
output = [
project_name,
project_version,
"${VARIANT}",
"${FAMILY}",
f"lt{lt_version}",
]
output = join("${BUILD_DIR}", "_".join(output)) + ".uf2"
cmd = [
"@${UF2OTA_PY}",
f'--output "{output}"',
"--family ${FAMILY}",
"--board ${VARIANT}",
f"--version {lt_version}",
f'--fw "{project_name}:{project_version}"',
f"--date {int(now.timestamp())}",
"write",
inputs,
]
print(f"|-- {basename(env.subst(output))}")
env.Execute(" ".join(cmd))
env.Append(
BUILDERS=dict(
BuildUF2OTA=Builder(
action=[env.VerboseAction(env_uf2ota, "Building UF2 OTA image")]
)
)
)

View File

@@ -32,6 +32,9 @@ def env_add_defaults(env, platform_name: str, sdk_name: str):
VARIANT=board.get("build.variant"),
LDSCRIPT_SDK=board.get("build.ldscript_sdk"),
LDSCRIPT_ARDUINO=board.get("build.ldscript_arduino"),
# Link2Bin tool
LINK2BIN="${PYTHONEXE} ${LT_DIR}/tools/link2bin.py",
UF2OTA_PY="${PYTHONEXE} ${LT_DIR}/tools/uf2ota/uf2ota.py",
)
env.Replace(**vars)
for k, v in vars.items():