Cypress FX2 - py libusb code?

RayS rays at blue-cove.com
Wed Aug 5 12:21:35 EDT 2009


An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090805/da9c7e46/attachment-0001.html>
-------------- next part --------------
"""
To run the single C file USB bulk read program properly,
you must provide a source of data to the CY7C68013 FIFO bus, 
like the Simple dual channel A/D system, otherwise the reading 
call will time out in one second, you will get a bunch of zeros, 
and the ninth status number will be something negative instead of 512. 
"""

import sys
import os
import time
import usb


def PrintDevInfo(dev):
    """Print device information."""
    print "Device:", dev.filename
    print "  Device class:",dev.deviceClass
    print "  Device sub class:",dev.deviceSubClass
    print "  Device protocol:",dev.deviceProtocol
    print "  Max packet size:",dev.maxPacketSize
    print "  idVendor:",dev.idVendor
    print "  idProduct:",dev.idProduct
    print "  Device Version:",dev.deviceVersion
    print "  Device SerialNumber:",dev.iSerialNumber

    for config in dev.configurations:
        print "  Configuration:", config.value
        print "    Total length:", config.totalLength
        print "    selfPowered:", config.selfPowered
        print "    remoteWakeup:", config.remoteWakeup
        print "    maxPower:", config.maxPower
        for intf in config.interfaces:
            print "    Interface:",intf[0].interfaceNumber
            for alt in intf:
                print "    Alternate Setting:",alt.alternateSetting
                print "      Interface class:",alt.interfaceClass
                print "      Interface sub class:",alt.interfaceSubClass
                print "      Interface protocol:",alt.interfaceProtocol
                for ep in alt.endpoints:
                    print "      Endpoint:",hex(ep.address)
                    print "        Type:",ep.type
                    print "        Max packet size:",ep.maxPacketSize
                    print "        Interval:",ep.interval


firmware = 	[0x90, 0xE6, 0x0B, 0x74, 0x03, 0xF0,	##REVCTL = 0x03
		0x90, 0xE6, 0x04, 0x74, 0x80, 0xF0,	##FIFORESET = 0x80
		0x74, 0x08, 0xF0,				##FIFORESET = 0x08
		0xE4, 0xF0,						##FIFORESET = 0x00
		0x90, 0xE6, 0x01, 0x74, 0xCB, 0xF0,	##IFCONFIG = 0xCB
		0x90, 0xE6, 0x1B, 0x74, 0x0D, 0xF0,	##EP8FIFOCFG = 0x0D
		#0x90, 0xE6, 0x09, 0x74, 0x10, 0xF0,	##FIFOPINPOLAR = 0x10   TRUST!!!
		0x80, 0xFE]				
##while (1) {}
reset = 0x01
er = []
endpoint = 8

# find all of the USB busses
busses = usb.busses()
#print busses
# Find one device
rdev = None
for bus in busses:
    for dev in bus.devices:
        if dev.idVendor == 0x04B4 and dev.idProduct == 0x8613:
            rdev = dev
if rdev==None:
	print "Could not find a CY7C68013\ndev.idVendor == 0x04B4 and dev.idProduct == 0x8613"
	sys.exit()
else:
	dev = rdev
#PrintDevInfo(dev)
endpoint=8
current_handle = dev.open()

requestType = 0x40
request = 0xa0
#buffer = ''.zfill(4096)
value=0
index=0
timeout=1000
er.append(('RESET', current_handle.controlMsg(requestType, request, reset, value, index, timeout) ))##RESET

time.sleep(0.1)

for i in range(len(firmware)):
	#er.append(('ctrlMsg', i,  current_handle.controlMsg(0x40, 0xa0, firmware[i], value=i, index=0)))
	er.append(('ctrlMsg', i,  len(current_handle.controlMsg(0x40, 0xa0, firmware[i], value=i, index=0))))
er.append(('UNRESET', current_handle.controlMsg(requestType, request, reset+1, value, index, timeout))) ##UNRESET

time.sleep(0.1)

# we know that there's one config, one interface, and one endpoint
conf = dev.configurations[0]
er.append(( "confs", len(dev.configurations)))
er.append(conf.interfaces.count(0))#, conf.interfaces.index(1)
iface = conf.interfaces[0][1]
er.append( ("ifaces[0]", len(conf.interfaces[0])))
endpoint = iface.endpoints[1]
er.append(( "endpoints", len(iface.endpoints)))
er.append(( "iin", iface.interfaceNumber))
er.append( current_handle.setConfiguration(conf))

er.append( current_handle.claimInterface(iface))
er.append( current_handle.setAltInterface(iface))
try:
	er.append( current_handle.bulkRead(endpoint.address, 512, 1000))
except usb.USBError, e:
	print '  bulkRead error: ', e.args
	print '    return data:'
	for e in er: print '    ', e
	PrintDevInfo(dev)
	sys.exit()
for i in range(512):
	print " %02x " % buffer[i],
current_handle.releaseInterface(0)
dev.close()
print "\n status: ", er[-1]


More information about the Python-list mailing list