From: Dan White Date: Sun, 14 Oct 2012 03:13:45 +0000 (-0500) Subject: Use custom SPI interface with libftdi X-Git-Tag: bootrom-initial-submission~72 X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=b0839e4545b4f4c91e8ec3541bc4eb46af521c58;p=430.git Use custom SPI interface with libftdi --- diff --git a/python-lib/calibrate.py b/python-lib/calibrate.py index 66cfa3c..15db71b 100644 --- a/python-lib/calibrate.py +++ b/python-lib/calibrate.py @@ -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 = [] diff --git a/python-lib/mpsse-test.py b/python-lib/mpsse-test.py index 1cfae60..ba02941 100755 --- a/python-lib/mpsse-test.py +++ b/python-lib/mpsse-test.py @@ -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) diff --git a/python-lib/slope-test.py b/python-lib/slope-test.py index 2250296..2236025 100644 --- a/python-lib/slope-test.py +++ b/python-lib/slope-test.py @@ -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)) diff --git a/python-lib/usbio.py b/python-lib/usbio.py index 745f79d..d9c0f53 100644 --- a/python-lib/usbio.py +++ b/python-lib/usbio.py @@ -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 + ) #