pyBusPirateLite package

Submodules

pyBusPirateLite.BitBang module

class pyBusPirateLite.BitBang.BitBang(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

Provide access to the Bus Pirate bitbang mode

adc

Returns the voltage from ADC pin

Returns:
float

Voltage measured at ADC pin

disable_PWM(self)[source]

Clear/disable PWM

enable_PWM(self, frequency, dutycycle=0.5)[source]

Enable PWM output

Parameters:
frequency: float

PWM frequency in Hz

dutycycle: float

Duty cycle between 0 (0%) and 1 (100%)

Notes

Configure and enable pulse-width modulation output in the AUX pin. Requires a 5 byte configuration sequence. Responds 0x01 after a complete sequence is received. The PWM remains active after leaving binary bitbang mode! Equations to calculate the PWM frequency and period are in the PIC24F output compare manual. Bit 0 and 1 of the first configuration byte set the prescaler value. The Next two bytes set the duty cycle register, high 8bits first. The final two bytes set the period register, high 8bits first.

Parameter calculation stolen from http://codepad.org/qtYpZmIF

get_next_adc_voltage(self)[source]
outputs
Returns:
byte

Current state of the pins PIN_AUX, PIN_MOSI, PIN_CLK, PIN_MISO, PIN_CS

pins

Get pins status

Returns:
byte

Current state of the pins PIN_POWER, PIN_PULLUP, PIN_AUX, PIN_MOSI, PIN_CLK, PIN_MISO, PIN_CS

selftest(self, complete=False)[source]

Self test

Parameters:
complete: bool

Requires jumpers between +5 and Vpu, +3.3 and ADC

Returns:
int

Number of errors

Notes

Self-tests are access from the binary bitbang mode. There are actually two self-tests available. T he full test is the same as self-test in the user terminal, it requires jumpers between two sets of pins in order to test some features. The short test eliminates the six checks that require jumpers.

After the test is complete, the Bus Pirate responds with the number of errors. It also echoes any input plus the number of errors. The MODE LED blinks if the test was successful, or remains solid if there were errors. Exit the self-test by sending 0xff, the Bus Pirate will respond 0x01 and return to binary bitbang mode.

start_getting_adc_voltages(self)[source]

Start continuously getting adc voltages.

Notes

use memberfunction enter_bb to exit, use get_next_adc_voltage to get the next one.

stop_getting_adc_voltages(self)[source]

I was encountering problems resetting out of adc mode, so I wrote this little function

pyBusPirateLite.I2C module

class pyBusPirateLite.I2C.I2C(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

Provide access to the Bus Pirate I2C interface

SPEEDS = {'100kHz': 2, '400kHz': 3, '50kHz': 1, '5kHz': 0}
ack(self)[source]

Send ACK

Send an I2C ACK bit after reading a byte. Tells a slave device that you will read another byte.

Raises:
ProtocolError

Did not get expected response

aux(self, cmd)[source]

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
check_i2c

Test if we are still in I2C mode

Returns:
True if in I2C mode
Raises:
BPError if not in I2C mode
configure(self, power=False, pullup=False, aux=False, cs=False)[source]

Configure peripherals w=power, x=pullups, y=AUX, z=CS

Enable (1) and disable (0) Bus Pirate peripherals and pins. Bit w enables the power supplies, bit x toggles the on-board pull-up resistors, y sets the state of the auxiliary pin, and z sets the chip select pin. Features not present in a specific hardware version are ignored. Bus Pirate responds 0×01 on success.

enter(self)[source]

Enter I2C mode

Once in binary I2C mode, send 0×01 to get the current mode version string. The Bus Pirate responds ‘I2Cx’, where x is the raw I2C protocol version (currently 1). Get the version string at any time by sending 0×01 again. This command is the same in all binary modes, the current mode can always be determined by sending 0x01.

Raises:
BPError

If I2C mode could not be entered

nack(self)[source]

Send NACK

Send an I2C NACK bit after reading a byte. Tells a slave device that you will stop reading, next bit should be an I2C stop bit.

Raises:
ProtocolError

Did not get expected response

pin_mapping = {'AUX': 2, 'CS': 1}
sniffer(self)[source]

Sniff traffic on an I2C bus.

[/] - Start/stop bit - escape character precedes a data byte value +/- - ACK/NACK Sniffed traffic is encoded according to the table above. Data bytes are escaped with the ‘’ character. Send a single byte to exit, Bus Pirate responds 0x01 on exit.

speed

Return current I2C clock speed

Returns:
str

I2C clock speed (5kHz, 50kHz, 100kHz, 400kHz)

start(self)[source]

Send an I2C start bit

Raises:
ProtocolError

Did not get expected response

stop(self)[source]

Send an I2C stop bit

Raises:
ProtocolError

Did not get expected response

transfer(self, txdata)[source]

Bulk I2C write, send 1-16 bytes

Bulk I2C allows multi-byte writes. The Bus Pirate expects xxxx+1 data bytes. Up to 16 data bytes can be sent at once. Note that 0000 indicates 1 byte because there’s no reason to send 0.

BP replies 0×01 to the bulk I2C command. After each data byte the Bus Pirate returns the ACK (0x00) or NACK (0x01) bit from the slave device.

Parameters:
txdata: List of bytes

Data to send (1-16 bytes)

Returns:
status: List of bools

Returns True(ACK) or False(NACK) for each transmitted byte

Raises:
ValueError

If more than 16 bytes are requested to be sent

write_then_read(self, numtx, numrx, txdata)[source]

Write then read

This command internally sends I2C start, writes from 0-4096 bytes, then reads 0-4096 bytes into the Bus Pirates internal buffer, ACKing each byte internally until the final byte at which point it sends an NACK stop bit.

All data for this command can be sent at once, and it will be buffered in the Bus Pirate. The write and read operations happen once the completed command has been passed to the Bus Pirate. Any write data is internally buffered by the Bus Pirate. At the end of the operation, any read data is returned from the buffer, be aware that the write buffer is re-used as the read buffer, as such any write data needs to be re-loaded if the command is re-executed.

pyBusPirateLite.I2Chigh module

class pyBusPirateLite.I2Chigh.I2Chigh(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.I2C.I2C

High level I2C transactions, not included in uc class

command(self, i2caddr, cmd)[source]

Writes one byte command to slave

get_byte(self, i2caddr, addr)[source]

Read one byte from address addr

get_word(self, i2caddr, addr)[source]

Reads two byte value (big-endian) from address addr

set_byte(self, i2caddr, addr, value)[source]

Write one byte to address addr

set_word(self, i2caddr, addr, value)[source]

Writes two byte value (big-endian) to address addr

pyBusPirateLite.SPI module

class pyBusPirateLite.SPI.SPI(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

CFG_CLK_EDGE = 2
CFG_IDLE = 4
CFG_PUSH_PULL = 8
CFG_SAMPLE = 1
PIN_AUX = 2
PIN_CS = 1
PIN_POWER = 8
PIN_PULLUP = 4
SPEEDS = {'125kHz': 1, '1MHz': 3, '2.6MHz': 5, '250kHz': 2, '2MHz': 4, '30kHz': 0, '4MHz': 6, '8MHz': 7}
config
cs

Return chip select pin status

enter(self)[source]

Enter raw SPI mode

Once in raw bitbang mode, send 0x01 to enter raw SPI mode. The Bus Pirate responds ‘SPIx’, where x is the raw SPI protocol version (currently 1). Get the version string at any time by sending 0x01 again.

Raises:
BPError

Could not enter SPI mode

modestring

Return mode version string

pins
sniffer(self, cs)[source]

Sniff SPI traffic when CS low(10)/all(01) TODO

Parameters:
cs : Bool

True: Capture when CS is low False: Capture all

Notes

0000 11XX The SPI sniffer is implemented in hardware and should work up to 10MHz. It follows the configuration settings you entered for SPI mode. The sniffer can read all traffic, or filter by the state of the CS pin.

[/] - CS enable/disable xy - escape character () precedes two byte values X (MOSI pin) and Y (MISO pin) (updated in v5.1)

Sniffed traffic is encoded according to the table above. The two data bytes are escaped with the ‘’ character to help locate data in the stream.

Send the SPI sniffer command to start the sniffer, the Bus Pirate responds 0x01 then sniffed data starts to flow. Send any byte to exit. Bus Pirate responds 0x01 on exit. (0x01 reply location was changed in v5.8)

If the sniffer can’t keep with the SPI data, the MODE LED turns off and the sniff is aborted. (new in v5.1)

The sniffer follows the output clock edge and output polarity settings of the SPI mode, but not the input sample phase.

More detailed notes on the SPI sniffer in the SPI user terminal documentation.

speed
transfer(self, txdata)[source]

Bulk SPI transfer, send/read 1-16 bytes

Bulk SPI allows direct byte reads and writes. The Bus Pirate expects xxxx+1 data bytes. Up to 16 data bytes can be sent at once, each returns a byte read from the SPI bus during the write.

Note that 0000 indicates 1 byte because there’s no reason to send 0. BP replies 0x01 to the bulk SPI command, and returns the value read from SPI after each data byte write.

The way it goes together:

The upper 4 bit of the command byte are the bulk read command (0001xxxx) xxxx = the number of bytes to read. 0000=1, 0001=2, etc, up to 1111=16 If we want to read (0001) four bytes (0011=3=read 4) the full command is 00010011 (0001 + 0011 ). Convert from binary to hex and it is 0x13

Parameters:
txdata: List of bytes

Data to send (1-16 bytes)

Returns:
List containing received data
Raises:
ValueError

If more than 16 bytes are requested to be sent

write_then_read(self, numtx, numrx, txdata, cs=True)[source]

Write then read

This command was developed to help speed ROM programming with Flashrom. It might be helpful for a lot of common SPI operations. It enables chip select, writes 0-4096 bytes, reads 0-4096 bytes, then disables chip select.

All data for this command can be sent at once, and it will be buffered in the Bus Pirate. The write and read operations happen all at once, and the read data is buffered. At the end of the operation, the read data is returned from the buffer. The goal is to meet the stringent timing requirements of some ROM chips by buffering everything instead of letting the serial port delay things.

Write then read command format: +—————|--------------------------------|——————————-|-----------------------------+ |command (1byte)| number of write bytes (2bytes) | number of read bytes (2bytes) | bytes to write (0-4096bytes)| +—————|--------------------------------|——————————-|—————————–+

Return data format: +———————-|-----------------------------------+ | success/0x01 (1byte) | bytes read from SPI (0-4096bytes) | +----------------------|———————————–+

  1. First send the write then read command (00000100)
  2. The next two bytes (High8/Low8) set the number of bytes to write (0 to 4096)
  3. The next two bytes (h/l) set the number of bytes to read (0 to 4096)
  4. If the number of bytes to read or write are out of bounds, the Bus Pirate will return 0x00 now
  5. Now send the bytes to write. Bytes are buffered in the Bus Pirate, there is no acknowledgment that a byte is received.
  6. Now the Bus Pirate will write the bytes to SPI and read/return the requsted number of read bytes
  7. CS goes low, all write bytes are sent at once
  8. Read starts immediately, all bytes are put into a buffer at max SPI speed (no waiting for UART)
  9. At the end of the read, CS goes high
  10. The Bus Pirate now returns 0x01, success
  11. Finally, the buffered read bytes are returned via the serial port

Except as described above, there is no acknowledgment that a byte is received.

Parameters:
numtx : int

Number of bytes to write

numrx : int

Number of bytes to read

txdata : list

Data to send

cs : bool

Generate CS transitions (default=True)

Raises:
ProtocolError

If data could not be sent

pyBusPirateLite.UART module

class pyBusPirateLite.UART.UART(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

begin_input(self)[source]
echo
end_input(self)[source]
enter(self)[source]

Enter UART mode

Raises:
BPError

Could not enter UART mode

enter_bridge_mode(self)[source]

UART bridge mode (reset to exit)

Starts a transparent UART bridge using the current configuration. Unplug the Bus Pirate to exit.

manual_speed_cfg(self, baud)[source]

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.

modestring

Return mode version string

read_cfg(self)[source]
set_cfg(self, cfg)[source]
class pyBusPirateLite.UART.UARTCfg[source]

Bases: object

DATABITS = 12
OUTPUT_TYPE = 16
POLARITY = 1
STOPBITS = 2
class pyBusPirateLite.UART.UARTSpeed[source]

Bases: object

pyBusPirateLite.UC module

class pyBusPirateLite.UC.UC(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.BitBang.BitBang, pyBusPirateLite.I2C.I2C, pyBusPirateLite.onewire.OneWire, pyBusPirateLite.rawwire.RawWire, pyBusPirateLite.SPI.SPI, pyBusPirateLite.UART.UART

This class brings together all of the modules under a single class, allowing you to switch to other modules, do a function, and then switch back transparently. The class will keep track of where you are and raise an Error if you do something wrong.

The variables bp_port, bp_dir, and bp_config store the values that it sees in each respective mode, and the variable mode stores which mode the bus pirate is in.

IMPORTANT: Keep in mind that switching modes always makes the pins go to HiZ and the power supplies turn off. This can suck for certain applications (like if you are manualy wiggling a clock or reset signal), but you can design around it with external pullups and external inverters. YOU HAVE TO RECONFIGURE ALL SETTINGS WHENEVER YOU SWITCH MODES.

Note: current tested versions are only BBIO and I2C, but the other ones should work. Go to ________________.com and post any errors, problems or helpful revisions so that the code can be updated

pyBusPirateLite.base module

exception pyBusPirateLite.base.BPError[source]

Bases: OSError

class pyBusPirateLite.base.BusPirate(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: object

Base class for all modes. This contains low-level functions for direct hardware access.

Note: This class also contains some of the older functions that are now probably outdated

PIN_AUX = 16
PIN_CLK = 4
PIN_CS = 1
PIN_MISO = 2
PIN_MOSI = 8
PIN_POWER = 64
PIN_PULLUP = 32
adc_value

Read and return the voltage on the analog input pin.

connect(self, portname='', speed=115200, timeout=0.1)[source]

Will try to automatically find a port regardless of os

Parameters:
portname : str

Name of comport (e.g. /dev/ttyUSB0 or COM3)

speed : int

Communication speed, use default of 115200

timeout : int

Timeout in s to wait for reply

Raises:
ImportError

If helper function to find serial port is not available

IOError

If device could not be opened

disconnect(self)[source]

Disconnect bus pirate, close com port

enter(self)[source]

Enter bitbang mode. Will be overriden by other classes

enter_bb(self)[source]

Enter bitbang mode

This is the be-all-end-all restart function. It will keep trying to get the bus pirate into bit bang mode even if it is stuck. Call this to get the bus pirate into a known state (bb mode)

This command resets the Bus Pirate into raw bitbang mode from the user terminal. 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.

Raises:
IOError

If device is not connected

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.

get_port(self)[source]

Detect Buspirate and return first detected port

Returns:
str

First valid port name

hw_reset(self)[source]

Reset Bus Pirate

The Bus Pirate responds 0x01 and then performs a complete hardware reset. The hardware and firmware version is printed (same as the ‘i’ command in the terminal), and the Bus Pirate returns to the user terminal interface. Send 0x00 20 times to enter binary mode again.

power_on

Enable or disable the built-in power supplies. Note that the power supplies reset every time you change modes.

This is a read-only attribute due to API limitations of the buspirate firmware.

recurse(self, func, *args)[source]
recurse_end(self)[source]
recurse_flush(self, func, *args)[source]
response(self, byte_count=1, binary=False)[source]

Request a number of bytes

Parameters:
byte_count : int

Number of bytes to read

binary : bool

Return binary (True) or unicode values (False)

set_power_on(self, val)[source]
timeout(self, timeout=0.1)[source]
write(self, value)[source]
exception pyBusPirateLite.base.ProtocolError[source]

Bases: OSError

pyBusPirateLite.base.bulk_trans(self, byte_count=1, byte_string=None)[source]

this is how you send data in most of the communication modes. See the i2c example function in common_functions. Send the data, and read the returned array. In I2C: A ‘1’ means that it was NOT ACKNOWLEDGED, and a ‘0’ means that it WAS ACKNOWLEDGED (the reason for this is because this is what the bus pirate itself does…) In modes other than I2C I think it returns whatever data it gets while sending, but this feature is untested. PLEASE REPORT so that I can document it.

pyBusPirateLite.base.read_byte(self)[source]

Reads a byte from the bus, returns the byte. You must ACK or NACK each byte manually. NO ERROR CHECKING (obviously)

pyBusPirateLite.base.send_start_bit(self)[source]
pyBusPirateLite.base.send_stop_bit(self)[source]

pyBusPirateLite.common_functions module

pyBusPirateLite.common_functions.i2c_write_data(bp_device, data)[source]

send data, first byte should be address. NOTE: Address must include the write bit Created by Peter Huewe peterhuewe@gmx.de

pyBusPirateLite.common_functions.init_bb(bp_device, power='on', pullups='on')[source]

initializes bit bang mode with the most common settings

Parameters

pyBusPirateLite.common_functions.init_i2c(bp_device, power=True, pullups=True, speed=1)[source]

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

pyBusPirateLite.common_functions.sniff_i2c_devices(bp_device, power=False)[source]

pyBusPirateLite.onewire module

Binary1WIRE mode: 00000000 - reset to BBIO 00000001 - mode version string (1W01) 00000010 - 1wire reset 00000100 - read byte 00001000 - ROM search macro (0xf0) 00001001 - ALARM search macro (0xec) 0001xxxx - Bulk transfer, send 1-16 bytes (0=1byte!) 0100wxyz - Configure peripherals w=power, x=pullups, y=AUX, z=CS ( 0101wxyz - Read peripherals (planned, not implemented)

class pyBusPirateLite.onewire.OneWire(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

enter_1wire(self)[source]
reset(self)[source]

pyBusPirateLite.rawwire module

class pyBusPirateLite.rawwire.RawWire(portname='', speed=115200, timeout=0.1, connect=True)[source]

Bases: pyBusPirateLite.base.BusPirate

bulk_clock_ticks(self, ticks=1)[source]
clock_high(self)[source]
clock_low(self)[source]
clock_tick(self)[source]
data_high(self)[source]
data_low(self)[source]
enter(self)[source]

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.

peek(self)[source]
read_bit(self)[source]
start_bit(self)[source]

is kept in because it was in for legacy code, I recommend you use send_start_bit

stop_bit(self)[source]

is kept in because it was in for legacy code, I recommend you use send_stop_bit

wire_cfg(self, pins=0)[source]
class pyBusPirateLite.rawwire.RawWireCfg[source]

Bases: object

LSB = 2
NA = 1
OUTPUT = 8

Module contents