187 lines
5.6 KiB
Python
187 lines
5.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Flash Doc/音色文件/0907/2.res (or --bin) to W25Q128 @ FLASH_ADDR_SONG_HAITIAN via RTT."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
import time
|
|
|
|
DEFAULT_DEVICES = ("Cortex-M4", "AT32F403AC", "AT32F403A")
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
REPO = os.path.dirname(os.path.dirname(ROOT))
|
|
DEFAULT_BIN = os.path.join(REPO, "Doc", "音色文件", "0907", "2.res")
|
|
FLASH_ADDR_SONG_HAITIAN = 0x0009D07D
|
|
|
|
|
|
def connect_jlink(device: str):
|
|
import pylink
|
|
|
|
jlink = pylink.JLink()
|
|
jlink.open()
|
|
try:
|
|
jlink.exec_command("HideDeviceSelection = 1")
|
|
except Exception:
|
|
pass
|
|
jlink.set_tif(pylink.enums.JLinkInterfaces.SWD)
|
|
|
|
last_error = None
|
|
for candidate in (device, *DEFAULT_DEVICES):
|
|
try:
|
|
try:
|
|
jlink.exec_command(f"Device = {candidate}")
|
|
except Exception:
|
|
pass
|
|
jlink.connect(candidate)
|
|
print(f"Connected as {candidate}")
|
|
return jlink
|
|
except Exception as exc:
|
|
last_error = exc
|
|
jlink.close()
|
|
raise SystemExit(f"Failed to connect J-Link: {last_error}")
|
|
|
|
|
|
def find_rtt_control_block(jlink, ram_base: int = 0x20000000, ram_size: int = 0x18000):
|
|
needle = b"SEGGER RTT"
|
|
chunk = 0x1000
|
|
was_halted = jlink.halted()
|
|
if not was_halted:
|
|
jlink.halt()
|
|
try:
|
|
for off in range(0, ram_size, chunk):
|
|
data = bytes(jlink.memory_read8(ram_base + off, min(chunk, ram_size - off)))
|
|
idx = data.find(needle)
|
|
if idx >= 0:
|
|
return ram_base + off + idx
|
|
finally:
|
|
if not was_halted:
|
|
if hasattr(jlink, "restart"):
|
|
jlink.restart()
|
|
else:
|
|
jlink.go()
|
|
time.sleep(0.1)
|
|
return None
|
|
|
|
|
|
def rtt_read_text(jlink, timeout_s: float = 0.2) -> str:
|
|
deadline = time.time() + timeout_s
|
|
chunks: list[bytes] = []
|
|
while time.time() < deadline:
|
|
try:
|
|
data = bytes(jlink.rtt_read(0, 512) or [])
|
|
except Exception:
|
|
data = b""
|
|
if data:
|
|
chunks.append(data)
|
|
deadline = time.time() + timeout_s
|
|
else:
|
|
time.sleep(0.02)
|
|
return b"".join(chunks).decode("ascii", errors="replace")
|
|
|
|
|
|
def wait_for(jlink, marker: str, timeout_s: float = 30.0) -> str:
|
|
deadline = time.time() + timeout_s
|
|
buf = ""
|
|
while time.time() < deadline:
|
|
buf += rtt_read_text(jlink, 0.15)
|
|
if marker in buf:
|
|
return buf
|
|
raise SystemExit(f"Timeout waiting for {marker!r}\n--- RTT ---\n{buf[-800:]}")
|
|
|
|
|
|
def send_bytes(jlink, payload: bytes) -> None:
|
|
off = 0
|
|
while off < len(payload):
|
|
try:
|
|
n = jlink.rtt_write(0, list(payload[off : off + 64]))
|
|
except Exception:
|
|
time.sleep(0.05)
|
|
continue
|
|
if n is None or n <= 0:
|
|
time.sleep(0.02)
|
|
continue
|
|
off += n
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--bin", default=DEFAULT_BIN)
|
|
parser.add_argument("--device", default="Cortex-M4")
|
|
parser.add_argument("--no-reset", action="store_true")
|
|
args = parser.parse_args()
|
|
|
|
if not os.path.isfile(args.bin):
|
|
raise SystemExit(f"missing bin: {args.bin}")
|
|
|
|
data = open(args.bin, "rb").read()
|
|
size = len(data)
|
|
print(f"bin={args.bin} size={size} addr=0x{FLASH_ADDR_SONG_HAITIAN:08X}")
|
|
|
|
jlink = connect_jlink(args.device)
|
|
try:
|
|
cb = find_rtt_control_block(jlink)
|
|
if cb is None:
|
|
raise SystemExit("RTT control block not found")
|
|
print(f"RTT CB @ 0x{cb:08X}")
|
|
jlink.rtt_start(cb)
|
|
time.sleep(0.3)
|
|
_ = rtt_read_text(jlink, 0.3)
|
|
|
|
if not args.no_reset:
|
|
print("hardware reset...")
|
|
jlink.reset(halt=False)
|
|
time.sleep(3.5)
|
|
try:
|
|
jlink.rtt_stop()
|
|
except Exception:
|
|
pass
|
|
cb = find_rtt_control_block(jlink)
|
|
if cb is None:
|
|
raise SystemExit("RTT CB missing after reset")
|
|
print(f"RTT CB @ 0x{cb:08X}")
|
|
jlink.rtt_start(cb)
|
|
time.sleep(0.5)
|
|
_ = rtt_read_text(jlink, 0.5)
|
|
|
|
cmd = f"flash haitian {size}\n".encode("ascii")
|
|
send_bytes(jlink, cmd)
|
|
print("sent flash command, waiting GO...")
|
|
log = wait_for(jlink, "FLASH_HAITIAN_GO", 20.0)
|
|
print(log.strip().splitlines()[-1])
|
|
time.sleep(0.2)
|
|
|
|
chunk = 256
|
|
sent = 0
|
|
t0 = time.time()
|
|
receiving = False
|
|
while sent < size:
|
|
end = min(sent + chunk, size)
|
|
send_bytes(jlink, data[sent:end])
|
|
sent = end
|
|
log = wait_for(jlink, "FLASH_ACK", 60.0)
|
|
if (not receiving) and ("FLASH_RX" in log or "FLASH_ACK" in log):
|
|
receiving = True
|
|
print("device receiving...")
|
|
if "FLASH_HAITIAN_OK" in log:
|
|
print(log.strip().splitlines()[-1])
|
|
print("Done. Local song bank updated.")
|
|
return
|
|
if sent % (4 * 1024) == 0 or sent == size:
|
|
elapsed = time.time() - t0
|
|
print(f" {sent}/{size} ({100.0 * sent / size:.1f}%) {elapsed:.1f}s")
|
|
|
|
log = wait_for(jlink, "FLASH_HAITIAN_OK", 60.0)
|
|
print(log.strip().splitlines()[-1])
|
|
print("Done. Local song bank updated.")
|
|
finally:
|
|
try:
|
|
jlink.rtt_stop()
|
|
except Exception:
|
|
pass
|
|
jlink.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|