Commit 90387d28 authored by Juergen Hasch's avatar Juergen Hasch Committed by GitHub

Merge pull request #7 from juhasch/fix/cleanup

Fix/cleanup
parents 58028a9e 475bce06
# pyBusPirateLite
Python library for BusPirate based on code from Garrett Berg.
pyBusPirateLite
===============
Python library for BusPirate based on code from Garrett Berg.
It tries to be more Pythonic than the original code.
This library allows using the following modes:
......@@ -15,9 +17,9 @@ For more information about the BusPirate see http://dangerousprototypes.com/docs
Based on code from Garrett Berg <cloudform511@gmail.com>
(http://dangerousprototypes.com/2011/03/14/new-version-of-pybuspiratelite-python-library/)
-------------------------
## Examples
Examples
--------
### SPI
......
......@@ -14,13 +14,11 @@
# serve to show the default.
import sys
import os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.insert(1, '/Users/juhasch/git/PhysicalQuantities/')
#os.path.abspath('..'))
sys.path.insert(1, '../')
import pyBusPirateLite
......@@ -34,11 +32,9 @@ import pyBusPirateLite
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.mathjax',
'sphinx.ext.viewcode',
'numpydoc',
'sphinx.ext.autosummary',
'IPython.sphinxext.ipython_console_highlighting'
'sphinx.ext.autosummary'
]
# Add any paths that contain templates here, relative to this directory.
......@@ -56,7 +52,7 @@ master_doc = 'index'
# General information about the project.
project = 'pyBusPirateLite'
copyright = '2016, Juergen Hasch'
copyright = '2017, Juergen Hasch'
author = 'Jürgen Hasch'
# The version info for the project you're documenting, acts as replacement for
......@@ -64,9 +60,9 @@ author = 'Jürgen Hasch'
# built documents.
#
# The short X.Y version.
version = pyBusPirateLite.__version__
version = str(pyBusPirateLite.__version__)
# The full version, including alpha/beta/rc tags.
release = pyBusPirateLite.__version__
release = str(pyBusPirateLite.__version__)
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
......@@ -146,7 +142,7 @@ html_theme = 'alabaster'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
#html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
......
.. pyBusPirateLite documentation master file
*****************************
pyBusPirateLite Documentation
================================
*****************************
# pyBusPirateLite
Python library for BusPirate
Python library for BusPirate based on code from Garrett Berg.
It tries to be more Pythonic than the original code. This mostly means using getters/setters instead of explicit
function calls.
This library allows using the following modes:
* SPI
* I2C
* UART
* Bitbang
* Onewire
* Rawwire
* SPI
* I2C
* UART
* Bitbang
* Onewire
* Rawwire
For more information about the BusPirate see http://dangerousprototypes.com/docs/Bus_Pirate
Based on code from Garrett Berg <cloudform511@gmail.com>
(http://dangerousprototypes.com/2011/03/14/new-version-of-pybuspiratelite-python-library/)
Examples
########
SPI::
from pyBusPirateLite.SPI import *
spi = SPI()
spi.pins = PIN_POWER | PIN_CS
spi.config = CFG_PUSH_PULL | CFG_IDLE
spi.speed = '1MHz'
# send two bytes and receive answer
spi.cs = True
data = spi.transfer( [0x82, 0x00])
spi.cs = False
Bitbang::
from pyBusPirateLite.BitBang import BitBang
from pyBusPirateLite.BBIO_base import PIN_AUX
bb = BitBang()
bb.outputs = PIN_AUX
bb.pins = 0 # set aux pin = 0
bb.pins = PIN_AUX # set aux pin = 1
2C::
from pyBusPirateLite.SPI import *
i2c = I2C()
i2c.pins = PIN_POWER | PIN_CS
i2c.speed = '50kHz'
Get BusPirate serial port::
from pyBusPirateLite.BitBang import BitBang
bb = BitBang(connect=False)
port = bb.get_port()
print(port)
Classes
#######
pyBusPirateLite.BBIO_base.BBIO_base
-----------------------------------
.. autoclass:: pyBusPirateLite.BBIO_base.BBIO_base
:members:
:undoc-members:
:show-inheritance:
pyBusPirateLite.BitBang.BitBang
-------------------------------
.. autoclass:: pyBusPirateLite.BitBang.BitBang
:members:
:undoc-members:
:show-inheritance:
pyBusPirateLite.I2C.I2C
-----------------------
.. autoclass:: pyBusPirateLite.I2C.I2C
:members:
:undoc-members:
:show-inheritance:
pyBusPirateLite.SPI.SPI
-----------------------
.. autoclass:: pyBusPirateLite.SPI.SPI
:members:
:undoc-members:
:show-inheritance:
pyBusPirateLite.UART.UART
-------------------------
**Contents**
.. autoclass:: pyBusPirateLite.UART.UART
:members:
:undoc-members:
:show-inheritance:
*User Guide:*
pyBusPirateLite.UC.UC
---------------------
.. toctree::
:maxdepth: 2
.. autoclass:: pyBusPirateLite.UC.UC
:members:
:undoc-members:
:show-inheritance:
*Reference:*
pyBusPirateLite.onewire.OneWire
-------------------------------
.. toctree::
:maxdepth: 2
.. autoclass:: pyBusPirateLite.onewire.OneWire
:members:
:undoc-members:
:show-inheritance:
Quantity
Unit
constants
pyBusPirateLite.rawwire.RawWire
-------------------------------
**Indices and tables**
.. autoclass:: pyBusPirateLite.rawwire.RawWire
:members:
:undoc-members:
:show-inheritance:
Indices and tables
------------------
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
* Summary :
*
* Created on Jan 26, 2011
* @author: garrett
"""
import select
from time import sleep
import serial
......@@ -35,7 +28,7 @@ PIN_POWER = 0x40
class BBIO_base:
"""functions used in every mode, the base of class. Most of these you can
"""Functions used in every mode, the base of class. Most of these you can
probably ignore, as they are just used in the other member classes
Note: Also contains some of the older functions that are now probably outdated
"""
......@@ -66,9 +59,13 @@ class BBIO_base:
It also resets to raw bitbang mode from raw SPI mode, or any other protocol mode.
This command always returns a five byte bitbang version string "BBIOx", w
here x is the current protocol version (currently 1).
Some terminals send a NULL character (0x00) on start-up, causing the Bus Pirate to enter binary mode when
it wasn't wanted. To get around this, you must now enter 0x00 at least 20 times to enter raw bitbang mode.
Note: The Bus Pirate user terminal could be stuck in a configuration menu when your program attempts to enter
Notes
-----
The Bus Pirate user terminal could be stuck in a configuration menu when your program attempts to enter
binary mode. One way to ensure that you're at the command line is to send <enter> at least 10 times,
and then send '#' to reset. Next, send 0x00 to the command line 20+ times until you get the BBIOx version string.
After entering bitbang mode, you can enter other binary protocol modes.
......@@ -87,7 +84,7 @@ class BBIO_base:
r = self.response(1, True)
if r:
break
for i in range(2):
for m in range(2):
self.write(0x00)
self.timeout(self.minDelay * 10)
......@@ -106,7 +103,7 @@ class BBIO_base:
raise BPError('Could not enter bitbang mode')
def enter(self):
"""Enter bitbang mode
"""Enter bitbang mode.
Will be overriden by other classes
"""
if self.mode == 'bb':
......@@ -156,12 +153,12 @@ class BBIO_base:
return port.name
def connect(self, portname='', speed=115200, timeout=0.1):
""" will try to automatically find a port regardless of os
"""Will try to automatically find a port regardless of os
Parameters
----------
portname : str
Name of comport (/dev/bus_pirate or COM3)
Name of comport (e.g. /dev/ttyUSB0 or COM3)
speed : int
Communication speed, use default of 115200
timeout : int
......@@ -191,6 +188,10 @@ class BBIO_base:
if self.port:
self.port.close()
def __exit__(self):
""" Disconnect bus pirate when exiting"""
self.disconnect()
def timeout(self, timeout = 0.1):
sleep(timeout)
......@@ -216,19 +217,19 @@ class BBIO_base:
def recurse_end(self):
self._attempts_ = 0
def recurse(self, function, *args):
def recurse(self, func, *args):
if self._attempts_ < 15:
self._attempts_ += 1
return function(*args)
return func(*args)
raise IOError('bus pirate malfunctioning')
def recurse_flush(self, function, *args):
def recurse_flush(self, func, *args):
if self._attempts_ < 15:
self._attempts_ += 1
for n in range(5):
self.write(0x00)
self.port.flushInput()
return function(*args)
return func(*args)
raise IOError('bus pirate malfunctioning')
......@@ -238,9 +239,10 @@ Note: Some of these do not have error checking implemented
checking. This is as planned, since all of these
depend on the device you are interfacing with)"""
def send_start_bit(self):
self.write(0x02)
resp = self.response(1, True)
self.response(1, True)
if self.response(1, True) == '\x01':
self.recurse_end()
return 1
......
......@@ -261,9 +261,9 @@ class BitBang(BBIO_base):
else:
raise ValueError('frequency requested is invalid')
prescaler=Prescaler
dutycycle=OCR
period=PRy
prescaler = Prescaler
dutycycle = OCR
period = PRy
self.write(0x12)
self.write(prescaler)
......
......@@ -275,9 +275,9 @@ class I2C(BBIO_base):
0x?? - read position 256 - the requested number of bytes read from the I2C bus
"""
self.write(0x08)
self.write(numtx>>8 & 0xff)
self.write(numtx >> 8 & 0xff)
self.write(numtx & 0xff)
self.write(numrx>>8 & 0xff)
self.write(numrx >> 8 & 0xff)
self.write(numrx & 0xff)
for data in txdata:
self.write(data)
......@@ -287,15 +287,21 @@ class I2C(BBIO_base):
def aux(self, cmd):
""" Provides extended use of AUX pin. Requires one command byte. Bus Pirate acknowledges 0x01.
Command Function
+-----|------------+
|0x00 | AUX/CS low |
|0x01 | AUX/CS high|
|0x02 | AUX/CS HiZ |
|0x03 | AUX read |
|0x10 | use AUX |
|0x20 | use CS |
+-----|------------+
+--------+------------+
|Command | Function |
+========+============+
| 0x00 | AUX/CS low |
+--------+------------+
| 0x01 | AUX/CS high|
+--------+------------+
| 0x02 | AUX/CS HiZ |
+--------+------------+
| 0x03 | AUX read |
+--------+------------+
| 0x10 | use AUX |
+--------+------------+
| 0x20 | use CS |
+--------+------------+
"""
self.write(0x09)
if cmd in [0x00, 0x01, 0x02, 0x03, 0x10, 0x20]:
......
......@@ -28,14 +28,14 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
from .BBIO_base import BBIO_base, BPError, ProtocolError
SPI_speed = { '30kHz' : 0b000,
'125kHz': 0b001,
'250kHz': 0b010,
'1MHz' : 0b011,
'2MHz' : 0b100,
'2.6MHz': 0b101,
'4MHz' : 0b110,
'8MHz' : 0b111}
SPI_speed = {'30kHz' : 0b000,
'125kHz': 0b001,
'250kHz': 0b010,
'1MHz' : 0b011,
'2MHz' : 0b100,
'2.6MHz': 0b101,
'4MHz' : 0b110,
'8MHz' : 0b111}
CFG_SAMPLE = 0x01
CFG_CLK_EDGE = 0x02
......@@ -220,7 +220,7 @@ class SPI(BBIO_base):
rxdata = self.response(length, True)
return rxdata
def write_then_read(self, numtx, numrx, txdata, cs = True):
def write_then_read(self, numtx, numrx, txdata, cs=True):
""" Write then read
This command was developed to help speed ROM programming with Flashrom. It might be helpful for a lot of common
......@@ -275,9 +275,9 @@ class SPI(BBIO_base):
self.write(0x04)
else:
self.write(0x05)
self.write(numtx>>8 & 0xff)
self.write(numtx >> 8 & 0xff)
self.write(numtx & 0xff)
self.write(numrx>>8 & 0xff)
self.write(numrx >> 8 & 0xff)
self.write(numrx & 0xff)
for data in txdata:
self.write(data)
......
......@@ -127,7 +127,7 @@ class UART(BBIO_base):
Use the UART manual [PDF] or an online calculator to find the correct value (key values: fosc 32mHz,
clock divider = 2, BRGH=1) . Bus Pirate responds 0x01 to each byte. Settings take effect immediately.
"""
BRG = (FOSC / (4 * baud)) - 1
BRG = (FOSC // (4 * baud)) - 1
BRGH = ((BRG >> 8) & 0xFF)
BRGL = (BRG & 0xFF)
self.write(0x03)
......
......@@ -55,4 +55,3 @@ class UC(BBIO, I2C, _1WIRE, RawWire, SPI, UART):
can be updated
"""
pass
......@@ -26,4 +26,4 @@ You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
__version__ = 2.0
__version__ = 0.2
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of I2C data transfer
from pyBusPirateLite.I2C import *
i2c = I2C()
......@@ -36,10 +36,10 @@ inary1WIRE mode:
# 0101wxyz - Read peripherals (planned, not implemented)
"""
from .BitBang import BBIO
from .BitBang import BBIO_base
class OneWire(BBIO):
class OneWire(BBIO_base):
def enter_1wire(self):
self.check_mode('bb')
self.write(0x04)
......
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created by Sean Nelson on 2009-10-14.
Copyright 2009 Sean Nelson <audiohacked@gmail.com>
......@@ -108,7 +110,7 @@ class RawWire(BBIO_base):
self.timeout(0.1)
return self.response(1)
#if someone who cares could write a more user-friendly wire_cfg that would be cool
# if someone who cares could write a more user-friendly wire_cfg that would be cool
# (make it similar to my configure_peripherals)
def bulk_clock_ticks(self, ticks = 1):
......
......@@ -6,7 +6,7 @@ def read(fname):
setup(
name = "pyBusPirateLite",
version = "0.1",
version = "0.2",
author = "Juergen Hasch",
author_email = "juergen.hasch@elbonia.de",
description = ("Python library for BusPirate"),
......
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