new action syntax for make.py + support xc3sprog
[litex.git] / programmer.py
1 import subprocess
2 import os
3
4 class Programmer:
5 def __init__(self, platform_name, flash_proxy_dir):
6 self.platform_name = platform_name
7 self.flash_proxy_dir = flash_proxy_dir
8
9 def find_flash_proxy(self, name):
10 if self.flash_proxy_dir is not None:
11 search_dirs = [self.flash_proxy_dir]
12 else:
13 search_dirs = ["~/.mlabs", "/usr/local/share/mlabs", "/usr/share/mlabs"]
14 for d in search_dirs:
15 fulldir = os.path.abspath(os.path.expanduser(d))
16 fullname = os.path.join(fulldir, name)
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
29 def load_bitstream(self, bitstream_file):
30 cmds = """cable milkymist
31 detect
32 pld load {bitstream}
33 quit
34 """.format(bitstream=bitstream_file)
35 _run_urjtag(cmds)
36
37 def flash(self, address, data_file):
38 flash_proxy = self.find_flash_proxy("fjmem-" + self.platform_name + ".bit")
39 cmds = """cable milkymist
40 detect
41 pld load "{flash_proxy}"
42 initbus fjmem opcode=000010
43 frequency 6000000
44 detectflash 0
45 endian big
46 flashmem "{address}" "{data_file}" noverify
47 """.format(flash_proxy=flash_proxy, address=address, data_file=data_file)
48 _run_urjtag(cmds)
49
50 class XC3SProg(Programmer):
51 needs_bitreverse = False
52
53 def load_bitstream(self, bitstream_file):
54 subprocess.call(["xc3sprog", "-c", "papilio", bitstream_file])
55
56 def flash(self, address, data_file):
57 flash_proxy = self.find_flash_proxy("bscan_spi_lx9_papilio.bit")
58 subprocess.call(["xc3sprog", "-c", "papilio", "-I"+flash_proxy, "{}:w:0x{:x}:BIN".format(data_file, address)])
59
60 def create_programmer(platform_name, *args, **kwargs):
61 return {
62 "mixxeo": UrJTAG,
63 "m1": UrJTAG,
64 "papilio_pro": XC3SProg
65 }[platform_name](platform_name, *args, **kwargs)