My favorites | Sign in
Project Home Downloads Wiki Issues
New issue   Search
  Advanced search   Search tips   Subscriptions
Issue 181: handle watchdog resets in stk500v2 (Mega 2560) bootloader
16 people starred this issue and may be notified of changes. Back to list
Status:  Verified
Closed:  Mar 2013

Sign in to add a comment
Reported by, Jan 10, 2010
when a watchdog reset occurs, the watchdog timer stays enabled (as
described on p.52 of the atmega 168 datasheet) - and this leads to the
watchdog resetting again and again in the bootloader, requiring a hard
powerdown by the user.

the current ATmegaBOOT_168.c now has the WATCHDOG_MODS part from the
lilypad bootloader - but i think whether or not that is being used
(skipping the bootloader code early on after a watchdog reset), the
watchdog registers should be cleared anyway.

the attached patch is against 0013, but it should still apply with
some offset to the latest (0017) source

kind regards,

diff -u orig/ATmegaBOOT_168.c new/ATmegaBOOT_168.c
--- orig/ATmegaBOOT_168.c       Fri Feb  6 12:56:00 2009
+++ new/ATmegaBOOT_168.c        Wed Mar  4 14:08:31 2009
@@ -258,13 +258,12 @@
       uint8_t ch,ch2;
       uint16_t w;

       ch = MCUSR;
       MCUSR = 0;

       WDTCSR |= _BV(WDCE) | _BV(WDE);
       WDTCSR = 0;
       // Check if the WDT was used to reset, in which case we dont
bootload and skip straight to the code. woot.
       if (! (ch &  _BV(EXTRF))) // if its a not an external reset...
               app_start();  // skip bootloader
Apr 21, 2010
I'll verify this soon. Anyone knows if there is a convenient way to find out if a wdt 
reset occured in my code later? Could I set some registers I read later?
Apr 13, 2011
Project Member #2
Note that this is fixed in the Uno Bootloader "optiboot",
but that a similar fix is needed in the stk500v2 bootloader
used on the atmega2560 for Uno MEGA.
Apr 24, 2011

any news on the change in the bootloader for mega2560 ?

Jun 24, 2011
Same question, any news for the mega2560?
Jul 13, 2011
I also need this function this very important function for Arduino Mega Board!!
Thanks so much
Jul 28, 2011
Hello, any news on a release date ? It looks like it is just a small adjustment and you would make a lot of people happy :-)

Thanks in advance,
Aug 31, 2011
please, this is really important. Watchdogs are really important, otherwise the Arduinos are not capable of doing reliable control in dangerous environments. And its not a lot of work...
Sep 14, 2011
please, can somebody solve this for the mega2560?
Sep 15, 2011
as the original reporter: please be aware that the bootloader can't be updated just by installing a new Arduino software on the host computer.. one needs at least an ICSP programmer (or brand new boards that have it fixed) for this

so any questions for release date etc seems a bit out of place really.
Sep 15, 2011
many people have a second arduino to use as an ISP. So this is not really a problem.

Is the source code of the original bootloader available, and are there instructions to build it ?
Oct 17, 2011
Project Member #11
(No comment was entered for this change.)
Summary: handle watchdog resets in stk500v2 (Mega 2560) bootloader
Dec 28, 2011
any updates or patch to test?
Dec 28, 2011
There is a c file that should fix it, however I don't have time testing it.

Dec 28, 2011
thanks. is it possible to upload the new boot loader to atmega2560 using ArduinoISP ? I've read here and there of problems due to > 128kb ram
Dec 28, 2011
i have no clue, sorry!
Dec 28, 2011
you need a recent version of avrdude
otherwise it says the destination address is invalid
nothing special just the current avr gcc tool chain 
you can also just build avrdude standalone and use that.
Jan 1, 2012
Project Member #19
Does anyone have an Arduino sketch to test this watchdog fix. I have it done but no way to test it


Jan 1, 2012
something like this

#include <avr/wdt.h>

print "Hello world\n";

