2017-01-30 09:24:43 +08:00
|
|
|
#!/usr/bin/env python3
|
2016-01-05 07:50:59 +08:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import tempfile
|
2016-07-15 21:31:27 +08:00
|
|
|
import shutil
|
2018-01-19 15:39:55 +08:00
|
|
|
import re
|
2018-01-20 14:21:32 +08:00
|
|
|
import atexit
|
2017-11-01 17:34:10 +08:00
|
|
|
from functools import partial
|
2018-01-27 23:43:27 +08:00
|
|
|
from collections import defaultdict
|
2016-01-05 07:50:59 +08:00
|
|
|
|
2019-11-10 15:55:17 +08:00
|
|
|
from sipyco import common_args
|
|
|
|
|
2019-11-14 11:42:31 +08:00
|
|
|
from artiq import __version__ as artiq_version
|
2016-01-26 09:04:06 +08:00
|
|
|
from artiq import __artiq_dir__ as artiq_dir
|
2018-01-19 16:28:04 +08:00
|
|
|
from artiq.remoting import SSHClient, LocalClient
|
2016-01-05 07:50:59 +08:00
|
|
|
from artiq.frontend.bit2bin import bit2bin
|
|
|
|
|
2018-01-19 16:28:04 +08:00
|
|
|
|
2016-01-19 12:41:42 +08:00
|
|
|
def get_argparser():
|
2016-01-05 07:50:59 +08:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
description="ARTIQ flashing/deployment tool",
|
|
|
|
epilog="""\
|
|
|
|
Valid actions:
|
|
|
|
|
2019-12-21 14:18:28 +08:00
|
|
|
* gateware: write main gateware bitstream to flash
|
|
|
|
* rtm_gateware: write RTM gateware bitstream to flash
|
2017-12-28 03:14:41 +08:00
|
|
|
* bootloader: write bootloader to flash
|
2016-01-05 07:50:59 +08:00
|
|
|
* storage: write storage image to flash
|
2017-12-31 12:08:11 +08:00
|
|
|
* firmware: write firmware to flash
|
2019-12-21 14:18:28 +08:00
|
|
|
* load: load main gateware bitstream into device (volatile but fast)
|
|
|
|
* rtm_load: load RTM gateware bitstream into device
|
2018-11-28 18:33:32 +08:00
|
|
|
* erase: erase flash memory
|
2016-03-01 03:45:41 +08:00
|
|
|
* start: trigger the target to (re)load its gateware bitstream from flash
|
2016-01-05 07:50:59 +08:00
|
|
|
|
|
|
|
Prerequisites:
|
|
|
|
|
|
|
|
* Connect the board through its/a JTAG adapter.
|
|
|
|
* Have OpenOCD installed and in your $PATH.
|
|
|
|
* Have access to the JTAG adapter's devices. Udev rules from OpenOCD:
|
|
|
|
'sudo cp openocd/contrib/99-openocd.rules /etc/udev/rules.d'
|
|
|
|
and replug the device. Ensure you are member of the
|
|
|
|
plugdev group: 'sudo adduser $USER plugdev' and re-login.
|
|
|
|
""")
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2019-11-14 11:42:31 +08:00
|
|
|
parser.add_argument("--version", action="version",
|
|
|
|
version="ARTIQ v{}".format(artiq_version),
|
|
|
|
help="print the ARTIQ version number")
|
|
|
|
|
2019-11-10 15:55:17 +08:00
|
|
|
common_args.verbosity_args(parser)
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2018-01-20 00:24:59 +08:00
|
|
|
parser.add_argument("-n", "--dry-run",
|
|
|
|
default=False, action="store_true",
|
2018-01-20 01:44:23 +08:00
|
|
|
help="only show the openocd script that would be run")
|
2018-01-19 15:39:55 +08:00
|
|
|
parser.add_argument("-H", "--host", metavar="HOSTNAME",
|
|
|
|
type=str, default=None,
|
2019-03-08 19:35:20 +08:00
|
|
|
help="SSH host where the board is located")
|
2018-05-05 10:50:01 +08:00
|
|
|
parser.add_argument("-J", "--jump",
|
|
|
|
type=str, default=None,
|
|
|
|
help="SSH host to jump through")
|
2018-08-13 12:12:36 +08:00
|
|
|
parser.add_argument("-t", "--target", default="kasli",
|
2018-01-17 09:21:19 +08:00
|
|
|
help="target board, default: %(default)s, one of: "
|
2019-05-19 16:30:10 +08:00
|
|
|
"kasli sayma metlino kc705")
|
2018-01-22 18:25:10 +08:00
|
|
|
parser.add_argument("-V", "--variant", default=None,
|
2019-03-08 19:47:24 +08:00
|
|
|
help="board variant. Autodetected if only one is installed.")
|
2018-01-20 01:44:23 +08:00
|
|
|
parser.add_argument("-I", "--preinit-command", default=[], action="append",
|
2017-11-01 17:34:10 +08:00
|
|
|
help="add a pre-initialization OpenOCD command. "
|
2019-03-08 19:35:20 +08:00
|
|
|
"Useful for selecting a board when several are connected.")
|
2016-01-05 07:50:59 +08:00
|
|
|
parser.add_argument("-f", "--storage", help="write file to storage area")
|
2019-03-08 19:47:24 +08:00
|
|
|
parser.add_argument("-d", "--dir", help="look for board binaries in this directory")
|
|
|
|
parser.add_argument("--srcbuild", help="board binaries directory is laid out as a source build tree",
|
|
|
|
default=False, action="store_true")
|
2020-04-14 18:22:06 +08:00
|
|
|
parser.add_argument("--no-rtm-jtag", help="do not attempt JTAG to the RTM",
|
2019-12-08 15:29:31 +08:00
|
|
|
default=False, action="store_true")
|
2016-04-22 16:33:44 +08:00
|
|
|
parser.add_argument("action", metavar="ACTION", nargs="*",
|
2020-04-14 18:22:06 +08:00
|
|
|
default=[],
|
|
|
|
help="actions to perform, default: flash everything")
|
2016-01-19 12:41:42 +08:00
|
|
|
return parser
|
|
|
|
|
2021-07-26 17:01:24 +08:00
|
|
|
def openocd_root():
|
2021-05-13 14:45:23 +08:00
|
|
|
openocd = shutil.which("openocd")
|
|
|
|
if not openocd:
|
|
|
|
raise FileNotFoundError("OpenOCD is required but was not found in PATH. Is it installed?")
|
2021-07-26 17:01:24 +08:00
|
|
|
return os.path.dirname(os.path.dirname(openocd))
|
|
|
|
|
2016-01-19 12:41:42 +08:00
|
|
|
|
2017-08-21 05:23:56 +08:00
|
|
|
def scripts_path():
|
|
|
|
p = ["share", "openocd", "scripts"]
|
|
|
|
if os.name == "nt":
|
|
|
|
p.insert(0, "Library")
|
2021-07-26 17:01:24 +08:00
|
|
|
return os.path.abspath(os.path.join(openocd_root(), *p))
|
2017-08-21 05:23:56 +08:00
|
|
|
|
|
|
|
|
2017-10-23 21:21:51 +08:00
|
|
|
def proxy_path():
|
2021-07-26 17:01:24 +08:00
|
|
|
return os.path.abspath(os.path.join(openocd_root(), "share", "bscan-spi-bitstreams"))
|
2017-10-23 21:21:51 +08:00
|
|
|
|
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def find_proxy_bitfile(filename):
|
|
|
|
for p in [proxy_path(), os.path.expanduser("~/.migen"),
|
|
|
|
"/usr/local/share/migen", "/usr/share/migen"]:
|
|
|
|
full_path = os.path.join(p, filename)
|
|
|
|
if os.access(full_path, os.R_OK):
|
|
|
|
return full_path
|
2018-01-20 15:22:27 +08:00
|
|
|
raise FileNotFoundError("Cannot find proxy bitstream {}"
|
2018-01-20 14:40:35 +08:00
|
|
|
.format(filename))
|
|
|
|
|
|
|
|
|
|
|
|
def add_commands(script, *commands, **substs):
|
|
|
|
script += [command.format(**substs) for command in commands]
|
|
|
|
|
|
|
|
|
2017-08-21 05:23:56 +08:00
|
|
|
class Programmer:
|
2018-01-20 01:44:23 +08:00
|
|
|
def __init__(self, client, preinit_script):
|
|
|
|
self._client = client
|
2018-01-20 14:40:35 +08:00
|
|
|
self._board_script = []
|
2018-01-30 11:12:06 +08:00
|
|
|
self._preinit_script = [
|
|
|
|
"gdb_port disabled",
|
|
|
|
"tcl_port disabled",
|
|
|
|
"telnet_port disabled"
|
|
|
|
] + preinit_script
|
2018-01-27 23:43:27 +08:00
|
|
|
self._loaded = defaultdict(lambda: None)
|
2018-01-30 14:56:50 +08:00
|
|
|
self._script = ["init"]
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-19 15:39:55 +08:00
|
|
|
def _transfer_script(self, script):
|
2018-01-20 01:44:23 +08:00
|
|
|
if isinstance(self._client, LocalClient):
|
|
|
|
return "[find {}]".format(script)
|
2018-01-19 15:39:55 +08:00
|
|
|
|
|
|
|
def rewriter(content):
|
|
|
|
def repl(match):
|
2018-01-19 16:28:04 +08:00
|
|
|
return self._transfer_script(match.group(1).decode()).encode()
|
|
|
|
return re.sub(rb"\[find (.+?)\]", repl, content, re.DOTALL)
|
2018-01-19 15:39:55 +08:00
|
|
|
|
|
|
|
script = os.path.join(scripts_path(), script)
|
2018-01-28 00:26:02 +08:00
|
|
|
return self._client.upload(script, rewriter)
|
2018-01-20 01:44:23 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def add_flash_bank(self, name, tap, index):
|
|
|
|
add_commands(self._board_script,
|
|
|
|
"target create {tap}.{name}.proxy testee -chain-position {tap}.tap",
|
|
|
|
"flash bank {name} jtagspi 0 0 0 0 {tap}.{name}.proxy {ir:#x}",
|
|
|
|
tap=tap, name=name, ir=0x02 + index)
|
|
|
|
|
2018-11-28 18:33:32 +08:00
|
|
|
def erase_flash(self, bankname):
|
|
|
|
self.load_proxy()
|
|
|
|
add_commands(self._script,
|
|
|
|
"flash probe {bankname}",
|
|
|
|
"flash erase_sector {bankname} 0 last",
|
|
|
|
bankname=bankname)
|
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def load(self, bitfile, pld):
|
2018-01-27 23:43:27 +08:00
|
|
|
os.stat(bitfile) # check for existence
|
|
|
|
|
|
|
|
if self._loaded[pld] == bitfile:
|
|
|
|
return
|
|
|
|
self._loaded[pld] = bitfile
|
|
|
|
|
2018-01-28 00:26:02 +08:00
|
|
|
bitfile = self._client.upload(bitfile)
|
2018-01-20 14:40:35 +08:00
|
|
|
add_commands(self._script,
|
2018-03-22 17:20:45 +08:00
|
|
|
"pld load {pld} {{{filename}}}",
|
2018-01-20 14:40:35 +08:00
|
|
|
pld=pld, filename=bitfile)
|
|
|
|
|
|
|
|
def load_proxy(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-01-28 00:26:02 +08:00
|
|
|
def write_binary(self, bankname, address, filename):
|
2018-01-27 23:43:27 +08:00
|
|
|
self.load_proxy()
|
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
size = os.path.getsize(filename)
|
2018-01-28 00:26:02 +08:00
|
|
|
filename = self._client.upload(filename)
|
2018-01-20 14:40:35 +08:00
|
|
|
add_commands(self._script,
|
|
|
|
"flash probe {bankname}",
|
|
|
|
"flash erase_sector {bankname} {firstsector} {lastsector}",
|
2018-03-22 17:20:45 +08:00
|
|
|
"flash write_bank {bankname} {{{filename}}} {address:#x}",
|
|
|
|
"flash verify_bank {bankname} {{{filename}}} {address:#x}",
|
2018-01-20 14:40:35 +08:00
|
|
|
bankname=bankname, address=address, filename=filename,
|
|
|
|
firstsector=address // self._sector_size,
|
|
|
|
lastsector=(address + size - 1) // self._sector_size)
|
|
|
|
|
2018-01-28 00:26:02 +08:00
|
|
|
def read_binary(self, bankname, address, length, filename):
|
|
|
|
self.load_proxy()
|
|
|
|
|
|
|
|
filename = self._client.prepare_download(filename)
|
|
|
|
add_commands(self._script,
|
|
|
|
"flash probe {bankname}",
|
2018-03-22 17:20:45 +08:00
|
|
|
"flash read_bank {bankname} {{{filename}}} {address:#x} {length}",
|
2018-01-28 00:26:02 +08:00
|
|
|
bankname=bankname, filename=filename, address=address, length=length)
|
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def start(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2018-01-20 01:44:23 +08:00
|
|
|
def script(self):
|
|
|
|
return [
|
2018-01-20 14:40:35 +08:00
|
|
|
*self._board_script,
|
2018-01-20 01:44:23 +08:00
|
|
|
*self._preinit_script,
|
|
|
|
*self._script,
|
|
|
|
"exit"
|
|
|
|
]
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2018-01-20 01:44:23 +08:00
|
|
|
def run(self):
|
|
|
|
cmdline = ["openocd"]
|
|
|
|
if isinstance(self._client, LocalClient):
|
|
|
|
cmdline += ["-s", scripts_path()]
|
2018-01-20 02:53:49 +08:00
|
|
|
cmdline += ["-c", "; ".join(self.script())]
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2018-03-22 19:10:29 +08:00
|
|
|
cmdline = [arg.replace("{", "{{").replace("}", "}}") for arg in cmdline]
|
2018-01-20 01:44:23 +08:00
|
|
|
self._client.run_command(cmdline)
|
2018-01-28 00:26:02 +08:00
|
|
|
self._client.download()
|
|
|
|
|
|
|
|
self._script = []
|
2017-11-01 17:34:10 +08:00
|
|
|
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
class ProgrammerXC7(Programmer):
|
|
|
|
_sector_size = 0x10000
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def __init__(self, client, preinit_script, board, proxy):
|
2018-01-20 01:44:23 +08:00
|
|
|
Programmer.__init__(self, client, preinit_script)
|
2018-01-20 14:40:35 +08:00
|
|
|
self._proxy = proxy
|
2018-01-20 01:44:23 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
add_commands(self._board_script,
|
|
|
|
"source {boardfile}",
|
|
|
|
boardfile=self._transfer_script("board/{}.cfg".format(board)))
|
|
|
|
self.add_flash_bank("spi0", "xc7", index=0)
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-30 14:56:50 +08:00
|
|
|
add_commands(self._script, "xadc_report xc7.tap")
|
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def load_proxy(self):
|
|
|
|
self.load(find_proxy_bitfile(self._proxy), pld=0)
|
2017-08-21 05:23:56 +08:00
|
|
|
|
|
|
|
def start(self):
|
2018-01-20 14:40:35 +08:00
|
|
|
add_commands(self._script,
|
|
|
|
"xc7_program xc7.tap")
|
2017-08-21 05:23:56 +08:00
|
|
|
|
|
|
|
|
2020-04-14 18:22:06 +08:00
|
|
|
class ProgrammerAMCRTM(Programmer):
|
2018-01-20 14:40:35 +08:00
|
|
|
_sector_size = 0x10000
|
2017-12-13 21:20:16 +08:00
|
|
|
|
2018-01-20 01:44:23 +08:00
|
|
|
def __init__(self, client, preinit_script):
|
2018-01-20 14:40:35 +08:00
|
|
|
Programmer.__init__(self, client, preinit_script)
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
add_commands(self._board_script,
|
2018-01-30 14:56:50 +08:00
|
|
|
"source {}".format(self._transfer_script("fpga/xilinx-xadc.cfg")),
|
|
|
|
|
2017-08-21 05:23:56 +08:00
|
|
|
"interface ftdi",
|
|
|
|
"ftdi_device_desc \"Quad RS232-HS\"",
|
|
|
|
"ftdi_vid_pid 0x0403 0x6011",
|
|
|
|
"ftdi_channel 0",
|
|
|
|
# EN_USB_JTAG on ADBUS7: out, high
|
|
|
|
# nTRST on ADBUS4: out, high, but R46 is DNP
|
|
|
|
"ftdi_layout_init 0x0098 0x008b",
|
2017-11-01 20:11:18 +08:00
|
|
|
"reset_config none",
|
|
|
|
"adapter_khz 5000",
|
2017-08-21 05:23:56 +08:00
|
|
|
"transport select jtag",
|
2018-01-19 16:28:04 +08:00
|
|
|
# tap 0, pld 0
|
|
|
|
"source {}".format(self._transfer_script("cpld/xilinx-xc7.cfg")),
|
|
|
|
# tap 1, pld 1
|
2017-11-01 20:11:18 +08:00
|
|
|
"set CHIP XCKU040",
|
2018-01-20 14:40:35 +08:00
|
|
|
"source {}".format(self._transfer_script("cpld/xilinx-xcu.cfg")))
|
|
|
|
self.add_flash_bank("spi0", "xcu", index=0)
|
|
|
|
self.add_flash_bank("spi1", "xcu", index=1)
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-30 14:56:50 +08:00
|
|
|
add_commands(self._script, "echo \"RTM FPGA XADC:\"", "xadc_report xc7.tap")
|
2018-01-30 15:17:11 +08:00
|
|
|
add_commands(self._script, "echo \"AMC FPGA XADC:\"", "xadc_report xcu.tap")
|
2018-01-30 14:56:50 +08:00
|
|
|
|
2018-01-20 14:40:35 +08:00
|
|
|
def load_proxy(self):
|
2019-10-04 17:50:45 +08:00
|
|
|
self.load(find_proxy_bitfile("bscan_spi_xcku040.bit"), pld=1)
|
2017-08-21 05:23:56 +08:00
|
|
|
|
|
|
|
def start(self):
|
2018-01-30 14:56:50 +08:00
|
|
|
add_commands(self._script, "xcu_program xcu.tap")
|
2017-08-21 05:23:56 +08:00
|
|
|
|
|
|
|
|
2020-04-14 18:22:06 +08:00
|
|
|
class ProgrammerAMC(Programmer):
|
2019-05-19 16:37:40 +08:00
|
|
|
_sector_size = 0x10000
|
|
|
|
|
|
|
|
def __init__(self, client, preinit_script):
|
|
|
|
Programmer.__init__(self, client, preinit_script)
|
|
|
|
|
|
|
|
add_commands(self._board_script,
|
|
|
|
"source {}".format(self._transfer_script("fpga/xilinx-xadc.cfg")),
|
|
|
|
|
|
|
|
"interface ftdi",
|
|
|
|
"ftdi_device_desc \"Quad RS232-HS\"",
|
|
|
|
"ftdi_vid_pid 0x0403 0x6011",
|
|
|
|
"ftdi_channel 0",
|
|
|
|
# EN_USB_JTAG on ADBUS7: out, high
|
|
|
|
# nTRST on ADBUS4: out, high, but R46 is DNP
|
|
|
|
"ftdi_layout_init 0x0098 0x008b",
|
|
|
|
"reset_config none",
|
|
|
|
"adapter_khz 5000",
|
|
|
|
"transport select jtag",
|
|
|
|
"set CHIP XCKU040",
|
|
|
|
"source {}".format(self._transfer_script("cpld/xilinx-xcu.cfg")))
|
|
|
|
self.add_flash_bank("spi0", "xcu", index=0)
|
|
|
|
self.add_flash_bank("spi1", "xcu", index=1)
|
|
|
|
|
|
|
|
add_commands(self._script, "echo \"AMC FPGA XADC:\"", "xadc_report xcu.tap")
|
|
|
|
|
|
|
|
def load_proxy(self):
|
2020-02-03 18:07:26 +08:00
|
|
|
self.load(find_proxy_bitfile("bscan_spi_xcku040.bit"), pld=0)
|
2019-05-19 16:37:40 +08:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
add_commands(self._script, "xcu_program xcu.tap")
|
|
|
|
|
|
|
|
|
2016-01-19 12:41:42 +08:00
|
|
|
def main():
|
2018-01-19 15:39:55 +08:00
|
|
|
args = get_argparser().parse_args()
|
2019-11-10 15:55:17 +08:00
|
|
|
common_args.init_logger_from_args(args)
|
2016-01-05 07:50:59 +08:00
|
|
|
|
|
|
|
config = {
|
2018-01-15 20:59:11 +08:00
|
|
|
"kasli": {
|
2018-03-01 00:47:02 +08:00
|
|
|
"programmer": partial(ProgrammerXC7, board="kasli", proxy="bscan_spi_xc7a100t.bit"),
|
|
|
|
"gateware": ("spi0", 0x000000),
|
|
|
|
"bootloader": ("spi0", 0x400000),
|
|
|
|
"storage": ("spi0", 0x440000),
|
|
|
|
"firmware": ("spi0", 0x450000),
|
2018-01-15 20:59:11 +08:00
|
|
|
},
|
2018-01-28 03:53:43 +08:00
|
|
|
"sayma": {
|
2020-04-14 18:22:06 +08:00
|
|
|
"programmer": ProgrammerAMCRTM,
|
2018-03-01 00:47:02 +08:00
|
|
|
"gateware": ("spi0", 0x000000),
|
|
|
|
"bootloader": ("spi1", 0x000000),
|
|
|
|
"storage": ("spi1", 0x040000),
|
|
|
|
"firmware": ("spi1", 0x050000),
|
2018-05-09 19:47:29 +08:00
|
|
|
"rtm_gateware": ("spi1", 0x200000),
|
2016-01-05 07:50:59 +08:00
|
|
|
},
|
2019-05-19 16:30:10 +08:00
|
|
|
"metlino": {
|
2020-04-14 18:22:06 +08:00
|
|
|
"programmer": ProgrammerAMC,
|
2019-05-19 16:30:10 +08:00
|
|
|
"gateware": ("spi0", 0x000000),
|
|
|
|
"bootloader": ("spi1", 0x000000),
|
|
|
|
"storage": ("spi1", 0x040000),
|
|
|
|
"firmware": ("spi1", 0x050000),
|
|
|
|
},
|
2018-08-13 12:12:36 +08:00
|
|
|
"kc705": {
|
|
|
|
"programmer": partial(ProgrammerXC7, board="kc705", proxy="bscan_spi_xc7k325t.bit"),
|
|
|
|
"gateware": ("spi0", 0x000000),
|
|
|
|
"bootloader": ("spi0", 0xaf0000),
|
|
|
|
"storage": ("spi0", 0xb30000),
|
|
|
|
"firmware": ("spi0", 0xb40000),
|
|
|
|
},
|
2018-01-19 15:39:55 +08:00
|
|
|
}[args.target]
|
2016-01-05 07:50:59 +08:00
|
|
|
|
2018-01-19 15:39:55 +08:00
|
|
|
bin_dir = args.dir
|
2017-08-21 05:23:56 +08:00
|
|
|
if bin_dir is None:
|
2019-03-08 19:47:24 +08:00
|
|
|
bin_dir = os.path.join(artiq_dir, "board-support")
|
|
|
|
|
2020-04-14 18:22:06 +08:00
|
|
|
needs_artifacts = not args.action or any(
|
2019-12-08 15:29:31 +08:00
|
|
|
action in args.action
|
|
|
|
for action in ["gateware", "rtm_gateware", "bootloader", "firmware", "load", "rtm_load"])
|
2019-03-08 19:47:24 +08:00
|
|
|
variant = args.variant
|
2019-05-07 17:20:13 +08:00
|
|
|
if needs_artifacts and variant is None:
|
2019-03-08 19:47:24 +08:00
|
|
|
variants = []
|
|
|
|
if args.srcbuild:
|
|
|
|
for entry in os.scandir(bin_dir):
|
|
|
|
if entry.is_dir():
|
|
|
|
variants.append(entry.name)
|
|
|
|
else:
|
|
|
|
prefix = args.target + "-"
|
|
|
|
for entry in os.scandir(bin_dir):
|
|
|
|
if entry.is_dir() and entry.name.startswith(prefix):
|
|
|
|
variants.append(entry.name[len(prefix):])
|
2019-03-17 15:57:50 +08:00
|
|
|
if args.target == "sayma":
|
|
|
|
try:
|
2019-10-06 17:28:14 +08:00
|
|
|
variants.remove("rtm")
|
2019-03-17 15:57:50 +08:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2019-03-08 19:47:24 +08:00
|
|
|
if len(variants) == 0:
|
|
|
|
raise FileNotFoundError("no variants found, did you install a board binary package?")
|
|
|
|
elif len(variants) == 1:
|
|
|
|
variant = variants[0]
|
|
|
|
else:
|
|
|
|
raise ValueError("more than one variant found for selected board, specify -V. "
|
|
|
|
"Found variants: {}".format(" ".join(sorted(variants))))
|
2019-05-07 17:20:13 +08:00
|
|
|
if needs_artifacts:
|
|
|
|
if args.srcbuild:
|
|
|
|
variant_dir = variant
|
|
|
|
else:
|
|
|
|
variant_dir = args.target + "-" + variant
|
2019-11-05 15:18:58 +08:00
|
|
|
if args.target == "sayma":
|
|
|
|
if args.srcbuild:
|
2019-11-28 17:38:29 +08:00
|
|
|
rtm_variant_dir = "rtm"
|
2019-11-05 15:18:58 +08:00
|
|
|
else:
|
|
|
|
rtm_variant_dir = "sayma-rtm"
|
2018-01-20 14:40:35 +08:00
|
|
|
|
2020-04-14 18:22:06 +08:00
|
|
|
if not args.action:
|
|
|
|
if args.target == "sayma" and variant != "simplesatellite" and variant != "master":
|
|
|
|
args.action = "gateware rtm_gateware bootloader firmware start".split()
|
|
|
|
else:
|
|
|
|
args.action = "gateware bootloader firmware start".split()
|
|
|
|
|
2018-01-19 15:39:55 +08:00
|
|
|
if args.host is None:
|
|
|
|
client = LocalClient()
|
|
|
|
else:
|
2018-05-05 10:50:01 +08:00
|
|
|
client = SSHClient(args.host, args.jump)
|
2018-01-19 15:39:55 +08:00
|
|
|
|
2020-04-14 18:22:06 +08:00
|
|
|
if args.target == "sayma" and args.no_rtm_jtag:
|
|
|
|
programmer_cls = ProgrammerAMC
|
|
|
|
else:
|
|
|
|
programmer_cls = config["programmer"]
|
|
|
|
programmer = programmer_cls(client, preinit_script=args.preinit_command)
|
2016-01-05 07:50:59 +08:00
|
|
|
|
2019-03-08 19:47:24 +08:00
|
|
|
def artifact_path(this_variant_dir, *path_filename):
|
|
|
|
if args.srcbuild:
|
|
|
|
# source tree - use path elements to locate file
|
|
|
|
return os.path.join(bin_dir, this_variant_dir, *path_filename)
|
2018-01-20 15:22:27 +08:00
|
|
|
else:
|
2019-03-08 19:47:24 +08:00
|
|
|
# flat tree - all files in the same directory, discard path elements
|
|
|
|
*_, filename = path_filename
|
|
|
|
return os.path.join(bin_dir, this_variant_dir, filename)
|
2018-01-20 15:22:27 +08:00
|
|
|
|
2018-03-01 00:47:02 +08:00
|
|
|
def convert_gateware(bit_filename, header=False):
|
|
|
|
bin_handle, bin_filename = tempfile.mkstemp(
|
|
|
|
prefix="artiq_", suffix="_" + os.path.basename(bit_filename))
|
|
|
|
with open(bit_filename, "rb") as bit_file, \
|
|
|
|
open(bin_handle, "wb") as bin_file:
|
|
|
|
if header:
|
|
|
|
bin_file.write(b"\x00"*8)
|
|
|
|
bit2bin(bit_file, bin_file)
|
|
|
|
if header:
|
2018-03-02 01:32:19 +08:00
|
|
|
magic = 0x5352544d # "SRTM", see sayma_rtm target
|
2018-03-01 00:47:02 +08:00
|
|
|
length = bin_file.tell() - 8
|
|
|
|
bin_file.seek(0)
|
|
|
|
bin_file.write(magic.to_bytes(4, byteorder="big"))
|
|
|
|
bin_file.write(length.to_bytes(4, byteorder="big"))
|
|
|
|
atexit.register(lambda: os.unlink(bin_filename))
|
|
|
|
return bin_filename
|
|
|
|
|
2018-03-22 17:11:21 +08:00
|
|
|
for action in args.action:
|
|
|
|
if action == "gateware":
|
|
|
|
gateware_bin = convert_gateware(
|
2019-03-08 19:47:24 +08:00
|
|
|
artifact_path(variant_dir, "gateware", "top.bit"))
|
2018-03-22 17:11:21 +08:00
|
|
|
programmer.write_binary(*config["gateware"], gateware_bin)
|
2019-12-06 22:37:50 +08:00
|
|
|
elif action == "rtm_gateware":
|
2020-04-14 18:22:06 +08:00
|
|
|
rtm_gateware_bin = convert_gateware(
|
|
|
|
artifact_path(rtm_variant_dir, "gateware", "top.bit"), header=True)
|
|
|
|
programmer.write_binary(*config["rtm_gateware"],
|
|
|
|
rtm_gateware_bin)
|
2018-03-22 17:11:21 +08:00
|
|
|
elif action == "bootloader":
|
2019-03-08 19:47:24 +08:00
|
|
|
bootloader_bin = artifact_path(variant_dir, "software", "bootloader", "bootloader.bin")
|
2018-03-22 17:11:21 +08:00
|
|
|
programmer.write_binary(*config["bootloader"], bootloader_bin)
|
|
|
|
elif action == "storage":
|
|
|
|
storage_img = args.storage
|
|
|
|
programmer.write_binary(*config["storage"], storage_img)
|
|
|
|
elif action == "firmware":
|
2018-08-30 01:33:05 +08:00
|
|
|
if variant.endswith("satellite"):
|
2018-03-22 17:11:21 +08:00
|
|
|
firmware = "satman"
|
2017-12-14 10:36:03 +08:00
|
|
|
else:
|
2018-03-22 17:11:21 +08:00
|
|
|
firmware = "runtime"
|
|
|
|
|
2019-03-08 19:47:24 +08:00
|
|
|
firmware_fbi = artifact_path(variant_dir, "software", firmware, firmware + ".fbi")
|
2018-03-22 17:11:21 +08:00
|
|
|
programmer.write_binary(*config["firmware"], firmware_fbi)
|
|
|
|
elif action == "load":
|
|
|
|
if args.target == "sayma":
|
2019-03-08 19:47:24 +08:00
|
|
|
gateware_bit = artifact_path(variant_dir, "gateware", "top.bit")
|
2018-03-22 17:11:21 +08:00
|
|
|
programmer.load(gateware_bit, 1)
|
|
|
|
else:
|
2019-03-08 19:47:24 +08:00
|
|
|
gateware_bit = artifact_path(variant_dir, "gateware", "top.bit")
|
2018-03-22 17:11:21 +08:00
|
|
|
programmer.load(gateware_bit, 0)
|
2019-12-08 15:29:31 +08:00
|
|
|
elif action == "rtm_load":
|
2020-04-14 18:22:06 +08:00
|
|
|
rtm_gateware_bit = artifact_path(rtm_variant_dir, "gateware", "top.bit")
|
|
|
|
programmer.load(rtm_gateware_bit, 0)
|
2018-03-22 17:11:21 +08:00
|
|
|
elif action == "start":
|
|
|
|
programmer.start()
|
2018-11-28 18:33:32 +08:00
|
|
|
elif action == "erase":
|
2019-05-19 16:30:10 +08:00
|
|
|
if args.target == "sayma" or args.target == "metlino":
|
2018-11-28 18:33:32 +08:00
|
|
|
programmer.erase_flash("spi0")
|
|
|
|
programmer.erase_flash("spi1")
|
|
|
|
else:
|
|
|
|
programmer.erase_flash("spi0")
|
2018-03-22 17:11:21 +08:00
|
|
|
else:
|
|
|
|
raise ValueError("invalid action", action)
|
2017-08-21 05:23:56 +08:00
|
|
|
|
2018-01-20 00:24:59 +08:00
|
|
|
if args.dry_run:
|
2018-01-20 01:44:23 +08:00
|
|
|
print("\n".join(programmer.script()))
|
2018-01-20 00:24:59 +08:00
|
|
|
else:
|
2018-01-20 14:21:32 +08:00
|
|
|
programmer.run()
|
2016-01-05 07:50:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|