forked from gligli/tools
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add XFlash.py host tool (doesn't do much for the moment)
- Loading branch information
1 parent
4816d8c
commit 3ba22a3
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env python | ||
import usb | ||
import sys | ||
import struct | ||
import pprint | ||
|
||
pp = pprint.PrettyPrinter() | ||
|
||
class XFlash: | ||
def __init__(self, usbdev): | ||
self.devhandle = usbdev.open() | ||
self.devhandle.setConfiguration(1) | ||
self.devhandle.claimInterface(0) | ||
|
||
self.ep_out = 0x05 | ||
self.ep_in = 0x82 | ||
|
||
def __del__(self): | ||
try: | ||
self.devhandle.releaseInterface(0) | ||
del self.devhandle | ||
except: | ||
pass | ||
|
||
def cmd(self, cmd, argA=0, argB=0): | ||
buffer = struct.pack("LL", argA, argB) | ||
|
||
self.devhandle.controlMsg(requestType = usb.TYPE_VENDOR, | ||
request = cmd, | ||
value = 0x00, | ||
index = 0x00, | ||
buffer = buffer) | ||
|
||
def flashInit(self): | ||
self.cmd(0x03) | ||
|
||
buffer = self.devhandle.bulkRead(self.ep_in, 4, 1000) | ||
buffer = ''.join([chr(x) for x in buffer]) | ||
|
||
return struct.unpack("L", buffer)[0] | ||
|
||
def flashDeInit(self): | ||
self.cmd(0x04) | ||
|
||
def flashStatus(self): | ||
self.cmd(0x05) | ||
|
||
buffer = self.devhandle.bulkRead(self.ep_in, 4, 1000) | ||
buffer = ''.join([chr(x) for x in buffer]) | ||
|
||
return struct.unpack("L", buffer)[0] | ||
|
||
|
||
usbdev = None | ||
|
||
for bus in usb.busses(): | ||
for dev in bus.devices: | ||
if dev.idVendor == 0xFFFF and dev.idProduct == 4: | ||
usbdev = dev | ||
|
||
if not usbdev: | ||
print "XFlash USB hardware not found." | ||
sys.exit(1) | ||
|
||
print "Using XFlash @ [%s]" % (usbdev.filename) | ||
|
||
xf = XFlash(usbdev) | ||
|
||
try: | ||
print "FlashConfig: 0x%08x" % (xf.flashInit()) | ||
print "FlashStatus: 0x%08x" % (xf.flashStatus()) | ||
except: | ||
xf.flashDeInit() | ||
|