From df2a78cf2a2416a4334fe08d8ce9e97b12f84021 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 10 Sep 2019 12:06:57 -0700 Subject: [PATCH 01/12] Version bump to 0.0.6 --- python/i2cdriver.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/python/i2cdriver.py b/python/i2cdriver.py index 9306fb7..bb7f1f7 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -4,7 +4,7 @@ import time import struct from collections import OrderedDict -__version__ = '0.0.5' +__version__ = '0.0.6' PYTHON2 = (sys.version_info < (3, 0)) @@ -280,16 +280,16 @@ class I2CDriver: self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n)) return self.ser.read(n) - def regwr(self, dev, reg, *vv): + def regwr(self, dev, reg, vv): """Write a device's register. :param dev: 7-bit I2C device address :param reg: register address 0-255 - :param vv: sequence of values to write + :param vv: value to write. Either a single byte, or a sequence To set device 0x34 byte register 7 to 0xA1: - >>> i2c.regwr(0x34, 7, [0xa1]) + >>> i2c.regwr(0x34, 7, 0xa1) If device 0x75 has a big-endian 16-bit register 102 you can set it to 4999 with: @@ -300,6 +300,8 @@ class I2CDriver: if r: r = self.write(struct.pack("B", reg)) if r: + if isinstance(vv, int): + vv = struct.pack("B", vv) r = self.write(vv) self.stop() return r From f00bee59c9244cd4e66a3467f23a9a94a63a57e1 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 10 Sep 2019 12:07:23 -0700 Subject: [PATCH 02/12] Fix EDS DIG2 raw method --- python/EDS.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/EDS.py b/python/EDS.py index 64e48f7..a1d435b 100644 --- a/python/EDS.py +++ b/python/EDS.py @@ -14,7 +14,7 @@ class Dig2: def raw(self, b0, b1): """ Set all 8 segments from the bytes b0 and b1 """ - self.i2.regwr(self.a, 0, b0, b1) + self.i2.regwr(self.a, 0, struct.pack("BB", b0, b1)) def hex(self, b): """ Display a hex number 0-0xff """ From a6c58a5be3551a894dde631061a937e62bf59f58 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Sat, 4 Jan 2020 06:51:34 -0800 Subject: [PATCH 03/12] setspeed() and setpullups() now update their respective variables [#27] --- python/i2cdriver.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/i2cdriver.py b/python/i2cdriver.py index bb7f1f7..5ec13ee 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -143,6 +143,7 @@ class I2CDriver: assert s in (100, 400) c = {100:b'1', 400:b'4'}[s] self.__ser_w(c) + self.speed = s def setpullups(self, s): """ @@ -152,6 +153,7 @@ class I2CDriver: """ assert 0 <= s < 64 self.__ser_w([ord('u'), s]) + self.pullups = s def scan(self, silent = False): """ Performs an I2C bus scan. From 57dc5a4f7ef9b4b1044bc4cd8310c20fc5c54719 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Sat, 4 Jan 2020 06:53:36 -0800 Subject: [PATCH 04/12] Version 0.0.7 --- python/i2cdriver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/i2cdriver.py b/python/i2cdriver.py index 5ec13ee..c6a4dd5 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -4,7 +4,7 @@ import time import struct from collections import OrderedDict -__version__ = '0.0.6' +__version__ = '0.0.7' PYTHON2 = (sys.version_info < (3, 0)) From b62ad81b27167b6779e3af8f5fa512f5e4949010 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Sat, 4 Jan 2020 06:57:21 -0800 Subject: [PATCH 05/12] Slow down remote controll polling --- python/samples/EDS-REMOTE.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/samples/EDS-REMOTE.py b/python/samples/EDS-REMOTE.py index f0c23c1..4861408 100644 --- a/python/samples/EDS-REMOTE.py +++ b/python/samples/EDS-REMOTE.py @@ -5,6 +5,7 @@ from i2cdriver import I2CDriver, EDS if __name__ == '__main__': i2 = I2CDriver(sys.argv[1], True) + i2.setspeed(400) d = EDS.Remote(i2) @@ -17,3 +18,4 @@ if __name__ == '__main__': if r is not None: (code, timestamp) = r print("Raw code: %02x %02x %02x %02x (time %.2f)" % (code[0], code[1], code[2], code[3], timestamp)) + time.sleep(.25) From cf5f80f3f38af901170cfbf7e133c44eee98e245 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 7 Jan 2020 13:56:37 -0800 Subject: [PATCH 06/12] Acceptance test for i2cdriver and i2cmini firmware --- python/i2cdriver.py | 6 + python/tests/accept.py | 334 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 340 insertions(+) create mode 100644 python/tests/accept.py diff --git a/python/i2cdriver.py b/python/i2cdriver.py index c6a4dd5..06dd143 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -35,6 +35,8 @@ class START(_I2CEvent): f.writerow(("START", self.rrw(), str(self.addr), self.rack())) else: assert False, "unsupported format" + def __eq__(self, other): + return (self.addr, self.rw, self.ack) == (other.addr, other.rw, other.ack) class STOP(_I2CEvent): def __repr__(self): @@ -44,6 +46,8 @@ class STOP(_I2CEvent): f.writerow(("STOP", None, None, None)) else: assert False, "unsupported format" + def __eq__(self, other): + return isinstance(other, STOP) class BYTE(_I2CEvent): def __init__(self, b, rw, ack): @@ -57,6 +61,8 @@ class BYTE(_I2CEvent): f.writerow(("BYTE", self.rrw(), str(self.b), self.rack())) else: assert False, "unsupported format" + def __eq__(self, other): + return (self.b, self.rw, self.ack) == (other.b, other.rw, other.ack) class I2CDriver: """ diff --git a/python/tests/accept.py b/python/tests/accept.py new file mode 100644 index 0000000..6d2d784 --- /dev/null +++ b/python/tests/accept.py @@ -0,0 +1,334 @@ +import sys +import time +import struct +import random + +import i2cdriver +import unittest + +DUT = "dut" # grn0 +AGG = "agg" # blk1 + +def bit(b, x): + return 1 & (x >> b) + +class TestDUT(unittest.TestCase): + def setUp(self): + self.i2 = i2cdriver.I2CDriver(DUT) + self.ag = i2cdriver.I2CDriver(AGG) + + def init(self): + self.i2.reboot() + self.i2.setspeed(400) + self.i2.getstatus() + return self.i2 + + def lm75_read(self, i, reg): + (tr,) = struct.unpack(">h", i.regrd(0x48, reg, 2)) + return tr + + def lm75_slow_read(self, i, reg): + i.start(0x48, 0) + i.write(struct.pack("B", reg)) + i.start(0x48, 1) + (tr,) = struct.unpack(">h", i.read(2)) + i.stop() + return tr + + def lm75_write(self, i, reg, v): + i.start(0x48, 0) + i.write(struct.pack(">Bh", reg, v)) + i.stop() + + def stack0(self): + self.s0 = self.i2.introspect() + + def stacksame(self): + s1 = self.i2.introspect() + for i in ("ds", "sp"): + self.assertEqual(self.s0[i], s1[i]) + + def confirm(self): + # Basic i2c confirmation + self.assertEqual(self.lm75_read(self.i2, 2), 0x4b00) + + def confirm_sampling(self): + # Check that analog sampling is happening + econvs = { + "i2cdriver1" : {0,1,2}, + "i2cdriverm" : {0} + }[self.i2.product] + s = set() + while len(s) < len(econvs): + s.add(self.i2.introspect()["convs"]) + self.assertEqual(s, econvs) + + def test_temperature(self): + # Confirm onboard temperature sensor is reasonable and changing + i2 = self.i2 + i2.getstatus() + onboard = i2.temp + external = (self.lm75_read(i2, 0) >> 5) * 0.125 + self.assertTrue(abs(onboard - external) < 10) + + # Wait up to 10 seconds for temperature to change + t0 = time.time() + while onboard == i2.temp: + i2.getstatus() + self.assertTrue(time.time() < (t0 + 10)) + + def test_coldstart(self): + i2 = self.init() + s = i2.introspect() + self.assertEqual(i2.scl, 1) + self.assertEqual(i2.sda, 1) + + def test_scan(self): + i2 = self.init() + def det(a): + r = i2.start(a, 0) + i2.stop() + return r + scan = [det(a) for a in range(128)] + e = [(i == 0x48) for i in range(128)] + self.assertEqual(scan, e) + + def test_lm75_reg(self): + i2 = self.i2 + self.stack0() + vals = (0, -128, 0x7f80) + for a in vals: + self.lm75_write(i2, 2, a) + for b in vals: + self.lm75_write(i2, 3, b) + self.assertEqual(self.lm75_read(i2, 2), a) + self.assertEqual(self.lm75_read(i2, 3), b) + self.assertEqual(self.lm75_slow_read(i2, 2), a) + self.assertEqual(self.lm75_slow_read(i2, 3), b) + self.lm75_write(i2, 2, 0x4b00) + self.lm75_write(i2, 3, 0x5000) + self.assertEqual(self.lm75_read(i2, 2), 0x4b00) + self.assertEqual(self.lm75_read(i2, 3), 0x5000) + self.stacksame() + + def test_setspeed(self): + i2 = self.init() + self.stack0() + for s in (100, 400, 400, 100, 400): + i2.setspeed(s) + i2.getstatus() + self.assertEqual(i2.speed, s) + self.confirm() + self.stacksame() + + def test_cap_idle(self): + i2 = self.init() + c = i2.capture_start() + t0 = time.time() + d = i2.ser.read(15) + t1 = time.time() + i2.capture_stop() + + self.assertEqual(d, bytes(15)) + self.assertTrue(0.4 < (t1 - t0) < 0.6) + + def test_cap_0(self): + def test_0(): + self.lm75_write(ag, 2, 0x4b00) + return [ + i2cdriver.START(0x48, 0, 1), + i2cdriver.BYTE(0x02, 0, True), + i2cdriver.BYTE(0x4b, 0, True), + i2cdriver.BYTE(0x00, 0, True), + i2cdriver.STOP() + ] + def test_1(): + self.lm75_slow_read(ag, 2) + return [ + i2cdriver.START, + (0x90, True), + (0x02, True), + i2cdriver.START, + (0x91, True), + (0x4b, True), + (0x00, False), + i2cdriver.STOP + ] + + i2 = self.init() + ag = self.ag + for t in (test_0, ): # test_1): + c = i2.capture_start() + time.sleep(.1) + ee = t() + for e,a in zip(ee, c()): + self.assertEqual(a, e) + i2.capture_stop() + + def test_pullups(self): + i2 = self.init() + i2.getstatus() + self.assertEqual(i2.pullups, 0b100100) + + rr = random.sample(list(range(64)), 64) + if i2.product == "i2cdriver1": + respins = (0, 1, 3, 13, 14, 16) + else: + respins = (10, 11, 12, 6, 7, 8) + for r in rr: + i2.setpullups(r) + i2.getstatus() + self.assertEqual(i2.pullups, r) + s = i2.introspect() + p = s["P0"] + (s["P1"] << 8) + (s["P2"] << 16) + d = s["P0MDOUT"] + (s["P1MDOUT"] << 8) + (s["P2MDOUT"] << 16) + for b,pb in enumerate(respins): + self.assertEqual(bit(pb, p), 1) + self.assertEqual(bit(b, r), bit(pb, d)) + + def test_zz5s(self): + i2 = self.init() + time.sleep(5) + i2.getstatus() + self.assertTrue(i2.uptime in (4,5,6)) + + def checkmode(self, c): + self.i2.getstatus() + self.assertEqual(self.i2.mode, c) + + def test_bitbang(self): + i2 = self.init() + self.checkmode('I') + self.stack0() + i2.ser.write(b'b') + for i in range(1000): # Square wave for a while + i2.ser.write(bytes([0b1111, 0b0101])) + i2.ser.write(bytes([0b1010, 0b11010])) # Float, request a byte + self.assertEqual(i2.ser.read(1), bytes([3])) # both should be high + i2.ser.write(bytes([0b0101])) # Leave driven low + i2.ser.write(b'@') + self.checkmode('B') + i2.restore() + self.checkmode('I') + self.stacksame() + self.assertEqual(self.lm75_read(i2, 2), 0x4b00) + + def test_bitbang_idem(self): + # Confirm bitbang mode idempotence + i2 = self.init() + if i2.product != "spidriver1": + return + for n in [0b1101, 0b1011, 0b0000, 0b1111] + list(range(16)): + i2.ser.write(b'b' + bytes([n, 0x40])) + s1 = i2.introspect() + self.assertEqual(bit(0, n), bit(2, s1["P0MDOUT"])) + self.assertEqual(bit(1, n), bit(2, s1["P0"])) + self.assertEqual(bit(2, n), bit(4, s1["P1MDOUT"])) + self.assertEqual(bit(3, n), bit(4, s1["P1"])) + i2.ser.write(b'b' + bytes([0x40])) + s2 = i2.introspect() + for i in ("P0", "P1", "P0MDOUT", "P1MDOUT"): + self.assertEqual(s1[i], s2[i]) + self.assertEqual(i2.introspect()["SMB0CF"], 0x00) + i2.restore() + self.assertEqual(i2.introspect()["SMB0CF"], 0xd8) + + def test_bitbang_bidir(self): + self.stack0() + dd = (self.i2, self.ag) + [i2.ser.write(b'b') for i2 in dd] + LOW = 0b01 + HIGH = 0b11 + INPUT = 0b10 + def port(d, sda, scl, read = False): + d.ser.write(bytes([sda | (scl << 2) | (int(read) << 4)])) + if read: + (r,) = d.ser.read(1) + return (r & 1, (r >> 1) & 1) + + for sda in (LOW, HIGH, LOW): + for scl in (HIGH, LOW, HIGH): + expected = (int(sda == HIGH), int(scl == HIGH)) + + for (tx,rx) in [(0,1), (1,0)]: + port(dd[tx], sda, scl) + port(dd[rx], INPUT, INPUT) + self.assertEqual(expected, port(dd[rx], INPUT, INPUT, True)) + + [i2.ser.write(b'@') for i2 in dd] + [i2.restore() for i2 in dd] + self.stacksame() + + def test_reset(self): + i2 = self.init() + self.stack0() + i2.reset() + self.stacksame() + for i in range(100): + i2.reset() + self.stacksame() + self.confirm() + + def test_sampling(self): + self.confirm_sampling() + + def test_weigh(self): + # Confirm resistance measurement + i2 = self.init() + ag = self.ag + self.stack0() + + def sample(p, pv): + i2.setpullups(p) + i2.ser.write(b'v' + bytes([pv])) + while True: + i2.ser.write(b'w') + r = i2.ser.read(1) + if r[0] == 0: + break + return struct.unpack("2B", i2.ser.read(2)) + + def estimate(a, hi, res): + if a == 0: + return 0 + v = a / hi + return (res / v) - res + def mean(s): + return sum(s) / len(s) + def resistance(rr): + if rr == []: + return 0 + return 1 / sum([1/r for r in rr]) + def pullups(): + sHH = sample(0b111111, 0b111111) + sAA = sample(0b001001, 0b110110) + sBB = sample(0b010010, 0b101101) + sCC = sample(0b100100, 0b011011) + sda_r = mean((estimate(sAA[0], sHH[0], 2200), + estimate(sBB[0], sHH[0], 4300), + estimate(sCC[0], sHH[0], 4700))) + scl_r = mean((estimate(sAA[1], sHH[1], 2200), + estimate(sBB[1], sHH[1], 4300), + estimate(sCC[1], sHH[1], 4700))) + return (sda_r, scl_r) + + for x in range(64): + # print(x) + ag.setpullups(x) + esda = resistance([r for i,r in enumerate([2200, 4300, 4700]) if bit(i, x)]) + escl = resistance([r for i,r in enumerate([2200, 4300, 4700]) if bit(3 + i, x)]) + # print("expected %d" % esda, escl) + sda,scl = pullups() + # print("SDA pullup %d, SCL pullup %d" % (sda,scl)) + + def close(e, a): + margin = max(100, e / 10) + return abs(a - e) < margin + self.assertTrue(close(esda, sda)) + self.assertTrue(close(escl, scl)) + self.confirm_sampling() + + self.init() # restore pullups + +if __name__ == '__main__': + unittest.main() From f57ca82ee178af8d6663a4751dabd064773cd5fa Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 7 Jan 2020 17:42:36 -0800 Subject: [PATCH 07/12] Correctly hangle regrd with count of 256 or greater. [#28] --- python/i2cdriver.py | 18 ++++++++++++------ python/tests/accept.py | 7 +++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/python/i2cdriver.py b/python/i2cdriver.py index 06dd143..94758c5 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -267,7 +267,7 @@ class I2CDriver: :param dev: 7-bit I2C device address :param reg: register address 0-255 - :param fmt: :py:func:`struct.unpack` format string for the register contents + :param fmt: :py:func:`struct.unpack` format string for the register contents, or an integer byte count If device 0x75 has a 16-bit register 102, it can be read with: @@ -276,17 +276,23 @@ class I2CDriver: """ if isinstance(fmt, str): - n = struct.calcsize(fmt) - self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n)) - r = struct.unpack(fmt, self.ser.read(n)) + r = struct.unpack(fmt, self.regrd(dev, reg, struct.calcsize(fmt))) if len(r) == 1: return r[0] else: return r else: n = fmt - self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n)) - return self.ser.read(n) + if n <= 256: + self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n & 0xff)) + return self.ser.read(n) + else: + self.start(dev, 0) + self.write([reg]) + self.start(dev, 1) + r = self.read(n) + self.stop() + return r def regwr(self, dev, reg, vv): """Write a device's register. diff --git a/python/tests/accept.py b/python/tests/accept.py index 6d2d784..6f0e974 100644 --- a/python/tests/accept.py +++ b/python/tests/accept.py @@ -111,6 +111,13 @@ class TestDUT(unittest.TestCase): self.assertEqual(self.lm75_read(i2, 3), 0x5000) self.stacksame() + def test_regrd256(self): + i2 = self.i2 + reg = 3 + self.lm75_write(i2, reg, 0x7480) + for n in (127, 128, 129): + self.assertEqual(i2.regrd(0x48, reg, ">" + str(n) + "h"), (0x7480,) * n) + def test_setspeed(self): i2 = self.init() self.stack0() From 449e93185315d120b1c6e0bf35e08b923cd97483 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 7 Jan 2020 17:43:50 -0800 Subject: [PATCH 08/12] Version 0.0.8 --- python/i2cdriver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/i2cdriver.py b/python/i2cdriver.py index 94758c5..f2a1dd8 100644 --- a/python/i2cdriver.py +++ b/python/i2cdriver.py @@ -4,7 +4,7 @@ import time import struct from collections import OrderedDict -__version__ = '0.0.7' +__version__ = '0.0.8' PYTHON2 = (sys.version_info < (3, 0)) From e327a2ddb1a19dd9fbfcfde230e18beb19b0743d Mon Sep 17 00:00:00 2001 From: James Bowman Date: Mon, 13 Jan 2020 19:22:23 -0800 Subject: [PATCH 09/12] all tests now passing on Python 2.7 --- python/tests/accept.py | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/python/tests/accept.py b/python/tests/accept.py index 6f0e974..3b71246 100644 --- a/python/tests/accept.py +++ b/python/tests/accept.py @@ -1,3 +1,5 @@ +from __future__ import print_function, division + import sys import time import struct @@ -12,6 +14,9 @@ AGG = "agg" # blk1 def bit(b, x): return 1 & (x >> b) +def byte(x): + return struct.pack("B", x) + class TestDUT(unittest.TestCase): def setUp(self): self.i2 = i2cdriver.I2CDriver(DUT) @@ -118,6 +123,20 @@ class TestDUT(unittest.TestCase): for n in (127, 128, 129): self.assertEqual(i2.regrd(0x48, reg, ">" + str(n) + "h"), (0x7480,) * n) + def test_regwr(self): + i2c = self.i2 + sa0 = 0x48 + wdata = [] + lcnt = 16 + for i in range(lcnt): + x = random.randint(0, 255) + wdata.append(x) + print(wdata) + i2c.regwr(sa0, 0x00, wdata) + time.sleep(0.5) + x = i2c.regrd(sa0, 0x00, "<16B") + print(x) + def test_setspeed(self): i2 = self.init() self.stack0() @@ -136,7 +155,7 @@ class TestDUT(unittest.TestCase): t1 = time.time() i2.capture_stop() - self.assertEqual(d, bytes(15)) + self.assertEqual(d, b'\x00' * 15) self.assertTrue(0.4 < (t1 - t0) < 0.6) def test_cap_0(self): @@ -209,10 +228,10 @@ class TestDUT(unittest.TestCase): self.stack0() i2.ser.write(b'b') for i in range(1000): # Square wave for a while - i2.ser.write(bytes([0b1111, 0b0101])) - i2.ser.write(bytes([0b1010, 0b11010])) # Float, request a byte - self.assertEqual(i2.ser.read(1), bytes([3])) # both should be high - i2.ser.write(bytes([0b0101])) # Leave driven low + i2.ser.write(byte(0b1111) + byte(0b0101)) + i2.ser.write(byte(0b1010) + byte(0b11010)) # Float, request a byte + self.assertEqual(struct.unpack("B", i2.ser.read(1)), (3, )) # both should be high + i2.ser.write(byte(0b0101)) # Leave driven low i2.ser.write(b'@') self.checkmode('B') i2.restore() @@ -226,13 +245,13 @@ class TestDUT(unittest.TestCase): if i2.product != "spidriver1": return for n in [0b1101, 0b1011, 0b0000, 0b1111] + list(range(16)): - i2.ser.write(b'b' + bytes([n, 0x40])) + i2.ser.write(b'b' + byte(n) + byte(0x40)) s1 = i2.introspect() self.assertEqual(bit(0, n), bit(2, s1["P0MDOUT"])) self.assertEqual(bit(1, n), bit(2, s1["P0"])) self.assertEqual(bit(2, n), bit(4, s1["P1MDOUT"])) self.assertEqual(bit(3, n), bit(4, s1["P1"])) - i2.ser.write(b'b' + bytes([0x40])) + i2.ser.write(b'b' + byte(0x40)) s2 = i2.introspect() for i in ("P0", "P1", "P0MDOUT", "P1MDOUT"): self.assertEqual(s1[i], s2[i]) @@ -248,9 +267,9 @@ class TestDUT(unittest.TestCase): HIGH = 0b11 INPUT = 0b10 def port(d, sda, scl, read = False): - d.ser.write(bytes([sda | (scl << 2) | (int(read) << 4)])) + d.ser.write(byte(sda | (scl << 2) | (int(read) << 4))) if read: - (r,) = d.ser.read(1) + (r,) = struct.unpack("B", d.ser.read(1)) return (r & 1, (r >> 1) & 1) for sda in (LOW, HIGH, LOW): @@ -287,10 +306,10 @@ class TestDUT(unittest.TestCase): def sample(p, pv): i2.setpullups(p) - i2.ser.write(b'v' + bytes([pv])) + i2.ser.write(b'v' + byte(pv)) while True: i2.ser.write(b'w') - r = i2.ser.read(1) + r = struct.unpack("B", i2.ser.read(1)) if r[0] == 0: break return struct.unpack("2B", i2.ser.read(2)) From 96534d6c0d988414942a19b70eb7981361479f2b Mon Sep 17 00:00:00 2001 From: James Bowman Date: Mon, 13 Jan 2020 19:25:15 -0800 Subject: [PATCH 10/12] Remove debug --- python/tests/accept.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/tests/accept.py b/python/tests/accept.py index 3b71246..637f6e9 100644 --- a/python/tests/accept.py +++ b/python/tests/accept.py @@ -131,11 +131,11 @@ class TestDUT(unittest.TestCase): for i in range(lcnt): x = random.randint(0, 255) wdata.append(x) - print(wdata) + # print(wdata) i2c.regwr(sa0, 0x00, wdata) time.sleep(0.5) x = i2c.regrd(sa0, 0x00, "<16B") - print(x) + # print(x) def test_setspeed(self): i2 = self.init() From 036902b032b580255bdb9a394edc1777e9d77663 Mon Sep 17 00:00:00 2001 From: James Bowman Date: Thu, 16 Jan 2020 12:08:41 -0800 Subject: [PATCH 11/12] Sphinx doc first cut --- python/docs/Makefile | 20 +++++++++ python/docs/conf.py | 55 +++++++++++++++++++++++ python/docs/go | 4 ++ python/docs/index.rst | 101 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 python/docs/Makefile create mode 100644 python/docs/conf.py create mode 100644 python/docs/go create mode 100644 python/docs/index.rst diff --git a/python/docs/Makefile b/python/docs/Makefile new file mode 100644 index 0000000..d4bb2cb --- /dev/null +++ b/python/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/python/docs/conf.py b/python/docs/conf.py new file mode 100644 index 0000000..4bebeee --- /dev/null +++ b/python/docs/conf.py @@ -0,0 +1,55 @@ +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +import sys +import os +sys.path.insert(0, os.path.abspath('..')) + +# -- Project information ----------------------------------------------------- + +project = 'i2cdriver' +copyright = '2020, Excamera Labs' +author = 'Excamera Labs' + + +# -- General configuration --------------------------------------------------- + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + "sphinx.ext.autodoc", + "sphinx.ext.intersphinx" +] + +intersphinx_mapping = {'https://docs.python.org/3/': None} + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'alabaster' + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] diff --git a/python/docs/go b/python/docs/go new file mode 100644 index 0000000..81fb7d5 --- /dev/null +++ b/python/docs/go @@ -0,0 +1,4 @@ +set -e + +# pip install --upgrade --force-reinstall .. +make html diff --git a/python/docs/index.rst b/python/docs/index.rst new file mode 100644 index 0000000..067a176 --- /dev/null +++ b/python/docs/index.rst @@ -0,0 +1,101 @@ +.. i2cdriver documentation master file, created by + sphinx-quickstart on Thu Jan 16 10:21:28 2020. + You can adapt this file completely to your liking, but it should at least + contain the root `toctree` directive. + +Excamera I2CDriver Python API +============================= + + +zxxx + +.. toctree:: + :maxdepth: 2 + +xxcc + +Official packages are available on PyPI. + +https://pypi.org/project/i2cdriver/ + + +The main page for I2CDriver includes the complete User Guide: + +https://i2cdriver.com + + +System Requirements +------------------- + +Because it is a pure Python module, ``i2cdriver`` can run on any system supported by ``pyserial``. +This includes: + +- Windows 7 or 10 +- Mac OS +- Linux, including all Ubuntu distributions + +Both Python 2.7 and 3.x are supported. + +Installation +------------ + +The ``i2cdriver`` package can be installed from PyPI using ``pip``:: + + $ pip install i2cdriver + +Quick start +----------- + +To connect to the I2CDriver and scan the bus for connected devices:: + + >>> import i2cdriver + >>> i2c = i2cdriver.I2CDriver("/dev/ttyUSB0") + >>> i2c.scan() + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- 1C -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + 48 -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + 68 -- -- -- -- -- -- -- + -- -- -- -- -- -- -- -- + [28, 72, 104] + +To read the temperature in Celsius from a connected LM75 sensor: + + >>> d=i2cdriver.EDS.Temp(i2c) + >>> d.read() + 17.875 + >>> d.read() + 18.0 + +The User Guide at https://i2cdriver.com has more examples. + +Module Contents +--------------- + +.. autoclass:: i2cdriver.I2CDriver + :member-order: bysource + :members: + setspeed, + setpullups, + scan, + reset, + start, + read, + write, + stop, + regwr, + regrd, + getstatus, + monitor + +.. autoclass:: i2cdriver.START +.. autoclass:: i2cdriver.STOP +.. autoclass:: i2cdriver.BYTE From 23b12eec6e0a7bb2dabf018e05e8244fe662ec0c Mon Sep 17 00:00:00 2001 From: James Bowman Date: Tue, 21 Jan 2020 10:37:51 -0800 Subject: [PATCH 12/12] Add 'x' command (bus reset) to C common header and i2ccl. [#30] --- c/common/i2cdriver.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/c/common/i2cdriver.c b/c/common/i2cdriver.c index 181587a..318cc99 100644 --- a/c/common/i2cdriver.c +++ b/c/common/i2cdriver.c @@ -312,6 +312,15 @@ void i2c_scan(I2CDriver *sd, uint8_t devices[128]) (void)readFromSerialPort(sd->port, devices + 8, 112); } +uint8_t i2c_reset(I2CDriver *sd) +{ + charCommand(sd, 'x'); + uint8_t a[1]; + if (readFromSerialPort(sd->port, a, 1) != 1) + return 0; + return a[0]; +} + int i2c_start(I2CDriver *sd, uint8_t dev, uint8_t op) { uint8_t start[2] = {'s', (dev << 1) | op}; @@ -382,6 +391,15 @@ int i2c_commands(I2CDriver *sd, int argc, char *argv[]) ); break; + case 'x': + { + uint8_t sda_scl = i2c_reset(sd); + printf("Bus reset. SDA = %d, SCL = %d\n", + 1 & (sda_scl >> 1), + 1 & sda_scl); + } + break; + case 'd': { uint8_t devices[128]; @@ -468,6 +486,7 @@ int i2c_commands(I2CDriver *sd, int argc, char *argv[]) fprintf(stderr, "Commands are:"); fprintf(stderr, "\n"); fprintf(stderr, " i display status information (uptime, voltage, current, temperature)\n"); + fprintf(stderr, " x I2C bus reset\n"); fprintf(stderr, " d device scan\n"); fprintf(stderr, " w dev write bytes to I2C device dev\n"); fprintf(stderr, " p send a STOP\n");