Sunday, January 6, 2013

Bus Pirate - a handy tool for inventors

Last year I discovered an interesting hardware tool - the Bus Pirate. The thingie looked quite promising, so I couldn't resist and ordered it (I got it from Seeed Studio, as they ship worldwide for a very low cost).

When I received the Bus Pirate, I did the usual quick checks - plugged into the nearest machine, installed drivers and played for a minute with the integrated shell, but nothing more. The Pirate collected dust on my shelf for a couple of months. Then it came the moment when I run out of USB-serial converters (I typically use UM232H as they are not too expensive and works well with both Linux & Windows). As you can guess, it's impossible to order new hardware during the holidays, so I had to dig deep in my drawers to check what's there to save my day. I found one FT2232H (dual channel USB-to-anything on steroids!) and the Bus Pirate. I've knew that the FT2232H will work for me, and I was more curious to put the Bus Pirate in action, so I plugged it in.

What I needed was very simple - a USB-serial board. The Bus Pirate can be configured to work like a USB-serial "bridge" (that's how they call it in the docs). It's not a rocket science to do this, actually quite the opposite. Connect the Bus Pirate to your box, install the device drivers if needed and open your favorite terminal emulator (puTTY, screen), select the proper serial port and do the actual configuration. Press "?" to print the integrated shell help:
HiZ>?
General                                 Protocol interaction
---------------------------------------------------------------------------
?       This help                       (0)     List current macros
=X/|X   Converts X/reverse X            (x)     Macro x
~       Selftest                        [       Start
#       Reset                           ]       Stop
$       Jump to bootloader              {       Start with read
&/%     Delay 1 us/ms                   }       Stop
a/A/@   AUXPIN (low/HI/READ)            "abc"   Send string
b       Set baudrate                    123
c/C     AUX assignment (aux/CS)         0x123
d/D     Measure ADC (once/CONT.)        0b110   Send value
f       Measure frequency               r       Read
g/S     Generate PWM/Servo              /       CLK hi
h       Commandhistory                  \       CLK lo
i       Versioninfo/statusinfo          ^       CLK tick
l/L     Bitorder (msb/LSB)              -       DAT hi
m       Change mode                     _       DAT lo
o       Set output type                 .       DAT read
p/P     Pullup resistors (off/ON)       !       Bit read
s       Script engine                   :       Repeat e.g. r:10
v       Show volts/states               .       Bits to read/write e.g. 0x55.2
w/W     PSU (off/ON)            <x>/<x= >/<0>   Usermacro x/assign x/list all

Press "m" to select the Bus Pirate mode of operation and select "3" for UART mode:
HiZ>m
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
9. DIO
x. exit(without change)
(1)>3

Select your sample rate (I used 115200):
Set serial port speed: (bps)
 1. 300
 2. 1200
 3. 2400
 4. 4800
 5. 9600
 6. 19200
 7. 38400
 8. 57600
 9. 115200
10. BRG raw value
(1)>9

Select your data bits/parity settings, I usually just hit ENTER as defaults are OK for my case (8N1):
Data bits and parity:
 1. 8, NONE *default
 2. 8, EVEN
 3. 8, ODD
 4. 9, NONE
(1)>

...same here:
Stop bits:
 1. 1 *default
 2. 2
(1)>

...and here:
Receive polarity:
 1. Idle 1 *default
 2. Idle 0
(1)>

Select your UART output type - usually you'll need "normal", but YMMV:
Select output type:
 1. Open drain (H=Hi-Z, L=GND)
 2. Normal (H=3.3V, L=GND)

(1)>2
Ready

You need 2 more steps - list currently available UART macros (optional) and activate the "UART bridge" macro (needed):
UART>(0)
 0.Macro menu
 1.Transparent bridge
 2. Live monitor
 3.Bridge with flow control
UART>(1)
UART bridge
Reset to exit
Are you sure? y

From this moment on, the Bus Pirate will act move data across the the line as if it was regular USB-serial convertor (and it's about the same price as the FTDI modules). Keep in your mind that the only way to get out the Bus Pirate of this mode is to power-cycle the device (unplug/plug the USB connector), as there's no RESET button on the PCB.

Enjoy playing with this toy!

P.S.: I knew that FTDI made various USB-to-something chips and I even had some of them, but the reason why exactly the UM232H came to my attention is that one of my colleagues asked me for a favor to debug his ATmega <-> USB JTAG setup, and it was based on this chip. The JTAG worked very well in the end and I was pleasantly surprised. Thanks Hristo for the idea!

P.S.2: With Linux the FTDI chip can work in 2 different configurations - using ftdi_sio to act as regular usb-serial port, and with libftdi to act as multi-purpose USB interface (I2C/SPI/bit-bang/etc). Also I usually use MPSSE for lazy SPI access. Something cool to share - libftdi & libmpsse compile and work fine with embedded ARM Linux.