2015-07-02 10:51:03 -04:00
|
|
|
import os
|
2015-04-13 14:54:19 -04:00
|
|
|
import sys
|
|
|
|
import subprocess
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-09-10 13:53:15 -04:00
|
|
|
from migen.build.generic_programmer import GenericProgrammer
|
|
|
|
from migen.build.xilinx import common
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2014-07-30 05:35:21 -04:00
|
|
|
def _run_urjtag(cmds):
|
2015-04-13 14:07:07 -04:00
|
|
|
with subprocess.Popen("jtag", stdin=subprocess.PIPE) as process:
|
|
|
|
process.stdin.write(cmds.encode("ASCII"))
|
|
|
|
process.communicate()
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-02-26 06:10:41 -05:00
|
|
|
class UrJTAG(GenericProgrammer):
|
2015-04-13 14:07:07 -04:00
|
|
|
needs_bitreverse = True
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-07-05 04:43:40 -04:00
|
|
|
def __init__(self, cable, flash_proxy_basename=None):
|
|
|
|
GenericProgrammer.__init__(self, flash_proxy_basename)
|
|
|
|
self.cable = cable
|
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def load_bitstream(self, bitstream_file):
|
2015-07-05 04:43:40 -04:00
|
|
|
cmds = """cable {cable}
|
2014-07-30 05:35:21 -04:00
|
|
|
detect
|
|
|
|
pld load {bitstream}
|
|
|
|
quit
|
2015-07-05 04:43:40 -04:00
|
|
|
""".format(bitstream=bitstream_file, cable=self.cable)
|
2015-04-13 14:07:07 -04:00
|
|
|
_run_urjtag(cmds)
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def flash(self, address, data_file):
|
|
|
|
flash_proxy = self.find_flash_proxy()
|
2015-07-05 04:43:40 -04:00
|
|
|
cmds = """cable {cable}
|
2014-07-30 05:35:21 -04:00
|
|
|
detect
|
|
|
|
pld load "{flash_proxy}"
|
|
|
|
initbus fjmem opcode=000010
|
|
|
|
frequency 6000000
|
|
|
|
detectflash 0
|
|
|
|
endian big
|
|
|
|
flashmem "{address}" "{data_file}" noverify
|
2015-07-05 04:43:40 -04:00
|
|
|
""".format(flash_proxy=flash_proxy, address=address, data_file=data_file,
|
|
|
|
cable=self.cable)
|
2015-04-13 14:07:07 -04:00
|
|
|
_run_urjtag(cmds)
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-02-26 06:10:41 -05:00
|
|
|
class XC3SProg(GenericProgrammer):
|
2015-04-13 14:07:07 -04:00
|
|
|
needs_bitreverse = False
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, cable, flash_proxy_basename=None):
|
|
|
|
GenericProgrammer.__init__(self, flash_proxy_basename)
|
|
|
|
self.cable = cable
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def load_bitstream(self, bitstream_file):
|
|
|
|
subprocess.call(["xc3sprog", "-v", "-c", self.cable, bitstream_file])
|
2014-07-30 05:35:21 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def flash(self, address, data_file):
|
|
|
|
flash_proxy = self.find_flash_proxy()
|
|
|
|
subprocess.call(["xc3sprog", "-v", "-c", self.cable, "-I"+flash_proxy, "{}:w:0x{:x}:BIN".format(data_file, address)])
|
2015-02-26 06:31:19 -05:00
|
|
|
|
2015-02-26 22:22:22 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-02-26 22:22:22 -05:00
|
|
|
class FpgaProg(GenericProgrammer):
|
2015-04-13 14:07:07 -04:00
|
|
|
needs_bitreverse = False
|
2015-02-26 22:22:22 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def __init__(self, flash_proxy_basename=None):
|
|
|
|
GenericProgrammer.__init__(self, flash_proxy_basename)
|
2015-02-26 22:22:22 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def load_bitstream(self, bitstream_file):
|
|
|
|
subprocess.call(["fpgaprog", "-v", "-f", bitstream_file])
|
2015-02-26 22:22:22 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def flash(self, address, data_file):
|
|
|
|
if address != 0:
|
|
|
|
raise ValueError("fpga prog needs a main bitstream at address 0")
|
|
|
|
flash_proxy = self.find_flash_proxy()
|
|
|
|
subprocess.call(["fpgaprog", "-v", "-sa", "-r", "-b", flash_proxy,
|
|
|
|
"-f", data_file])
|
2015-02-26 22:22:22 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-21 15:27:11 -04:00
|
|
|
def _run_impact(cmds):
|
2015-07-02 10:44:39 -04:00
|
|
|
with subprocess.Popen("impact -batch", stdin=subprocess.PIPE, shell=True) as process:
|
2015-04-13 14:07:07 -04:00
|
|
|
process.stdin.write(cmds.encode("ASCII"))
|
|
|
|
process.communicate()
|
2015-07-02 10:51:03 -04:00
|
|
|
return process.returncode
|
2015-03-21 15:27:11 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-07-02 10:44:39 -04:00
|
|
|
def _create_xsvf(bitstream_file, xsvf_file):
|
2015-07-02 10:51:03 -04:00
|
|
|
assert os.path.exists(bitstream_file), bitstream_file
|
|
|
|
assert not os.path.exists(xsvf_file), xsvf_file
|
|
|
|
assert 0 == _run_impact("""
|
2015-07-02 10:44:39 -04:00
|
|
|
setPreference -pref KeepSVF:True
|
|
|
|
setMode -bs
|
|
|
|
setCable -port xsvf -file {xsvf}
|
|
|
|
addDevice -p 1 -file {bitstream}
|
|
|
|
program -p 1
|
|
|
|
quit
|
|
|
|
""".format(bitstream=bitstream_file, xsvf=xsvf_file))
|
2015-03-21 15:27:11 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-21 15:27:11 -04:00
|
|
|
class iMPACT(GenericProgrammer):
|
2015-04-13 14:07:07 -04:00
|
|
|
needs_bitreverse = False
|
2015-03-21 15:27:11 -04:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
def load_bitstream(self, bitstream_file):
|
|
|
|
cmds = """setMode -bs
|
2015-03-21 15:27:11 -04:00
|
|
|
setCable -p auto
|
|
|
|
addDevice -p 1 -file {bitstream}
|
|
|
|
program -p 1
|
|
|
|
quit
|
|
|
|
""".format(bitstream=bitstream_file)
|
2015-04-13 14:07:07 -04:00
|
|
|
_run_impact(cmds)
|
2015-03-21 15:27:11 -04:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-03-02 21:06:39 -05:00
|
|
|
def _run_vivado(path, ver, cmds):
|
2015-04-13 14:07:07 -04:00
|
|
|
if sys.platform == "win32" or sys.platform == "cygwin":
|
|
|
|
vivado_cmd = "vivado -mode tcl"
|
|
|
|
else:
|
|
|
|
settings = common.settings(path, ver)
|
|
|
|
vivado_cmd = "bash -c \"source " + settings + "&& vivado -mode tcl\""
|
|
|
|
with subprocess.Popen(vivado_cmd, stdin=subprocess.PIPE, shell=True) as process:
|
|
|
|
process.stdin.write(cmds.encode("ASCII"))
|
|
|
|
process.communicate()
|
2015-02-26 06:31:19 -05:00
|
|
|
|
2015-04-13 14:45:35 -04:00
|
|
|
|
2015-02-26 06:31:19 -05:00
|
|
|
class VivadoProgrammer(GenericProgrammer):
|
2015-04-13 14:07:07 -04:00
|
|
|
needs_bitreverse = False
|
2015-10-12 14:06:29 -04:00
|
|
|
def __init__(self, vivado_path="/opt/Xilinx/Vivado", vivado_ver=None,
|
|
|
|
flash_part="n25q256-3.3v-spi-x1_x2_x4"):
|
2015-04-13 14:07:07 -04:00
|
|
|
GenericProgrammer.__init__(self)
|
|
|
|
self.vivado_path = vivado_path
|
|
|
|
self.vivado_ver = vivado_ver
|
2015-10-12 14:06:29 -04:00
|
|
|
self.flash_part = flash_part
|
2015-04-13 14:07:07 -04:00
|
|
|
|
|
|
|
def load_bitstream(self, bitstream_file):
|
|
|
|
cmds = """open_hw
|
2015-02-26 06:31:19 -05:00
|
|
|
connect_hw_server
|
|
|
|
open_hw_target [lindex [get_hw_targets -of_objects [get_hw_servers localhost]] 0]
|
|
|
|
|
|
|
|
set_property PROBES.FILE {{}} [lindex [get_hw_devices] 0]
|
|
|
|
set_property PROGRAM.FILE {{{bitstream}}} [lindex [get_hw_devices] 0]
|
|
|
|
|
|
|
|
program_hw_devices [lindex [get_hw_devices] 0]
|
|
|
|
refresh_hw_device [lindex [get_hw_devices] 0]
|
|
|
|
|
|
|
|
quit
|
|
|
|
""".format(bitstream=bitstream_file)
|
2015-04-13 14:07:07 -04:00
|
|
|
_run_vivado(self.vivado_path, self.vivado_ver, cmds)
|
2015-02-27 03:02:21 -05:00
|
|
|
|
2015-04-13 14:07:07 -04:00
|
|
|
# XXX works to flash bitstream, adapt it to flash bios
|
|
|
|
def flash(self, address, data_file):
|
|
|
|
cmds = """open_hw
|
2015-02-27 03:02:21 -05:00
|
|
|
connect_hw_server
|
|
|
|
open_hw_target [lindex [get_hw_targets -of_objects [get_hw_servers localhost]] 0]
|
2015-10-12 14:06:29 -04:00
|
|
|
create_hw_cfgmem -hw_device [lindex [get_hw_devices] 0] -mem_dev [lindex [get_cfgmem_parts {{{flash_part}}}] 0]
|
2015-02-27 03:02:21 -05:00
|
|
|
|
|
|
|
set_property PROGRAM.BLANK_CHECK 0 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.ERASE 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.CFG_PROGRAM 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.VERIFY 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
refresh_hw_device [lindex [get_hw_devices] 0]
|
|
|
|
|
|
|
|
set_property PROGRAM.ADDRESS_RANGE {{use_file}} [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.FILES [list "{data}" ] [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0]]
|
|
|
|
set_property PROGRAM.UNUSED_PIN_TERMINATION {{pull-none}} [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.BLANK_CHECK 0 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.ERASE 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.CFG_PROGRAM 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
set_property PROGRAM.VERIFY 1 [ get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
|
|
|
|
startgroup
|
|
|
|
if {{![string equal [get_property PROGRAM.HW_CFGMEM_TYPE [lindex [get_hw_devices] 0]] [get_property MEM_TYPE [get_property CFGMEM_PART [get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]]]] }} {{ create_hw_bitstream -hw_device [lindex [get_hw_devices] 0] [get_property PROGRAM.HW_CFGMEM_BITFILE [ lindex [get_hw_devices] 0]]; program_hw_devices [lindex [get_hw_devices] 0]; }};
|
|
|
|
program_hw_cfgmem -hw_cfgmem [get_property PROGRAM.HW_CFGMEM [lindex [get_hw_devices] 0 ]]
|
|
|
|
endgroup
|
|
|
|
|
|
|
|
quit
|
2015-10-12 14:06:29 -04:00
|
|
|
""".format(data=data_file, flash_part=self.flash_part)
|
2015-04-13 14:07:07 -04:00
|
|
|
_run_vivado(self.vivado_path, self.vivado_ver, cmds)
|
2015-07-02 10:03:44 -04:00
|
|
|
|
|
|
|
|
|
|
|
class Adept(GenericProgrammer):
|
|
|
|
"""Using the Adept tool with an onboard Digilent "USB JTAG" cable.
|
|
|
|
|
|
|
|
You need to install Adept Utilities V2 from
|
|
|
|
http://www.digilentinc.com/Products/Detail.cfm?NavPath=2,66,828&Prod=ADEPT2
|
|
|
|
"""
|
|
|
|
|
|
|
|
needs_bitreverse = False
|
|
|
|
|
|
|
|
def __init__(self, board, index, flash_proxy_basename=None):
|
|
|
|
GenericProgrammer.__init__(self, flash_proxy_basename)
|
|
|
|
self.board = board
|
|
|
|
self.index = index
|
|
|
|
|
|
|
|
def load_bitstream(self, bitstream_file):
|
|
|
|
subprocess.call([
|
|
|
|
"djtgcfg",
|
|
|
|
"--verbose",
|
|
|
|
"prog", "-d", self.board,
|
|
|
|
"-i", str(self.index),
|
|
|
|
"-f", bitstream_file,
|
|
|
|
])
|
|
|
|
|
|
|
|
def flash(self, address, data_file):
|
|
|
|
raise ValueError("Flashing unsupported with DigilentAdept tools")
|