]> defiant.homedns.org Git - pyshared.git/commitdiff
added gpio to bus pirate
authorErik Andresen <erik@vontaene.de>
Sun, 13 Nov 2016 21:08:58 +0000 (22:08 +0100)
committerErik Andresen <erik@vontaene.de>
Sun, 13 Nov 2016 21:08:58 +0000 (22:08 +0100)
bus_pirate.py

index edc569949d07f7c35c62fd8b48361d23bf7d4917..aa7d51bf2494e541fdb0b4309e8638898475fa58 100644 (file)
@@ -4,6 +4,15 @@
 import serial
 import struct
 
+dPinToBit = {
+               "POWER": 6,
+               "PULLUP": 5,
+               "AUX": 4,
+               "MOSI": 3,
+               "CLK": 2,
+               "MISO": 1,
+               "CS": 0
+}
 
 # http://dangerousprototypes.com/docs/Bitbang
 # http://dangerousprototypes.com/docs/SPI_(binary)
@@ -11,6 +20,8 @@ class BP:
        def __init__(self, sDevice):
                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)
@@ -20,7 +31,7 @@ class BP:
                for i in range(20):
                        if self.command(chr(0x0), 5) == "BBIO1":
                                return
-               raise Exception()
+               raise Exception("Failed to enter bit bang mode")
 
        def mode_spi(self, mode, speed):
                if self.command(chr(0x1), 4) != "SPI1":
@@ -40,20 +51,13 @@ class BP:
                elif mode == 3:
                        self.spi_command(chr(0b10001110))
 
-
        def spi_set_speed(self, speed):
                lSpeeds = ["30kHz", "125kHz", "250kHz", "1MHz", "2MHz", "2.6MHz", "4MHz", "8MHz"]
                val = lSpeeds.index(speed)
                self.spi_command(chr(0b01100000 | val))
 
-       def command_byte_set(self, bit):
-               enable = 1<<bit
-               state = ord(self.command(chr(0x80 | enable, 1)))
-               if not state & enable:
-                       raise Exception("Got 0x%x, expected 0xc0" % state)
-
        def power_enable(self):
-               self.command_byte_set(6)
+               self.set_io("POWER", True)
        
        def get_adc(self):
                s = self.command(chr(0x14), 2)
@@ -69,3 +73,36 @@ class BP:
                if len(data) > 4096:
                        raise Exception("SPI Data String too long")
                return self.spi_command(struct.pack(">Bhh%ds" % len(data), 0x04, len(data), num_read, data))
+
+       def set_io_output(self, pin):
+               if pin not in dPinToBit.keys():
+                       raise Exception("Bad output pin")
+               self.io_dir &= ~(1 << dPinToBit[pin])
+               state = 0b01000000 | self.io_dir
+               self.command(chr(state), 1)
+
+       def set_io_input(self, pin):
+               if pin not in dPinToBit.keys():
+                       raise Exception("Bad input pin")
+               self.io_dir |= (1 << dPinToBit[pin])
+               state = 0b01000000 | self.io_dir
+               self.command(chr(state), 1)
+
+       def set_io(self, pin, val):
+               if pin not in dPinToBit.keys():
+                       raise Exception("Bad output pin")
+               if val:
+                       self.io_state |=  (1 << dPinToBit[pin])
+               else:
+                       self.io_state &= ~(1 << dPinToBit[pin])
+               state = self.update_io()
+
+       def update_io(self):
+               state = self.command(chr(0b10000000 | self.io_state), 1)
+               return ord(state)
+
+       def get_io(self, pin):
+               if pin not in dPinToBit.keys():
+                       raise Exception("Bad I/O pin")
+               state = self.update_io()
+               return state & (1 << dPinToBit[pin])