diff --git a/i2c_interfaces.py b/i2c_interfaces.py index 10a7b68..d7df5c2 100644 --- a/i2c_interfaces.py +++ b/i2c_interfaces.py @@ -27,6 +27,36 @@ class I2CInterface: pass +class DevI2CInterface(I2CInterface): + def __init__(self, bus) -> None: + super().__init__() + from smbus import SMBus, i2c_msg + self.bus = SMBus(bus) + self.msg = i2c_msg + + def read(self, addr: int, length: int) -> bytes: + m = self.msg.read(addr, length) + self.bus.i2c_rdwr(m) + return bytes(m.buf[0:length]) + + + def write(self, addr: int, data: bytes) -> None: + m = self.msg.write(addr, data) + self.bus.i2c_rdwr(m) + + def write_read(self, addr: int, data: bytes, length: int) -> bytes: + mw = self.msg.write(addr, data) + mr = self.msg.read(addr, length) + self.bus.i2c_rdwr(mw, mr) + return bytes(mr.buf[0:length]) + + def open(self) -> None: + pass + + def close(self) -> None: + pass + + class JLinkSwdI2CInterface(I2CInterface): GPIO_BASE = { 'PA': 0x40010800,