Thursday, December 4, 2008

Arduino on the AVR Butterfly

When I started my Itron meter reader project I had to get the AVR Butterfly up and running under AVR Studio. This was a little tedious but eventually I got it working. While I was doing that I thought that it would be useful to be able to run Arduino code on the Butterfly. I looked around a bit and found that there was a thread on the Arduino.cc forum about doing just this.

I went on with AVR Studio, but just the other day user Nick Lott posted some of the groundwork that sets up the Arduino IDE to work with the butterfly. After a bit of poking around with it to get the clock frequency and USART set up properly, it's working great. The AnalogInput sample even reads the light sensor and writes to the piezo speaker out of the box (the brighter the light, the faster it ticks).

I made the following changes.

Edit boards.txt to update to 8Mhz:
bfly.build.f_cpu=8000000L

This didn't seem to have any effect so I edited the butterfly\makefile to update to 8Mhz:
F_CPU = 8000000

Update 2009-Jan-30

I have written (or adapted code for) several classes to handle the Butterfly's hardware. The LCD, temp sensor, dataflash chip, and RTC clock all have easy-to-use interfaces now. The LCD class could be better, it needs some error checking and doesn't integrate with the Print class as nicely as it could, but it's quite functional.

The project is up on Google Code, feel free to check it out.

8 comments:

Comfortable and relaxed said...

Hi Dave,
Got an old Butterfly I thought I'd tinker with again.
Forgive my raw noobness, but is this code still around somewhere? Nothing in the download section...

Dave said...

You'll have to pull the code from the SVN repository, I didn't assemble it as a file. If you don't have SVN, try the TortoiseSVN package.

You'll need to use an older version of Arduino (probably 0023), as the project has not been updated for Arduino 1.0.

I started looking at updating it (there is a branch in the repo with a few changes related to this, but it isn't working right now), but it looks to me like it would probably be best to just start clean for Arduino 1.0. IIRC the biggest changes were in working with the clock.

Comfortable and relaxed said...

Thanks very much. Still on 22 anyway.

Comfortable and relaxed said...

Hi again Dave,

I'm sure you have better things to do than help me out. I'm pretty sure that I've got everything downloaded and put into the correct directories, but when I compine one of your example sketches, I get the following error:

"/butterduino-0022/hardware/tools/avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr5/crtm169.o:(.init9+0x0): undefined reference to `main'"

Can you easily tell what I have done wrong, or am I just wasting your time?

Please feel free to tell me to go away and get an education :)

Dave said...

Well, I can't give you a solid answer, but I have a vague notion that I've run into that before.

If I recall correctly, Arduino uses a main program file that it uses to call the routines defined in the sketch. Something about the changes you've got there may have broken that relationship such that it cannot find 'main'.

You should probably install a clean copy of Arduino 22 in a separate directory and look at the file it uses to define 'main', and then compare that to what you have in your butterino setup.

Sorry I can't give you a more direct answer than that, but as I recall the setup was a little weird to begin with, and it's been a few years since I looked at it. You may have to do some digging in the unmodifed Arduino 22 to work out how the Arduino compilation chain is supposed to work, then compare that to what happens in butterino.

Comfortable and relaxed said...

Thanks! Exactly what I needed.

...hardware\arduino\cores\butterfly\main.cpp was mis-named main.cxx and was missing #include

Examples now compile ok - just got to get it to upload.

Installed bootloader using AVR pocket programmer (USBtiny clone) and it seemed to work. That is, the original application is gone from the Butterfly, so I either installed a bootloader or fried it.

Now tinkering with various USB - UART converters to see if I can talk to the thing....

Dave said...

Excellent! Glad to hear it's working.

Is the cricket demo app in there? I thought that one was fun :)

Are you planning to build something in particular?

Comfortable and relaxed said...

Not there yet.

AVRdude finds the programmer, but the cupboard is bare: Id = "" and the rest is garbage, then I get:

"avrdude: error: buffered memory access not supported. Maybe it isn't a butterfly/AVR109 but a AVR910 device?"

In the past I've got this sort of error from hardware connection problems, so I'll have a play around and see what I can do with it.

The cricket app is there - reminds me of thinkgeeks "annoyatron" :)

No special project specifically for this butterfly. I bought it years ago and thought I'd raise it from the dead - it is Easter, after all...

I normally use ATmega328's and Attiny85's - mostly for hobby stuff and small industrial control projects (I'm an industrial elecrician - not a programmer).

I also have a bit of a "hobby" making industrial "joke" pieces: DIN mount boxes with oddly labled blinkenlights, analogue gauges and industrial connections which I like to install hidden away in industrial control cabinets where maybe someday, somebody will find it and think "WTF???" I put some lead weights in them and fill them up with potting compound, so hopefully they will drive some curious skickybeak wild one day...

Other people apparently have a life...