Use custom SPI interface with libftdi
authorDan White <dan@whiteaudio.com>
Sun, 14 Oct 2012 03:13:45 +0000 (22:13 -0500)
committerDan White <dan@whiteaudio.com>
Sun, 14 Oct 2012 03:13:45 +0000 (22:13 -0500)
python-lib/calibrate.py
python-lib/mpsse-test.py
python-lib/slope-test.py
python-lib/usbio.py

index 66cfa3c41db3af5328dff3b00f377f26e0035280..15db71b8238de2aa9e7cc125a5c758a77c39b462 100644 (file)
@@ -186,12 +186,17 @@ def mux_b_offset(x, c):
 def chain_a_offset(x, n, mux_offset):
     xi = int(round(x))
     cn = chain.h[n]
+
+    #cn.nco.rst = 0
+    #cn.nco.fcw = 1
+
     cn.cal = 1
     cn.otaA.se = 0
     cn.otaA.cint = 0
     cn.otaA.offset = xi
     cn.otaA.gain = 15
     chain.write()
+
     sleep(0.5)
     adc.read()
     values = []
index 1cfae60d37071a82259d7b7f7ac6c5e0d780fe5e..ba0294191095ed9a2b4dd3cc7d0e03620bf0b42e 100755 (executable)
@@ -143,8 +143,8 @@ def get_i_supply():
 spi0 = usbio.AtoiSPI0(1000e3) #port A
 
 spi1 = usbio.AtoiSPI1(1000e3)  #port B
-spi1.context.pidle = spi1.context.pstop = 0xf8
-spi1.Stop()
+#spi1.context.pidle = spi1.context.pstop = 0xf8
+#spi1.Stop()
 
 i2c = usbio.I2C(interface=usbio.ftdi.INTERFACE_C, scl=0, sda=1,
         vid=0x0403, pid=0x6011, delay=100e-6)
index 225029613c8a10f8462a3e38aff3bf7207235afa..2236025237b90933df39185c3fb016eedd08bb4b 100644 (file)
@@ -25,18 +25,21 @@ if 1:
     dac.vbias_core(0.05)
     dac.vbias_buf(0.05)
 
+    #c0.nco.fcw = 1
+    #c0.nco.rst = 1
+
     c0.otaA.fast = 1
     c0.otaA.gain = 0
     c0.otaA.se = 1
     c0.otaB.se = 1
     c0.otaA.cint = 1
-    c0.otaA.offset = 0
+    c0.otaA.offset = 63
     chain.write()
 
 
     vcm = 1.25
     vos = vcm + 0.000
-    vdos = -0.02
+    vdos = 0.00
 
     dac.vcmi(vcm)
     dac.vina(vos)
@@ -55,7 +58,7 @@ if 1:
     itime = 0.100
     htime = 0.000
     z()
-    for vd in arange(-0.08, 0.08001, 0.010):
+    for vd in arange(-0.08, 0.08001, 0.005):
         print vd
         vdiff(vd)
         z()
@@ -119,7 +122,7 @@ if 0:
     savefig('dvdt-vs-vd.pdf')
 
 
-if 1:
+if 0:
     interactive(False)
     close('all')
     figure(figsize=(5.0, 4.0))
index 745f79d2a8a4e9aad469629713fb59f43576bd86..d9c0f530355ac638dc8a49a868318476087025f6 100644 (file)
@@ -351,6 +351,7 @@ class SPI(object):
         # setup the driver for this interface, init usb
         self.ftdi = ftdi #local reference to speed lookups
         self.context = self.ftdi.new()
+        self.ftdi.init(self.context)
         self.ftdi.set_interface(self.context, interface)
 
         # TODO: what should chunksize be? 4k is libftdi default,
@@ -369,9 +370,9 @@ class SPI(object):
         self.ftdi.set_bitmode(self.context, int(pindir), self.ftdi.BITMODE_RESET)
         self.ftdi.set_bitmode(self.context, int(pindir), self.ftdi.BITMODE_MPSSE)
         self.set_freq(freq)
-        self._pindir = 0
-        self._pinstate = 0
-        self.set_pins(pinstate, pindir)
+        self._pindir = pindir
+        self._pinstate = pinstate
+        self.set_pins(pinstate, pindir, audit_cs=False)
 
         #cs doesn't (shouldn't) change, cache the values for SPEED
         self._cs_cmd_cache = {}
