Sunday, January 9, 2011

Bus Ninja!

Why have I not heard of this before? Bus Ninja is a clone of the Bus Pirate that runs on Arduino and Teensy!  If this works, it should let me snoop on the SPI traffic on my Davis VP2 console while I wait for my Bus Pirate to arrive.

After letting the author know that the download link to his software wasn't working, I dove in.  The homepage for this project lets you know that there is some makefile tweaking involved.  First thing I had to sort out was that I have a Boarduino equipped with an Atmega 328P and not an Arduino with an Atmega 168.  The file has a line with


but I figured that would just affect the compilation and I'd be OK to leave this as is. The difference between the two is just memory size: the pinouts are the same. So then I did

make BOARD=ARDUINO clean

which went fine, but


died with

cc1: error: invalid parameter 'inline-call-cost'

Hmmmm. Looks like the version of avr-gcc in arch linux doesn't have this parameter so I had to comment that CFLAG out of Makefile.  Everything compiled nicely after that.

Next, I didn't want to load up the Arduino IDE, preferring instead to program the chip from the command line.  I have a Sparkfun FTDI breakout board, so I needed to get Linux talking to this thing.  Let's plug it in and see if Linux sees it?

# dmesg
usb 3-1: new full speed USB device using uhci_hcd and address 2
usbcore: registered new interface driver usbserial
USB Serial support registered for generic
usbcore: registered new interface driver usbserial_generic
usbserial: USB Serial Driver core
USB Serial support registered for FTDI USB Serial Device
ftdi_sio 3-1:1.0: FTDI USB Serial Device converter detected
usb 3-1: Detected FT232RL
usb 3-1: Number of endpoints 2
usb 3-1: Endpoint 1 MaxPacketSize 64
usb 3-1: Endpoint 2 MaxPacketSize 64
usb 3-1: Setting MaxPacketSize 64
usb 3-1: FTDI USB Serial Device converter now attached to ttyUSB0
usbcore: registered new interface driver ftdi_sio
ftdi_sio: v1.6.0:USB FTDI Serial Converters Driver

So far, so good. But I went down many blind alleys after this. First, because I am using this FTDI serial converter, I thought I needed to install the Arch Linux libftdi package. Turns out I don't. Second, a lot of stuff I read seemed to indicate that if I wanted to do this from the command line, I needed a patched version of avrdude (see here).  Turns out I don't.  Basically, with the Sparkfun FTDI board, you can tell avrdude that you are using the stk500v1 programmer.  But, the Sparkfun board adds some special sauce: it brings out DTR on Pin 6 of the header to reset the board.  We need to tell this to avrdude.  So the config file for the stk500v1 programmer ends up looking like this in /etc/avrdude.conf (note the last line)

  id = "stk500v1";
  desc = "Atmel STK500 Version 1.x firmware";
  type = stk500;
  reset = 6;

Now let's see if this works. As root, do

# avrdude -C /etc/avrdude.conf -c stk500v1 -p m328p -P /dev/ttyUSB0 -U hfuse:r:-:h -b 57600

This harmless command tells avrdude to read the setting of the hfuse on the micro. Note some very important things here:
  • I am using -p m328p here because my Boarduino has an Atmega 328P rather than the 168 in many Arduinos.
  • I am using -P /dev/ttyUSB0 because that is what dmesg told me was being used.
  • I am using -b 57600 to lower the baud rate from the default.  I WASTED SEVERAL FRUSTRATING HOURS ON THIS.  Go too fast and avrdude comes back with the very unhelpful 'avrdude: stk500_recv(): programmer is not responding'. Your mileage wil vary depending on the speed of your processor.  If you get this dreaded message, try slowing things down even further.
So at this point, I've got avrdude talking to my boarduino.  I can now see if I can load the Bus Ninja code.  This is going to take a tweak of the file based on what was figured out above. Edit this file in the src directory so that the PROGRAM_CMD line reads:

PROGRAM_CMD=avrdude -C /etc/avrdude.conf -pm328p -cstk500v1 -P/dev/ttyUSB0 -b57600 -D -Uflash:w:$(TARGET).hex

I've taken the spaces out between the command line arguments as avrdude doesn't seem to care either way.  Now, all it takes as root is

# make BOARD=ARDUINO program

to load the Bus Ninja code into the Arduino.  YES!  Stay tuned for next time when I'll try messing with the Bus Ninja program itself.


  1. Unfortunately, I haven't implemented a SPI sniffer in Bus Ninja, just SPI control.

  2. D'oh! No matter. I learned a lot doing this, and it might help someone else.