Commit b7af1ac9 authored by juhasch's avatar juhasch

Small cleanups

parent e6aa8561
#! /usr/bin/env python #! /usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
* Summary :
*
* Created on Jan 26, 2011
* @author: garrett
"""
import select
from time import sleep from time import sleep
import serial import serial
...@@ -87,7 +80,7 @@ class BBIO_base: ...@@ -87,7 +80,7 @@ class BBIO_base:
r = self.response(1, True) r = self.response(1, True)
if r: if r:
break break
for i in range(2): for m in range(2):
self.write(0x00) self.write(0x00)
self.timeout(self.minDelay * 10) self.timeout(self.minDelay * 10)
...@@ -191,6 +184,10 @@ class BBIO_base: ...@@ -191,6 +184,10 @@ class BBIO_base:
if self.port: if self.port:
self.port.close() self.port.close()
def __exit__(self):
""" Disconnect bus pirate when exiting"""
self.disconnect()
def timeout(self, timeout = 0.1): def timeout(self, timeout = 0.1):
sleep(timeout) sleep(timeout)
...@@ -216,19 +213,19 @@ class BBIO_base: ...@@ -216,19 +213,19 @@ class BBIO_base:
def recurse_end(self): def recurse_end(self):
self._attempts_ = 0 self._attempts_ = 0
def recurse(self, function, *args): def recurse(self, func, *args):
if self._attempts_ < 15: if self._attempts_ < 15:
self._attempts_ += 1 self._attempts_ += 1
return function(*args) return func(*args)
raise IOError('bus pirate malfunctioning') raise IOError('bus pirate malfunctioning')
def recurse_flush(self, function, *args): def recurse_flush(self, func, *args):
if self._attempts_ < 15: if self._attempts_ < 15:
self._attempts_ += 1 self._attempts_ += 1
for n in range(5): for n in range(5):
self.write(0x00) self.write(0x00)
self.port.flushInput() self.port.flushInput()
return function(*args) return func(*args)
raise IOError('bus pirate malfunctioning') raise IOError('bus pirate malfunctioning')
...@@ -238,10 +235,11 @@ Note: Some of these do not have error checking implemented ...@@ -238,10 +235,11 @@ Note: Some of these do not have error checking implemented
checking. This is as planned, since all of these checking. This is as planned, since all of these
depend on the device you are interfacing with)""" depend on the device you are interfacing with)"""
def send_start_bit(self): def send_start_bit(self):
self.check_mode(['i2c', 'raw']) self.check_mode(['i2c', 'raw'])
self.write(0x02) self.write(0x02)
resp = self.response(1, True) self.response(1, True)
if self.response(1, True) == '\x01': if self.response(1, True) == '\x01':
self.recurse_end() self.recurse_end()
return 1 return 1
......
...@@ -21,10 +21,9 @@ You should have received a copy of the GNU General Public License ...@@ -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/>. along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
""" """
import serial
from .BBIO_base import BBIO_base, BPError, ProtocolError from .BBIO_base import BBIO_base, BPError, ProtocolError
class BitBang(BBIO_base): class BitBang(BBIO_base):
def __init__(self, portname='', speed=115200, timeout=0.1, connect=True): def __init__(self, portname='', speed=115200, timeout=0.1, connect=True):
""" Provide access to the Bus Pirate bitbang mode """ Provide access to the Bus Pirate bitbang mode
...@@ -176,13 +175,14 @@ class BitBang(BBIO_base): ...@@ -176,13 +175,14 @@ class BitBang(BBIO_base):
def stop_getting_adc_voltages(self): def stop_getting_adc_voltages(self):
"""I was encountering problems resetting out of adc mode, so I wrote this """I was encountering problems resetting out of adc mode, so I wrote this
little function""" little function"""
self.check_mode('adc') #self.check_mode('adc')
self.port.flushInput() self.port.flushInput()
for i in range(5): for i in range(5):
self.write(0x00) self.write(0x00)
#r, w, e = select.select([self.port], [], [], 0.01); #r, w, e = select.select([self.port], [], [], 0.01);
r = self.response(1, True) r = self.response(1, True)
if (r): break; if r:
break
self.port.flushInput() self.port.flushInput()
self.enter_bb() self.enter_bb()
return 1 return 1
...@@ -262,9 +262,9 @@ class BitBang(BBIO_base): ...@@ -262,9 +262,9 @@ class BitBang(BBIO_base):
else: else:
raise ValueError('frequency requested is invalid') raise ValueError('frequency requested is invalid')
prescaler=Prescaler prescaler = Prescaler
dutycycle=OCR dutycycle = OCR
period=PRy period = PRy
self.write(0x12) self.write(0x12)
self.write(prescaler) self.write(prescaler)
......
...@@ -28,14 +28,14 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>. ...@@ -28,14 +28,14 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
from .BBIO_base import BBIO_base, BPError, ProtocolError from .BBIO_base import BBIO_base, BPError, ProtocolError
SPI_speed = { '30kHz' : 0b000, SPI_speed = {'30kHz' : 0b000,
'125kHz': 0b001, '125kHz': 0b001,
'250kHz': 0b010, '250kHz': 0b010,
'1MHz' : 0b011, '1MHz' : 0b011,
'2MHz' : 0b100, '2MHz' : 0b100,
'2.6MHz': 0b101, '2.6MHz': 0b101,
'4MHz' : 0b110, '4MHz' : 0b110,
'8MHz' : 0b111} '8MHz' : 0b111}
CFG_SAMPLE = 0x01 CFG_SAMPLE = 0x01
CFG_CLK_EDGE = 0x02 CFG_CLK_EDGE = 0x02
...@@ -220,7 +220,7 @@ class SPI(BBIO_base): ...@@ -220,7 +220,7 @@ class SPI(BBIO_base):
rxdata = self.response(length, True) rxdata = self.response(length, True)
return rxdata 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 """ Write then read
This command was developed to help speed ROM programming with Flashrom. It might be helpful for a lot of common 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): ...@@ -275,9 +275,9 @@ class SPI(BBIO_base):
self.write(0x04) self.write(0x04)
else: else:
self.write(0x05) self.write(0x05)
self.write(numtx>>8 & 0xff) self.write(numtx >> 8 & 0xff)
self.write(numtx & 0xff) self.write(numtx & 0xff)
self.write(numrx>>8 & 0xff) self.write(numrx >> 8 & 0xff)
self.write(numrx & 0xff) self.write(numrx & 0xff)
for data in txdata: for data in txdata:
self.write(data) self.write(data)
......
...@@ -127,7 +127,7 @@ class UART(BBIO_base): ...@@ -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, 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. 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) BRGH = ((BRG >> 8) & 0xFF)
BRGL = (BRG & 0xFF) BRGL = (BRG & 0xFF)
self.write(0x03) self.write(0x03)
......
...@@ -55,4 +55,3 @@ class UC(BBIO, I2C, _1WIRE, RawWire, SPI, UART): ...@@ -55,4 +55,3 @@ class UC(BBIO, I2C, _1WIRE, RawWire, SPI, UART):
can be updated can be updated
""" """
pass pass
...@@ -26,4 +26,4 @@ You should have received a copy of the GNU General Public License ...@@ -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/>. 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): ...@@ -80,7 +80,7 @@ def i2c_write_data(bp_device, data):
return ack_signals return ack_signals
def sniff_i2c_devices(bp_device, power='off'): def sniff_i2c_devices(bp_device, power=False):
init_i2c(bp_device, power) init_i2c(bp_device, power)
working_addr = [] working_addr = []
for n in range(128): for n in range(128):
......
...@@ -36,10 +36,10 @@ inary1WIRE mode: ...@@ -36,10 +36,10 @@ inary1WIRE mode:
# 0101wxyz - Read peripherals (planned, not implemented) # 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): def enter_1wire(self):
self.check_mode('bb') self.check_mode('bb')
self.write(0x04) self.write(0x04)
......
...@@ -21,7 +21,7 @@ You should have received a copy of the GNU General Public License ...@@ -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/>. 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: class RawWireCfg:
...@@ -110,16 +110,16 @@ class RawWire(BBIO_base): ...@@ -110,16 +110,16 @@ class RawWire(BBIO_base):
self.timeout(0.1) self.timeout(0.1)
return self.response(1) return self.response(1)
def wire_cfg(self, pins = 0): def wire_cfg(self, pins=0):
self.check_mode('raw') self.check_mode('raw')
self.port.write(chr(0x80 | pins)) self.port.write(chr(0x80 | pins))
self.timeout(0.1) self.timeout(0.1)
return self.response(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) # (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.check_mode('raw')
self.port.write(chr(0x20 | (ticks - 1))) self.port.write(chr(0x20 | (ticks - 1)))
self.timeout(0.1) self.timeout(0.1)
......
...@@ -6,7 +6,7 @@ def read(fname): ...@@ -6,7 +6,7 @@ def read(fname):
setup( setup(
name = "pyBusPirateLite", name = "pyBusPirateLite",
version = "0.1", version = "0.2",
author = "Juergen Hasch", author = "Juergen Hasch",
author_email = "juergen.hasch@elbonia.de", author_email = "juergen.hasch@elbonia.de",
description = ("Python library for BusPirate"), 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