Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace shelling out to fxload with CypressFX.FX2 calls #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ conda:
conda config --add channels timvideos
conda install openocd
pip install pyusb
pip install git+https://github.com/John-K/CypressFX.git
pip install pep8
pip install autopep8
pip install setuptools-pep8
Expand Down
39 changes: 19 additions & 20 deletions hdmi2usb/modeswitch/boards.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

from collections import namedtuple

from . import lsusb as usbapi
from . import libusb as usbapi
from . import files
from CypressFX import FX2


def assert_in(needle, haystack):
Expand Down Expand Up @@ -103,7 +104,10 @@ def firmware_path(filepath):
class Board(BoardBase):

def tty(self):
return self.dev.tty()
try:
return self.dev.tty()
except AttributeError:
return None


def load_fx2(board, mode=None, filename=None, verbose=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move the libusb version of load_fx2 into libusb.py and load_fx2 which uses the binary into lsusb.py?

Expand All @@ -122,27 +126,22 @@ def load_fx2(board, mode=None, filename=None, verbose=False):

sys.stderr.write("Using FX2 firmware %s\n" % filename)

cmdline = "fxload -t fx2lp".split()
cmdline += ["-D", str(board.dev.path)]
cmdline += ["-I", filepath]
if verbose:
cmdline += ["-v", ]

if verbose:
sys.stderr.write("Running %r\n" % " ".join(cmdline))

env = os.environ.copy()
env['PATH'] = env['PATH'] + ':/usr/sbin:/sbin'
fx2 = FX2(board.dev.libusb_open())
if not fx2:
print("Could not intantiate FX2 from {}".format(board.dev._str()))
raise

try:
output = subprocess.check_output(
cmdline, stderr=subprocess.STDOUT, env=env)
if verbose > 2:
sys.stderr.write(output.decode('utf-8'))
except subprocess.CalledProcessError as e:
if b"can't modify CPUCS: Protocol error\n" not in e.output:
print(e.output)
bytes_programmed = fx2.load_intelhex_firmware(filepath)
if bytes_programmed < 1:
print("Was unable to program any bytes to {}".format(
board.dev._str()))
raise
if verbose > 2:
print("Programmed {} bytes".format(bytes_programmed))
except IOError as e:
print(e.args[-1])
raise


def flash_fx2(board, filename, verbose=False):
Expand Down
2 changes: 1 addition & 1 deletion hdmi2usb/modeswitch/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def find_boards(args):
board.state,
board.dev.path,
))
for sp in board.dev.syspaths:
for sp in getattr(board.dev, "syspaths", []):
sys.stderr.write(" %s\n" % (sp,))

if board.dev.inuse():
Expand Down
14 changes: 11 additions & 3 deletions hdmi2usb/modeswitch/libusb.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class LibDevice(DeviceBase):
def inuse(self, dev=None):
try:
if dev is None:
dev = usb.core.find(bus=self.path.bus,
address=self.path.address)
dev = self.libusb_open()

# config = dev.get_active_configuration()
active = False
Expand All @@ -37,7 +36,7 @@ def inuse(self, dev=None):

def detach(self):
# Detach any driver currently attached.
dev = usb.core.find(bus=self.path.bus, address=self.path.address)
dev = self.libusb_open()

if not self.inuse(dev):
return True
Expand All @@ -47,6 +46,15 @@ def detach(self):
if dev.is_kernel_driver_active(inf.bInterfaceNumber):
dev.detach_kernel_driver(inf.bInterfaceNumber)

def drivers(self):
if self.inuse():
return ["unknown"]
else:
return []

def libusb_open(self):
return usb.core.find(bus=self.path.bus, address=self.path.address)


Device = LibDevice

Expand Down