From 7199ca5398037ba300606d6aa8bc626c94453a68 Mon Sep 17 00:00:00 2001 From: Dan White Date: Fri, 13 Apr 2012 16:28:47 -0500 Subject: [PATCH] bugfix usbio library --- python-lib/usbio.py | 209 ++++++++++++++++++++------------------------ 1 file changed, 97 insertions(+), 112 deletions(-) diff --git a/python-lib/usbio.py b/python-lib/usbio.py index c8c70a5..283a698 100644 --- a/python-lib/usbio.py +++ b/python-lib/usbio.py @@ -2,13 +2,9 @@ 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 @@ -37,6 +33,23 @@ from pyftdi.pyftdi.spi import SpiController # 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 @@ -56,10 +69,7 @@ class NCO(object): @rst.setter def rst(self, value): - if value: - v = 1 - else: - v = 0 + v = truth(value) self._word[self.RST_POS] = v @property @@ -104,10 +114,7 @@ class OTA(object): @cint.setter def cint(self, value): - if value: - v = 1 - else: - v = 0 + v = truth(value) self.word[self.CINT_POS] = v @property @@ -117,10 +124,7 @@ class OTA(object): @zero.setter def zero(self, value): - if value: - v = 1 - else: - v = 0 + v = truth(value) self._word[self.ZERO_POS] = v @property @@ -131,10 +135,7 @@ class OTA(object): @se.setter def se(self, value): - if value: - v = 1 - else: - v = 0 + v = truth(value) self._word[self.SE_POS] = v @property @@ -144,10 +145,7 @@ class OTA(object): @fast.setter def fast(self, value): - if value: - v = 1 - else: - v = 0 + v = truth(value) self._word[self.FAST_POS] = v @property @@ -242,11 +240,8 @@ class Harmonic(object): @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): @@ -369,14 +364,15 @@ class AD524x(object): 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 @@ -384,33 +380,24 @@ class AD524x(object): @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 @@ -426,34 +413,28 @@ class AD524x(object): 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() @@ -489,15 +470,20 @@ class AD524x(object): 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) @@ -529,58 +515,57 @@ class I2C(object): 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('