Mega 2560 R3, eXtreme Burner and AVR Programmer

Need some guidance please.

I have an older faulty Mega2560 that I need to replace.

Seems to be a USB problem, as the PC ( XP sp3 ) only sees an "Unknown Device" and I have spent weeks looking for a solution.

I want to work out how to program the old Mega2560 board using an AVR Programmer and ICSP connection.

However, I can not risk damaging or destroying the existing Mega2560, so here is my plan :

Use a new Mega2560 and AVR Programmer. Once I get these working well and reliably, try to re-program the old board. If this does not work, then I can upload my sketch to the new Mega2560 and replace the board.

Purchased a new Mega2560 R3, and an AVR Programmer
http://www.jyetech.com/Products/073/e073.php

and downloaded the latest version of eXtreme Burner 1.3 from

I have made a cable to connect pins :

AVR Prog pin 9 - MISO  to  Mega ICSP pin 1
AVR Prog pin 2 - Vcc  to  Mega ICSP pin 2
AVR Prog pin 7 - SCK  to  Mega ICSP pin 3
AVR Prog pin 1 - MOSI  to  Mega ICSP pin 4
AVR Prog pin 5 - RST  to  Mega ICSP pin 5
AVR Prog pin 4 - GND  to  Mega ICSP pin 6

I am a bit confused here, as I don't know if the "Power Target" jumper should be on, and if the Vcc pin should be connected to the Mega2560.

If I leave these off, and I power the Mega using the USB cable, will the fact that it is plugged into the USB somehow prevent the AVR Prog from uploading over ICSP ?

When I connect the new Mega to the PC USB cable, the PC immediately detects the board, installs the driver, and creates Com3.

Next, I connect the AVR Programmer to the Mega2560 with the newly made cable ( tested with m.meter and all connected, no shorts ).

Plug the AVR Prog into USB, and the PC detects and installs the "LibUSB-Win32 Devices" > "USBasp"

Start eXtreme Burner, set the chip to ATmega2560 and open an existing sketch ( Blink ) and "build only" - works fine.

Use the "Read All" option, and retreives data from the Mega2560 board. ( does report errors if either the AVR Prog or Mega is disconnected, so reasonably certain it is communicating with the Mega ).

Here is where the problem started ...

I think, if I understand correctly, using the AVR Prog to upload has erased everything on the chip and uploaded the hex file.

This means that I can no longer use the Arduino IDE to upload to the board with USB ?

Tried to upload Blink, and takes a few minutes and eventually returns :

Binary sketch size: 1,718 bytes (of a 258,048 byte maximum) - 0% used
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

I think that the IDE can definately see the board, because if I unplug the board from the USB cable, it reports :

Binary sketch size: 1,718 bytes (of a 258,048 byte maximum) - 0% used
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: ser_send(): write error: sorry no info avail
processing.app.debug.RunnerException: Problem uploading to board.  See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.
	at processing.app.debug.BasicUploader.uploadUsingPreferences(BasicUploader.java:126)
	at processing.app.Sketch.upload(Sketch.java:1649)
	at processing.app.Sketch.exportApplet(Sketch.java:1608)
	at processing.app.Sketch.exportApplet(Sketch.java:1580)
	at processing.app.Editor$DefaultExportHandler.run(Editor.java:2408)
	at java.lang.Thread.run(Thread.java:619)

Next I ( again ) assume that I would need to reload the bootloader to the Mega2560 board ?

Looked around the net, and I can't seem to find any info that agrees - there seem to be multiple bootloaders for the same board ?

I also saw multiple sites with info about the fuses for the Mega2560

If I use eXtreme Burner to read the fuses, I get :

Low 0xFF
High 0xD8
Ext. 0x0F
Lock 0xOF

I am not certain that these are correct. Ever since I 'experimented' with some of the fuse values I saw in different related threads, I can upload to the board ( using eXtreme Burner ) but it does not run after I reset the board.

So the problem is 3 part :

  1. what should the fuses be set to ?
  2. Jumper and pin connections from the AVR to Mega.
  3. where is the correct bootloader for the mega2560 R3 board.
  4. How to re-enable the USB upload option for the board.
  5. is my understanding of the problem correct ?

You can install the current bootloader:
Tools: Burn Bootloader
which will set the fuses also, per the settings in boards.txt
This will allow uploading sketches via the USB port if the onboard USB/Serial path is working.
The bootoader is in the IDE directory, look in the /hardware folder I think.

If not, you can use
File: Upload Using Programmer
to load your sketch and not have any bootloader.

Hi CrossRoads

Many Thanks for the post.

