Tuesday, March 26, 2013

Repairing an Arduino Mega or Uno USB port

Both the Arduino Mega 2560 and the Arduino Uno R3 use a micro-controller (the ATmega16U2 in the case of the Mega and the ATmega8U2with the Uno) instead of the FTDI chip used in previous boards. This makes the USB port more versatile. The downside is that it can stop working. You may have damaged your board beyond repair. However, in many cases the device simply needs re-programming.

This link  Programming the DFU describes how to program or re-program the micro-controller. There is a bit of problem though.  If you're in the situation that your Mega is not being recognized by either the Arduino IDE or the system then you will not be able to program it this way. The good news is that you can still program it in-system with an in-system programmer. You won't be able to do it with an FTDI serial interface either because that will end up talking to the main microprocessor on the board not the auxiliary processor.

The cheapest and simplest solution is likely to be a USBASP programmer available here. You will connect the programmer via a six pin adapter available here to the ICSP connector closest to the USB micro-controller (that is, the one furthest from the main micro-controller on the board). You may need to bend out the shield pins a little to get the adapter onto the pins because the key needs to be towards the outside of the board.  This diagram shows the ICSP for USB interface on an Uno. It is in a similar location on the Mega.


You'll need a driver for the USBASP if you're on Windows available here. If you're on Mac or Linux your USBASP programmer will simply show up when it's plugged into the USB port.

Once you have the USBASP connected all you have to do is use AVRDUDE (the program that you use to program the Arduino's main micro-controller) to flash the contents of the hex file: MEGA-dfu_and_usbserial_combined.hex to the USB micro-controller. This hex file is a combination of boot-loader and the USB serial code.

This example is for the Mega on a Mac, the process is very similar for a Uno on Linux & Windows. The hex file for the Uno is Arduino-COMBINED-dfu-usbserial-atmega16u2-Uno-Rev3.hex. You will also have to set the device to be -p ATmega8U2 for the Uno rather than -p ATMega16U2 for the Mega.

Open a Terminal window and go to the location of the hex file for the USB controller:

cd /Applications/Arduino.app/Contents/Resources/Java/hardware/arduino/firmwares 

Then issue the following command:

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr/bin/avrdude -c usbasp -p ATmega16U2 -F -U flash:w:MEGA-dfu_and_usbserial_combined.hex 

AVRDUDE should do it's job and you should see something like this:

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e9489
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: current erase-rewrite cycle count is -231 (if being tracked)
avrdude: erasing chip
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "MEGA-dfu_and_usbserial_combined.hex"
avrdude: input file MEGA-dfu_and_usbserial_combined.hex auto detected as Intel Hex
avrdude: writing flash (7452 bytes):

Writing | ################################################## | 100% 3.79s



avrdude: 7452 bytes of flash written
avrdude: verifying flash memory against MEGA-dfu_and_usbserial_combined.hex:
avrdude: load data flash data from input file MEGA-dfu_and_usbserial_combined.hex:
avrdude: input file MEGA-dfu_and_usbserial_combined.hex auto detected as Intel Hex
avrdude: input file MEGA-dfu_and_usbserial_combined.hex contains 7452 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.53s



avrdude: verifying ...
avrdude: 7452 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done.  Thank you.


And you're done. 

P.S. If you see a signature zeros rather than the one expected, you may have the adapter the wrong way around...


 





3 comments:

  1. I got following result and cannot flash the 16u2 firmware. May I know if you have any idea with this?

    avrdude: warning: cannot set sck period. please check for usbasp firmware update
    .
    avrdude: error: programm enable: target doesn't answer. 1
    avrdude: initialization failed, rc=-1
    avrdude: AVR device initialized and ready to accept instructions
    avrdude: Device signature = 0x000000
    avrdude: Yikes! Invalid device signature.
    avrdude: Expected signature for ATmega16U2 is 1E 94 89
    avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed

    To disable this feature, specify the -D option.

    avrdude done. Thank you.

    ReplyDelete
  2. Check that your board has power. The key is that AVRDUDE cannot communicate with your board. If it has power there must be something more wrong with your board than just the programming of the 16u2.

    ReplyDelete