73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
# Copyright (C) 2026 Hector van der Aa <hector@h3cx.dev>
|
|
# Copyright (C) 2026 Association Exergie <association.exergie@gmail.com>
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
from pathlib import Path
|
|
import neoecu.common
|
|
import subprocess
|
|
import sys
|
|
|
|
|
|
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 find_single_elf(build_path: Path, pattern: str, core: str) -> Path:
|
|
matches = list(build_path.glob(pattern))
|
|
|
|
if not matches:
|
|
raise FileNotFoundError(f"No {pattern} found in {core} build directory")
|
|
|
|
if len(matches) > 1:
|
|
raise RuntimeError(f"Multiple {core} ELF files found: {matches}")
|
|
|
|
return matches[0]
|
|
|
|
|
|
def run(root_path: Path, args):
|
|
config = neoecu.common.load_config(root_path)
|
|
try:
|
|
cores = neoecu.common.select_cores(config, args)
|
|
except ValueError as exc:
|
|
print(exc)
|
|
sys.exit(1)
|
|
|
|
if "CM4" in cores:
|
|
m4_root = neoecu.common.core_root(root_path, config, "CM4")
|
|
m4_jlink_scripts = m4_root / ".jlink_scripts"
|
|
m4_build = m4_root / "build" / "zephyr"
|
|
m4_bin = find_single_elf(m4_build, "zephyr.elf", "CM4")
|
|
m4_flash_script = gen_flash_script(m4_jlink_scripts, "CM4", m4_bin)
|
|
subprocess.run(["JLinkExe", "-CommanderScript", f"{m4_flash_script}"], cwd=m4_root)
|
|
|
|
if "CM7" in cores:
|
|
m7_root = neoecu.common.core_root(root_path, config, "CM7")
|
|
m7_jlink_scripts = m7_root / ".jlink_scripts"
|
|
m7_build = m7_root / "CM7" / "build"
|
|
m7_bin = find_single_elf(m7_build, "*_CM7.elf", "CM7")
|
|
m7_flash_script = gen_flash_script(m7_jlink_scripts, "CM7", m7_bin)
|
|
subprocess.run(["JLinkExe", "-CommanderScript", f"{m7_flash_script}"], cwd=m7_root)
|