Monday, June 13, 2011

Using the Arduino's ICSP: Workaround for a Fried ATmel8U2

As long as you didn't damage the micro-controller, you still might be able to use your Arduino board. This post assumes that you have the same problem that I do, a busted USB chip.

I searched all over the internet for a solution for the Arduino Mega 2560, but never found a definitive answer. After much trail and error, I think I may have found a sure way to upload sketches to your Arduino Mega 2560 without a functioning USB chip. This problem is a lot more trivial for the other boards, but the ATmega2560 chip causes a lot of new problems due to its large memory size. The solutions that worked for the other boards don't apply to the Arduino Mega 2560 because its flash memory is larger than 128kb and therefore uses a different uploading protocol.

You might have wondered why I didn't include a video in the last blog post on motor drivers... probably not. Long story short, I fried the ATmel8U2 chip on my Arduino Mega board. The ATmel8U2 chip replaced the FTDI chip from the earlier Arduino models and controls the USB-to-serial communications. Without this chip you can't use the USB cable to upload sketches, but thankfully there is an alternative way to program your Arduino.

This 6-pin header is where you'll connect your AVR programmer
If you've ever worked with an ATmega chip before, you might have an AVR programmer already lying around that you can use. I am a PIC man, so I did not and had to order one. Before you decide to use the AVR programmer you already have or order a new one, make sure it is actually compatable with the 2560.

The Arduino Mega 2560 is the first Arduino to have more than 128kb of flash memory and has to use a different protocol for programming. You cannot just use any AVR Programmer, you have to make sure that it is compatible with the ATmega2560 chip. The USBtinyISP is a nice programmer you could probably build yourself that can program the ATmega328 and 168. But, it is unable to handle any chip with more than 128kb of memory. If you have a Arduino Mega 2560, make sure that the AVR programmer you buy is not a USBtiny clone.

You could buy the official ATmel AVRISP mkII programmer for $34 from their website, this is guaranteed to be compatible with the 2560. I purchased an AVRISP mkII clone from Pololu, the Pololu USB AVR Programmer for $19.95. It comes with a 6-pin ISP programming cable and a USB-mini cable.

http://www.pololu.com/catalog/product/1300

Pololu has a User Guide to help you set-up your programmer and provides downloads for the drivers and AVR Programming software.

If you have any board other than the Mega 2560 (less than 128kb of flash memory), there is a way to set the Arduino IDE to use the ISP programmer instead of the USB by editing the boards.txt file and changing the upload method. The Arduino IDE uses avrdude behind the scenes to upload your sketch and has AVRISP compatibility. This method didn't work for my board and programmer, so I'm not going to go into details. Google is your friend.

After many wasted hours messing with avrdude, I found a way to upload sketches written in the Arduino IDE with the Pololu USB AVR programmer using AVR Studio 4.

Start a new file in AVR Studio for your Atmega 2560 chip. Plug in your AVR programmer and your Arduino Board. You will need to power both the programmer and the Arduino board separately, the programmer doesn't provide power for the Arduino. Connect the 6-pin ISP cable to your Arduino's ISP header.

Goto Tools>Program AVR>Connect...


Select AVRISP as your platform and whichever COM port your programmer is connected to. Auto may or may not work.


In the Main Tab, set your device to the ATmega2560 and your ISP frequency to 1.843 MHz (not sure if the freq change is necessary, but it works for me). If you click the Read Signature button, it will check the pin connections and make sure it's plugged in correctly.


If you look inside of the boards.txt file located in your Arduino directory, you can find the Fuse and Lock Bits settings for the ATmega2560. Double check the Fuse Tab to make sure that they match with the ones above, I didn't have to fix anything here. If not, change them and hit the program button.


Under the Lock Bits Tab, you will need to edit the values. The default value should be 0xFF, but the required value in the boards.txt file is 0x0F. If you try to type in 0x0F, it will change to 0xCF. I just left it at 0xCF and clicked the Program button. If it gives you a verification error, Uncheck the Verify after programming box and re-click the Program button.


Depending on what happened to your board, you may need to re-burn your bootloader onto your chip. Locate the hex file in your Arduino folder:
arduino-0022\hardware\arduino\bootloaders\stk500v2\stk500boot_v2_mega2560.hex
Load it into the Flash section of the Program Tab, but don't click the button just yet.


