Commit 4f8ae283 authored by Jürgen Hasch's avatar Jürgen Hasch

More API updates

parent 1a4f7411
......@@ -7,59 +7,37 @@ http://dangerousprototypes.com/2011/03/14/new-version-of-pybuspiratelite-python-
-------------------------
This is a rewrite for the pyBusPirateLite library. Really more than a rewrite,
it is a complete overhaul. I first started it with the intention of making a
library that allowed you to interface the bus pirate as if it were a microcontroller.
Well, that is still what the library is for, but as time has gone on (and I have used
the library more and more in creating my extend-able oscilloscope program) I went at
it and put recursion in all of the main functions. The reason for this is because
sometimes the bus pirate gets stuck, or a communication is failed, etc, and you have
to try and re-send something. I got really sick of continuouly resending things
explicitly in my code, so I made this library keep trying until it succeeded.
What this means is that if you call a function and it fails the first time,
the function will try again, as many as 15 times, to get it to work. If it doesn't
work, it probably means you don't have the bus pirate connected :D If it doesn't
work it will simply raise an error, as there is probably an error in your code,
not mine (and if it is in mine, then tell me so that I can say this with more
confidence! )
This library has been significantly updated an modified. The main goal was to make it simpler to use
and more pythonic.
So take a look at the library and try it out with your old code. Let me know what
you think!
It automatically tries to find the right comport, connects to the bus pirate and is typically used only in
a given mode to avoid confusion.
Use the library as follows:
Examples
--------
1) instantiate a UC object:
my_buspirate = UC()
1. SPI
2) connect:
my_buspirate.connect() #will normally work in linux.
from pyBusPirateLite.SPI import SPI, CFG_PUSH_PULL, CFG_IDLE
from pyBusPirateLite.BBIO_base import PIN_POWER, PIN_CS
# OR
spi = SPI()
spi.cfg_pins = PIN_POWER | PIN_CS
spi.config = CFG_PUSH_PULL | CFG_IDLE
spi.speed = '1MHz'
my_buspirate.connect(port) #define your own port
# send two bytes and receive answer
spi.cs = True
data = spi.transfer( [0x82, 0x00])
spi.cs = False
3) do stuff:
my_buspirate.enter_bb() #always do first after connected. gets into bit bang
print(ord(data[2]))
my_buspirate.enter_i2c() # get into i2c mode
... do stuff in i2c...
2. I2C
my_buspirate.enter_bb() #get back into bb mode
from pyBusPirateLite.SPI import I2C
from pyBusPirateLite.BBIO_base import PIN_POWER, PIN_CS
my_buspirate.configure_peripherals(
power = 'on', pullups = 'on') # turn on power and
# pullups, can be used in any
# mode
my_buspirate.set_dir(0x00) # set the direction of all the pins to output
my_buspirate.set_port(0b10101) # set the pins to output 10101
# (AUX is the high pin, MISO the low pin.
# Specify reverse order (AUX still high, but
# CS low) by setting translate = False)
...etc...
almost everything in the file BitBang.py implements recursion--so you can be sure that
if you tell the bus pirate to do something, it will do it!
i2c = I2C()
i2c.cfg_pins = PIN_POWER | PIN_CS
i2c.speed = '50kHz'
......@@ -2,13 +2,13 @@
# -*- coding: utf-8 -*-
# Example of SPI data transfer
from pyBusPirateLite.SPI import SPI
from pyBusPirateLite.BBIO_base import PinCfg
from pyBusPirateLite.SPI import SPI, CFG_PUSH_PULL, CFG_IDLE
from pyBusPirateLite.BBIO_base import PIN_POWER, PIN_CS
spi = SPI()
spi.cfg_pins(PinCfg.POWER | PinCfg.CS )
spi.config = SPI_cfg['PUSH_PULL'] | SPI_cfg['IDLE']
spi.outputs = PIN_POWER | PIN_CS
spi.state = PIN_POWER | PIN_CS
spi.config = CFG_PUSH_PULL | CFG_IDLE
spi.speed = '1MHz'
# send two bytes and receive answer
......
This diff is collapsed.
This diff is collapsed.
......@@ -36,7 +36,16 @@ pin_mapping = {'AUX': 0b10,
class I2C(BBIO_base):
def __init__(self, portname='', speed=115200, timeout=1):
""" Provide access to the Bus Pirate I2C hardware
""" Provide access to the Bus Pirate I2C interface
Parameters
----------
portname : str
Name of comport (/dev/bus_pirate or COM3)
speed : int
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
Example
-------
......
......@@ -37,21 +37,31 @@ SPI_speed = { '30kHz' : 0b000,
'4MHz' : 0b110,
'8MHz' : 0b111}
SPI_cfg = { 'PUSH_PULL' : 0x08,
'IDLE' : 0x04,
'CLK_EDGE' : 0x02,
'SAMPLE' : 0x01}
CFG_SAMPLE = 0x01
CFG_CLK_EDGE = 0x02
CFG_IDLE = 0x04
CFG_PUSH_PULL = 0x08
class SPI(BBIO_base):
def __init__(self, portname='', speed=115200, timeout=1):
""" Provide high-speed access to the Bus Pirate SPI hardware
Parameters
----------
portname : str
Name of comport (/dev/bus_pirate or COM3)
speed : int
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
Example
-------
>>> spi = SPI()
>>> spi.pins(PinCfg.POWER | PinCfg.CS )
>>> spi.config(SPI_cfg['PUSH_PULL'] | SPI_cfg['IDLE'])
>>> spi.set_speed(SPI_speed['1MHz'])
>>> spi.state = PIN_POWER | PIN_CS
>>> spi.config(CFG_PUSH_PULL | CFG_IDLE)
>>> spi.speed = '1MHz'
>>> spi.cs = True
>>> data = spi.transfer( [0x82, 0x00])
>>> spi.cs = False
......@@ -86,6 +96,13 @@ class SPI(BBIO_base):
self.recurse_flush(self.enter)
raise BPError('Could not enter SPI mode')
@property
def modestring(self):
""" Return mode version string """
self.write(0x01)
self.timeout(self.minDelay * 10)
return self.response(4)
@property
def config(self):
return self.spi_config
......@@ -104,12 +121,15 @@ class SPI(BBIO_base):
Parameters
----------
cfg
Values defined in SPICfg
OUT_TYPE: pin output (0 = HiZ, 1 = push-pull)
IDLE: clock idle phase
CLK_EDIGE: clock edge
SAMP: sample time (0 = middle)
cfg : byte
CFG_SAMPLE: sample time (0 = middle)
CFG_CLK_EDGE: clock edge (1 = active to idle
CFG_IDLE: clock idle phase (0 = low)
CFG_PUSH_PULL: pin output (0 = HiZ, 1 = push-pull)
Examples
-------
>>> spi.config = CFG_PUSH_PULL | CFG_IDLE
Raises
------
......@@ -249,4 +269,35 @@ class SPI(BBIO_base):
self.write(0x03)
if self.response(1, True) != '\x01':
raise ProtocolError("CS could not be set")
self._cs = value
\ No newline at end of file
self._cs = value
@property
def speed(self):
return self.spi_speed
@speed.setter
def speed(self, frequency):
""" Set SPI speed
Parameters
----------
frequency : str
SPI clock speed (30kHz, 125kHz, 250kHz, 1MHz, 2MHz, 2.6MHz, 4MHz, 8MHz)
Raises
------
ProtocolError
If I2C speed could not be set
"""
try:
clock = SPI_speed[frequency]
except KeyError:
raise ValueError('Clock speed not supported')
self.write(0x60 | clock)
if self.response(1, True) != 0x01:
raise ProtocolError('Could not set SPI speed')
self.spi_speed = frequency
......@@ -49,6 +49,27 @@ class UARTSpeed:
class UART(BBIO_base):
def __init__(self, portname='', speed=115200, timeout=1):
""" Provide the Bus Pirate UART interface
Parameters
----------
portname : str
Name of comport (/dev/bus_pirate or COM3)
speed : int
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
Example
-------
>>> spi = UART()
"""
super().__init__()
self.connect(portname, speed, timeout)
self.enter()
self.uart_config = None
def enter(self):
"""
......@@ -73,21 +94,6 @@ class UART(BBIO_base):
self.recurse_flush(self.enter)
raise BPError('Could not enter UART mode')
def exit(self):
""" Exit to bitbang mode
This command resets the Bus Pirate into raw bitbang mode from the user terminal.
It also resets to raw bitbang mode from any protocol mode. This command always returns a five byte bitbang
version string "BBIOx", where x is the current bitbang protocol version (currently 1).
"""
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.write(0x00)
self.timeout(self.minDelay * 10)
if self.response(5) == "BBIO1":
super().enter()
raise ProtocolError('Could not return to bitbang mode')
def manual_speed_cfg(self, baud):
""" Manual baud rate configuration, send 2 bytes
......@@ -110,13 +116,9 @@ class UART(BBIO_base):
return self.response()
def begin_input(self):
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0x04))
def end_input(self):
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0x05))
def enter_bridge_mode(self):
......@@ -124,22 +126,16 @@ class UART(BBIO_base):
Starts a transparent UART bridge using the current configuration. Unplug the Bus Pirate to exit.
"""
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0x0f))
self.timeout(0.1)
self.response(1, True)
def set_cfg(self, cfg):
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0xC0 | cfg))
self.timeout(0.1)
return self.response(1, True)
def read_cfg(self):
if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0xd0))
self.timeout(0.1)
return self.response(1, True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment