class I2C(object):
def __init__(self, interface=ftdi.INTERFACE_A, scl=0, sda=1,
- vid=0x0403, pid=0x6011, delay=0.001, timeout=100):
+ vid=0x0403, pid=0x6011, delay=0.001, timeout=100,
+ pindir=0xc0, pinstate=0xc0):
"""Bitbanged I2C for use on FT4232H ports C and D.
interface = [0,1,2,3]
scl/sda = port pin numbers
+ pindir = initial bitfield of pin directions
+ pinstate = initial pin state
"""
self.interface = interface
self.scl = scl
ftdi.usb_purge_buffers(self.context)
self.started = False
+ self.pinDir(pindir)
+ self.pinState(pinstate)
+
def _clear_scl(self):
self.io[self.scl] = 1
ftdi.set_bitmode(self.context, int(self.io), ftdi.BITMODE_BITBANG)
data.append(self._read_byte(0, stop=True))
return data
+ def pinDir(self, dirs):
+ """dirs bitfield has '1' for output, ignores scl/sda bit positions.
+ """
+ d = intbv(dirs, max=2**8)
+ d[self.scl] = self.io[self.scl]
+ d[self.sda] = self.io[self.sda]
+ print 'io:', b(self.io, 8), '-->',
+ self.io[:] = d
+ print b(self.io, 8)
+ ftdi.set_bitmode(self.context, int(self.io), ftdi.BITMODE_BITBANG)
+
+ def pinState(self, bitfield):
+ """Set GPIO pins to given bitfield state, ignoring scl/sda pins."""
+ b = intbv(bitfield, max=2**8)
+ b[self.scl] = self.io[self.scl]
+ b[self.sda] = self.io[self.sda]
+ print 'port:', b(self.port, 8), '-->',
+ self.port[:] = d
+ print b(self.port, 8)
+ ftdi.write_data(self.context, chr(self.port), 1)
+
+ def RESET(self, value):
+ """Set pin connected to RESET line on devboard."""
+ reset_pin = 6
+ self.port[reset_pin] = intbv(value, max=2**1)
+ ftdi.write_data(self.context, chr(self.port), 1)
+
+ def NCO_CLK(self, value):
+ """Set pin connected to NCO_CLK line on devboard."""
+ clk_pin = 7
+ self.port[clk_pin] = intbv(value, max=2**1)
+ ftdi.write_data(self.context, chr(self.port), 1)
+
+
class NCO(object):
RST_POS = 14