Example #1
0
 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()
Example #2
0
 def open(self, usb_reset=True):
     self._debug(3, "MPSSE: Open")
     ftdi.usb_open(self._ctx, FT232H_VID, FT232H_PID)
     if usb_reset:
         ftdi.usb_reset(self._ctx)
     ftdi.read_data_set_chunksize(self._ctx, 65535)
     ftdi.write_data_set_chunksize(self._ctx, 65535)
     # RESET MPSSE
     ftdi.set_bitmode(self._ctx, 0, 0)
     # Enable MPSSE
     ftdi.set_bitmode(self._ctx, 0, 2)
     # Set Latency timer to 16ms
     ftdi.set_latency_timer(self._ctx, 16)
Example #3
0
	def __init__(self):
		self.ftdic = ftdi1.new()
		print "usb_open ret = ", ftdi1.usb_open(self.ftdic, 0x0403, 0x8c81)
		print "set_bitmode ret = ", ftdi1.set_bitmode(self.ftdic, 0x00, ftdi1.BITMODE_RESET)		# Reset port to normal operation
		print "set_baudrate ret = ", ftdi1.set_baudrate(self.ftdic, 115200)		# 115200 baud
		print "setflowctrl ret = ", ftdi1.setflowctrl(self.ftdic, ftdi1.SIO_DISABLE_FLOW_CTRL)
		print "setdtr ret = ", ftdi1.setdtr(self.ftdic, 1)
		print "setrts ret = ", ftdi1.setrts(self.ftdic, 1)
 def open_comms(self):
     self.ftdic = ftdi.new()
     ret = ftdi.usb_open(self.ftdic, self.idVendor, self.idProduct)
     print ret
     ret = ftdi.usb_purge_buffers(self.ftdic)
     print ret
     ret = ftdi.set_bitmode(self.ftdic, 0xFF, ftdi.BITMODE_BITBANG)
     print ret
Example #5
0
def switch(mode):
    global c
    c = ftdi.new()
    # TODO(benjaminfair): error checking
    ftdi.usb_open_desc(c, 0x0403, 0x6001, None, os.environ.get("FTDI_SERIAL"))
    ftdi.set_bitmode(c, 0xFF, ftdi.BITMODE_BITBANG)
    atexit.register(ftdi.free, c)

    disable()

    if mode == 'run':
        set(SWITCH)
    elif mode == 'debug':
        clear(SWITCH)
    elif mode == 'disable':
        return

    enable()
Example #6
0
def setNRST(ftdic, value):
    global bitmode
    if (value):
        bitmode |= NRST
    else:
        bitmode &= ~NRST
    ret = ftdi.set_bitmode(ftdic, bitmode, ftdi.BITMODE_CBUS)
    if ret < 0:
        print('Cannot enable bitbang mode')
        os._exit(1)
Example #7
0
def read(ftdic, length):
    data = '\0' * length
    tc = ftdi1.read_data_submit(ftdic, data, length)
    if not tc:
        raise Exception('FTDI read error')
    ret = ftdi1.transfer_data_done(tc)
    if ret < 0:
        raise Exception(ftdi1.get_error_string(ftdic))
    return list(bytearray(data))


ftdic = ftdi1.new()
ftdi1.usb_open_desc_index(ftdic, ftdi_vendor, ftdi_product, None, None, 0)
ftdi1.usb_reset(ftdic)
ftdi1.set_interface(ftdic, ftdi1.INTERFACE_A)
ftdi1.set_bitmode(ftdic, 0, ftdi1.BITMODE_MPSSE)

b = []

# Calculate required clock rate, we bitbang out a clock on the data bit,
# so we need to run at the clock_rate * 2
hz = clock_rate * 2
numerator = 30000000.0
if hz < 1000:
    b += cmd(ftdi1.EN_DIV_5)
    numerator /= 5.0
else:
    b += cmd(ftdi1.DIS_DIV_5)
divisor = int(round(numerator / hz - 1))
if divisor < 0:
    divisor = 0
Example #8
0
def red(context):
    actuate(context, RED_ON)
    actuate(context, GREEN_OFF)


def green(context):
    # turn on Green light
    actuate(context, GREEN_ON)
    actuate(context, RED_OFF)


def off(context):
    actuate(context, RED_OFF)
    actuate(context, GREEN_OFF)


context = ftd.new()
ftd.usb_open(context, 0x0403, 0x6001)
ftd.set_bitmode(context, 0xff, ftd.BITMODE_BITBANG)
# Everything off --- shouldn't be necessary.
ftd.write_data(context, bytes([0x00]))

if len(sys.argv) == 1:
    off(context)
elif sys.argv[1] == 'red':
    red(context)
elif sys.argv[1] == 'green':
    green(context)
else:
    print("Usage: lavalamps [red|green]")
Example #9
0
              (ret, ftdi.get_error_string(ftdic)))
        os._exit(1)
    print('Device #%d: manufacturer="%s" description="%s" serial="%s"\n' %
          (i, manufacturer, description, serial))
    curnode = curnode.next
    i += 1

