From de55955410407a00cc51bfaf86c9bbe7ca1c9852 Mon Sep 17 00:00:00 2001 From: Hector van der Aa Date: Sun, 22 Mar 2026 12:29:21 +0100 Subject: [PATCH] Flashing implemented --- src/neoecu/cli.py | 2 +- src/neoecu/commands/flash.py | 78 +++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/src/neoecu/cli.py b/src/neoecu/cli.py index abde4e6..f7c2e64 100644 --- a/src/neoecu/cli.py +++ b/src/neoecu/cli.py @@ -41,7 +41,7 @@ def main(): run(project_root, args) elif args.command == "flash": from .commands.flash import run - run() + run(project_root) elif args.command == "envcheck": from .commands.envcheck import run run() diff --git a/src/neoecu/commands/flash.py b/src/neoecu/commands/flash.py index a0e47ae..8c57538 100644 --- a/src/neoecu/commands/flash.py +++ b/src/neoecu/commands/flash.py @@ -1,5 +1,79 @@ # Copyright (C) 2026 Hector van der Aa # Copyright (C) 2026 Association Exergie # SPDX-License-Identifier: GPL-3.0-or-later -def run(): - print("Flashing NeoECU...") + +from pathlib import Path +import neoecu.common +import subprocess + +def gen_flash_script(jlink_path: Path, core: str, bin_path: Path) -> Path: + jlink_path.mkdir(parents=True, exist_ok=True) + script_path = jlink_path / "flash.jlink" + script_path.unlink(missing_ok=True) + new_script: str = "" + if (core == "M7") or (core == "CM7"): + new_script += "device STM32H747XI_M7\n" + elif (core == "M4") or (core == "CM4"): + new_script += "device STM32H747XI_M4\n" + else: + raise RuntimeError(f"Unknown core: {core}") + + new_script += "si SWD\n" + new_script += "speed auto\n" + new_script += "\n" + new_script += "connect\n" + new_script += "\n" + new_script += f'loadfile "{bin_path}"\n' + new_script += "\n" + new_script += "r\n" + new_script += "g\n" + new_script += "exit" + + script_path.write_text(new_script); + return script_path + + + + +def run(root_path: Path): + + config = neoecu.common.load_config(root_path) + + m7_root = root_path / config["build"]["m7_dir"] + m4_root = root_path / config["build"]["m4_dir"] + + m7_jlink_scripts = m7_root / ".jlink_scripts" + m4_jlink_scripts = m4_root / ".jlink_scripts" + + m7_build = m7_root / "CM7" / "build" + + matches = list(m7_build.glob("*_CM7.elf")) + + if not matches: + raise FileNotFoundError("No *_CM7.elf found in build directory") + + if len(matches) > 1: + raise RuntimeError(f"Multiple CM7 ELF files found: {matches}") + + m7_bin = matches[0] + + m4_build = m4_root / "build" / "zephyr" + + matches = list(m4_build.glob("zephyr.elf")) + + if not matches: + raise FileNotFoundError("No *_CM4.elf found in build directory") + + if len(matches) > 1: + raise RuntimeError(f"Multiple CM4 ELF files found: {matches}") + + m4_bin = matches[0] + + m7_flash_script = gen_flash_script(m7_jlink_scripts, "CM7", m7_bin) + m4_flash_script = gen_flash_script(m4_jlink_scripts, "CM4", m4_bin) + + subprocess.run(["JLinkExe", "-CommanderScript", f"{m4_flash_script}"], cwd=m4_root) + subprocess.run(["JLinkExe", "-CommanderScript", f"{m7_flash_script}"], cwd=m7_root) + + +