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()