X-Git-Url: https://defiant.homedns.org/gitweb/?a=blobdiff_plain;f=bus_pirate.py;h=4ef9c61d4eacf79612be535156477e553b4d770b;hb=6af0c706c1dd1b0ae4cbc38adf1f056a8686aae4;hp=08305f5837e308b35307ca5ad000973195cebeae;hpb=db48b5cd6a350ff55943d1afd48e55724eca44e0;p=pyshared.git diff --git a/bus_pirate.py b/bus_pirate.py index 08305f5..4ef9c61 100644 --- a/bus_pirate.py +++ b/bus_pirate.py @@ -107,12 +107,19 @@ class BP: state = self.update_io() return state & (1 << dPinToBit[pin]) - def mode_i2c(self, bEnablePower=False, bEnablePullup=False): + def mode_i2c(self, bEnablePower=False, bEnablePullup=False, iSpeedkHz=100): if self.command(chr(0x2), 4) != "I2C1": raise Exception() - # 100kHz - ret = self.command(chr(0b01100010), 1) + dSpeeds = { + 5: 0x0, + 50: 0x1, + 100: 0x2, + 400: 0x3, + } + if iSpeedkHz not in dSpeeds.keys(): + raise Exception("Invalid I2C speed") + ret = self.command(chr(0b01100000 | dSpeeds[iSpeedkHz]), 1) if ord(ret) != 0x1: raise Exception() @@ -128,7 +135,7 @@ class BP: 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..) - msg = struct.pack(">BHHBB%ds" % len(s), 0x08, 2+len(s), 0, addr<<1, reg, s) + msg = struct.pack(">BHHBB%ds" % len(s), 0x08, 2+len(s), 0, addr, reg, s) ret = self.command(msg, 1) if ord(ret[0]) != 0x1: @@ -139,7 +146,7 @@ class BP: 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<<1) | 0x1) + msg = struct.pack(">BHHB", 0x08, 1, num_read, addr | 0x1) ret = self.command(msg, 1 + num_read) if ord(ret[0]) != 0x1: @@ -149,7 +156,7 @@ class BP: def i2c_search(self): for i in range(128): - msg = struct.pack(">BHHB", 0x08, 1, 1, i<<1) + msg = struct.pack(">BHHB", 0x08, 1, 1, i) ret = self.command(msg, 1) if ord(ret) == 0x1: print "Found I2C Addr: 0x%x" % (i & ~0x1)