#!/usr/bin/env python3 """Flash the SII EEPROM image (generated by gen_eeprom.py) into the LAN9252 over the EtherCAT cable — no external programmer, no HBI needed. Usage: ecat_flash_eeprom.py [adapter-substr] (default: I226) Steps: load gen_eeprom.py in-process (its asserts validate the image), probe the EEPROM byte order with a scratch word, write every non-0xFFFF word with immediate read-back verify, then restore the scratch word. Afterwards POWER CYCLE the board so the ESC reloads its config. """ import contextlib import importlib.util import io import struct import sys import pysoem GEN = r"G:\Robot_v2\DCU\DCU_APP\GD32E508_DCU_APP\comm\ssc\port\gen_eeprom.py" SCRATCH_WORD = 0x0200 # inside the 0xFF filler, far beyond the categories def load_image(): spec = importlib.util.spec_from_file_location("gen_eeprom", GEN) mod = importlib.util.module_from_spec(spec) buf = io.StringIO() with contextlib.redirect_stdout(buf): spec.loader.exec_module(mod) # runs its own self-check asserts img = bytes(mod.eeprom) print(f"镜像加载成功: {len(img)} 字节, checksum=0x{img[14]:02X}, " f"VendorID=0x{struct.unpack_from(' 1 else "I226" img = load_image() slave = open_slave(substr) verify_off = probe_word_layout(slave) total = len(img) // 2 n_written = n_fail = 0 fails = [] for word in range(total): val = struct.unpack_from(">> 请给板子断电重新上电(ESC 重新加载 EEPROM 配置),") print(">>> 然后运行 run_scan.bat I226 验证身份字。") if __name__ == "__main__": main()