From d55e9e63f510c1a2bd186acc55b0906d11dddde5 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Sun, 9 Jan 2022 19:38:19 +0100 Subject: [PATCH] Add /dev/i2c driver --- i2c_interfaces.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) 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,