def __init__(self, timeout=2000): usb = None if sys.platform == 'win32': usb = ftd3xx.create(0, ftd3xx._ftd3xx_win32.FT_OPEN_BY_INDEX) elif sys.platform == 'linux2': usb = ftd3xx.create(0, ftd3xx._ftd3xx_linux.FT_OPEN_BY_INDEX) if usb is None: raise Exception("Could not find or open Device!") if sys.platform == 'win32' and usb.getDriverVersion() < 0x01020006: usb.close() raise Exception("Old kernel driver version. Please update driver!") if usb.getDeviceDescriptor().bcdUSB < 0x300: print( "*** Warning: Device is NOT connected using USB3.0 cable or port!" ) cfg = usb.getChipConfiguration() numChannel = [4, 2, 1, 0, 0][cfg.ChannelConfig] if numChannel != 1: usb.close() raise Exception("Number of Channels invalid! (numChannel=%d)" % (numChannel, )) usb.setPipeTimeout(0x02, timeout) usb.setPipeTimeout(0x82, timeout) self._usb = usb self._chunk = 16384
def DemoCyclePort(): # reset device port logging.debug("Reset device port") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.resetDevicePort() D3XX.close() D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() if sys.platform == 'win32': # cycle device port logging.debug("Cycle device port") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.cycleDevicePort() D3XX.close() D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() return True
def DemoSetChipConfiguration(): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # set chip configuration without using getChipConfiguration() logging.debug("set chip configuration without using getChipConfiguration") cfg = _ft.FT_60XCONFIGURATION() Manufacturer = "ManufacturerSet" ProductDescription = "ProductDescriptionSet" SerialNumber = "SerialNumberSet" SetInfoForStringDescriptor(cfg, Manufacturer, ProductDescription, SerialNumber) cfg.VendorID = _ft.FT_CONFIGURATION_DEFAULT_VENDORID cfg.ProductID = _ft.FT_CONFIGURATION_DEFAULT_PRODUCTID_601 cfg.bInterval = _ft.FT_CONFIGURATION_DEFAULT_INTERRUPT_INTERVAL cfg.PowerAttributes = _ft.FT_CONFIGURATION_DEFAULT_POWERATTRIBUTES cfg.PowerConsumption = _ft.FT_CONFIGURATION_DEFAULT_POWERCONSUMPTION cfg.Reserved2 = 0 cfg.FIFOClock = _ft.FT_CONFIGURATION_DEFAULT_FIFOCLOCK cfg.FIFOMode = _ft.FT_CONFIGURATION_DEFAULT_FIFOMODE cfg.ChannelConfig = _ft.FT_CONFIGURATION_DEFAULT_CHANNELCONFIG cfg.OptionalFeatureSupport = _ft.FT_CONFIGURATION_DEFAULT_OPTIONALFEATURE cfg.BatteryChargingGPIOConfig = _ft.FT_CONFIGURATION_DEFAULT_BATTERYCHARGING cfg.FlashEEPROMDetection = _ft.FT_CONFIGURATION_DEFAULT_FLASHDETECTION cfg.MSIO_Control = _ft.FT_CONFIGURATION_DEFAULT_MSIOCONTROL cfg.GPIO_Control = _ft.FT_CONFIGURATION_DEFAULT_GPIOCONTROL DisplayChipConfiguration(cfg) D3XX.setChipConfiguration(cfg) D3XX.close(True) D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() # reopen to display current chip configuration logging.debug("get new chip configuration") if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False readCfg = D3XX.getChipConfiguration() DisplayChipConfiguration(readCfg) D3XX.close() D3XX = 0 return True
def DemoResetChipConfiguration(bDisplay=True): if bDisplay == True: logging.debug("reset chip configuration") if sys.platform == 'linux': DemoTurnOffPipeThreads() # set default chip configuration D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # Hack: Reset it to some decent config.. cfg = D3XX.getChipConfiguration() cfg.ProductID = 0x601e cfg.bInterval = 0x09 cfg.PowerAttributes = 0xe0 cfg.PowerConsumption = 0x60 cfg.Reserved2 = 0x00 cfg.FIFOClock = _ft.FT_CONFIGURATION_FIFO_CLK_100 # cfg.FIFOClock = _ft.FT_CONFIGURATION_FIFO_CLK_66 # cfg.FIFOClock = _ft.FT_CONFIGURATION_FIFO_CLK_50 # cfg.FIFOClock = _ft.FT_CONFIGURATION_FIFO_CLK_40 cfg.FIFOMode = _ft.FT_CONFIGURATION_FIFO_MODE_245 cfg.ChannelConfig = _ft.FT_CONFIGURATION_CHANNEL_CONFIG_1 cfg.OptionalFeatureSupport = 0x03c2 cfg.BatteryChargingGPIOConfig = 0xe4 cfg.MSIO_Control = 0x00010800 D3XX.setChipConfiguration(cfg) D3XX.close(True) D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() # reopen to display chip configuration if bDisplay == True: if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) D3XX.close() D3XX = 0 return True
def DemoOpenDeviceBy(): if sys.platform == 'linux': DemoTurnOffPipeThreads() # get description and serial number of device at index 0 ftd3xx.createDeviceInfoList() DEVICELIST = ftd3xx.getDeviceInfoList() # open device by index openby = 0 logging.debug("Open by index [%d]" % openby) D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if (D3XX is None): logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.close() D3XX = 0 # open device by description if sys.platform == 'linux': DemoTurnOffPipeThreads() ftd3xx.createDeviceInfoList() DEVICELIST = ftd3xx.getDeviceInfoList() openby = DEVICELIST[0].Description logging.debug("Open by description [%s]" % openby.decode('utf-8')) D3XX = ftd3xx.create(openby, _ft.FT_OPEN_BY_DESCRIPTION) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.close() D3XX = 0 # open device by serial number if sys.platform == 'linux': DemoTurnOffPipeThreads() ftd3xx.createDeviceInfoList() DEVICELIST = ftd3xx.getDeviceInfoList() openby = DEVICELIST[0].SerialNumber logging.debug("Open by serial number [%s]" % openby.decode('utf-8')) D3XX = ftd3xx.create(openby, _ft.FT_OPEN_BY_SERIAL_NUMBER) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.close() D3XX = 0 return True
def EnableBatteryChargingDetectionFeature(bEnable): D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get and display current chip configuration cfg = D3XX.getChipConfiguration() #DisplayChipConfiguration(cfg) # enable or disable battery charging detection # BatteryChargingGPIOConfig Default setting : 11100100b (0xE4 - FT_CONFIGURATION_DEFAULT_BATTERYCHARGING) # 7 - 6 : DCP = 11b (GPIO1 = 1 GPIO0 = 1) # 5 - 4 : CDP = 10b (GPIO1 = 1 GPIO0 = 0) # 3 - 2 : SDP = 01b (GPIO1 = 0 GPIO0 = 1) # 1 - 0 : Unknown/Off = 00b (GPIO1 = 0 GPIO0 = 0) if bEnable: cfg.OptionalFeatureSupport |= _ft.FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLEBATTERYCHARGING cfg.BatteryChargingGPIOConfig |= _ft.FT_CONFIGURATION_DEFAULT_BATTERYCHARGING else: cfg.OptionalFeatureSupport &= ~_ft.FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLEBATTERYCHARGING #DisplayChipConfiguration(cfg) D3XX.setChipConfiguration(cfg) D3XX.close() D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() # verify the chip configuration D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) D3XX.close() D3XX = 0 if bEnable and not ( cfg.OptionalFeatureSupport & _ft.FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLEBATTERYCHARGING): return False return True
def DemoSuspendTimeout(): if sys.platform == 'linux': return True logging.debug("Get set suspend timeout") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get current timeout timeout = D3XX.getSuspendTimeout() logging.debug("default timeout = %d" % timeout) # set the desired timeout D3XX.setSuspendTimeout(timeout * 2) logging.debug("set timeout = %d" % (timeout * 2)) # verify if timeout changed timeoutNew = D3XX.getSuspendTimeout() logging.debug("new timeout = %d" % timeoutNew) if (timeoutNew != timeout * 2): return False # revert to original timeout D3XX.setSuspendTimeout(timeout) logging.debug("revert timeout = %d" % timeout) D3XX.close() D3XX = 0 return True
def CreatePipe(): # open the first device (index 0) D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if (D3XX is None): print( "ERROR: Please check if another D3XX application is open!") return False # get the version numbers of driver and firmware if (sys.platform == 'win32' and D3XX.getDriverVersion() < 0x01020006): print( "ERROR: Old kernel driver version. Please update driver from Windows Update or FTDI website!") D3XX.close() return False # check if USB3 or USB2 devDesc = D3XX.getDeviceDescriptor() bUSB3 = devDesc.bcdUSB >= 0x300 if (bUSB3 == False): print( "Warning: Device is connected using USB2 cable or through USB2 host controller!") D3XX.setPipeTimeout(0x02, 10) D3XX.setPipeTimeout(0x82, 10) return D3XX
def DemoVersions(): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # getLibraryVersion ulLibraryVersion = D3XX.getLibraryVersion() logging.debug("LibraryVersion = %#08X" % ulLibraryVersion) # getDriverVersion ulDriverVersion = D3XX.getDriverVersion() logging.debug("DriverVersion = %#08X" % ulDriverVersion) # getFirmwareVersion ulFirmwareVersion = D3XX.getFirmwareVersion() logging.debug("FirmwareVersion = %#08X" % ulFirmwareVersion) D3XX.close() D3XX = 0 # check driver version if (sys.platform == 'win32'): if (ulDriverVersion < 0x01020006): logging.debug( "ERROR: Old kernel driver version. Please update driver from Windows Update or FTDI website!" ) return False return True
def openUSB(): import ftd3xx if sys.platform == 'win32': import ftd3xx._ftd3xx_win32 as _ft elif sys.platform == 'linux2': import ftd3xx._ftd3xx_linux as _ft usb = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if usb is None: print("*** ERROR: Can't find or open Device!") return False, None if (sys.platform == 'win32' and usb.getDriverVersion() < 0x01020006): print("*** ERROR: Old kernel driver version. Please update driver!") usb.close() return False, None if usb.getDeviceDescriptor().bcdUSB < 0x300: print( "*** Warning: Device is NOT connected using USB3.0 cable or port!") return False, None cfg = usb.getChipConfiguration() numChannels = [4, 2, 1, 0, 0] numChannel = numChannels[cfg.ChannelConfig] if numChannel != 1: print("*** ERROR:Number of Channels invalid! (numChannel=%d)" % (numChannel, )) return False, None return True, usb
def FT_init() -> ftd3xx.FTD3XX: numDevices = ftd3xx.createDeviceInfoList() if numDevices == 0: print("no dev") else: devList = ftd3xx.getDeviceInfoList() devIndex = 0 for i in range(numDevices): if devList[i].ID == 0x403601f: devIndex = i else: print("not find our dev") sys.exit() D3XX = ftd3xx.create(devIndex, _ft.FT_OPEN_BY_INDEX) devDesc = D3XX.getDeviceDescriptor() bUSB3 = devDesc.bcdUSB >= 0x300 if (bUSB3 == False): print( "Warning: Device is connected using USB2 cable or through USB2 host controller!" ) cfg = D3XX.getChipConfiguration() channelConfig = [ "4 Channels", "2 Channels", "1 Channel", "1 OUT Pipe", "1 IN Pipe" ] print("\tChannelConfig = %#04x (%s)" % (cfg.ChannelConfig, channelConfig[cfg.ChannelConfig])) return D3XX
def DemoModifyChipConfiguration(): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get and display current chip configuration logging.debug("get and modify current chip configuration") cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) # modify chip configuration Manufacturer = "ManufacturerMod" ProductDescription = "ProductDescriptionMod" SerialNumber = "SerialNumberMod" newCfg = cfg SetInfoForStringDescriptor(newCfg, Manufacturer, ProductDescription, SerialNumber) D3XX.setChipConfiguration(newCfg) D3XX.close(True) D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() # reopen to display current chip configuration logging.debug("get new chip configuration") if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False readCfg = D3XX.getChipConfiguration() DisplayChipConfiguration(readCfg) STRDESC = GetInfoFromStringDescriptor(readCfg.StringDescriptors) D3XX.close() D3XX = 0 return True
def __init__ ( self, debug ): self.debug = debug; try: import ftd3xx import sys if sys.platform == 'win32': import ftd3xx._ftd3xx_win32 as _ft elif sys.platform == 'linux2': import ftd3xx._ftd3xx_linux as _ft except: raise RuntimeError("ERROR: FTD3XX from FTDIchip.com is required"); try: # check connected devices numDevices = ftd3xx.createDeviceInfoList() if (numDevices == 0): print("ERROR: No FTD3XX device is detected."); return False; # devList = ftd3xx.getDeviceInfoList(); devIndex = 0; # Assume a single device and open first device self.D3XX = ftd3xx.create(devIndex, _ft.FT_OPEN_BY_INDEX); if (self.D3XX is None): print("ERROR: Please check if another D3XX application is open!"); return False; # Reset the FT600 to make sure starting fresh with nothing in FIFOs self.D3XX.resetDevicePort(); # Flush self.D3XX.close(); self.D3XX = ftd3xx.create(devIndex, _ft.FT_OPEN_BY_INDEX); # check if USB3 or USB2 devDesc = self.D3XX.getDeviceDescriptor(); bUSB3 = devDesc.bcdUSB >= 0x300; # validate chip configuration cfg = self.D3XX.getChipConfiguration(); # Timeout is in ms,0=Blocking. Defaults to 5,000 rts = self.D3XX.setPipeTimeout( pipeid = 0xFF, timeoutMS = 1000 ); # process loopback for all channels except: raise RuntimeError("ERROR: Unable to open USB Port " ); return;
def EnableNotificationFeature(bEnable): D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get and display current chip configuration cfg = D3XX.getChipConfiguration() #DisplayChipConfiguration(cfg) # enable or disable notification for 1st channel if bEnable: cfg.OptionalFeatureSupport |= _ft.FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLENOTIFICATIONMESSAGE_INCH1 cfg.Interval = _ft.FT_CONFIGURATION_DEFAULT_INTERRUPT_INTERVAL else: cfg.OptionalFeatureSupport &= ~_ft.FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLENOTIFICATIONMESSAGE_INCH1 #DisplayChipConfiguration(cfg) D3XX.setChipConfiguration(cfg) D3XX.close() D3XX = 0 # wait until device has reenumerated DemoWaitForDeviceReenumeration() # display the chip configuration D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) D3XX.close() D3XX = 0 if bEnable and not ( cfg.OptionalFeatureSupport & _ft. FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLENOTIFICATIONMESSAGE_INCH1): return False return True
def DemoNotificationTransfer(): logging.debug("enable notification feature on channel 1") EnableNotificationFeature(True) D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False D3XX.setSuspendTimeout(0) logging.debug("Set notification event") eventCondition = threading.Event() eventCondition.ulRecvNotificationLength = int(0x0) eventCondition.ucEndpointNo = int(0x0) D3XX.setNotificationCallback(eventCondition) buffwrite = bytearray(4096) logging.debug("writePipe %d" % len(buffwrite)) buffwrite[:] = itertools.repeat(0xAA, len(buffwrite)) #print binascii.hexlify(buffwrite) bytesWritten = D3XX.writePipe(0x02, str(buffwrite), len(buffwrite)) bytesRead = 0 while (bytesRead < bytesWritten): while not eventCondition.is_set(): eventCondition.wait(1) ulRecvNotificationLength = eventCondition.ulRecvNotificationLength ucEndpointNo = eventCondition.ucEndpointNo #print "ulRecvNotificationLength %d ucEndpointNo %d" % (ulRecvNotificationLength, ucEndpointNo) #readOutput = D3XX.readPipe(ucEndpointNo, ulRecvNotificationLength) readOutput = D3XX.readPipe(0x82, 512) bytesRead += readOutput['bytesTransferred'] logging.debug("readPipe %d" % bytesRead) buffread = readOutput['bytes'] logging.debug("clearNotificationCallback") D3XX.clearNotificationCallback() D3XX.close() D3XX = 0 logging.debug("") logging.debug("disable notification feature on channel 1") EnableNotificationFeature(False) return True
def run(self): # Reset readout system ------------------------------------------------------------------------------------------------- self.serial_obj.execute_cmd("set_nrst 0") time.sleep(0.5) self.serial_obj.execute_cmd("set_nrst 1") time.sleep(0.5) # Power on chip -------------------------------------------------------------------------------------------------------- self.serial_obj.execute_cmd("set_vdd2_en 1") time.sleep(0.5) self.serial_obj.execute_cmd("set_vdd1_en 1") self.serial_obj.execute_cmd("set_avdd_en 1") time.sleep(2) #self.serial_obj.execute_cmd("power_on") # FT601Q --------------------------------------------------------------------------------------------------------------- # - Check connected devices numDevices = ftd3xx.createDeviceInfoList() if (numDevices == 0): print("FT601 not connected !") return # - Open the first device (index 0) self.usb_obj = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) self.usb_obj.setPipeTimeout(0x82, 0) # - Set stream pipe self.usb_obj.setStreamPipe(0x82, self.data_size) # Main Loop ------------------------------------------------------------------------------------------------------------ # - init self.offset = np.zeros((128, 128)) self.av_list = [] self.px_list = [] self.run = True while (self.run): data = self.usb_obj.readPipeEx(0x82, self.data_size, raw=True) print('%.16f' % time.time()) num = data['bytesTransferred'] data = data['bytes'] #print(num, data[0], data[-1]) threading.Thread(target=self.save_and_plot, args=(data, )).start() self.usb_obj.close() #self.serial_obj.execute_cmd("power_off") # ---------------------------------------------------------------------------------------------------------------------- self.done_s.emit(self.file_path)
def DemoGetChipConfiguration(bDisplay=True): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get and display current chip configuration logging.debug("get current chip configuration") cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) D3XX.close() D3XX = 0 return True
def __init__(self): try: import ftd3xx import sys if sys.platform == 'win32': import ftd3xx._ftd3xx_win32 as _ft elif sys.platform == 'linux2': import ftd3xx._ftd3xx_linux as _ft except: raise RuntimeError("ERROR: FTD3XX from FTDIchip.com is required") raise RuntimeError( "ERROR: Unable to import serial\n" + "PySerial from sourceforge.net is required for Serial Port access." ) try: # check connected devices numDevices = ftd3xx.createDeviceInfoList() if (numDevices == 0): print("ERROR: No FTD3XX device is detected.") return False devList = ftd3xx.getDeviceInfoList() # Just open the first device (index 0) devIndex = 0 self.D3XX = ftd3xx.create(devIndex, _ft.FT_OPEN_BY_INDEX) if (self.D3XX is None): print( "ERROR: Please check if another D3XX application is open!") return False # check if USB3 or USB2 devDesc = self.D3XX.getDeviceDescriptor() bUSB3 = devDesc.bcdUSB >= 0x300 # validate chip configuration cfg = self.D3XX.getChipConfiguration() # process loopback for all channels except: raise RuntimeError("ERROR: Unable to open USB Port ") return
def DemoGpioBatteryCharging(): logging.debug("enable battery charging detection feature") EnableBatteryChargingDetectionFeature(True) D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False mask = _ft.FT_GPIO_MASK_GPIO_0 | _ft.FT_GPIO_MASK_GPIO_1 # enable gpio by setting gpio direction direction = (_ft.FT_GPIO_DIRECTION_OUT << _ft.FT_GPIO_0) | ( _ft.FT_GPIO_DIRECTION_OUT << _ft.FT_GPIO_1) D3XX.enableGPIO(mask, direction) # read gpio # BatteryChargingGPIOConfig Default setting : 11100100b (0xE4) # 7 - 6 : DCP = 11b (GPIO1 = 1 GPIO0 = 1) # 5 - 4 : CDP = 10b (GPIO1 = 1 GPIO0 = 0) # 3 - 2 : SDP = 01b (GPIO1 = 0 GPIO0 = 1) # 1 - 0 : Unknown/Off = 00b (GPIO1 = 0 GPIO0 = 0) # Since device is connected to a host machine, then we should get SDP (GPIO1 = 0 GPIO0 = 1) bcdtype = [ 'UNKNOWN', 'SDP - Standard Downstream Port', 'CDP - Charging Downstream Port', 'DCP - Dedicated Charging Port' ] data = D3XX.readGPIO() logging.debug("detected battery charging type: [%s]" % bcdtype[data]) logging.debug("") D3XX.close() D3XX = 0 logging.debug("disable battery charging detection feature") EnableBatteryChargingDetectionFeature(False) return True
def GetChipConfiguration(bDisplay=True): if sys.platform == 'linux': TurnOffPipeThreads() ftd3xx.createDeviceInfoList() ftd3xx.getDeviceInfoList() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return None # get and display current chip configuration cfg = D3XX.getChipConfiguration() if bDisplay == True and cfg != None: DisplayChipConfiguration(cfg) D3XX.close() D3XX = 0 return cfg
def SetChipConfiguration(cfg=None, bDisplay=False): if sys.platform == 'linux': TurnOffPipeThreads() ftd3xx.createDeviceInfoList() ftd3xx.getDeviceInfoList() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False if bDisplay == True and cfg != None: DisplayChipConfiguration(cfg) D3XX.setChipConfiguration(cfg) D3XX.close() D3XX = 0 WaitForDeviceReenumeration() return True
def DemoAbortTransfer(): logging.debug("Abort transfer") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False thread = threading.Thread(target=DemoAbortThread, args=(D3XX, )) thread.start() # readpipe is blocking when overlapped parameter is not set # it will be unblocked by the thread DemoAbortThread logging.debug("readPipe") D3XX.setPipeTimeout(0x82, 0) D3XX.readPipeEx(0x82, 1024) thread.join() D3XX.close() D3XX = 0 logging.debug("") return True
def DemoPipeTimeout(): if sys.platform == 'linux': return True logging.debug("Get set pipe timeout") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False pipe = 0x02 # get timeout for pipe 0x02 timeoutMS = D3XX.getPipeTimeout(pipe) logging.debug("default timeout = %d" % timeoutMS) # set desired timeout for pipe 0x02 D3XX.setPipeTimeout(pipe, timeoutMS * 2) logging.debug("set timeout = %d" % (timeoutMS * 2)) # verify if timeout changed timeoutMSNew = D3XX.getPipeTimeout(pipe) logging.debug("new timeout = %d" % timeoutMSNew) if (timeoutMSNew != timeoutMS * 2): return False # revert to original timeout D3XX.setPipeTimeout(pipe, timeoutMS) logging.debug("revert timeout = %d" % timeoutMS) D3XX.close() D3XX = 0 return True
def DemoGpioSetGet(): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False mask = _ft.FT_GPIO_MASK_GPIO_0 | _ft.FT_GPIO_MASK_GPIO_1 # enable gpio by setting gpio direction direction = (_ft.FT_GPIO_DIRECTION_OUT << _ft.FT_GPIO_0) | ( _ft.FT_GPIO_DIRECTION_OUT << _ft.FT_GPIO_1) D3XX.enableGPIO(mask, direction) logging.debug("enable gpio[gpio0: %d, gpio1: %d]" % (direction & _ft.FT_GPIO_MASK_GPIO_0, (direction & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # set gpio pull both high pull = (_ft.FT_GPIO_PULL_HIGH << _ft.FT_GPIO_0) | ( _ft.FT_GPIO_PULL_HIGH << _ft.FT_GPIO_1) D3XX.setGPIOPull(mask, pull) logging.debug("pull gpio [gpio0: %d, gpio1: %d]" % (pull & _ft.FT_GPIO_MASK_GPIO_0, (pull & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # read gpio data = D3XX.readGPIO() logging.debug("read gpio [gpio0: %d, gpio1: %d]" % (data & _ft.FT_GPIO_MASK_GPIO_0, (data & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # write gpio both low data = (_ft.FT_GPIO_VALUE_LOW << _ft.FT_GPIO_0) | ( _ft.FT_GPIO_VALUE_LOW << _ft.FT_GPIO_1) D3XX.writeGPIO(mask, data) logging.debug("write gpio [gpio0: %d, gpio1: %d]" % (data & _ft.FT_GPIO_MASK_GPIO_0, (data & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # read gpio data = D3XX.readGPIO() logging.debug("read gpio [gpio0: %d, gpio1: %d]" % (data & _ft.FT_GPIO_MASK_GPIO_0, (data & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # write gpio both high data = (_ft.FT_GPIO_VALUE_HIGH << _ft.FT_GPIO_0) | ( _ft.FT_GPIO_VALUE_HIGH << _ft.FT_GPIO_1) D3XX.writeGPIO(mask, data) logging.debug("write gpio [gpio0: %d, gpio1: %d]" % (data & _ft.FT_GPIO_MASK_GPIO_0, (data & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) # read gpio data = D3XX.readGPIO() logging.debug("read gpio [gpio0: %d, gpio1: %d]" % (data & _ft.FT_GPIO_MASK_GPIO_0, (data & _ft.FT_GPIO_MASK_GPIO_1) >> _ft.FT_GPIO_1)) D3XX.close() D3XX = 0 logging.debug("") return True
def main(channelsToTest=[0], transferSize=0, transferIteration=16, bWrite=False, bRead=True, bStressTest=False): logging.debug( "INPUT: CHANNELS=%s, SIZE=%d, ITERATION=%d, WRITE=%s, READ=%s, STRESS=%s" % (channelsToTest, transferSize, transferIteration, bWrite, bRead, bStressTest)) logging.debug("") logging.debug("") # raise exception on error # ftd3xx.raiseExceptionOnError(True) if sys.platform == 'linux': DemoTurnOffPipeThreads() # check connected devices numDevices = ftd3xx.createDeviceInfoList() if (numDevices == 0): logging.debug( "ERROR: Please check environment setup! No device is detected.") return False logging.debug("Detected %d device(s) connected." % numDevices) devList = ftd3xx.getDeviceInfoList() DisplayDeviceList(numDevices, devList) devIndex = SelectDevice(numDevices) # open the first device (index 0) D3XX = ftd3xx.create(devIndex, _ft.FT_OPEN_BY_INDEX) if (D3XX is None): logging.debug( "ERROR: Please check if another D3XX application is open!") return False # get the version numbers of driver and firmware DisplayVersions(D3XX) if (sys.platform == 'win32' and D3XX.getDriverVersion() < 0x01020006): logging.debug( "ERROR: Old kernel driver version. Please update driver from Windows Update or FTDI website!" ) D3XX.close() return False # check if USB3 or USB2 devDesc = D3XX.getDeviceDescriptor() bUSB3 = devDesc.bcdUSB >= 0x300 if (bUSB3 == False and transferSize == 16 * 1024 * 1024): transferSize = 4 * 1024 * 1024 if (bUSB3 == False): logging.debug( "Warning: Device is connected using USB2 cable or through USB2 host controller!" ) # validate chip configuration cfg = D3XX.getChipConfiguration() DisplayChipConfiguration(cfg) numChannels = [4, 2, 1, 0, 0] numChannels = numChannels[cfg.ChannelConfig] if (numChannels == 0): numChannels = 1 if (cfg.ChannelConfig == _ft.FT_CONFIGURATION_CHANNEL_CONFIG_1_OUTPIPE ): bWrite = True bRead = False elif (cfg.ChannelConfig == _ft.FT_CONFIGURATION_CHANNEL_CONFIG_1_INPIPE ): bWrite = False bRead = True if (cfg.OptionalFeatureSupport & _ft. FT_CONFIGURATION_OPTIONAL_FEATURE_ENABLENOTIFICATIONMESSAGE_INCHALL ): logging.debug( "invalid chip configuration: notification callback is set") D3XX.close() return False # delete invalid channels for channel in range(len(channelsToTest) - 1, -1, -1): if (channelsToTest[channel] >= numChannels): del channelsToTest[channel] if (len(channelsToTest) == 0): D3XX.close() return # process loopback for all channels error = ProcessStreaming(D3XX, channelsToTest, transferSize, transferIteration, bWrite, bRead, bStressTest) if error: DisplayTroubleshootingGuide("STREAMER", devList, devIndex, cfg) D3XX.close() D3XX = 0 return True
#coding:utf-8 # Python2.7.12 x86 # WangXuan import sys import ftd3xx from struct import pack if sys.platform == 'win32': import ftd3xx._ftd3xx_win32 as _ft elif sys.platform == 'linux2': import ftd3xx._ftd3xx_linux as _ft if __name__ == '__main__': D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: print("ERROR: Can't find or open Device!") sys.exit() if (sys.platform == 'win32' and D3XX.getDriverVersion() < 0x01020006): print("ERROR: Old kernel driver version. Please update driver!") D3XX.close() sys.exit() devDesc = D3XX.getDeviceDescriptor() if devDesc.bcdUSB < 0x300: print("Warning: Device is NOT connected using USB3.0 cable or port!") cfg = D3XX.getChipConfiguration() numChannels = [4, 2, 1, 0, 0] numChannels = numChannels[cfg.ChannelConfig]
def DemoDescriptors(): if sys.platform == 'linux': DemoTurnOffPipeThreads() D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # getVIDPID VID, PID = D3XX.getVIDPID() logging.debug("VID = %#04X" % VID) logging.debug("PID = %#04X" % PID) logging.debug("") if sys.platform == 'win32': # getDeviceInfo DEVINFO = D3XX.getDeviceInfo() logging.debug("Type = %d" % DEVINFO['Type']) logging.debug("ID = %#08X" % DEVINFO['ID']) logging.debug("Serial = %s" % DEVINFO['Serial'].decode('utf-8')) logging.debug("Description = %s" % DEVINFO['Description'].decode('utf-8')) logging.debug("") # getDeviceDescriptor DEVDESC = D3XX.getDeviceDescriptor() STRDESCMANU = D3XX.getStringDescriptor(DEVDESC.iManufacturer) STRDESCPROD = D3XX.getStringDescriptor(DEVDESC.iProduct) STRDESCSERN = D3XX.getStringDescriptor(DEVDESC.iSerialNumber) logging.debug("Device Descriptor") logging.debug("\tbLength = %d" % DEVDESC.bLength) logging.debug("\tbDescriptorType = %d" % DEVDESC.bDescriptorType) logging.debug("\tbcdUSB = %#04X (%s)" % (DEVDESC.bcdUSB, "USB2" if (DEVDESC.bcdUSB < 0x300) else "USB3")) logging.debug("\tbDeviceClass = %#02X" % DEVDESC.bDeviceClass) logging.debug("\tbDeviceSubClass = %#02X" % DEVDESC.bDeviceSubClass) logging.debug("\tbDeviceProtocol = %#02X" % DEVDESC.bDeviceProtocol) logging.debug("\tbMaxPacketSize0 = %#02X (%d)" % (DEVDESC.bMaxPacketSize0, DEVDESC.bMaxPacketSize0)) logging.debug("\tidVendor = %#04X" % DEVDESC.idVendor) logging.debug("\tidProduct = %#04X" % DEVDESC.idProduct) logging.debug("\tbcdDevice = %#04X" % DEVDESC.bcdDevice) if sys.platform == 'win32': logging.debug("\tiManufacturer = %#02X (%s)" % (DEVDESC.iManufacturer, str(STRDESCMANU.szString))) logging.debug("\tiProduct = %#02X (%s)" % (DEVDESC.iProduct, str(STRDESCPROD.szString))) logging.debug("\tiSerialNumber = %#02X (%s)" % (DEVDESC.iSerialNumber, str(STRDESCSERN.szString))) else: logging.debug("\tiManufacturer = %#02X" % (DEVDESC.iManufacturer)) logging.debug("\tiProduct = %#02X" % (DEVDESC.iProduct)) logging.debug("\tiSerialNumber = %#02X" % (DEVDESC.iSerialNumber)) logging.debug("\tbNumConfigurations = %#02X" % DEVDESC.bNumConfigurations) logging.debug("") # getConfigurationDescriptor CFGDESC = D3XX.getConfigurationDescriptor() logging.debug("Configuration Descriptor") logging.debug("\tbLength = %d" % CFGDESC.bLength) logging.debug("\tbDescriptorType = %d" % CFGDESC.bDescriptorType) logging.debug("\twTotalLength = %#04X (%d)" % (CFGDESC.wTotalLength, CFGDESC.wTotalLength)) logging.debug("\tbNumInterfaces = %#02X" % CFGDESC.bNumInterfaces) logging.debug("\tbConfigurationValue = %#02X" % CFGDESC.bConfigurationValue) logging.debug("\tiConfiguration = %#02X" % CFGDESC.iConfiguration) bSelfPowered = "Self-powered" if ( CFGDESC.bmAttributes & _ft.FT_SELF_POWERED_MASK) else "Bus-powered" bRemoteWakeup = "Remote wakeup" if (CFGDESC.bmAttributes & _ft.FT_REMOTE_WAKEUP_MASK) else "" logging.debug("\tbmAttributes = %#02X (%s %s)" % (CFGDESC.bmAttributes, bSelfPowered, bRemoteWakeup)) logging.debug("\tMaxPower = %#02X (%d mA)" % (CFGDESC.MaxPower, CFGDESC.MaxPower)) logging.debug("") # getInterfaceDescriptor # getPipeInformation for i in range(CFGDESC.bNumInterfaces): IFDESC = D3XX.getInterfaceDescriptor(i) logging.debug("\tInterface Descriptor [%d]" % i) logging.debug("\t\tbLength = %d" % IFDESC.bLength) logging.debug("\t\tbDescriptorType = %d" % IFDESC.bDescriptorType) logging.debug("\t\tbInterfaceNumber = %#02X" % IFDESC.bInterfaceNumber) logging.debug("\t\tbAlternateSetting = %#02X" % IFDESC.bAlternateSetting) logging.debug("\t\tbNumEndpoints = %#02X" % IFDESC.bNumEndpoints) logging.debug("\t\tbInterfaceClass = %#02X" % IFDESC.bInterfaceClass) logging.debug("\t\tbInterfaceSubClass = %#02X" % IFDESC.bInterfaceSubClass) logging.debug("\t\tbInterfaceProtocol = %#02X" % IFDESC.bInterfaceProtocol) logging.debug("\t\tiInterface = %#02X" % IFDESC.iInterface) logging.debug("") for j in range(IFDESC.bNumEndpoints): PIPEIF = D3XX.getPipeInformation(i, j) logging.debug("\t\tPipe Information [%d]" % j) logging.debug("\t\t\tPipeType = %d" % PIPEIF.PipeType) logging.debug("\t\t\tPipeId = %#02X" % PIPEIF.PipeId) logging.debug("\t\t\tMaximumPacketSize = %#02X" % PIPEIF.MaximumPacketSize) logging.debug("\t\t\tInterval = %#02X" % PIPEIF.Interval) logging.debug("") D3XX.close() D3XX = 0 return True
def DemoLoopback(bStreamingMode=False): result = True channel = 0 if sys.platform == 'linux': epout = channel epin = channel else: epout = 0x02 + channel epin = 0x82 + channel size = 4096 logging.debug("Write/read synchronous loopback of string") D3XX = ftd3xx.create(0, _ft.FT_OPEN_BY_INDEX) if D3XX is None: logging.debug( "ERROR: Please check if another D3XX application is open!") return False # enable streaming mode if bStreamingMode and sys.platform == 'linux': D3XX.setStreamPipe(epout, size) D3XX.setStreamPipe(epin, size) # if python 2.7.12 if sys.version_info.major == 2: for x in range(0, 10): buffwrite = ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(size)) bytesWritten = D3XX.writePipe(epout, buffwrite, size) bytesRead = 0 buffread = "" while (bytesRead < bytesWritten): output = D3XX.readPipeEx(epin, bytesWritten - bytesRead) bytesRead += output['bytesTransferred'] buffread += output['bytes'] # compare data compare = True if (buffread[:bytesRead] != buffwrite[:bytesWritten]): compare = False logging.debug( "[%d] writePipe [%d] bytes, readPipe [%d] bytes, compare = %s" % (x, bytesWritten, bytesRead, compare)) if compare == False: result = False break # elif python 3.5.2 # version 3 does not support implicit bytes to string conversion elif sys.version_info.major == 3: # flush old crap out first D3XX.readPipeEx(epin, size, raw=True, timeout=0) for x in range(0, 10): buffwrite = ''.join( random.choice(string.ascii_letters + string.digits) for _ in range(size)) # text4k = '0123456789ABCDEF' * (4096 // 16) # buffwrite = ('<START %02d>' % x) + text4k[:-29] + '<qwerty_END_asdfgh>' # buffwrite = (chr(ord('A') + x) * 10) + text4k[:-20] + (chr(ord('a') + x) * 10) # buffwrite = (chr(ord('A') + x) * 10) + text4k[:-20] + (chr(ord('a') + x) * 10) buffwrite = buffwrite.encode('latin1') bytesWritten = D3XX.writePipe(epout, buffwrite, size) bytesRead = 0 buffread = bytes() while (bytesRead < bytesWritten): output = D3XX.readPipeEx(epin, bytesWritten - bytesRead, raw=True, timeout=0) bytesRead += output['bytesTransferred'] buffread += output['bytes'] # compare data compare = True logging.debug(buffwrite[:bytesWritten]) logging.debug(buffread[:bytesRead]) if (buffread[:bytesRead] != buffwrite[:bytesWritten]): compare = False logging.debug( "[%d] writePipe [%d] bytes, readPipe [%d] bytes, compare = %s" % (x, bytesWritten, bytesRead, compare)) if compare == False: result = False break # disable streaming mode if bStreamingMode and sys.platform == 'linux': D3XX.clearStreamPipe(epout) D3XX.clearStreamPipe(epin) D3XX.close() D3XX = 0 logging.debug("") return result