Commit 1a4f7411 authored by Jürgen Hasch's avatar Jürgen Hasch

Major update with cleaner API and Python3 support

parent 913ac51e
...@@ -2,24 +2,18 @@ ...@@ -2,24 +2,18 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Example of SPI data transfer # Example of SPI data transfer
from pyBusPirateLite.SPI import * from pyBusPirateLite.SPI import SPI
from pyBusPirateLite.BBIO_base import PinCfg from pyBusPirateLite.BBIO_base import PinCfg
spi = SPI() spi = SPI()
spi.connect('COM3')
spi.enter_bb()
spi.enter_spi()
spi.cfg_pins(PinCfg.POWER | PinCfg.CS ) spi.cfg_pins(PinCfg.POWER | PinCfg.CS )
spi.cfg_spi( 0x0c ) spi.config = SPI_cfg['PUSH_PULL'] | SPI_cfg['IDLE']
spi.set_speed(SPISpeed._1MHZ) spi.speed = '1MHz'
# send two bytes and receive answer # send two bytes and receive answer
spi.cs_low() spi.cs = True
data = spi.transfer( [0x82, 0x00]) data = spi.transfer( [0x82, 0x00])
spi.cs_high() spi.cs = False
print(ord(data[2])) print(ord(data[2]))
spi.reset()
spi.port.close()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -23,7 +23,7 @@ You should have received a copy of the GNU General Public License ...@@ -23,7 +23,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 BBIO from .BBIO_base import BBIO_base, BPError, ProtocolError
FOSC = (32000000 / 2) FOSC = (32000000 / 2)
...@@ -48,9 +48,58 @@ class UARTSpeed: ...@@ -48,9 +48,58 @@ class UARTSpeed:
_115200 = 0b1001 _115200 = 0b1001
class UART(BBIO): class UART(BBIO_base):
def enter(self):
"""
Raises
------
BPError
Could not enter SPI mode
"""
if self.mode == 'uart':
return
if self.mode != 'bb':
super().enter()
self.write(0x03)
self.timeout(self.minDelay * 10)
if self.response(4) == "ART1":
self.mode = 'uart'
self.bp_port = 0b00 # two bit port
self.bp_config = 0b0000
self.recurse_end()
return 1
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): def manual_speed_cfg(self, baud):
self.check_mode('uart') """ Manual baud rate configuration, send 2 bytes
Configures the UART using custom baud rate generator settings. This command is followed by two data bytes that
represent the BRG register value. Send the high 8 bits first, then the low 8 bits.
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.
"""
if self.mode == 'uart':
raise BPError('Not in UART mode')
BRG = ((FOSC) / (4 * baud)) - 1 BRG = ((FOSC) / (4 * baud)) - 1
BRGH = ((BRG >> 8) & 0xFF) BRGH = ((BRG >> 8) & 0xFF)
BRGL = (BRG & 0xFF) BRGL = (BRG & 0xFF)
...@@ -61,27 +110,36 @@ class UART(BBIO): ...@@ -61,27 +110,36 @@ class UART(BBIO):
return self.response() return self.response()
def begin_input(self): def begin_input(self):
self.check_mode('uart') if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0x04)) self.port.write(chr(0x04))
def end_input(self): def end_input(self):
self.check_mode('uart') if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0x05)) self.port.write(chr(0x05))
def enter_bridge_mode(self): def enter_bridge_mode(self):
self.check_mode('uart') """ UART bridge mode (reset to exit)
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.port.write(chr(0x0f))
self.timeout(0.1) self.timeout(0.1)
return self.response(1, True) self.response(1, True)
def set_cfg(self, cfg): def set_cfg(self, cfg):
self.check_mode('uart') if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0xC0 | cfg)) self.port.write(chr(0xC0 | cfg))
self.timeout(0.1) self.timeout(0.1)
return self.response(1, True) return self.response(1, True)
def read_cfg(self): def read_cfg(self):
self.check_mode('uart') if self.mode == 'uart':
raise BPError('Not in UART mode')
self.port.write(chr(0xd0)) self.port.write(chr(0xd0))
self.timeout(0.1) self.timeout(0.1)
return self.response(1, True) return self.response(1, True)
...@@ -7,6 +7,9 @@ Copyright 2009 Sean Nelson <audiohacked@gmail.com> ...@@ -7,6 +7,9 @@ Copyright 2009 Sean Nelson <audiohacked@gmail.com>
Overhauled and edited by Garrett Berg on 2011- 1 - 22 Overhauled and edited by Garrett Berg on 2011- 1 - 22
Copyright 2011 Garrett Berg <cloudform511@gmail.com> Copyright 2011 Garrett Berg <cloudform511@gmail.com>
Updated and made Python3 compatible by Juergen Hasch, 20160501
Copyright 2016 Juergen Hasch <python@elbonia.de>
This file is part of pyBusPirate. This file is part of pyBusPirate.
pyBusPirate is free software: you can redistribute it and/or modify pyBusPirate is free software: you can redistribute it and/or modify
......
...@@ -22,23 +22,42 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>. ...@@ -22,23 +22,42 @@ along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
from . import I2C from . import I2C
import sys
def init_i2c(bp_device, power=True, pullups=True, speed=I2C.I2CSpeed._50KHZ):
def init_i2c(bp_device, power='on', pullups='on', speed=I2C.I2CSpeed._50KHZ): """initializes i2c mode with some common settings hardwired
"""initializes i2c mode with some common settings hardwired"""
Parameters
---------
bp_device
Bus pirate device
power : bool
Turn on power
pullups : bool
Turn on pullups
speed: IC2.I2CSpeed
Transfer speed
Returns
-------
bool
True if device could be initialized
"""
if not bp_device.enter_I2C(): if not bp_device.enter_I2C():
return None return False
if not bp_device.configure_peripherals(power, pullups): if not bp_device.configure_peripherals(power, pullups):
return None return False
if not bp_device.set_speed(speed): if not bp_device.set_speed(speed):
return None return False
bp_device.timeout(0.1) bp_device.timeout(0.1)
return 1 return True
def init_bb(bp_device, power='on', pullups='on'): def init_bb(bp_device, power='on', pullups='on'):
"""initializes bit bang mode with the most common settings""" """initializes bit bang mode with the most common settings
Parameters
"""
if not bp_device.enter_bb(): if not bp_device.enter_bb():
return None return None
if not bp_device.configure_peripherals(power, pullups): if not bp_device.configure_peripherals(power, pullups):
......
...@@ -40,6 +40,18 @@ from .BitBang import BBIO ...@@ -40,6 +40,18 @@ from .BitBang import BBIO
class OneWire(BBIO): class OneWire(BBIO):
def enter_1wire(self):
self.check_mode('bb')
self.write(0x04)
self.timeout(self.minDelay * 10)
if self.response(4) == "1W01":
self.mode = '1wire'
self.bp_port = 0b00 # two bit port
self.bp_config = 0b0000
self._attempts_ = 1
return 1
return self.recurse_flush(self.enter_1wire)
def reset(self): def reset(self):
self.check_mode('1wire') self.check_mode('1wire')
self.port.write(chr(0x02)) self.port.write(chr(0x02))
......
...@@ -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 BBIO from .BitBang import BitBang
class RawWireCfg: class RawWireCfg:
...@@ -31,7 +31,27 @@ class RawWireCfg: ...@@ -31,7 +31,27 @@ class RawWireCfg:
OUTPUT = 0x08 OUTPUT = 0x08
class RawWire(BBIO): class RawWire(BBIO_base):
def enter(self):
"""Enter raw wire mode
Raw-wire binary mode provides access to the Bus Pirate's raw 2- and 3-wire libraries.
This new mode will make it easier to script operations on arbitrary serial protocols
used by devices like smart cards, shift registers, etc.
"""
if self.mode == 'raw':
return
self.reset()
self.write(0x05)
self.timeout(self.minDelay * 10)
if self.response(4) == "RAW1":
self.mode = 'raw'
self.bp_port = 0b00 # two bit port
self.bp_config = 0b0000
self.recurse_end()
return 1
return self.recurse_flush(self.enter_rawwire)
def start_bit(self): def start_bit(self):
"""is kept in because it was in for legacy code, """is kept in because it was in for legacy code,
I recommend you use send_start_bit""" I recommend you use send_start_bit"""
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
def testpin(bp_device, *pins): def testpin(bp_device, *pins):
#pin dir testing #pin dir testing
print 'config', bp_device.configure_peripherals(power = 'ON', pullups = 'on') print('config', bp_device.configure_peripherals(power = True, pullups = True))
bp_device.set_port(0) bp_device.set_port(0)
bp_device.set_dir(0) bp_device.set_dir(0)
...@@ -15,12 +15,12 @@ def testpin(bp_device, *pins): ...@@ -15,12 +15,12 @@ def testpin(bp_device, *pins):
bp_device.set_pin_dir(pin, 'out') bp_device.set_pin_dir(pin, 'out')
for n in range(5): #do full loop 5 times for n in range(5): #do full loop 5 times
raw_input('changing %s to 1, press enter' % pin) raw_input('changing %s to 1, press enter' % pin)
print 'error checking:', bp_device.set_pin(pin) print('error checking:', bp_device.set_pin(pin))
print 'adc currently reads', bp_device.get_adc_voltage(), 'volts' print('adc currently reads', bp_device.get_adc_voltage(), 'volts')
raw_input('now setting output to 0, press enter') raw_input('now setting output to 0, press enter')
print 'error checking:', bp_device.clear_pin(pin) print('error checking:', bp_device.clear_pin(pin))
print 'adc currently reads', bp_device.get_adc_voltage(), 'volts' print('adc currently reads', bp_device.get_adc_voltage(), 'volts')
raw_input('changing dir to input. press enter for next pin') raw_input('changing dir to input. press enter for next pin')
bp_device.set_pin_dir(pin, 'in') bp_device.set_pin_dir(pin, 'in')
...@@ -31,7 +31,7 @@ def pin_dir_test(bp_device, *pins): ...@@ -31,7 +31,7 @@ def pin_dir_test(bp_device, *pins):
whenever you change the directions (and reseting to output no less). Put whenever you change the directions (and reseting to output no less). Put
a 5V pullup resistor on the pin and run this to see the effect''' a 5V pullup resistor on the pin and run this to see the effect'''
#pin dir testing #pin dir testing
print 'config', bp_device.configure_peripherals(power = 'ON', pullups = 'on') print 'config', bp_device.configure_peripherals(power = True, pullups = True)
bp_device.set_port(0) bp_device.set_port(0)
bp_device.set_dir(0x1F) bp_device.set_dir(0x1F)
......
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