61 lines
2.7 KiB
Python
61 lines
2.7 KiB
Python
import subprocess, shutil, os, time
|
|
|
|
base = os.path.join(r"C:\Users\qjyu\Documents\SoundWalker", "一诺国际吉他",
|
|
r"Code\YNGJ-GT1-M - AT32F403ARCT7")
|
|
stage = r"C:\Temp\k1flash"
|
|
os.makedirs(stage, exist_ok=True)
|
|
|
|
out_src = os.path.join(base, r"project\IAR_V7.4\YNGJ-GT1-M\Exe\YNGJ-GT1-M.out")
|
|
out_dst = os.path.join(stage, "YNGJ-GT1-M.out")
|
|
with open(out_src, "rb") as a, open(out_dst, "wb") as b:
|
|
shutil.copyfileobj(a, b)
|
|
print("copied", os.path.getsize(out_dst), flush=True)
|
|
|
|
gen_src = os.path.join(base, r"project\IAR_V7.4\settings\YNGJ-GT1-M.YNGJ-GT1-M.general.xcl")
|
|
drv_src = os.path.join(base, r"project\IAR_V7.4\settings\YNGJ-GT1-M.YNGJ-GT1-M.driver.xcl")
|
|
lines = open(gen_src, encoding="utf-8", errors="replace").read().splitlines()
|
|
new = []
|
|
for ln in lines:
|
|
new.append(f'"{out_dst}" ' if (".out" in ln and "YNGJ" in ln) else ln)
|
|
open(os.path.join(stage, "general.xcl"), "w", encoding="utf-8", newline="\n").write("\n".join(new) + "\n")
|
|
shutil.copy2(drv_src, os.path.join(stage, "driver.xcl"))
|
|
|
|
for n in ("cspybat.exe", "CSpyBat.exe", "JLink.exe", "IarIdePm.exe",
|
|
"JLinkGUIServer.exe", "JLinkRTTClient.exe", "JFlash.exe"):
|
|
subprocess.run(["taskkill", "/F", "/IM", n], capture_output=True)
|
|
time.sleep(1.5)
|
|
|
|
# Force Artery AT32F403ACx (RCT7) — avoid Cortex-M4 / wrong-chip popup
|
|
drv_path = os.path.join(stage, "driver.xcl")
|
|
drv_lines = open(drv_path, encoding="utf-8", errors="replace").read().splitlines()
|
|
forced = []
|
|
for ln in drv_lines:
|
|
if ln.strip().startswith("--jlink_device"):
|
|
continue
|
|
forced.append(ln)
|
|
forced.append('--jlink_device=AT32F403AC')
|
|
open(drv_path, "w", encoding="utf-8", newline="\n").write("\n".join(forced) + "\n")
|
|
print("driver device forced: AT32F403AC", flush=True)
|
|
|
|
cspy = r"C:\Program Files (x86)\IAR Systems\Embedded Workbench 7.3\common\bin\cspybat.exe"
|
|
cmd = [cspy, "-f", os.path.join(stage, "general.xcl"),
|
|
f"--debug_file={out_dst}", "--download_only", "--backend",
|
|
"-f", drv_path]
|
|
logp = os.path.join(stage, "cspy.log")
|
|
print("cspybat start (AT32F403AC / FlashAT32F403ARCT7)", flush=True)
|
|
with open(logp, "w", encoding="utf-8", errors="replace") as log:
|
|
r = subprocess.run(cmd, stdout=log, stderr=subprocess.STDOUT, timeout=180)
|
|
print("cspy exit", r.returncode, flush=True)
|
|
print(open(logp, encoding="utf-8", errors="replace").read()[-2000:])
|
|
if r.returncode != 0:
|
|
raise SystemExit(r.returncode)
|
|
|
|
jlink = r"C:\Program Files\SEGGER\JLink_V818\JLink.exe"
|
|
reset = os.path.join(base, r"tools\reset_run.jlink")
|
|
print("reset AT32F403AC", flush=True)
|
|
subprocess.run([jlink, "-Device", "AT32F403AC", "-If", "SWD",
|
|
"-Speed", "4000", "-AutoConnect", "1",
|
|
"-CommandFile", reset], capture_output=True, timeout=30)
|
|
time.sleep(3)
|
|
print("Done", flush=True)
|