jung6717 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

handle watchdog resets in stk500v2 (Mega 2560) bootloader #181

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
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

Original issue reported on code.google.com by gottfrie...@gmail.com on 10 Jan 2010 at 9:53

GoogleCodeExporter commented 8 years ago
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?

Original comment by phlo...@gmail.com on 21 Apr 2010 at 7:45

GoogleCodeExporter commented 8 years ago
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.

Original comment by wes...@gmail.com on 13 Apr 2011 at 5:46

GoogleCodeExporter commented 8 years ago
Hello,

any news on the change in the bootloader for mega2560 ?

Thanks.

Original comment by jm_...@hotmail.com on 24 Apr 2011 at 8:19

GoogleCodeExporter commented 8 years ago
Same question, any news for the mega2560?

Original comment by bernhard...@gmail.com on 24 Jun 2011 at 8:54

GoogleCodeExporter commented 8 years ago
I also need this function this very important function for Arduino Mega Board!!
Thanks so much

Original comment by dimitri....@gmail.com on 13 Jul 2011 at 8:41

GoogleCodeExporter commented 8 years ago
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.

Original comment by jm_...@hotmail.com on 28 Jul 2011 at 8:19

GoogleCodeExporter commented 8 years ago
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...

Original comment by bernhard...@gmail.com on 31 Aug 2011 at 1:56

GoogleCodeExporter commented 8 years ago
please, can somebody solve this for the mega2560?

Original comment by bernhard...@gmail.com on 15 Sep 2011 at 6:14

GoogleCodeExporter commented 8 years ago
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.

Original comment by gottfrie...@gmail.com on 15 Sep 2011 at 4:52

GoogleCodeExporter commented 8 years ago
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 ?

Original comment by rizziand...@gmail.com on 15 Sep 2011 at 5:20

GoogleCodeExporter commented 8 years ago

Original comment by dmel...@gmail.com on 18 Oct 2011 at 4:24

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
any updates or patch to test?

Original comment by rizziand...@gmail.com on 28 Dec 2011 at 8:27

GoogleCodeExporter commented 8 years ago
http://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.

Original comment by bernhard...@gmail.com on 28 Dec 2011 at 8:30

GoogleCodeExporter commented 8 years ago
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

Original comment by rizziand...@gmail.com on 28 Dec 2011 at 8:56

GoogleCodeExporter commented 8 years ago
i have no clue, sorry!

Original comment by bernhard...@gmail.com on 28 Dec 2011 at 8:58

GoogleCodeExporter commented 8 years ago
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.

Original comment by justinbe...@gmail.com on 28 Dec 2011 at 9:18

GoogleCodeExporter commented 8 years ago
Does anyone have an Arduino sketch to test this watchdog fix. I have it done 
but no way to test it

Mark

Original comment by mark.l.s...@gmail.com on 1 Jan 2012 at 10:14

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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);
}

Original comment by justinbe...@gmail.com on 1 Jan 2012 at 10:21

GoogleCodeExporter commented 8 years ago
of course that should be Serial.print("..")

Original comment by justinbe...@gmail.com on 1 Jan 2012 at 11:09

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
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);
    }
}

Original comment by mark.l.s...@gmail.com on 2 Jan 2012 at 12:56

GoogleCodeExporter commented 8 years ago
suggested patch

Original comment by wes...@gmail.com on 21 Feb 2012 at 7:45

Attachments:

GoogleCodeExporter commented 8 years ago
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"
                    );
    }

Original comment by justinbe...@gmail.com on 21 Feb 2012 at 8:39

GoogleCodeExporter commented 8 years ago
copy paste started to early above the code above is

    uint8_t ch;
    ch = MCUSR;

Original comment by justinbe...@gmail.com on 22 Feb 2012 at 10:34

GoogleCodeExporter commented 8 years ago
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

Original comment by mark.l.s...@gmail.com on 25 Feb 2012 at 12:49

GoogleCodeExporter commented 8 years ago
plz.. can you release the hex file? thx

Original comment by spark1...@gmail.com on 1 Mar 2012 at 3:32

GoogleCodeExporter commented 8 years ago
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:

Original comment by baltasa...@gmail.com on 21 Apr 2012 at 5:09

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks. I have been pulling my hair out on this issue for a while now.

Original comment by DDelmarD...@gmail.com on 24 Apr 2012 at 8:50

GoogleCodeExporter commented 8 years ago
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!

Original comment by christop...@gmail.com on 9 May 2012 at 2:50

GoogleCodeExporter commented 8 years ago
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 !!!

Original comment by baltasa...@gmail.com on 16 May 2012 at 11:35

GoogleCodeExporter commented 8 years ago
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?

Original comment by dmel...@gmail.com on 18 May 2012 at 7:02

GoogleCodeExporter commented 8 years ago
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.

Original comment by baltasa...@gmail.com on 22 May 2012 at 10:55

GoogleCodeExporter commented 8 years ago
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.

Original comment by dmel...@gmail.com on 23 May 2012 at 2:44

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
I burned the new bootloader but I couldn't upload any new sketches to my board. 
Any thoughts?

Original comment by christop...@gmail.com on 3 Jun 2012 at 9:56

GoogleCodeExporter commented 8 years ago
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.

Original comment by dmel...@gmail.com on 4 Jun 2012 at 12:20

GoogleCodeExporter commented 8 years ago
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

Original comment by Axle.Fo...@gmail.com on 8 Jun 2012 at 7:58

GoogleCodeExporter commented 8 years ago
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?

Original comment by c.mag...@bug.st on 24 Jan 2013 at 2:39

GoogleCodeExporter commented 8 years ago
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?

Original comment by tonyru...@gmail.com on 21 Feb 2013 at 5:51

GoogleCodeExporter commented 8 years ago
Someone recently reported the bootloader I uploaded here, worked fine:
 http://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.

Original comment by justinbe...@gmail.com on 21 Feb 2013 at 9:22

GoogleCodeExporter commented 8 years ago
@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

Original comment by c.mag...@arduino.cc on 21 Feb 2013 at 11:40

GoogleCodeExporter commented 8 years ago
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.

Original comment by tonyru...@gmail.com on 22 Feb 2013 at 3:12

GoogleCodeExporter commented 8 years ago
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.

Original comment by justinbe...@gmail.com on 22 Feb 2013 at 10:06

GoogleCodeExporter commented 8 years ago

Original comment by c.mag...@arduino.cc on 11 Mar 2013 at 11:57