@@ -443,6 +444,7 @@ class SPI(object):
         states (idle mode).  If audit_cs==False, allows setting the output
         state of the configured chip-select pins.
         """
+        print pinstate, pindir
         if pindir is not None:
             self._pindir = pindir
             #we are changing pin directions, audit them
@@ -458,12 +460,11 @@ class SPI(object):
             if audit_cs:
                 mask = self.cs['_mask'][1]
                 idle = self.cs['_idle'][1]
-                self._pinstate = ((pinstate & mask) +
-                                    (idle & mask))
+                self._pinstate = ((pinstate & mask) | (idle & mask))
             else:
                 self._pinstate = pinstate
 
-        #print 'set pins: %02x, %02x' % (self._pinstate, self._pindir)
+        print 'set pins: %02x, %02x' % (self._pinstate, self._pindir)
         cmd = chr(self.ftdi.SET_BITS_LOW) + chr(self._pinstate) + chr(self._pindir)
         self._raw_write(cmd)
 
@@ -494,7 +495,7 @@ class SPI(object):
         mode, pins = self.cs[device]
 
         # set SK before asserting CS to not violate timing
-        if False:
+        if True:
             # first, ensure SK starts in the correct state
             p = self._pinstate
             #if mode == 0:
@@ -514,7 +515,7 @@ class SPI(object):
             #print 'pins: %02x' % p
 
             p &= ~mask
-            p += pins & mask
+            p |= pins & mask
             self._pinstate = p
             cmd += chr(self.ftdi.SET_BITS_LOW) + chr(p) + chr(self._pindir)
             #print 'set pins: %02x, %02x' % (self._pinstate, self._pindir)
@@ -866,11 +867,11 @@ class Chain(object):
         return tuple(data)
 
     def write(self):
-        self.bus.SetCS(self.csname)
-        self.bus.Start()
+        #self.bus.SetCS(self.csname)
+        #self.bus.Start()
         s = str(self)
-        out = self.bus.Exchange(s)
-        self.bus.Stop()
+        out = self.bus.exchange(self.csname, s)
+        #self.bus.Stop()
         return out
 
 
@@ -936,13 +937,13 @@ class Mux(object):
         return tuple(b)
 
     def write(self):
-        self.bus.SetCS(self.csname)
+        #self.bus.SetCS(self.csname)
         s = str(self)
-        self.bus.Start()
-        sleep(0.01)
-        out = self.bus.Exchange(s)
-        sleep(0.01)
-        self.bus.Stop()
+        #self.bus.Start()
+        #sleep(0.01)
+        out = self.bus.exchange(self.csname, s)
+        #sleep(0.01)
+        #self.bus.Stop()
         return out
 
 
@@ -1585,31 +1586,49 @@ class AtoiSPI0(SPI):
                 )
 
 
-class AtoiSPI1(mpsse.MPSSE):
-    def __init__(self, freq):
-        super(AtoiSPI1, self).__init__()
-        self.Open(0x0403, 0x6011, mpsse.SPI0, int(freq),
-                interface=mpsse.IFACE_B)
-        self.SetCS('chain0_conf')
-
-    def SetCS(self, name):
-        """Setup the chip-select pins for a given 'name'."""
-        if name == 'chain0_conf':
-            self.context.pidle = self.context.pstop = 0xf8
-            self.context.pstart = 0xf0
-        elif name == 'chain0_mux':
-            self.context.pidle = self.context.pstop = 0xf8
-            self.context.pstart = 0xe8
-        elif name == 'chain1_conf':
-            self.context.pidle = self.context.pstop = 0xf8
-            self.context.pstart = 0xd8
-        elif name == 'chain1_mux':
-            self.context.pidle = self.context.pstop = 0xf8
-            self.context.pstart = 0xb8
-        else:
-            raise Exception, 'Unknown CS name: %s' % name
+if 0:
+    class AtoiSPI1(mpsse.MPSSE):
+        def __init__(self, freq):
+            super(AtoiSPI1, self).__init__()
+            self.Open(0x0403, 0x6011, mpsse.SPI0, int(freq),
+                    interface=mpsse.IFACE_B)
+            self.SetCS('chain0_conf')
+
+        def SetCS(self, name):
+            """Setup the chip-select pins for a given 'name'."""
+            if name == 'chain0_conf':
+                self.context.pidle = self.context.pstop = 0xf8
+                self.context.pstart = 0xf0
+            elif name == 'chain0_mux':
+                self.context.pidle = self.context.pstop = 0xf8
+                self.context.pstart = 0xe8
+            elif name == 'chain1_conf':
+                self.context.pidle = self.context.pstop = 0xf8
+                self.context.pstart = 0xd8
+            elif name == 'chain1_mux':
+                self.context.pidle = self.context.pstop = 0xf8
+                self.context.pstart = 0xb8
+            else:
+                raise Exception, 'Unknown CS name: %s' % name
 
 
+class AtoiSPI1(SPI):
+    def __init__(self, freq):
+        super(AtoiSPI1, self).__init__(
+                interface=ftdi.INTERFACE_B,
+                cs={'_idle': (-1, 0xf8),
+                    '_mask': (-1, 0xf8), #TODO
+                    'chain0_conf': (0, 0xf0),
+                    'chain0_mux':  (0, 0xe8),
+                    'chain1_conf': (0, 0xd8),
+                    'chain1_mux':  (0, 0xb8)},
+                freq=int(freq),
+                vid=0x0403,
+                pid=0x6011,
+                pindir=0xf8,
+                pinstate=0xf8,
+                latency=1
+                )
 
 
 #