mirror of https://github.com/m-labs/artiq.git
artiq_flash: refactor.
This commit is contained in:
parent
bcc39a8c9d
commit
1c7cb737ca
|
@ -62,7 +62,7 @@ Selecting a development board with artiq_flash
|
||||||
|
|
||||||
The board lock file also contains the openocd commands for selecting the corresponding developer board:
|
The board lock file also contains the openocd commands for selecting the corresponding developer board:
|
||||||
::
|
::
|
||||||
artiq_flash --preinit-command "$(cat /var/lib/artiq/boards/sayma-1)"
|
artiq_flash -I "$(cat /var/lib/artiq/boards/sayma-1)"
|
||||||
|
|
||||||
|
|
||||||
Using developer tools
|
Using developer tools
|
||||||
|
|
|
@ -43,7 +43,7 @@ Prerequisites:
|
||||||
|
|
||||||
parser.add_argument("-n", "--dry-run",
|
parser.add_argument("-n", "--dry-run",
|
||||||
default=False, action="store_true",
|
default=False, action="store_true",
|
||||||
help="Only show the openocd script that would be run")
|
help="only show the openocd script that would be run")
|
||||||
parser.add_argument("-H", "--host", metavar="HOSTNAME",
|
parser.add_argument("-H", "--host", metavar="HOSTNAME",
|
||||||
type=str, default=None,
|
type=str, default=None,
|
||||||
help="SSH host where the development board is located")
|
help="SSH host where the development board is located")
|
||||||
|
@ -52,7 +52,7 @@ Prerequisites:
|
||||||
"kc705 kasli sayma_amc sayma_rtm")
|
"kc705 kasli sayma_amc sayma_rtm")
|
||||||
parser.add_argument("-m", "--variant", default=None,
|
parser.add_argument("-m", "--variant", default=None,
|
||||||
help="board variant")
|
help="board variant")
|
||||||
parser.add_argument("--preinit-command", default=[], action="append",
|
parser.add_argument("-I", "--preinit-command", default=[], action="append",
|
||||||
help="add a pre-initialization OpenOCD command. "
|
help="add a pre-initialization OpenOCD command. "
|
||||||
"Useful for selecting a development board "
|
"Useful for selecting a development board "
|
||||||
"when several are connected.")
|
"when several are connected.")
|
||||||
|
@ -85,18 +85,14 @@ def proxy_path():
|
||||||
|
|
||||||
|
|
||||||
class Programmer:
|
class Programmer:
|
||||||
def __init__(self, client, target_file, preinit_commands):
|
def __init__(self, client, preinit_script):
|
||||||
self.client = client
|
self._client = client
|
||||||
if target_file:
|
self._preinit_script = preinit_script
|
||||||
self.target_file = self._transfer_script(target_file)
|
self._script = []
|
||||||
else:
|
|
||||||
self.target_file = None
|
|
||||||
self.preinit_commands = preinit_commands
|
|
||||||
self.prog = []
|
|
||||||
|
|
||||||
def _transfer_script(self, script):
|
def _transfer_script(self, script):
|
||||||
if isinstance(self.client, LocalClient):
|
if isinstance(self._client, LocalClient):
|
||||||
return script
|
return "[find {}]".format(script)
|
||||||
|
|
||||||
def rewriter(content):
|
def rewriter(content):
|
||||||
def repl(match):
|
def repl(match):
|
||||||
|
@ -104,20 +100,29 @@ class Programmer:
|
||||||
return re.sub(rb"\[find (.+?)\]", repl, content, re.DOTALL)
|
return re.sub(rb"\[find (.+?)\]", repl, content, re.DOTALL)
|
||||||
|
|
||||||
script = os.path.join(scripts_path(), script)
|
script = os.path.join(scripts_path(), script)
|
||||||
return self.client.transfer_file(script, rewriter)
|
return self._client.transfer_file(script, rewriter)
|
||||||
|
|
||||||
def _command(self, cmd):
|
def script(self):
|
||||||
self.prog.append(cmd.replace("{", "{{").replace("}", "}}"))
|
return [
|
||||||
|
*self._preinit_script,
|
||||||
|
"init",
|
||||||
|
*self._script,
|
||||||
|
"exit"
|
||||||
|
]
|
||||||
|
|
||||||
def init(self):
|
def run(self):
|
||||||
for command in self.preinit_commands:
|
cmdline = ["openocd"]
|
||||||
self._command(command)
|
if isinstance(self._client, LocalClient):
|
||||||
self._command("init")
|
cmdline += ["-s", scripts_path()]
|
||||||
|
cmdline += ["-c", "; ".join(script)]
|
||||||
|
|
||||||
def load(self, bitfile):
|
cmdline = [arg.replace("{", "{{").replace("}", "}}") for arg in self.script()]
|
||||||
|
self._client.run_command(cmdline)
|
||||||
|
|
||||||
|
def load(self, bitfile, pld):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def proxy(self, proxy_bitfile):
|
def proxy(self, proxy_bitfile, pld):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def flash_binary(self, flashno, address, filename):
|
def flash_binary(self, flashno, address, filename):
|
||||||
|
@ -126,54 +131,40 @@ class Programmer:
|
||||||
def start(self):
|
def start(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
def script(self):
|
|
||||||
return "\n".join(self.prog)
|
|
||||||
|
|
||||||
def do(self):
|
|
||||||
self._command("exit")
|
|
||||||
|
|
||||||
cmdline = ["openocd"]
|
|
||||||
if isinstance(self.client, LocalClient):
|
|
||||||
cmdline += ["-s", scripts_path()]
|
|
||||||
if self.target_file is not None:
|
|
||||||
cmdline += ["-f", self.target_file]
|
|
||||||
cmdline += ["-c", "; ".join(self.prog)]
|
|
||||||
|
|
||||||
self.client.run_command(cmdline)
|
|
||||||
|
|
||||||
|
|
||||||
class ProgrammerJtagSpi7(Programmer):
|
class ProgrammerJtagSpi7(Programmer):
|
||||||
def __init__(self, client, target, preinit_commands):
|
def __init__(self, client, target, preinit_script):
|
||||||
Programmer.__init__(self, client, os.path.join("board", target + ".cfg"),
|
Programmer.__init__(self, client, preinit_script)
|
||||||
preinit_commands)
|
|
||||||
self.init()
|
target_file = self._transfer_script(os.path.join("board", target + ".cfg"))
|
||||||
|
self._preinit_script.append("source {}".format(target_file))
|
||||||
|
|
||||||
def load(self, bitfile, pld=0):
|
def load(self, bitfile, pld=0):
|
||||||
bitfile = self.client.transfer_file(bitfile)
|
bitfile = self._client.transfer_file(bitfile)
|
||||||
self._command("pld load {} {{{}}}".format(pld, bitfile))
|
self._script.append("pld load {} {{{}}}".format(pld, bitfile))
|
||||||
|
|
||||||
def proxy(self, proxy_bitfile, pld=0):
|
def proxy(self, proxy_bitfile, pld=0):
|
||||||
proxy_bitfile = self.client.transfer_file(proxy_bitfile)
|
proxy_bitfile = self._client.transfer_file(proxy_bitfile)
|
||||||
self._command("jtagspi_init {} {{{}}}".format(pld, proxy_bitfile))
|
self._script.append("jtagspi_init {} {{{}}}".format(pld, proxy_bitfile))
|
||||||
|
|
||||||
def flash_binary(self, flashno, address, filename):
|
def flash_binary(self, flashno, address, filename):
|
||||||
# jtagspi_program supports only one flash
|
assert flashno == 0 # jtagspi_program supports only one flash
|
||||||
assert flashno == 0
|
|
||||||
filename = self.client.transfer_file(filename)
|
filename = self._client.transfer_file(filename)
|
||||||
self._command("jtagspi_program {{{}}} 0x{:x}".format(
|
self._script.append("jtagspi_program {{{}}} 0x{:x}".format(
|
||||||
filename, address))
|
filename, address))
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._command("xc7_program xc7.tap")
|
self._script.append("xc7_program xc7.tap")
|
||||||
|
|
||||||
|
|
||||||
class ProgrammerSayma(Programmer):
|
class ProgrammerSayma(Programmer):
|
||||||
sector_size = 0x10000
|
sector_size = 0x10000
|
||||||
|
|
||||||
def __init__(self, client, preinit_commands):
|
def __init__(self, client, preinit_script):
|
||||||
Programmer.__init__(self, client, None, preinit_commands)
|
Programmer.__init__(self, client, preinit_script)
|
||||||
|
|
||||||
self.prog += [
|
self._preinit_script += [
|
||||||
"interface ftdi",
|
"interface ftdi",
|
||||||
"ftdi_device_desc \"Quad RS232-HS\"",
|
"ftdi_device_desc \"Quad RS232-HS\"",
|
||||||
"ftdi_vid_pid 0x0403 0x6011",
|
"ftdi_vid_pid 0x0403 0x6011",
|
||||||
|
@ -198,30 +189,29 @@ class ProgrammerSayma(Programmer):
|
||||||
"flash bank xcu.spi0 jtagspi 0 0 0 0 xcu.proxy $XILINX_USER1",
|
"flash bank xcu.spi0 jtagspi 0 0 0 0 xcu.proxy $XILINX_USER1",
|
||||||
"flash bank xcu.spi1 jtagspi 0 0 0 0 xcu.proxy $XILINX_USER2"
|
"flash bank xcu.spi1 jtagspi 0 0 0 0 xcu.proxy $XILINX_USER2"
|
||||||
]
|
]
|
||||||
self.init()
|
|
||||||
|
|
||||||
def load(self, bitfile, pld=1):
|
def load(self, bitfile, pld=1):
|
||||||
bitfile = self.client.transfer_file(bitfile)
|
bitfile = self._client.transfer_file(bitfile)
|
||||||
self._command("pld load {} {{{}}}".format(pld, bitfile))
|
self._script.append("pld load {} {{{}}}".format(pld, bitfile))
|
||||||
|
|
||||||
def proxy(self, proxy_bitfile, pld=1):
|
def proxy(self, proxy_bitfile, pld=1):
|
||||||
self.load(proxy_bitfile, pld)
|
self.load(proxy_bitfile, pld)
|
||||||
self._command("reset halt")
|
self._script.append("reset halt")
|
||||||
|
|
||||||
def flash_binary(self, flashno, address, filename):
|
def flash_binary(self, flashno, address, filename):
|
||||||
sector_first = address // self.sector_size
|
sector_first = address // self.sector_size
|
||||||
size = os.path.getsize(filename)
|
size = os.path.getsize(filename)
|
||||||
assert size
|
|
||||||
sector_last = sector_first + (size - 1) // self.sector_size
|
sector_last = sector_first + (size - 1) // self.sector_size
|
||||||
assert sector_last >= sector_first
|
filename = self._client.transfer_file(filename)
|
||||||
filename = self.client.transfer_file(filename)
|
self._script += [
|
||||||
self._command("flash probe xcu.spi{}".format(flashno))
|
"flash probe xcu.spi{}".format(flashno),
|
||||||
self._command("flash erase_sector {} {} {}".format(flashno, sector_first, sector_last))
|
"flash erase_sector {} {} {}".format(flashno, sector_first, sector_last),
|
||||||
self._command("flash write_bank {} {{{}}} 0x{:x}".format(flashno, filename, address))
|
"flash write_bank {} {{{}}} 0x{:x}".format(flashno, filename, address),
|
||||||
self._command("flash verify_bank {} {{{}}} 0x{:x}".format(flashno, filename, address))
|
"flash verify_bank {} {{{}}} 0x{:x}".format(flashno, filename, address),
|
||||||
|
]
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
self._command("xcu_program xcu.tap")
|
self._script.append("xcu_program xcu.tap")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -285,7 +275,7 @@ def main():
|
||||||
else:
|
else:
|
||||||
client = SSHClient(args.host)
|
client = SSHClient(args.host)
|
||||||
|
|
||||||
programmer = config["programmer_factory"](client, preinit_commands=args.preinit_command)
|
programmer = config["programmer_factory"](client, preinit_script=args.preinit_command)
|
||||||
|
|
||||||
conv = False
|
conv = False
|
||||||
for action in args.action:
|
for action in args.action:
|
||||||
|
@ -345,10 +335,10 @@ def main():
|
||||||
raise ValueError("invalid action", action)
|
raise ValueError("invalid action", action)
|
||||||
|
|
||||||
if args.dry_run:
|
if args.dry_run:
|
||||||
print(programmer.script())
|
print("\n".join(programmer.script()))
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
programmer.do()
|
programmer.run()
|
||||||
finally:
|
finally:
|
||||||
if conv:
|
if conv:
|
||||||
os.unlink(bin)
|
os.unlink(bin)
|
||||||
|
|
Loading…
Reference in New Issue