]> defiant.homedns.org Git - pyshared.git/commitdiff
bus_pirate: add i2c ping
authorErik Andresen <erik@vontaene.de>
Sat, 21 Nov 2020 16:42:57 +0000 (17:42 +0100)
committerErik Andresen <erik@vontaene.de>
Sat, 21 Nov 2020 16:42:57 +0000 (17:42 +0100)
bus_pirate.py

index d7c8eaf0d483df61e87b463df04db1a0a8c2d868..02f3fb8211024a29eb0165be158aef32be520d82 100644 (file)
@@ -158,6 +158,15 @@ class BP:
                if ord(ret) != 0x1:
                        raise Exception()
 
+       def i2c_ping(self, addr):
+               # 1. Write
+               # command (1) | number of write bytes (2) | number of read bytes (2) | bytes to write (0..)
+               msg = struct.pack(">BHHB", 0x08, 1, 0, addr)
+               ret = self.command(msg, 1)
+
+               if ord(ret[0]) != 0x1:
+                       raise Exception("I2C ping failed")
+
        def i2c_write(self, addr, reg, s):
                # 1. Write
                # command (1) | number of write bytes (2) | number of read bytes (2) | bytes to write (0..)
@@ -169,7 +178,8 @@ class BP:
 
        def i2c_read(self, addr, reg, num_read):
                # set reg
-               self.i2c_write(addr, reg, "")
+               if reg != None:
+                       self.i2c_write(addr, reg, "")
 
                # command (1) | number of write bytes (2) | number of read bytes (2) | bytes to write (0..)
                msg = struct.pack(">BHHB", 0x08, 1, num_read, addr | 0x1)