I found that the USB extension cable fromPC to AVR Prog. was 'dodgy' and was causing a problem. I have replaced that. ( New USB extension will be returned to store on Tuesday )

I then changed to Arduino Builder ( v 0.8.3 ) and used the AVR Prog to upload the bootloader to the Mega2560, using the USBasp connection.

( ps .. removed external power from the Mega, connected the AVR Prog Vcc wire to the Mega ICSP pin, and added the Jumper on the AVR Prog board to 'Power Target' )

Took about 20 minutes to upload.

This worked, and I am now able to access and upload sketches from the Arduino IDE. Upload Blink and works perfectly.

From this, I assume that the USB chip is OK, and that the fuses are set correctly.

However, if I re-connect the Mega to the AVR prog, and again use Arduino Builder, and upload the Blink sketch, it does not work -- it does not blink like it does if I load the bootloader via AVR Prog. and then Blink from Arduino IDE over USB.

I 'think' the IDE adds the Blink code AFTER the bootloader, whereas the AVR Prog erases the flash before uploading only Blink.

Am I missing something to make the Blink run if there is no bootloader ( assuming that AVR Prog erases the bootloader if programming via ICSP ) ?

I know that the existing (new) Mega2560 will take the upload on USB, but I am wanting to know how to upload working code via ICSP so that I can recover my older damaged board.

ps .. running IDE > Tools > Burn Bootloader gives an error : avrdude: usbdev_open(): did not find any USB device "usb"

File:Upload Using Programmer, loads the sketch using the AVR programmer connected to the ICSP pins.

Most bootloaders will make the L LED flash - maybe continuously with three flashes every few seconds, or maybe just a couple flashes after a reset.
I haven't powered up a freshly bootloaded Mega in a while, I don't know what the current bootloader does.

CrossRoads:
File:Upload Using Programmer, loads the sketch using the AVR programmer connected to the ICSP pins.

Most bootloaders will make the L LED flash - maybe continuously with three flashes every few seconds, or maybe just a couple flashes after a reset.
I haven't powered up a freshly bootloaded Mega in a while, I don't know what the current bootloader does.

I believe the bootloader LED behavior is to continuously blink three flash bursts every so many seconds IF there is no sketch in flash memory to jump to, if there is it jumps to the sketch after just one three second burst. Keep in mind that different bootloaders for different boards over the years can have slightly different LED patterns.

Lefty

CrossRoads:
You can install the current bootloader:
Tools: Burn Bootloader
which will set the fuses also, per the settings in boards.txt

Are you them (silently) saying that the fuses are loaded as part of the bootloader hex file ?

If so, then (my) logic tells me that uploading any sketch (except a bootloader) using Auduino Builder is not going to work, because when it starts uploading it says :

avrdude.exe: Device signature = 0x1e8901
avrdude.exe: NOTE: FLASH memory has been specified, an erase cycle will be performed.
avrdude.exe: erasing chip

... and then continues to upload

so my thought is that if the chip is erased, then the fuses are gone as well ?

Is there a way to add / combine the hex file for the fuses with the hex file of the sketch ?

"Are you them (silently) saying that the fuses are loaded as part of the bootloader hex file ?"
No, I am Explicitly saying the IDE command
File:Burn bootloader
sets the fuses also.

When I do a project, I burn the bootloader as the first step just to get the fuses, even if I plan to use
File:Upload Using Programmer
to then upload my sketch and not have a bootloader. The fuses will remain as programmed, only the Flash is changed.

I don't use discrete avrdude commands, only
File:Burn bootloader

followed by
File:Upload Using programmer
or,
I remove the Programmer, connect up to USB/Serial interface and use
FIle:Upload sketch (or whatever happens when the menu icon for uploading is clicked)

There is no hex file for the fuses, just the three lines in boards.txt

Tools:Burn bootloader gives an error :

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

Under Tools: Select programmer
is your programmer selected?

Thanks for the pointers.

Changed programmer from
AVRISP mkII
to
USBavr

Displays :.. Burning Bootloader to I/O Board (this may take a minute)...

runs for around 1 hour and then error :

avrdude: verification error, first mismatch at byte 0x1e000
0xff != 0x0d
avrdude: verification error; content mismatch

I am using Arduino IDE 1.5.2

Does the sketch seem to run anyway after the verification error?

That was only the bootloader.

Once that is done ( and failed verification ) I am able to connect from IDE on USB and upload blink.

However, if I load Blink to ICSP with "Upload using programmer" then it does not run - the LED is permanently lit, and I can not access from IDE over USB again.
Just get the timeout error.

avrdude: stk500v2_ReceiveMessage(): timeout

