def close(self): """ Close the FTDI device. Will be automatically called when the program ends. """ if self._bus is not None: ftdi.free(self._bus) self._bus = None
def enumerate_device_serials(vid=FT232H_VID, pid=FT232H_PID): """Return a list of all FT232H device serial numbers connected to the machine. You can use these serial numbers to open a specific FT232H device by passing it to the FT232H initializer's serial parameter. """ try: # Create a libftdi context. ctx = None ctx = ftdi.new() # Enumerate FTDI devices. device_list = None count, device_list = ftdi.usb_find_all(ctx, vid, pid) if count < 0: raise RuntimeError('ftdi_usb_find_all returned error {0}: {1}'.format(count, ftdi.get_error_string(self._ctx))) # Walk through list of devices and assemble list of serial numbers. devices = [] while device_list is not None: # Get USB device strings and add serial to list of devices. ret, manufacturer, description, serial = ftdi.usb_get_strings(ctx, device_list.dev, 256, 256, 256) if serial is not None: devices.append(serial) device_list = device_list.next return devices finally: # Make sure to clean up list and context when done. if device_list is not None: ftdi.list_free(device_list) if ctx is not None: ftdi.free(ctx)
def close(self): if self._ctx is not None: self._debug(3, "MPSSE: Max buffer: {}".format(self._max_buffer)) self._debug(3, "MPSSE: Closed. FTDI Released") ftdi.free(self._ctx) self._ctx = None self._od = False self._rc = False
def openDevice(vendor, product, lowByteValue, lowByteDirection): context = ftdi1.new() assert context try: _ftdi1_check(context, ftdi1.usb_open, vendor, product) return Mpsse(context, lowByteValue, lowByteDirection) except: ftdi1.free(context) raise
def __exit__(self, *args): """Deinitialize FTDI context and close USB connection.""" self.purge() self.reset() if self.ftdic is not None: #ftdi.set_bitmode(self.ftdic, 0, ftdi.BITMODE_RESET) ftdi.free(self.ftdic) # free -> deinit -> usb_close_internal -> usb_close if self._debug_ftdi: self._debug("[FTDI] exit")
def __del__(self): # close usb ret = ftdi1.usb_close(self.ftdic) if ret < 0: print('unable to close ftdi device: %d (%s)' % (ret, ftdi1.get_error_string(self.ftdic))) print ('device closed') ftdi1.free(self.ftdic)
def __init__(self, VID=0x0403, PID=0x6010, _debug_out=None): """Open USB connection and initialize FTDI context.""" context = ftdi.new() if not (ftdi.usb_open(context, VID, PID) == 0): ftdi.free(context) self.ftdic = None raise IOError('could not open USB connection!') ftdi.set_bitmode(context, 0, ftdi.BITMODE_SYNCFF) self.ftdic = context self._debug_ftdi = False self._debug_out = _debug_out self._debug_lock = threading.Lock()
def main(): """Main program""" restart = True ftdic = ftdi.new() serial = None if len(sys.argv) > 1: serial = sys.argv[1] ret = ftdi.usb_open_desc(ftdic, 0x0403, 0x6001, None, serial) if ret < 0: print("ftdi.usb_open(): %d" % ret, file=sys.stderr) sys.exit(1) ret = ftdi.read_eeprom(ftdic) if ret < 0: print("ftdi.read_eeprom(): %d" % ret, file=sys.stderr) sys.exit(1) ret = ftdi.eeprom_decode(ftdic, True) if ret < 0: print("ftdi.eeprom_decode(): %d" % ret, file=sys.stderr) sys.exit(1) print() set_eeprom_value(ftdic, ftdi.CBUS_FUNCTION_0, ftdi.CBUS_TXLED) set_eeprom_value(ftdic, ftdi.CBUS_FUNCTION_1, ftdi.CBUS_RXLED) set_eeprom_value(ftdic, ftdi.CBUS_FUNCTION_2, ftdi.CBUS_IOMODE) set_eeprom_value(ftdic, ftdi.CBUS_FUNCTION_3, ftdi.CBUS_IOMODE) ret = ftdi.eeprom_build(ftdic); if ret < 0: print("ftdi.eeprom_build(): %d" % ret, file=sys.stderr) sys.exit(1) print("New EEPROM settings:") ret = ftdi.eeprom_decode(ftdic, True) if ret < 0: print("ftdi.eeprom_decode(): %d" % ret, file=sys.stderr) sys.exit(1) ret = ftdi.write_eeprom(ftdic); if ret < 0: print("ftdi.write_eeprom(): %d" % ret, file=sys.stderr) sys.exit(1) ftdi.free(ftdic)
def id(self): # pylint: disable=invalid-name,too-many-branches,too-many-return-statements """Return a unique id for the detected chip, if any.""" # There are some times we want to trick the platform detection # say if a raspberry pi doesn't have the right ID, or for testing try: return os.environ['BLINKA_FORCECHIP'] except KeyError: # no forced chip, continue with testing! pass # Special case, if we have an environment var set, we could use FT232H try: if os.environ['BLINKA_FT232H']: # we can't have ftdi1 as a dependency cause its wierd # to install, sigh. import ftdi1 as ftdi # pylint: disable=import-error try: ctx = None ctx = ftdi.new() # Create a libftdi context. # Enumerate FTDI devices. count, _ = ftdi.usb_find_all(ctx, 0, 0) if count < 0: raise RuntimeError( 'ftdi_usb_find_all returned error %d : %s' % count, ftdi.get_error_string(self._ctx)) if count == 0: raise RuntimeError('BLINKA_FT232H environment variable' + \ 'set, but no FT232H device found') finally: # Make sure to clean up list and context when done. if ctx is not None: ftdi.free(ctx) return FT232H except KeyError: # no FT232H environment var pass platform = sys.platform if platform == "linux" or platform == "linux2": return self._linux_id() if platform == "esp8266": return ESP8266 if platform == "samd21": return SAMD21 if platform == "pyboard": return STM32 # nothing found! return None
def main(): """Main program""" context = ftdi.new() version_info = ftdi.get_library_version() print("[FTDI version] major: %d, minor: %d, micro: %d" \ ", version_str: %s, snapshot_str: %s" % (version_info.major, version_info.minor, version_info.micro, version_info.version_str, version_info.snapshot_str)) # try to open an ftdi 0x6010 or 0x6001 ret = ftdi.usb_open(context, 0x0403, 0x6010) if ret < 0: ret = ftdi.usb_open(context, 0x0403, 0x6001) print("ftdi.usb_open(): %d" % ret) print("ftdi.set_baudrate(): %d" % ftdi.set_baudrate(context, 9600)) ftdi.free(context)
def main(): """Main program""" context = ftdi.new() version_info = ftdi.get_library_version() print("[FTDI version] major: %d, minor: %d, micro: %d" \ ", version_str: %s, snapshot_str: %s" % (version_info.major, version_info.minor, version_info.micro, version_info.version_str, version_info.snapshot_str)) # try to open an ftdi 0x6010 or 0x6001 ret = ftdi.usb_open(context, UM232H_B_VID, UM232H_B_PID) if ret < 0: ret = ftdi.usb_open(context, UM232H_B_VID, UM232H_B_PID) print("ftdi.usb_open(): %d" % ret) print("ftdi.set_baudrate(): %d" % ftdi.set_baudrate(context, 9600)) ftdi.free(context)
def bruteforce_connection_props(): for bits in [ftdi.BITS_8]: #[ftdi.BITS_7, ftdi.BITS_8]: for sbit in [ftdi.STOP_BIT_1, ftdi.STOP_BIT_15, ftdi.STOP_BIT_2]: for parity in [ ftdi.NONE, ftdi.ODD, ftdi.EVEN, ftdi.MARK, ftdi.SPACE ]: for break_ in [ftdi.BREAK_ON]: #, ftdi.BREAK_OFF]: print(bits, sbit, parity, break_) ctx = ftdi.new() ret = ftdi.usb_open(ctx, 0x0760, 0x0002) assert ret == 0 ret = ftdi.set_baudrate(ctx, 38400) assert ret == 0, 'baudrate error' ftdi.set_line_property2(ctx, bits, sbit, parity, break_) send_msg_led(ctx, 2, 0x49, True) input('waiting') ftdi.free(ctx)
def __init__(self, num_pixels, num_rows): # Create a libftdi context. ctx = None ctx = ftdi.new() # Define USB vendor and product ID vid = 0x0403 pid = 0x6014 # Enumerate FTDI devices. self.serial = None device_list = None count, device_list = ftdi.usb_find_all(ctx, vid, pid) while device_list is not None: # Get USB device strings and add serial to list of devices. ret, manufacturer, description, serial = ftdi.usb_get_strings(ctx, device_list.dev, 256, 256, 256) print 'return: {0}, manufacturer: {1}, description: {2}, serial: |{3}|'.format(ret,manufacturer,description,serial) if 'FTDI' in manufacturer and 'Serial' in description and 'FT' in serial: self.serial = serial device_list = device_list.next # Make sure to clean up list and context when done. if device_list is not None: ftdi.list_free(device_list) if ctx is not None: ftdi.free(ctx) # Create an FT232H object. self.ft232h = FT232H.FT232H(serial=self.serial) # Create an FT232H object. self.ft232h = FT232H.FT232H() # Create a SPI interface for the FT232H object. Set the SPI bus to 6mhz. self.spi = FT232H.SPI(self.ft232h, max_speed_hz=SPI_BAUD) # Create a pixel data buffer and lookup table. self.buffer = bytearray(num_pixels*BYTES_PER_PIXEL*3) self.lookup = self.build_byte_lookup() #print self.lookup self.set_brightness(MAX_INTENSITY/2) #set brightness to 25% by default self.num_pixels = num_pixels self.rows = num_rows self.cols = num_pixels/num_rows
def main(): ctx = ftdi.new() ret = ftdi.usb_open(ctx, 0x0760, 0x0002) assert ret == 0, 'ftdi.usb_open error: {}'.format(ret) ret = ftdi.set_baudrate(ctx, 38400) assert ret == 0, 'baudrate error' try: code.interact(local={ 'ctx': ctx, 'ftdi': ftdi, 'read_continuously': read_continuously, 'parse_continuously': parse_continuously, 'send_msg_slider': send_msg_slider, 'send_msg_led': send_msg_led, 'bruteforce_properties': bruteforce_properties, }, banner='ftdi & ctx imported', exitmsg='cleaning up!') finally: ftdi.free(ctx) print('done')
print 'Gathering data. Press Ctrl-C to quit.' output = [] try: while True: # Sleep based on baudrate time.sleep(1.0/BAUD) # Read the input and store to array ret, pins = ftdi.read_pins(ftdictx) # reads pins directly, needs sleep time to be exact output.append(pins) #ret, databuf = ftdi.read_data(ftdictx) # reads the data buffer, no need for exact timing (just fast enough to keep buffer from overflowing) #output.extend(databuf) except KeyboardInterrupt: pass ## Write captured data to file with open("output.bin", "wb") as outfile: writedata = bytearray(output) outfile.write(writedata) ## Cleanup: free devlist, reset bitmode, close usb, etc ftdi.list_free(devlist) ftdi.disable_bitbang(ftdictx) ftdi.set_bitmode(ftdictx, 0x00, ftdi.BITMODE_RESET) ret = ftdi.usb_close( ftdictx ) if ret < 0: print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdictx ) ) ) os._exit( 1 ) print ('device closed') ftdi.free( ftdictx )
def close(self): """Close the FTDI device. Will be automatically called when the program ends.""" if self._ctx is not None: ftdi.free(self._ctx) self._ctx = None
# read eeprom eeprom_addr = 1 ret, eeprom_val = ftdi.read_eeprom_location(ftdic, eeprom_addr) if (ret == 0): print('eeprom @ %d: 0x%04x\n' % (eeprom_addr, eeprom_val)) print('eeprom:') ret = ftdi.read_eeprom(ftdic) size = 128 ret, eeprom = ftdi.get_eeprom_buf(ftdic, size) if (ret == 0): for i in range(size): octet = eeprom[i] if sys.version_info[0] < 3: # python 2 octet = ord(octet) sys.stdout.write('%02x ' % octet) if (i % 8 == 7): print('') print('') # close usb ret = ftdi.usb_close(ftdic) if ret < 0: print('unable to close ftdi device: %d (%s)' % (ret, ftdi.get_error_string(ftdic))) os._exit(1) print ('device closed') ftdi.free(ftdic)
# read eeprom eeprom_addr = 1 ret, eeprom_val = ftdi.read_eeprom_location(ftdic, eeprom_addr) if (ret == 0): print('eeprom @ %d: 0x%04x\n' % (eeprom_addr, eeprom_val)) print('eeprom:') ret = ftdi.read_eeprom(ftdic) size = 128 ret, eeprom = ftdi.get_eeprom_buf(ftdic, size) if (ret == 0): for i in range(size): if isinstance(eeprom[i], str): octet = ord(eeprom[i]) # python2 else: octet = eeprom[i] # python3 sys.stdout.write('%02x ' % octet) if (i % 8 == 7): print('') print('') # close usb ret = ftdi.usb_close(ftdic) if ret < 0: print('unable to close ftdi device: %d (%s)' % (ret, ftdi.get_error_string(ftdic))) os._exit(1) print('device closed') ftdi.free(ftdic)
def __exit__(self, exeptype, value, traceback): ftdi.free(self.context)