Burning the Bootloader, Arduino Mega 2560 r3, unsolved issues

Hi All,

I needed to burn the Bootloader onto Arduino Mega2560 r3
because I got this Message Error in Arduino IDE, V1.05:

avrdude: stk500v2_ReceiveMessage(): timeout

I checked several configurations and tried also with a PC Win7 without success.

So I ordered an USBasp AVR-ISP-Programmer and plugged it into my Mac mini OS X V10.8.4.

1 .First I tried to burn the Bootloader using the Arduino-IDE
Board: Arduino Mega 2560
Serial Port: (not visable!)
Programmer: USBasp

I got these messages (after approx. 3 mins):

avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: verification error, first mismatch at byte 0x1e000
         0xff != 0x0d
avrdude: verification error; content mismatch
  1. I used avrdude:
$ avrdude -c usbasp -P usb -p m2560 -U flash:w:stk500boot_v2_mega2560.hex -v

and I got this output:

avrdude: Version 5.11.1, compiled on Jul 26 2013 at 11:40:23
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "/opt/local/etc/avrdude.conf"
         User configuration file is "/Users/luigi/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : usb
         Using Programmer              : usbasp
         AVR Part                      : ATMEGA2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : usbasp
         Description     : USBasp, http://www.fischl.de/usbasp/

avrdude: auto set sck period (because given equals null)
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 = 0x1e9801
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FD
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
avrdude: reading input file "stk500boot_v2_mega2560.hex"
avrdude: input file stk500boot_v2_mega2560.hex auto detected as Intel Hex
avrdude: writing flash (261406 bytes):

Writing | ################################################## | 100% 102.45s



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

Reading | ################################################## | 100% 68.49s



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

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as D8
avrdude: safemode: efuse reads as FD
avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Is there a way to solve these issues?

  1. Issue
avrdude: auto set sck period (because given equals null)
avrdude: warning: cannot set sck period. please check for usbasp firmware update.
  1. Issue
avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x1e000
         0xff != 0x0d
avrdude: verification error; content mismatch

But nevertheless I'm now able to upload sketches to the Arduino Mega 2560 again.

"input file stk500boot_v2_mega2560.hex contains 261406 bytes"

This looks way wrong to me. The file size is 21k so I don't think it can contain 261k bytes.

You may wish to try accessing the monitor in the Mega2560 bootloader just to be sure the system is crashed. You could have another issue...

http://avr-developers.com/bootloaderdocs/

@johnwasser
Thanks for your reply.

I used the file:
/Applications/Arduino/Contents/Resources/Java/hardware/arduino/bootloaders/stk500v2/stk500boot_v2_mega2560.hex

The file size is 21 KB

mrburnette:
You may wish to try accessing the monitor in the Mega2560 bootloader just to be sure the system is crashed. You could have another issue...

Bootloader for Arduino Mega2560

I'm not sure what you mean. But the Board works now again and I did a Loop-Back Test before I burned the Bootloader, it was okay.

johnwasser:
"input file stk500boot_v2_mega2560.hex contains 261406 bytes"

This looks way wrong to me. The file size is 21k so I don't think it can contain 261k bytes.

avrdude: writing flash (261406 bytes):

I think 261406 bytes is the flash memory size (256 KB = 262144 bytes)

Update:

  1. Issue solved. I've updated the USBasp-Firmware as described in

http://forum.arduino.cc/index.php?topic=149668.60

  1. Issue: open, I'm investigating, any ideas?

flurin:
2. Issue: open, I'm investigating, any ideas?

The USBasp programmers are using an older protocol that does not properly support more than 64K flash. You need a programmer that is using the stk500v2 protocol, like the AVRISPmkII.

The USBasp programmer will sort of work for burning bootloader, it will not verify correctly. It will not work for uploading sketches.

Thanks for the reply.
Generally I use the Arduino IDE to upload the sketches (3D-Printer firmware). I used the USBasp only to burn the bootloader. This works with the exception of the verifying issue. It seems to be a stupid error so my stupid solution is to use the avrdude -V option (Disable automatic verify check when uploading data).

hiduino:
The USBasp programmers are using an older protocol that does not properly support more than 64K flash. You need a programmer that is using the stk500v2 protocol, like the AVRISPmkII.

The USBasp programmer will sort of work for burning bootloader, it will not verify correctly. It will not work for uploading sketches.

Is the "verification error" really a specific USBasp issue?

http://forum.arduino.cc/index.php?topic=177070.0

http://forum.arduino.cc/index.php?topic=115531.0

