X-Git-Url: https://defiant.homedns.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=bus_pirate.py;h=02f3fb8211024a29eb0165be158aef32be520d82;hb=1c4ce051633cb05741796f49e178f4b3bde28c4a;hp=08305f5837e308b35307ca5ad000973195cebeae;hpb=db48b5cd6a350ff55943d1afd48e55724eca44e0;p=pyshared.git diff --git a/bus_pirate.py b/bus_pirate.py index 08305f5..02f3fb8 100644 --- a/bus_pirate.py +++ b/bus_pirate.py @@ -3,6 +3,7 @@ import serial import struct +import threading dPinToBit = { "POWER": 6, @@ -18,14 +19,16 @@ dPinToBit = { # http://dangerousprototypes.com/docs/SPI_(binary) class BP: def __init__(self, sDevice): + self.lock = threading.Lock() self.pSerial = serial.Serial(sDevice, baudrate=115200, timeout=0.1) self.mode_bit_bang() self.io_state = 0 self.io_dir = 0 def command(self, cmd, num_read): - self.pSerial.write(cmd) - return self.pSerial.read(num_read) + with self.lock: + self.pSerial.write(cmd) + return self.pSerial.read(num_read) def mode_bit_bang(self): for i in range(20): @@ -64,6 +67,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: @@ -107,12 +133,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() @@ -125,10 +158,19 @@ 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..) - 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: @@ -136,10 +178,11 @@ 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<<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 +192,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)