# open usb
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)

# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)

#for i in range( 8 ):
val = 0x0
print('enabling bit #%d (0x%02x)' % (i, val))
ftdi.write_data(ftdic, chr(val))
time.sleep(5.5)

val = 0x2
print('enabling bit #%d (0x%02x)' % (i, val))
ftdi.write_data(ftdic, chr(val))
time.sleep(5.5)
Example #10
0
#!/usr/bin/python3

import ftdi1
f = ftdi1.new()
ftdi1.usb_open(f, 0x0403, 0x6001)
ftdi1.set_bitmode(f, 1, 0)
ftdi1.write_data(f, bytes([0]))
ftdi1.write_data(f, bytes([1]))
ftdi1.usb_close(f)
Example #11
0
        os._exit(1)
    print('#%d: manufacturer="%s" description="%s" serial="%s"\n' %
          (i, manufacturer, description, serial))
    curnode = curnode.next
    i += 1

# open usb
ret = ftdi.usb_open(ftdic, 0x0403, 0x6001)
if ret < 0:
    print('unable to open ftdi device: %d (%s)' %
          (ret, ftdi.get_error_string(ftdic)))
    os._exit(1)


# bitbang
ret = ftdi.set_bitmode(ftdic, 0xff, ftdi.BITMODE_BITBANG)
if ret < 0:
    print('Cannot enable bitbang')
    os._exit(1)
print('turning everything on')
ftdi.write_data(ftdic, chr(0xff), 1)
time.sleep(1)
print('turning everything off\n')
ftdi.write_data(ftdic, chr(0x00), 1)
time.sleep(1)
for i in range(8):
    val = 2 ** i
    print('enabling bit #%d (0x%02x)' % (i, val))
    ftdi.write_data(ftdic, chr(val), 1)
    time.sleep(1)
ftdi.disable_bitbang(ftdic)
Example #12
0
# Initialize FTDI device connection.
ctx = ftdi.new()
ftdi.usb_open(ctx, 0x263d, 0x4001)
ftdi.usb_reset(ctx)

# Disable flow control. Commented out because it is unclear if this is necessary.
#self._check(ftdi.setflowctrl, ftdi.SIO_DISABLE_FLOW_CTRL)

# Change read & write buffers to maximum size, 65535 bytes.
ftdi.read_data_set_chunksize(ctx, 65535)
ftdi.write_data_set_chunksize(ctx, 65535)

# Clear pending read data & write buffers.
ftdi.usb_purge_buffers(ctx)
# Reset MPSSE
ftdi.set_bitmode(ctx, 0, 0)
# Enable MPSSE
ftdi.set_bitmode(ctx, 0, 2)


def ftwrite(string):
    length = len(string)
    ret = ftdi.write_data(ctx, string, length)
    if ret < 0:
        raise RuntimeError('ftdi_write_data failed with error {0}: {1}'.format(
            ret, ftdi.get_error_string(self._ctx)))
    if ret != length:
        raise RuntimeError(
            'ftdi_write_data expected to write {0} bytes but actually wrote {1}!'
            .format(length, ret))
Example #13
0
#    if ret < 0:
#        print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( ftdictx ) ) )
#        os._exit( 1 )
#    print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
#    curnode = curnode.next
#    i += 1

# Open USB to the first FT232H							 
# TODO: Way to select which FT232H if multiple found, using usb_open_dev() with the device instance from devlist[]
ret = ftdi.usb_open( ftdictx, FT232H_VID, FT232H_PID )
if ret < 0:
    print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( ftdictx ) ) )
    os._exit( 1 )

# Reset MPSSE by sending mask = 0x00 and mode = BITMODE_RESET
ret = ftdi.set_bitmode(ftdictx, 0x00, ftdi.BITMODE_RESET)
if ret < 0:
    print( 'unable to reset bitmode: %d (%s)' % ( ret, ftdi.get_error_string( ftdictx ) ) )
    os._exit( 1 )

# Configure the mode (see http://www.ftdichip.com/Support/Documents/DataSheets/ICs/DS_FT232H.pdf, section 4
#  http://www.ftdichip.com/Support/Documents/AppNotes/AN_232R-01_Bit_Bang_Mode_Available_For_FT232R_and_Ft245R.pdf,
#  and http://www.intra2net.com/en/developer/libftdi/documentation/ftdi_8h.html#a2250f7812c87a96ada7fd4f19b82f666)
ret = ftdi.set_bitmode( ftdictx, 0x00, ftdi.BITMODE_SYNCBB )		# Synchronous BitBang, D0-D7 input
if ret < 0:
    print( 'unable to set bitmode to syncbb: %d (%s)' % ( ret, ftdi.get_error_string( ftdictx ) ) )
    os._exit( 1 )

## Configure baud rate
ret = ftdi.set_baudrate(ftdictx, BAUD)
if ret < 0: