X-Git-Url: https://defiant.homedns.org/gitweb/?a=blobdiff_plain;f=bus_pirate.py;h=cdd259fa7581ee42dba23da879fbf10b3aa4d287;hb=2056613e07dc73a25e8426a6ca67fccc88e13fee;hp=f64c8e3cc60a3b24714196df4ee092a7076b0291;hpb=b72bb34f3d40f0e0b6178f261403f93cbda82d95;p=pyshared.git diff --git a/bus_pirate.py b/bus_pirate.py index f64c8e3..cdd259f 100644 --- a/bus_pirate.py +++ b/bus_pirate.py @@ -64,6 +64,29 @@ class BP: v = struct.unpack(">h", s) return v[0]/1024.0*6.6 + # http://codepad.org/qtYpZmIF + def pwm(self, freq, duty_cycle_percent): + lPrescaler = {0:1, 1:8 , 2:64, 3:256} + Fosc = 32e6 + Tcy = 2.0 / Fosc + period = 1.0 / freq + prescaler = 1 + + # find needed prescaler + for i in range(4): + prescaler = lPrescaler[i] + PRy = period * 1.0 / (Tcy * prescaler) + PRy = int(PRy - 1) + OCR = int(PRy * duty_cycle_percent) + + if PRy < (2 ** 16 - 1): + break # valid value for PRy, keep values + + cmd = struct.pack(">BBHH", 0b00010010, i, OCR, PRy) + ret = self.command(cmd, 1) + if ord(ret) != 0x1: + raise Exception() + def spi_command(self, cmd): ret = self.command(cmd, 1) if ord(ret) != 0x1: @@ -135,7 +158,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: @@ -146,7 +169,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: @@ -156,7 +179,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)