flurin:
Is the "verification error" really a specific USBasp issue?

Aside from actual wiring problems or clocking problems causing verification errors, any programmer that is not using the stk500v2 protocol will have issues dealing with more than 64K bytes of flash memory. That includes ArduinoISP and USBtinyISP.

My latest tests with USBasp:

  1. read the flash memory
avrdude -c usbasp -p m2560 -P usb -b 115200 -U flash:r:mega2560_flash.hex:i

avrdude: AVR device initialized and ready to accept instructions

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

avrdude: Device signature = 0x1e9801
avrdude: reading flash memory:

Reading | ################################################## | 100% 47.72s



avrdude: writing output file "mega2560_flash.hex"

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

I compared mega2560_flash.hex with stk500boot_v2_mega2560.hex
Starting with the address 0xE000 the data are the same.

  1. verify the flash memory with mega2560_flash.hex
avrdude -c usbasp -p m2560 -P usb -b 115200 -U flash:v:mega2560_flash.hex:i

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e9801
avrdude: verifying flash memory against mega2560_flash.hex:
avrdude: load data flash data from input file mega2560_flash.hex:
avrdude: input file mega2560_flash.hex auto detected as Intel Hex
avrdude: input file mega2560_flash.hex contains 261406 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 47.60s



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

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

It seems that USBasp is able to manage 256 KB flash memory if the file and flash size are the same (261406 bytes).
The verification error occurs only with automatic verify check.

any programmer that is not using the stk500v2 protocol will have issues dealing with more than 64K bytes of flash memory.

STK500v1 actually works OK with up to 64k WORDS of flash memory.
AVRDUDE is apparently "clever" and will use the "raw spi command" when the "Write memory" page doesn't support high memory. But it's not so smart about "read", which is why you can sometimes get successful bootloader burning that fails to "verify."
I haven't kept track of all the myriad protocols that avrdude supports, but I would be surprised if "stk500v2" is the ONLY one that supports >64k of flash.

It would be nice to have a stk500v2 version of ArduinoISP...

westfw:
It would be nice to have a stk500v2 version of ArduinoISP...

Nick Gammon has an ArduinoISP alternative that works with Mega2560:

I didn't test it yet.

westfw:
AVRDUDE is apparently "clever" and will use the "raw spi command" when the "Write memory" page doesn't support high memory. But it's not so smart about "read", which is why you can sometimes get successful bootloader burning that fails to "verify."
I haven't kept track of all the myriad protocols that avrdude supports, but I would be surprised if "stk500v2" is the ONLY one that supports >64k of flash.

There is a difference how Arduino IDE and averdude write Hex files:

Arduino IDE:

:020000023000CC
:10E000000D9489F10D94B2F10D94B2F10D94B2F129

Record Type 2 (Extend Segment Address Record)
and 0x10 bytes per line

avrdude:

:020000040003F7
:20000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00

Record Type 04 (Extend Linear Address Record)
and 0x20 bytes per line

I'm wondering whether this may be the problem. Is there a way to define the Intel HEX format either for Arduino IDE or avrdude?

Nick Gammon has an ArduinoISP alternative that works with Mega2560

Actually, he has an "optiloader" alternative, which is slightly different.

Examing the flash memory I noticed that the bootloader is written at two memory addresses, 0x1e000 and 0xe3000.

0x3e000 is correct, startaddress for the bootloader
0x1e000 ???

The verification error occurs at address 0x1e000 and obviously the meag2560_flash06.hex doesn't content data at address 0x1e000.

Any idea why avrdude writes the bootloader twice?

flurin:
Any idea why avrdude writes the bootloader twice?

It's not really. If you had an AVRISPmkII (stk500v2) programmer you will see that it only wrote to the upper 128K bytes(64K words). The lower 128K bytes are actually blank. What is happening is that it is reading the upper 128k twice, duplicating it in the lower 128K. That's why the verification fails, but the bootloader will work.

hiduino:
... What is happening is that it is reading the upper 128k twice, duplicating it in the lower 128K. That's why the verification fails, but the bootloader will work.

Your are right!

dump shows empty data at the address 0x1e000.

avrdude> dump flash 0x1e000 0x10
>>> dump flash 0x1e000 0x10 
1e000  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|

Okay, issue 2 solved :slight_smile: at least for me. Thanks.

Note: burning the bootloader using the Arduino IDE it shows the message "verification error ..." and stops execution, it doesn't set the lock back to 0x0f! Therefore it's safer to use avrdude for this purpose and as I posted above just set -V.