Add GPIO capability to unused I2C bitbang pins
authorDan White <dan@whiteaudio.com>
Sun, 13 May 2012 00:55:11 +0000 (19:55 -0500)
committerDan White <dan@whiteaudio.com>
Sun, 13 May 2012 00:55:11 +0000 (19:55 -0500)
python-lib/usbio.py

index 578dc3594e9d4e177a33d23463f7cf5ee7f8bfb8..9878dc4c896a041359934854b55fe0434eee53dc 100644 (file)
@@ -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