Saturday, July 4, 2009

AVRISP MKII Frustration and Workaround

Following the instructions from the Arduino.cc site for using the AVRISP MKII to burn the Atmega168 bootloader through the Arduino IDE yields the error message:

avrdude: usbdev_open(): did not find any USB device "usb"

Burning the bootloader through the AVR Studio IDE does not output any error messages in AVR Studio. But the Arduino on-board LED (Arduino pin 13) does not light after reset, suggesting that the bootloader was not correctly loaded. Not suprisingly, subsequent attempts to upload sketches through the Arduino IDE fail, yielding the error message:

avrdude: stk500_getsync(): not in sync: resp=0x00
avrdude: stk500_disable(): protocol error, expect=0x14, resp=0x51

This issue is discussed a lot on the web, but I've yet to find a solution. The following is a workaround I developed after days of frustration.

Connecting the AVRISP MKII:
If you have an Arduino board, and wish to program the Atmega168 through AVR Studio using the AVRISP MKII, simply:
1. power the Arduino board externally (a standard 9V wall-wart works fine),
2. connect the AVRISP MKII to your USB port,
3. Connect the 6-pin ribbon cable connector from the AVRISP MKII to the 6-pin header on the Arduino board. The header is labeled "ICSP." The correct orientation is that the red wire of the ribbon cable should be on the side of the header labeled "1." (There is also an inconspicuous raised triangle on the top of the connector that indicates which position is pin 1.)

(Of course, you can also program the Atmega168 with a simple breadboard and an AVRISP MKII. All you need to do is provide 5V (max) to the Atmega168, and connect the six pins from the AVRISP MKII ribbon cable to the appropriate pins on the Atmega 168. There are tutorials on the Arduino.cc site.)

Aside:
You can write your code in AVR Studio. For people with an embedded systems background who are just getting started with the Arduino, and are looking for a way to program the Atmega168 for use off the Arduino board, I recommend that you do not waste your time with the Arduino IDE. Like the Arduino IDE, AVR Studio is free. Unlike the Arduino IDE, AVR Studio is very flexible, allowing you to program the Atmega168 in Assembly or C (you need to download WinAVR to program in C), and does not tie you to the Arduino bootloader.

For everyone else, the Arduino is a great tool for starting out with microcontroller programming. Easy to learn, and tons of support, perfect for quick embedded systems prototyping without having to know Assembly, or even the ins and outs of your microcontroller.

The problem I've run into is that I've been developing my microcontroller code using the Arduino interface, but I am using the Atmega168 off the Arduino board, on a custom printed circuit board. Therefore, I need to burn my Arduino code onto off-the-shelf Atemga168 chips. At this point it's a bit of work to rewrite everything in AVR Studio without the Arduino functions. Eventually I'll buy an STK500. They're a bit expensive, but they make burning the Arduino bootloader hassle-free.

The workaround described below allows you to create the .hex file in the Arduino IDE, and burn it onto the Atmega168 in AVR Studio.

This is the workaround I've found:
Attemping to upload your code to the Arduino board does not work, but it does generate the .hex file for your code! The .hex file is saved in the "applet" folder in your sketch folder (the Arduino IDE creates an applet folder the first time you attempt to upload your sketch; each sketch has its own applet folder).
1. Work on your code in the Arduino interface.
2. When you are ready to upload, attempt an upload. This generates the .hex file. (You don't need to connect the Arduino board to attempt the upload).
3. From AVR Studio, program the Atmega168 (using the connections described above). On the "program" tab, in the "Flash" box, browse to the .hex file you just created. (You only need to do this once. The program tab points at the .hex file, so each time you update the .hex file in the previous step, just click the "Start" button in the Auto tab, or the "Program" button in the Program tab to program the device.)

That's it! If anyone can tell me how to burn the bootloader using the AVRISP MKII the way the Arduino team intended, it'd be much appreciated.

Even for those of you who had no trouble burning the bootloader, the method described above is helpful if your microcontroller is on a custom PCB, and you wish to update the code on your microcontroller without prying it off the board and sticking it into the Arduino board. Just compile the .hex file in the Arduino IDE, connect the AVRISP MKII directly to your PCB (of course, you'll need a 6-pin header on your PCB to plug the connector), and burn the .hex files through AVR Studio.

3 comments:

sumeoj said...

Hi! I'm pretty much a newbie with the arduino diecimilla (so far just tinkering around with it and seeing what I can do) but I was wondering- is it possible to debug the arduino using AVR Studio? I haven't tried it myself yet but seeing that you're familiar with AVR Studio I thought that you might know.

SustainableLab said...

I use the AVR studio debugger quite a bit (there's a Debug drop-down menu), but I haven't tried debugging Arduino code in AVR studio. It's too bad there's no debugger in the Arduino environment. I'd imagine you could copy your Arduino code into AVR Studio in an AVR GCC project (first you'll need to download WinAVR, also free). Again I've never tried this, but I'd imagine that to use the Arduino environment functions, (e.g. pinMode, digitalRead, etc.) you'll need to find the original C source code that defines those functions, and then paste them or include them in your AVR GCC project.

I hope that was helpful -- for a quicker response, you can email me directly.

byjove said...

The AVRISP MKII has a section with the following "Flash" and "EEPROM". Is the bootloader automatically installed when AtMega168 is selected? What is the purpose of the "EEPROM" section and when is it used? Once the bootloader is installed can I download any code already created and compiled in hex in C?