My favorites | Sign in
Project Home Downloads Wiki Issues
New issue   Search
for
  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
Owner:  dmel...@gmail.com
Closed:  Mar 2013


Sign in to add a comment
 
Reported by gottfrie...@gmail.com, 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,
gohai


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;

-#ifdef WATCHDOG_MODS
       ch = MCUSR;
       MCUSR = 0;

       WDTCSR |= _BV(WDCE) | _BV(WDE);
       WDTCSR = 0;
-
+#ifdef WATCHDOG_MODS
       // 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
#1 phlo...@gmail.com
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 wes...@gmail.com
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
#3 jm_...@hotmail.com
Hello,

any news on the change in the bootloader for mega2560 ?

Thanks.
Jun 24, 2011
#4 bernhard...@gmail.com
Same question, any news for the mega2560?
Jul 13, 2011
#5 dimitri....@gmail.com
I also need this function this very important function for Arduino Mega Board!!
Thanks so much
Jul 28, 2011
#6 jm_...@hotmail.com
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,
J.
Aug 31, 2011
#7 bernhard...@gmail.com
bump...
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
#8 bernhard...@gmail.com
please, can somebody solve this for the mega2560?
Sep 15, 2011
#9 gottfrie...@gmail.com
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
#10 rizziand...@gmail.com
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 dmel...@gmail.com
(No comment was entered for this change.)
Summary: handle watchdog resets in stk500v2 (Mega 2560) bootloader
Dec 28, 2011
#14 rizziand...@gmail.com
any updates or patch to test?
Dec 28, 2011
#15 bernhard...@gmail.com
https://code.google.com/p/optiboot/issues/detail?id=50&can=1&q=watchdog
There is a c file that should fix it, however I don't have time testing it.

Dec 28, 2011
#16 rizziand...@gmail.com
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
#17 bernhard...@gmail.com
i have no clue, sorry!
Dec 28, 2011
#18 justinbe...@gmail.com
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 mark.l.s...@gmail.com
Does anyone have an Arduino sketch to test this watchdog fix. I have it done but no way to test it

Mark

Jan 1, 2012
#21 justinbe...@gmail.com
something like this

#include <avr/wdt.h>

print "Hello world\n";
wdt_enable(WDTO_15MS);

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

Jan 1, 2012
#22 justinbe...@gmail.com
of course that should be Serial.print("..")
Jan 1, 2012
Project Member #24 mark.l.s...@gmail.com
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.begin(115200);
	Serial.println();
	Serial.println("WatchDog timer test (Setup)");
	
	wdt_enable(WDTO_15MS);

}

//*******************************************************************************
void loop()
{
int	ii;

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

Feb 20, 2012
Project Member #25 wes...@gmail.com
suggested patch
stk500-wdt.diff
936 bytes   View   Download
Feb 21, 2012
#26 justinbe...@gmail.com
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
#27 justinbe...@gmail.com
copy paste started to early above the code above is

    uint8_t ch;
    ch = MCUSR;

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

https://github.com/msproul/Arduino-stk500v2-bootloader

Mark
Mar 1, 2012
#29 spark1...@gmail.com
plz.. can you release the hex file? thx
Apr 20, 2012
#30 baltasa...@gmail.com
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:
stk500boot_v2_mega2560.hex
20.5 KB   View   Download
Apr 24, 2012
#31 DDelmarD...@gmail.com
Thanks. I have been pulling my hair out on this issue for a while now.
May 9, 2012
#32 christop...@gmail.com
Hello!

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
#33 baltasa...@gmail.com
This what Mark fixed on the file I attached:

//************************************************************************
//*	Edit History
//************************************************************************
//*	Jul  7,	2010	<MLS> = Mark Sproul msproul@skycharoit.com
//*	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: 
hardware\arduino\bootloaders\stk500v2\

Them burn this new bootloader on your mega2560 with arduino IDE.
That's it !!!
May 18, 2012
Project Member #34 dmel...@gmail.com
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: https://github.com/arduino/Arduino-stk500v2-bootloader and the hex file here: https://github.com/arduino/Arduino-stk500v2-bootloader/blob/master/goodHexFiles/stk500boot_v2_mega2560.hex

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
#35 baltasa...@gmail.com
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

Thanks.
May 22, 2012
Project Member #36 dmel...@gmail.com
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
#38 christop...@gmail.com
I burned the new bootloader but I couldn't upload any new sketches to my board. Any thoughts?
Jun 4, 2012
Project Member #39 dmel...@gmail.com
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
#40 Axle.Fo...@gmail.com
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
CLS

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

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

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

: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 c.mag...@bug.st
I've done a pull request here:

https://github.com/arduino/Arduino/pull/1183

a precompiled version with all the fix is here:

https://github.com/cmaglie/Arduino/blob/06ee62afc66e9917b3dc38bcf496de0e1a3d7d7d/hardware/arduino/bootloaders/stk500v2/stk500boot_v2_mega2560.hex

can someone test this one?

Feb 21, 2013
#42 tonyru...@gmail.com
Flashed the bootloader at https://github.com/cmaglie/Arduino/blob/06ee62afc66e9917b3dc38bcf496de0e1a3d7d7d/hardware/arduino/bootloaders/stk500v2/stk500boot_v2_mega2560.hex

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
#43 justinbe...@gmail.com
Someone recently reported the bootloader I uploaded here, worked fine:
 https://code.google.com/p/optiboot/issues/detail?id=50

That "working fine" bootloader is in use here:

https://code.google.com/p/ardupilot-mega/source/browse/#git%2FTools%2FAPM2_2560_bootloader

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

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

What board are you using? On which OS?

C

Feb 22, 2013
#45 tonyru...@gmail.com
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
if(MCUSR & _BV(WDRF))
{
/* 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
#46 justinbe...@gmail.com
Yes unfortunately the reboot reason (brown out, watchdog, reset push
button, power on) is not available to the sketch and the condition is
cleared.
You'd have to store the reboot reason somewhere, for the sketch to query.
Mar 11, 2013
Project Member #47 c.mag...@arduino.cc
(No comment was entered for this change.)
Status: Verified
Sign in to add a comment

Powered by Google Project Hosting