Bus Pirate Scripting in Python

From DP

Jump to: navigation , search

Getting Started

On Windows 7, users have reported success with the following:

On Ubuntu, installing the python package seems to provide everything necessary

Trying it out

To make sure you have everything working, hook up your Bus Pirate, and try the following:

ActivePython (ActiveState Software Inc.) based on
Python 2.7.1 (r271:86832, Feb  7 2011, 11:30:38) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information
>>> # import the I2C high-level abstraction package, to test
>>> from pyBusPirateLite.I2Chigh import *
>>> # create an instance of it, using the COM3 port and the Bus Pirate speed of 115200 baud, with a timeout of 5s
>>> i2c = I2Chigh("COM3", 115200, 5)
>>> # enter Bit Bang mode
>>> i2c.BBmode()
>>> # enter I2C mode
>>> i2c.enter_I2C()
>>> # turn on the power supply and enable the pullups
>>> i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS)
>>> # read the byte at register address "0x00" on an I2C device with address "0x68
>>> # this happens to be the "seconds" register of the DS3231 real-time clock (used in the popular ChronoDot)
>>> i2c.get_byte(0x68, 0x00)
>>> # define a function to report the seconds:
>>> def get_seconds():
...   return i2c.get_byte(0x68, 0x00)
>>> get_seconds()

Writing a Script

Using the Python console is a great way to try out and debug your script. As you try out commands and get the feel for how to use the device, you can define functions to take care of them for you, and then save your script.

Look in the pyBusPirate library for examples, there are a number of user-contributed scripts dealing with I2C, SPI, and the other interfaces the Bus Pirate supports.