print "I am going to not get stuck..\n";
for(x=0; x<100; x++) {
print "I am going to get stuck now..\n";
for(x=0; 1; x++) { 

Jan 1, 2012
of course that should be Serial.print("..")
Jan 1, 2012
Project Member #24
here is what I ended up with

//*	Test code to verify watch dog timer support in stk500v2 bootloader is working
//*	by Mark Sproul
//*	thanks to justinbeech for the help with this code
//*	Proper behavior
//*	When run, this program should restart over and over again as the watch dog timer
//*	times out. Then, when you want to upload a new program, it should work normally
//*	and run the bootloader.
//*	Then, if another program that does NOT use the watchdog timer gets loaded,
//*	it should also work properly without having to power cycle the chip

#include <avr/wdt.h>

void setup()
	pinMode(kLEDpin, OUTPUT);
	Serial.println("WatchDog timer test (Setup)");


void loop()
int	ii;

	digitalWrite(kLEDpin, LOW);
	Serial.println("I am going to not get stuck..");
	for(ii=0; ii<100; ii++)
	digitalWrite(kLEDpin, HIGH);
	Serial.println("I am going to get stuck now..");
	for(ii=0; ii<1000; ii++)
//		delay(2);

Feb 20, 2012
Project Member #25
suggested patch
936 bytes   View   Download
Feb 21, 2012
This is the one I settled on, the data sheet says to disable interrupts, I suppose in case another watchdog interrupt fires, and I definitely want the sketch to start immediately if the watchdog triggers, not go through the bootloader.

What I also think would be good is to communicate the reboot reason to the sketch: is it watchdog? reset button? brown out? or power up?

    __asm__ __volatile__ ("cli");
    __asm__ __volatile__ ("wdr");
    MCUSR = 0;
    WDTCSR |= _BV(WDCE) | _BV(WDE);
    WDTCSR = 0;
    __asm__ __volatile__ ("sei");

    // check if WDT generated the reset, if so, go straight to app
    if (ch & _BV(WDRF)) {
            asm volatile(
                    "clr    r30     \n\t"
                    "clr    r31     \n\t"
                    "ijmp   \n\t"

Feb 22, 2012
copy paste started to early above the code above is

    uint8_t ch;
    ch = MCUSR;

Feb 25, 2012
Project Member #28
I have the correct bootloader up on github now that has the watchdog 
timer fixed as well as several other bug fixes.

Mar 1, 2012
plz.. can you release the hex file? thx
Apr 20, 2012
Hi, mark I tryed to use the hex files on the zip files from your github link, but avrdude gives a memory address error and does not flash the mega2560 bootloader.

The only way I managed was on Arduino GIT Arduino\hardware\arduino\bootloaders\stk500v2 folder copya paste your source files from inside the zip and them issue a make mega2560, them with the HEX produced I was able to flash on the mega2560 board.
Thanks for the fixs and I hope you can fix this for others don't have the same problem.

Anyway here is the compiled HEX that worked for me:
20.5 KB   View   Download
Apr 24, 2012
Thanks. I have been pulling my hair out on this issue for a while now.
May 9, 2012

I have been searching the web for a solution to the mega 2560's watch dog timer issue.

Is the above it? If so, how does one go about using this solution? I am not very familiar with the arduino "lingo".

Could you prehaps make an instruction set on how to use this?

Myself and my classmates would be very grateful!
May 16, 2012
This what Mark fixed on the file I attached:

//*	Edit History
//*	Jul  7,	2010	<MLS> = Mark Sproul
//*	Jul  7,	2010	<MLS> Working on mega2560. No Auto-restart
//*	Jul  7,	2010	<MLS> Switched to 8K bytes (4K words) so that we have room for the monitor
//*	Jul  8,	2010	<MLS> Found older version of source that had auto restart, put that code back in
//*	Jul  8,	2010	<MLS> Adding monitor code
//*	Jul 11,	2010	<MLS> Added blinking LED while waiting for download to start
//*	Jul 11,	2010	<MLS> Added EEPROM test
//*	Jul 29,	2010	<MLS> Added recchar_timeout for timing out on bootloading
//*	Aug 23,	2010	<MLS> Added support for atmega2561
//*	Aug 26,	2010	<MLS> Removed support for BOOT_BY_SWITCH
//*	Sep  8,	2010	<MLS> Added support for atmega16
//*	Nov  9,	2010	<MLS>  Issue 392 :Fixed bug that 3 !!! in code would cause it to jump to monitor
//*	Jun 24,	2011	<MLS> Removed analogRead (was not used)
//*	Dec 29,	2011	<MLS>  Issue 181 : added watch dog timmer support
//*	Dec 29,	2011	<MLS>  Issue 505 :  bootloader is comparing the seqNum to 1 or the current sequence 
//*	Jan  1,	2012	<MLS>  Issue 543 : CMD_CHIP_ERASE_ISP now returns STATUS_CMD_FAILED instead of STATUS_CMD_OK
//*	Jan  1,	2012	<MLS>  Issue 543 : Write EEPROM now does something (NOT TESTED)
//*	Jan  1,	2012	<MLS>  Issue 544 : stk500v2 bootloader doesn't support reading fuses

So watch dog looks fixed ( issue 181 , this one here).
To use just download that file and replace the one inside: 

Them burn this new bootloader on your mega2560 with arduino IDE.
That's it !!!
May 18, 2012
Project Member #34
I've compiled a version of the bootloader for testing in the hopes of putting it into production.  It's based on the latest version of Mark's code; we just added a Makefile.  You can find the code here: and the hex file here:

Can you try this one out and let me know if you have any problems?  

In particular, I've sometimes seen uploads fail (with a verification error) when uploading very large programs (e.g. ~250 KB).  Does anyone else see this behavior?
Status: Accepted
Labels: Component-stk500v2
May 22, 2012
Humm... what's the diference from your compiled version to mine I posted before?
I did a compare on both HEX and only found one line diferent, around line 29/30

May 22, 2012
Project Member #36
Not sure.  I think they were both compiled from the same source.  Still, any testing anyone can do with the one I posted would be useful, because that's the one we've got under consideration for production use.
Jun 3, 2012
I burned the new bootloader but I couldn't upload any new sketches to my board. Any thoughts?
Jun 4, 2012
Project Member #39
Hmm, what programmer did you use?  What error do you get when try to upload?  You might try turning on verbose output for upload (in the preferences dialog) and repeating both the upload and bootloader burning to get more detailed information.
Jun 8, 2012
Make sure your fuses are set properly...  I use the following batch file to flash my bootloader for ArduPilot gear (which uses the 2560) via ATMEL JTAGICEmkII. I used AVR Studio 4 and simply placed the files into c:\New

@echo off
MODE CON:cols=80 lines=80
color F0
TITLE ArduPilotMega v14

set $step1=Fuse bits ISP only
set step1="C:\Program Files\Atmel\AVR Tools\JTAGICEmkII\jtagiceii.exe" -e -dATmega2560 -mj -q -cUSB -s -f0xD8FF -E0xFD -F0xD8FF -G0xFD

set $step2=Fuse bits JTAG and ISP
set step2="C:\Program Files\Atmel\AVR Tools\JTAGICEmkII\jtagiceii.exe" -e -dATmega2560 -mj -q -cUSB -s -f0x98FF -E0xFD -F0x98FF -G0xFD

set $step3=Program Bootloader onto ATmega2560
set step3="C:\Program Files\Atmel\AVR Tools\JTAGICEmkII\jtagiceii.exe" -e -dATmega2560 -q -pf -vf -if"c:\new\stk500boot_v2_mega2560.hex" -mj -cUSB -s -l0xCF -L0xCF

:print out all of the steps so you can see what you'd like to select...

echo 1=%$step1%
echo 2=%$step2%
echo 3=%$step3%

:set the parameter "step" to a number so we can use it later to call that step...
set /p step=Type a number to select a step:

if "%step%" == "1" CLS
if "%step%" == "1" echo %step1%
if "%step%" == "1" %step1%
if "%step%" == "2" CLS 
if "%step%" == "2" echo %step2%
if "%step%" == "2" %step2%
if "%step%" == "3" CLS 
if "%step%" == "3" echo %step3%
if "%step%" == "3" %step3%

goto start
Jan 24, 2013
Project Member #41
I've done a pull request here:

a precompiled version with all the fix is here:

can someone test this one?

Feb 21, 2013
Flashed the bootloader at

The good news was the watchdog timer did not expire in the bootloader however the bad news was I wasn't able to use the bootloader to upload a sketch.  As it stands, using the programmer requires a major disassembly of the unit.  Am I stuck on this one and have to use the programmer?
Feb 21, 2013
Someone recently reported the bootloader I uploaded here, worked fine:

That "working fine" bootloader is in use here:

So try that hex, and the stk500boot.c file is there for your inspection as
Feb 21, 2013
Project Member #44

We have checked the sketch upload, with two different Arduino Mega 2560, and it works.

What board are you using? On which OS?


Feb 22, 2013
Thanks for that.  The bootloader indeed worked fine.  The world is a slightly safer place now.  Did notice one thing however.  I wasn't able to capture the source of the reset with
/* watchdog reset */
Haven't looked through the source code to see if that bit is reset.  Watchdog resets aren't supposed to happen.  The system should safe until someone can take a look.
Feb 22, 2013
Yes unfortunately the reboot reason (brown out, watchdog, reset push
button, power on) is not available to the sketch and the condition is
You'd have to store the reboot reason somewhere, for the sketch to query.
Mar 11, 2013
Project Member #47
(No comment was entered for this change.)
Status: Verified
Sign in to add a comment

Powered by Google Project Hosting