from array import array
import struct
-import time
from myhdl import intbv
-from pyftdi.pyftdi.ftdi import Ftdi
-from pyftdi.pyftdi.spi import SpiController
-
# SPI interface
# 11..8 - gain
# 7..0 - offset
+
+def truth(v):
+ if isinstance(v, (int, intbv)):
+ # NB: ~True == -2
+ # ~False == -1
+ if v == -2 or v == 0:
+ return False
+ else:
+ return True
+ elif isinstance(v, bool):
+ return v
+ elif v:
+ return True
+ else:
+ return False
+
+
class NCO(object):
RST_POS = 14
FCW_WIDTH = 14
@rst.setter
def rst(self, value):
- if value:
- v = 1
- else:
- v = 0
+ v = truth(value)
self._word[self.RST_POS] = v
@property
@cint.setter
def cint(self, value):
- if value:
- v = 1
- else:
- v = 0
+ v = truth(value)
self.word[self.CINT_POS] = v
@property
@zero.setter
def zero(self, value):
- if value:
- v = 1
- else:
- v = 0
+ v = truth(value)
self._word[self.ZERO_POS] = v
@property
@se.setter
def se(self, value):
- if value:
- v = 1
- else:
- v = 0
+ v = truth(value)
self._word[self.SE_POS] = v
@property
@fast.setter
def fast(self, value):
- if value:
- v = 1
- else:
- v = 0
+ v = truth(value)
self._word[self.FAST_POS] = v
@property
@cal.setter
def cal(self, value):
- if value:
- v = 1
- else:
- v = 0
- self._cal = intbv(v, max=1)
+ v = truth(value)
+ self._cal = v
@property
def word(self):
def __init__(self, i2cbus, addr):
self.bus = i2cbus
- self.addr = intbv(ADDR_BASE + intbv(addr, max=2**2), max=2**7)
- self._sel = intbv(0, max=1)
- self._rs = intbv(0, max=1)
- self._sd = intbv(0, max=1)
+ self.addr = intbv(self.ADDR_BASE + intbv(addr, max=2**2), max=2**7)
+ self._sel = False
+ self._rs = False
+ self._sd = False
self._posA = intbv(0x80, max=2**8)
self._posB = intbv(0x80, max=2**8)
- self._gpo1 = intbv(0, max=1)
- self._gpo2 = intbv(0, max=1)
+ self.pos = (self._posA, self._posB)
+ self._gpo1 = False
+ self._gpo2 = False
self.gpo = (self._gpo1, self.gpo2)
@property
@sel.setter
def sel(self, value):
- if value:
- v = 1
- else:
- v = 0
- self._sel = intbv(v, max=1)
+ v = truth(value)
+ self._sel = v
@property
def reset(self): return self._rs
@reset.setter
def reset(self, value):
- if value:
- v = 1
- else:
- v = 0
- self._rs = intbv(v, max=1)
+ v = truth(value)
+ self._rs = v
@property
def shutdown(self): return self._sd
@shutdown.setter
def shutdown(self, value):
- if value:
- v = 1
- else:
- v = 0
- self._sd = intbv(v, max=1)
+ v = truth(value)
+ self._sd = v
@property
def posA(self): return self._posA
def posB(self, value):
self._posB = intbv(value, max=2**8)
- @property
- def pos(self):
- if self.pos:
- return self.posB
- else:
- return self.posA
+ #@property
+ #def pos(self):
+ #if self.pos:
+ #return self.posB
+ #else:
+ #return self.posA
@property
- def gpo1(self): return self.gpo1
+ def gpo1(self): return self._gpo1
@gpo1.setter
def gpo1(self, value):
- if value:
- v = intbv(1)[0]
- else:
- v = intbv(0)[0]
+ v = truth(value)
self._gpo1 = v
self.updateGPO()
@property
- def gpo2(self): return self.gpo2
+ def gpo2(self): return self._gpo2
@gpo2.setter
def gpo2(self, value):
- if value:
- v = intbv(1)[0]
- else:
- v = intbv(0)[0]
+ v = truth(value)
self._gpo2 = v
self.updateGPO()
def send(self, sel):
self.sel = sel
+ if sel:
+ pos = self.posB
+ else:
+ pos = self.posA
+
cmd = ((self.addr << 1) + self.WRITE,
self.instruction,
- self.pos)
+ pos)
self.lastSel = self.sel
if sel:
- self.lastB = self.pos
+ self.lastB = pos
else:
- self.lastA = self.pos
+ self.lastA = pos
self.bus.write(cmd)
raise NotImplementedError
-o = OTA()
-o.tune = 0x3ff
-print o.tune
-print o.word
-o.word[15] = 1
-print o.word
-
-print o.cint
-o.cint = 0
-print o.cint
-o.cint = 1
-print o.cint
-
-
-h = Harmonic()
-h.otaA.offset = -1
-print 'offset:', h.otaA.offset
-o = h.otaA.offset
+if __name__ == '__main__':
-def arr(a):
- return array('B', a)
+ if 0:
+ o = OTA()
+ o.tune = 0x3ff
+ print o.tune
+ print o.word
+ o.word[15] = 1
+ print o.word
+ print o.cint
+ o.cint = 0
+ print o.cint
+ o.cint = 1
+ print o.cint
+ h = Harmonic()
+ h.otaA.offset = -1
+ print 'offset:', h.otaA.offset
+ o = h.otaA.offset
+ def arr(a):
+ return array('B', a)
-if 1:
- sc0 = SpiController()
- sc0.configure(0x0403, 0x6011, 0, loopback=True)
+ if 1:
+ sc0 = SpiController()
+ sc0.configure(0x0403, 0x6011, 0, loopback=True)
- sc1 = SpiController()
- sc1.configure(0x0403, 0x6011, 1, loopback=True)
+ sc1 = SpiController()
+ sc1.configure(0x0403, 0x6011, 1, loopback=True)
- p0 = sc0.get_port(0)
- p1 = sc1.get_port(0)
+ p0 = sc0.get_port(0)
+ p1 = sc1.get_port(0)
- def rw(p, a, readlen=None):
- if readlen is None:
- readlen = len(a)
- cmd = array('B')
- cmd.fromstring(struct.pack('<BH', Ftdi.RW_BYTES_NVE_PVE_MSB, len(a)-1))
- cmd.extend(a)
- cmd.extend(p._controller._immediate)
- p._controller._ftdi.write_data(cmd)
- return p._controller._ftdi.read_data_bytes(readlen, 4)
+ def rw(p, a, readlen=None):
+ if readlen is None:
+ readlen = len(a)
+ cmd = array('B')
+ cmd.fromstring(struct.pack('<BH', Ftdi.RW_BYTES_NVE_PVE_MSB, len(a)-1))
+ cmd.extend(a)
+ cmd.extend(p._controller._immediate)
+ p._controller._ftdi.write_data(cmd)
+ return p._controller._ftdi.read_data_bytes(readlen, 4)
- print 'Loopback SPI interfaces 0, 1'
- a = [0,1,2,3,4,5,6,7,8,9]
- print a
- print rw(p0, arr(a))
+ print 'Loopback SPI interfaces 0, 1'
+ a = [0,1,2,3,4,5,6,7,8,9]
+ print a
+ print rw(p0, arr(a))
- a = [i for i in range(256)]
- print a
- print rw(p1, arr(a))
+ a = [i for i in range(256)]
+ print a
+ print rw(p1, arr(a))