From da113145e086b8f70b9cf9a9922ff0f16d02a8ac Mon Sep 17 00:00:00 2001 From: Dan White Date: Sat, 12 May 2012 19:55:11 -0500 Subject: [PATCH] Add GPIO capability to unused I2C bitbang pins --- python-lib/usbio.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/python-lib/usbio.py b/python-lib/usbio.py index 578dc35..9878dc4 100644 --- a/python-lib/usbio.py +++ b/python-lib/usbio.py @@ -70,11 +70,14 @@ def str2hex(x): 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 @@ -104,6 +107,9 @@ class I2C(object): 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) @@ -231,6 +237,40 @@ class I2C(object): 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 -- 2.25.1