Goto the Auto Tab and check the following boxes and click the Start Button. If everything was set correctly, you should now have the bootloader freshly installed onto your Arduino board. If you cannot upload the bootloader onto your chip, try updating the firmware on the Pololu USB AVR Programmer. Check the User Guide for details. If it still won't program, check to see if anything beside the USB chip was fried. Your Arduino may be more damaged than you thought.



As of right now, there is no easy way to convert your sketches into .hex files. When you compile your sketches in the Arduino IDE, it creates a temporary hex file that you will need to locate in your computer's TEMP folder. I am using Windows 7 so this might not apply to you. KEEP THE ARDUINO IDE PROGRAM OPEN WHILE YOU LOCATE THE HEX FILE. Open up the sketch that you want to upload and compile it. Go to your temp folder(you will need to have hidden folders viewable):
C:\Users\"Your Name"\AppData\Local\Temp
There will be a folder called "build" followed by a large string of random numbers. Inside the folder will be your sketch's hex file. It's easier to find if you have file extensions viewable. I suggest moving this file to your desktop or another folder as it will be deleted once you close the IDE program.


Go to the Program Tab and now upload the sketch's hex file in the flash section.


Finally, go back to the Auto Tab and click the Start button. Huzzah! As long as there were no errors your sketch should now be uploaded to your Arduino Mega 2560.

Manly tears were shed

It may not look like much to you, but after trying everything to fix this board for the past week... reading every single forum post... clicking on every single Google link... Getting that LED to finally blink was quite the accomplishment.

After I get some well deserved rest, I'll be sure to get back to updating the blag regularly. We almost lost the Arduino there, but the blag must go on.

7 comments:

  1. Hello Noah,

    First, thanks for documenting this.

    I'm trying to do ISP programming on my Arduino, in order to be able to later use it as a HID. I must say I understand the difficulties you went through to program the mega through ISP...

    I'm using an ArduinoMega2650v3, pololu USB AVR programmer and AVRstudio4.

    The programmer installation went OK, I upgraded the firmware to the latest version, and the programmer recognises the AtMega (blinking yellow led).

    However, AvrStudio4 is unable to connect to the programmer using AVRISP. Even after entering manually the COM port, it fails and goes back to AVRISP mkll, and still fails connection.

    Did you encounter this type of problem ? If so, can you give me a hint how you found ?

    ReplyDelete
  2. Got it to work by reinstalling AvrStudio without the Jungo drivers. Moving on to the next steps...

    ReplyDelete
  3. Hi,

    Thanks for the excellent writeup. I am using APM 1.4 (a board from DIYDrones having the ATMega2560). I wanted to interface an SD card to ICSP pin header (as it is SPI compatible). However, there are two ICSP pin headers on ATMega2560 board. What is the difference between the two exactly, I am unable to find which one to use: My board picture is here:

    http://code.google.com/p/ardupilot-mega/wiki/Hardware

    It says it has two SPI/ISP ports. Please help me, I am in the end of my project phase and I need to get this damn SD card to be interfaced for data logging on (50,51,52,53) pins.. I will owe you a lot, if this works!!

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. It says on the board picture that you linked. The ICSP interface that says AT320 SPI is for programming the PPM encoder. The other one is for the main chip.

      Delete
  4. I know that you said that it shouldn't be possible, but I have been able to upload to my Arduino MEGA 2560 with a USBtinyISP.

    ReplyDelete
  5. Nopp it can't doem.
    avrdude: ser_send(): write error: Bad file descriptor
    avrdude: stk500_send(): failed to send command to serial port
    avrdude: stk500v2_ReceiveMessage(): timeout
    avrdude: stk500v2_getsync(): timeout communicating with programmer
    the selected serial port avrdude: stk500v2_getsync(): timeout communicating with programmer
    does not exist or your board is not connected

    This is also more than cretin:
    No, the USBtiny does not create a serial port and cannot do that. It programs chips directly, using the ISP connection, not Serial. Arduinos are not AVR programmers, they are an AVR with a bootloader that runs over a serial port.

    So I'm happy now finding out about this article and wasting money on the TinyUSB to try to program my Mega with fried USB port.

    ReplyDelete