Samstag, 6. Februar 2010

Some thoughts on startiing hacking on the AVR Butterfly

Connecting the Butterfly
Without knowing much about the Butterfly, it was difficult to get things working at first. The Butterfly comes with pre-installed firmware which shows off the features. There are options like entering and displaying your name on the LED screen (the Butterfly actually comes with a  safety pin so you can use it as a name label on your shirt), displaying temperature (I think) and changing and displaying the date.

I figured it would be best to make an initial back up copy of the original Butterfly firmware. This wasn't particularly easy. I used a Fritzl ASP initially (modified by jw to handle shifting the TTL +5v +0v to a quasi RS232 +12v -12v). With avrdude I was able to retrieve and save the original firmware. The command and options were:

avrdude -p atmega169 -b 19200 -c butterfly -P /dev/ttyUSB0 -U flash:r:flash.hex:i

But in order to be able to actually talk to the Butterfly, you need to get it into a state where the bootloader is active (i.e. that the Butterfly doesn't just jump into the firmware's main()). The trick here is to reset the Butterfly (short the two contacts on the extreme bottom right of the Butterfly as you look at it from the front) and then immediately press the joystick down (take care not to waggle it here - otherwise it won't work) and within a couple of seconds enter the avrdude command above in the console. avrdude should tell you that it connected successfully to the programmer and was able to save the flash.hex file (you may need to touch the flash.hex file beforehand).

Things to watch out for
When connecting using a serial cable over the Butterfly UART pins, I followed the diagram in the Butterfly User's Manual. You need to remember that the serial connector shown in that diagram is the 'male' connector - the one with the pins. If you (like me) have the other connector - the 'female' side, then you are going to have to 'mirror' the diagram. You would therefore have the GND at the extreme left of the upper row (where the upper row has 5 holes) and you would have to find out accordingly where the RX and TX holes are.

Another thing to look out for when connecting the serial connector to the UART pins is that some serial cables are 'crossover' cables, designed to connect PC to PC. If this is the case, you will have to swap RX and TX when connecting to UART. If nothing is working, try changing the connections around. That's how I got it working (after about two hours wondering why nothing was happening).


A quick first firmware for the Butterfly

1  #define F_CPU 1000000UL // how fast is CPU?
2  #include
3  #include
4  int main()
5  {    DDRB |=(1<
6  for(;;)
7    {
8      PORTB &= ~(1<
9      _delay_ms(200.0);
10      PORTB |= (1<
11     _delay_ms(200.0);
12    }
13    return 0;
14 }