71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import os
|
|
import ftdi1 as ftdi
|
|
#import time
|
|
|
|
class ftio(object):
|
|
|
|
#self.ftdic = None
|
|
|
|
def __init__(self):
|
|
# initialize
|
|
self.ftdic = ftdi.new()
|
|
if self.ftdic == 0:
|
|
print( 'new failed: %d', ret )
|
|
os._exit( 1 )
|
|
|
|
# list all devices
|
|
ret, devlist = ftdi.usb_find_all( self.ftdic, 0x0403, 0x6001 )
|
|
if ret < 0:
|
|
print( 'ftdi_usb_find_all failed: %d (%s)' % ( ret, ftdi.get_error_string( self.ftdic ) ) )
|
|
os._exit( 1 )
|
|
print( 'Number of FTDI devices found: %d\n' % ret )
|
|
curnode = devlist
|
|
i = 0
|
|
while( curnode != None ):
|
|
ret, manufacturer, description, serial = ftdi.usb_get_strings( self.ftdic, curnode.dev )
|
|
if ret < 0:
|
|
print( 'ftdi_usb_get_strings failed: %d (%s)' % ( ret, ftdi.get_error_string( self.ftdic ) ) )
|
|
os._exit( 1 )
|
|
print( 'Device #%d: manufacturer="%s" description="%s" serial="%s"\n' % ( i, manufacturer, description, serial ) )
|
|
curnode = curnode.next
|
|
i += 1
|
|
#return self.ftdic
|
|
|
|
#int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode);
|
|
def open(self, mask):
|
|
# open usb
|
|
ret = ftdi.usb_open( self.ftdic, 0x0403, 0x6001 )
|
|
if ret < 0:
|
|
print( 'unable to open ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( self.ftdic ) ) )
|
|
os._exit( 1 )
|
|
# bitbang
|
|
ret = ftdi.set_bitmode( self.ftdic, mask, ftdi.BITMODE_BITBANG )
|
|
if ret < 0:
|
|
print( 'Cannot enable bitbang' )
|
|
os._exit( 1 )
|
|
|
|
def read_id(self):
|
|
# read chip id
|
|
ret, chipid = ftdi.read_chipid( self.ftdic )
|
|
print( 'FDTI chip id: %X\n' % chipid )
|
|
|
|
def close(self):
|
|
# close usb
|
|
ret = ftdi.usb_close( self.ftdic )
|
|
if ret < 0:
|
|
print( 'unable to close ftdi device: %d (%s)' % ( ret, ftdi.get_error_string( self.ftdic ) ) )
|
|
os._exit( 1 )
|
|
ftdi.free( self.ftdic )
|
|
|
|
# int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size);
|
|
def poke(self, value):
|
|
ftdi.write_data( self.ftdic, chr(value), 1 )
|
|
|
|
# int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size);
|
|
def peek(self):
|
|
ret, pins = ftdi.read_pins( self.ftdic )
|
|
return ret, ord(pins[0])
|