def str2hex(x):
return ''.join(map(lambda h: '%02x' % ord(h), x))
+
+class FTDI_Error(Exception):
+ pass
+
+class I2C_Error(Exception):
+ pass
+
+class SPI_Error(Exception):
+ pass
+
+
class I2C(object):
def __init__(self, interface=ftdi.INTERFACE_A, scl=0, sda=1,
vid=0x0403, pid=0x6011, delay=0.001, timeout=100,
e = self.ftdi.usb_open(self.context, vid, pid)
if e != 0:
- raise Exception, 'Could not open usb device, error: %i' % e
+ raise FTDI_Error('Could not open usb device, error: %i' % e)
self.ftdi.set_baudrate(self.context, 1000)
self.ftdi.usb_purge_buffers(self.context)
sleep(self.DELAY)
i += 1
if i > self.TIMEOUT:
- raise Exception, "Timeout waiting for SCL to go high."
+ raise I2C_Error("Timeout waiting for SCL to go high.")
def _start(self):
if self.started:
self._clock_stretch()
sleep(self.DELAY)
if self._read_sda() == 0:
- raise Exception, "Lost bus arbitration."
+ raise I2C_Error("Lost bus arbitration.")
self._clear_sda()
sleep(self.DELAY)
self._clear_scl()
sleep(self.DELAY)
if self._read_sda() == 0:
- raise Exception, "Lost bus arbitration."
+ raise I2C_Error("Lost bus arbitration.")
sleep(self.DELAY)
self.started = False
sleep(self.DELAY)
self._clock_stretch()
if bit and (self._read_sda() == 0):
- raise Exception, "Lost bus arbitration."
+ raise I2C_Error("Lost bus arbitration.")
sleep(self.DELAY)
self._clear_scl()
# source
e = self.ftdi.usb_open(self.context, vid, pid)
if e != 0:
- raise Exception, self.ftdi.get_error_string(self.context)
+ raise FTDI_Error(self.ftdi.get_error_string(self.context))
self.ftdi.set_latency_timer(self.context, max(1, min(255, latency)))
self.ftdi.set_bitmode(self.context, int(pindir), self.ftdi.BITMODE_RESET)
data = ''.join(map(chr, data))
ret = self.ftdi.write_data(self.context, data, len(data))
if ret < 0:
- raise Exception, self.ftdi.get_error_string(self.context)
+ raise FTDI_Error(self.ftdi.get_error_string(self.context))
def _raw_read(self, n):
"""Read n bytes from buffer."""
while len(r) < n:
ret, s = self.ftdi.read_data(self.context, n)
if ret < 0:
- raise Exception, self.ftdi.get_error_string(self.context)
+ raise FTDI_Error(self.ftdi.get_error_string(self.context))
r += s
#NOTE: including ftdi.SEND_IMMEDIATE commands make this redundant
self.ftdi.usb_purge_rx_buffer(self.context)
self._word += ((value & 0xf) << 0)#intbv(value, max=2**4)
+class ADS8201_Error(Exception):
+ pass
+
class ADS8201(object):
"""Configures and operates an ADS8201 over the given SPI bus."""
# register addresses
if g in (1, 2, 4, 8):
g = int(log2(g))
else:
- raise Exception, 'Gain is only (1, 2, 4, 8), you tried %i' % g
+ raise ADS8201_Error('Gain is only (1, 2, 4, 8), you tried %i' % g)
if (channel % 2) == 0:
self._ch[channel][2:0] = g
def channelMode(self, channel, mode):
"""Setup channel (pair) to SE or differential even/odd modes."""
if mode not in (0, 1, 3):
- raise Exception, 'Invalid mode: %i' % mode
+ raise ADS8201_Error('Invalid mode: %i' % mode)
if ((channel % 2) == 1):
- raise Exception, 'Cannot set mode on odd channel: %i' % channel
+ raise ADS8201_Error('Cannot set mode on odd channel: %i' % channel)
self._ch[channel][4:2] = mode
self.setRegister(int(channel/2), self._ch[channel])
#
def average(self, results, accurate=True):
if results not in (0, 1, 4, 8, 16):
- raise Exception, 'Invalid average number: %i, must be 0, 4, 8, 16' % results
+ raise ADS8201_Error('Invalid average number: %i, must be 0, 4, 8, 16' % results)
v = 0
if accurate:
v += 0b100
INT_ADC_DATA_READY = (1 << 0)
def interruptMode(self, mode):
if mode not in range(16):
- raise Exception, 'Invalid interrupt mode: %i' % mode
+ raise ADS8201_Error('Invalid interrupt mode: %i' % mode)
self._INT_SCR[4:0] = mode
self.setRegister(self.INT_SCR, self._INT_SCR)
"""Set trigger operating mode. NB: 0 (idle) is required for setting
most config registers."""
if mode in (1, 7):
- raise Exception, 'Invalid trigger mode: %i' % mode
+ raise ADS8201_Error('Invalid trigger mode: %i' % mode)
self._TRIGGER_SCR = intbv(mode, max=2**8)
self.setRegister(self.TRIGGER_SCR, self._TRIGGER_SCR)
return r
+class M25PExx_Error(Exception):
+ pass
+
class M25PExx(object):
def __init__(self, spibus, csname):
self.bus = spibus
def writePage(self, addr, data):
if ((addr & 0xff) + len(data)) > 256:
- raise Exception, 'Too much data: %i' % len(data)
+ raise M25PExx_Error('Too much data: %i' % len(data))
a = int2str(addr, 3*8)
while not self.isReady():
pass
def programPage(self, addr, data):
if ((addr & 0xff) + len(data)) > 256:
- raise Exception, 'Too much data: %i' % len(data)
+ raise M25PExx_Error('Too much data: %i' % len(data))
a = int2str(addr, 3*8)
while not self.isReady():
pass
def erasePage(self, addr):
if (addr & 0xff) != 0:
- raise Exception, 'Address must be a page boundary: %i' % addr
+ raise M25PExx_Error('Address must be a page boundary: %i' % addr)
while not self.isReady():
pass
self._write('\xdb' + a)
def write(self, addr, data):
if addr & 0xff != 0:
- raise Exception, 'Address must start on a page boundary (0x....00).'
+ raise M25PExx_Error('Address must start on a page boundary (0x....00).')
n = len(data)
nPages = (n // 256) + 1
def program(self, addr, data):
if addr & 0xff != 0:
- raise Exception, 'Address must start on a page boundary (0x....00).'
+ raise M25PExx_Error('Address must start on a page boundary (0x....00).')
n = len(data)
nPages = (n // 256) + 1