Need to reload the bootloader again I think.

Strange that I can not load Blink over ICSP either with IDE or eXtreme Burner or Arduino Builder. Would that point to some problem with the fuses, or with avrdude, as I think the IDE and Arduino Builder both use it. Not certain about eXtreme Burner.

I don't know. My bootloading experience is with Atmel AVR ISP MKii, so I don't normally see these kinds of problems.

OK. Here's a completely different look at the problem.

The ICSP upload of the hex of the bootloader works, but the hex of Blink does not.

Assuming that the fuse settings may be in the hex file, what's the possibility that the hex file that I have of the Blink sketch ( made with Arduino Builder ) may be faulty, or contain invalid values for something board specific ?

Does anyone have a hex file of the standard IDE sketch 'blink' that I can test uploading to ICSP ?

Have you tried just opening the Blink sketch in the IDE and programming it using
File: Upload Using Programmer?

Thanks CrossRoads

I have just done that, and set the Tools > Programmer to 'usbasp'

I did see lights flashing on the AVR Programmer.

Result on the Mega2560 is the same - LED remains on.

From this test, does it sound like something in a config file relating to the 'usbasp' programmer ?

the file : arduino-1.5.2\hardware\arduino\avr\programmers.txt contains :

usbasp.name=USBasp
usbasp.communication=usb
usbasp.protocol=usbasp
usbasp.program.protocol=usbasp
usbasp.program.tool=avrdude
usbasp.program.extra_params=-Pusb

I didn't realize you were using arduino-1.5.2.
I was going under the assumption that you had IDE 1.0.3 or 1.0.4
I have not used 1.5.x for anything yet.

Thanks CrossRoads

I have great progress / success, but not certain that it is correct, and don't know how to check it.

Did a search on the verification error that I was seeing after upload and verification of the bootloader :

avrdude: verification error, first mismatch at byte 0x1e000
         0xff != 0x0d

and found this page :

In the third post ( reply # 2 ) Andre_1949 mentions using different fuses.

Changed my avrdude batch file to use the fuses mentioned in the above linked post, to:

cd\
cd\ArduinoBuilder\code\Blink
avrdude -v -p atmega2560 -c usbasp -U lfuse:w:0xc2:m -U hfuse:w:0x99:m  -U efuse:w:0xff:m -U lock:w:0x3f:m -U flash:w:Blink.hex

and it uploads and works - LED flashes as expected.

I think that avrdude, as used by the Arduino IDE for 'Upload using Programmer', first reads the fuses, erases the chip, writes the hex file, and then writes the fuses back again. So if the fuses were incorrect before the upload, they will still be incorrect after the upload.

I think that I have confirmed this as follows :

This does not affect an upload of the Bootloader hex file using avrdude from the command line, as the fuses are contained in the hex file.

Any other command line upload fails because I was writing the wrong fuse values, as in :

avrdude -v -p atmega2560 -c USBasp -U lfuse:w:0xFF:m -U hfuse:w:0xD8:m  -U efuse:w:0xFD:m -U lock:w:0x0F:m -U flash:w:Blink.hex

Arduino IDE upload over USB was fine, because it utilised the bootloader and does not affect the fuses ( fuses that were in the bootloader hex file ).

Arduino IDE 'Upload using Programmer' fails, because the wrong fuses are being read from, and re-written to the board.

Also, after uploading from command line ( and changing the fuses, I can now upload from IDE 'using Programmer' and that also now works ( because it is now reading valid fuses, and re-writing those same valid fuses back at the end of the process ).

So the question now stands : am I using the correct fuse settings for the Mega 2560 R3 board ?

If you have the fuses that are in the boards.txt file, you have the correct fuses.

well that's the bit I just can't understand.

The boards.txt file shows :

mega2560.bootloader.low_fuses=0xFF
mega2560.bootloader.high_fuses=0xD8
mega2560.bootloader.extended_fuses=0xFD
mega2560.bootloader.file=stk500v2/stk500boot_v2_mega2560.hex
mega2560.bootloader.unlock_bits=0x3F
mega2560.bootloader.lock_bits=0x0F

but those did not work. I was banging my head against a brick wall trying to find the cause of the problem : suspect USB cable, usbavr drivers, pin settings, hex file creator, bootloader, AVR Programmer board, etc, etc.

And all the time, I was assuming that the fuse settings used in avrdude were correct.

The upload ( or more specifically the running of the uploaded sketch ) only worked using :
-U hfuse:w:0x99:m -U efuse:w:0xff:m -U lock:w:0x3f:m

Now I just need to figure out if these settings will damage the board in any way.