Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix base class and add unimplemented raw wire commands #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyBusPirateLite/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def adc_value(self):
def set_power_on(self, val):
self.write(0x80 | (self.PIN_POWER if val else 0))
self.response(1, binary=True)

power_on = property(None, set_power_on, doc="""
Enable or disable the built-in power supplies. Note that the power
supplies reset every time you change modes.
Expand Down Expand Up @@ -263,7 +264,7 @@ def timeout(self, timeout = 0.1):

def write(self, value):
self.port.write(value.to_bytes(1, 'big'))

def response(self, byte_count=1, binary=False):
"""Request a number of bytes

Expand Down
71 changes: 59 additions & 12 deletions pyBusPirateLite/rawwire.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@
# You should have received a copy of the GNU General Public License
# along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.

# See http://dangerousprototypes.com/blog/2009/10/27/binary-raw-wire-mode/

from .base import BusPirate


def bchar(char):
return bytearray([char])


class RawWireCfg:
NA = 0x01
LSB = 0x02
Expand Down Expand Up @@ -79,61 +85,102 @@ def enter(self):
def start_bit(self):
"""is kept in because it was in for legacy code,
I recommend you use send_start_bit"""
self.port.write(chr(0x02))
self.port.write(bchar(0x02))
self.timeout(0.1)
return self.response(1)

def stop_bit(self):
"""is kept in because it was in for legacy code,
I recommend you use send_stop_bit"""
self.port.write(chr(0x03))
self.port.write(bchar(0x03))
self.timeout(0.1)
return self.response(1)

def read_bit(self):
self.port.write(chr(0x07))
def cs_low(self):
self.port.write(bchar(0x04))
self.timeout(0.1)
return self.response(1)

def cs_high(self):
self.port.write(bchar(0x05))
self.timeout(0.1)
return self.response(1)

def read_byte(self):
self.port.write(bchar(0x06))
self.timeout(0.1)
return self.response(1, True)

def read_bit(self):
self.port.write(bchar(0x07))
self.timeout(0.1)
return self.response(1, False)

def peek(self):
self.port.write(chr(0x08))
self.port.write(bchar(0x08))
self.timeout(0.1)
return self.response(1)

def clock_tick(self):
self.port.write(chr(0x09))
self.port.write(bchar(0x09))
self.timeout(0.1)
return self.response(1)

def clock_low(self):
self.port.write(chr(0x0a))
self.port.write(bchar(0x0a))
self.timeout(0.1)
return self.response(1)

def clock_high(self):
self.port.write(chr(0x0b))
self.port.write(bchar(0x0b))
self.timeout(0.1)
return self.response(1)

def data_low(self):
self.port.write(chr(0x0c))
self.port.write(bchar(0x0c))
self.timeout(0.1)
return self.response(1)

def data_high(self):
self.port.write(chr(0x0d))
self.port.write(bchar(0x0d))
self.timeout(0.1)
return self.response(1)

# NOTE: Untested. Not clear if it returns 0x01 for each bit or byte,
# it looks like there are some typos in the documentation.
def bulk_bits(self, byte, n_bits):
if n_bits < 1 or n_bits > 8:
raise ValueError('Can only send 1 through 8 bits')
if type(byte) is str:
byte = ord(byte[0])
byte_val = byte >> (8 - n_bits)
self.port.write(bchar(0b00110000 | (n_bits - 1)))
self.port.write(bchar(byte_val))
self.timeout(0.1)
return self.response(1)

def wire_cfg(self, pins = 0):
self.port.write(chr(0x80 | pins))
"""1000wxyz – Config, w=HiZ/3.3v, x=2/3wire, y=msb/lsb, z=not used"""
self.port.write(bchar(0x80 | pins))
self.timeout(0.1)
return self.response(1)

def peripherals_cfg(self, flags=0):
"""0100wxyz – Configure peripherals w=power, x=pullups, y=AUX, z=CS"""
self.port.write(bchar(0b01000000 | flags))
self.timeout(0.1)
return self.response(1)

def speed_cfg(self, speed=0):
"""011000xx – Set speed, 3=~400kHz, 2=~100kHz, 1=~50kHz, 0=~5kHz"""
self.port.write(bchar(0b1100000 | speed))
self.timeout(0.1)
return self.response(1)

# 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):
self.port.write(chr(0x20 | (ticks - 1)))
self.port.write(bchar(0x20 | (ticks - 1)))
self.timeout(0.1)
return self.response(1)