Commit b7af1ac9 authored by juhasch's avatar juhasch

Small cleanups

parent e6aa8561
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
* Summary :
*
* Created on Jan 26, 2011
* @author: garrett
"""
import select
from time import sleep
import serial
......@@ -87,7 +80,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)
......@@ -191,6 +184,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 +213,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,10 +235,11 @@ 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.check_mode(['i2c', 'raw'])
self.write(0x02)
resp = self.response(1, True)
self.response(1, True)
if self.response(1, True) == '\x01':
self.recurse_end()
return 1
......
......@@ -21,10 +21,9 @@ You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import serial
from .BBIO_base import BBIO_base, BPError, ProtocolError
class BitBang(BBIO_base):
def __init__(self, portname='', speed=115200, timeout=0.1, connect=True):
""" Provide access to the Bus Pirate bitbang mode
......@@ -176,13 +175,14 @@ class BitBang(BBIO_base):
def stop_getting_adc_voltages(self):
"""I was encountering problems resetting out of adc mode, so I wrote this
little function"""
self.check_mode('adc')
#self.check_mode('adc')
self.port.flushInput()
for i in range(5):
self.write(0x00)
#r, w, e = select.select([self.port], [], [], 0.01);
r = self.response(1, True)
if (r): break;
if r:
break
self.port.flushInput()
self.enter_bb()
return 1
......@@ -262,9 +262,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)
......
......@@ -28,7 +28,7 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
from .BBIO_base import BBIO_base, BPError, ProtocolError
SPI_speed = { '30kHz' : 0b000,
SPI_speed = {'30kHz' : 0b000,
'125kHz': 0b001,
'250kHz': 0b010,
'1MHz' : 0b011,
......@@ -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
......@@ -80,7 +80,7 @@ def i2c_write_data(bp_device, data):
return ack_signals
def sniff_i2c_devices(bp_device, power='off'):
def sniff_i2c_devices(bp_device, power=False):
init_i2c(bp_device, power)
working_addr = []
for n in range(128):
......
......@@ -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)
......
......@@ -21,7 +21,7 @@ You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
from .BitBang import BitBang
from .BBIO_base import BBIO_base, BPError, ProtocolError
class RawWireCfg:
......@@ -110,16 +110,16 @@ class RawWire(BBIO_base):
self.timeout(0.1)
return self.response(1)
def wire_cfg(self, pins = 0):
def wire_cfg(self, pins=0):
self.check_mode('raw')
self.port.write(chr(0x80 | pins))
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):
def bulk_clock_ticks(self, ticks=1):
self.check_mode('raw')
self.port.write(chr(0x20 | (ticks - 1)))
self.timeout(0.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