|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | + |
| 4 | +class Programmer: |
| 5 | + def __init__(self, flash_proxy_basename=None): |
| 6 | + self.flash_proxy_basename = flash_proxy_basename |
| 7 | + self.flash_proxy_dirs = ["~/.mlabs", "/usr/local/share/mlabs", "/usr/share/mlabs"] |
| 8 | + |
| 9 | + def set_flash_proxy_dir(self, flash_proxy_dir): |
| 10 | + if flash_proxy_dir is not None: |
| 11 | + self.flash_proxy_dirs = [flash_proxy_dir] |
| 12 | + |
| 13 | + def find_flash_proxy(self): |
| 14 | + for d in self.flash_proxy_dirs: |
| 15 | + fulldir = os.path.abspath(os.path.expanduser(d)) |
| 16 | + fullname = os.path.join(fulldir, self.flash_proxy_basename) |
| 17 | + if os.path.exists(fullname): |
| 18 | + return fullname |
| 19 | + raise OSError("Failed to find flash proxy bitstream") |
| 20 | + |
| 21 | +def _run_urjtag(cmds): |
| 22 | + with subprocess.Popen("jtag", stdin=subprocess.PIPE) as process: |
| 23 | + process.stdin.write(cmds.encode("ASCII")) |
| 24 | + process.communicate() |
| 25 | + |
| 26 | +class UrJTAG(Programmer): |
| 27 | + needs_bitreverse = True |
| 28 | + needs_flash_proxy = True |
| 29 | + |
| 30 | + def load_bitstream(self, bitstream_file): |
| 31 | + cmds = """cable milkymist |
| 32 | +detect |
| 33 | +pld load {bitstream} |
| 34 | +quit |
| 35 | +""".format(bitstream=bitstream_file) |
| 36 | + _run_urjtag(cmds) |
| 37 | + |
| 38 | + def flash(self, address, data_file): |
| 39 | + flash_proxy = self.find_flash_proxy() |
| 40 | + cmds = """cable milkymist |
| 41 | +detect |
| 42 | +pld load "{flash_proxy}" |
| 43 | +initbus fjmem opcode=000010 |
| 44 | +frequency 6000000 |
| 45 | +detectflash 0 |
| 46 | +endian big |
| 47 | +flashmem "{address}" "{data_file}" noverify |
| 48 | +""".format(flash_proxy=flash_proxy, address=address, data_file=data_file) |
| 49 | + _run_urjtag(cmds) |
| 50 | + |
| 51 | +class XC3SProg(Programmer): |
| 52 | + needs_bitreverse = False |
| 53 | + needs_flash_proxy = True |
| 54 | + |
| 55 | + def __init__(self, cable, flash_proxy_basename=None): |
| 56 | + Programmer.__init__(flash_proxy_basename) |
| 57 | + self.cable = cable |
| 58 | + |
| 59 | + def load_bitstream(self, bitstream_file): |
| 60 | + subprocess.call(["xc3sprog", "-v", "-c", self.cable, bitstream_file]) |
| 61 | + |
| 62 | + def flash(self, address, data_file): |
| 63 | + flash_proxy = self.find_flash_proxy() |
| 64 | + subprocess.call(["xc3sprog", "-v", "-c", self.cable, "-I"+flash_proxy, "{}:w:0x{:x}:BIN".format(data_file, address)]) |
| 65 | + |
| 66 | +class USBBlaster(Programmer): |
| 67 | + needs_bitreverse = False |
| 68 | + needs_flash_proxy = False |
| 69 | + |
| 70 | + def load_bitstream(self, bitstream_file, port=0): |
| 71 | + usb_port = "[USB-"+str(port)+"]" |
| 72 | + subprocess.call(["quartus_pgm", "-m", "jtag", "-c", "USB-Blaster"+usb_port, "-o", "p;"+bitstream_file]) |
| 73 | + |
| 74 | + def flash(self, address, data_file): |
| 75 | + raise NotImplementedError |
0 commit comments