Arduino Forum

Using Arduino => Project Guidance => Topic started by: Embed on Oct 23, 2012, 08:45 pm

Title: Watchdog in Arduino Library - or at least support by bootloader
Post by: Embed on Oct 23, 2012, 08:45 pm
I think watchdog support is essential for many projects where an Arduino boards performs control functions 24h a day.
Reading many posts in the internet, it seems to be very complicated using Arduino 1.0.1 and the current standard bootloader for the AVR boards to get watchdog functionality working. One issue is, that there is no watchdog function in the Arduino Library that would allow processor-independent implementation of such function; currently, registers have to be set directly. The more severe issue is, that the bootloader does not de-activate the watchdog upon reset, so that one can end up with endless resets. There are patched bootloaders available, but without official Arduino support this remains tricky.
Please, at least starting with the new ARM processor generation of boards, support watchdog with a compatible bootloader. If feasible, dedicated library functions should be added to initialize and reset the watchdog.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: GrooveFlotilla on Oct 24, 2012, 08:51 am
I think the added complexity would put off most Arduino adopters.
By the time you've got to the stage of running an Arduino 24/7, the watchdog is a triviality, or, more likely, an irrelevance.

I have had projects running for months at a time, and can count the number of them that have had a watchdog on the fingers of one finger.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Oct 24, 2012, 09:01 am
Quote
... it seems to be very complicated using Arduino 1.0.1 ...


Doesn't seem complicated to me:

Code: [Select]

#include <avr/wdt.h>

void setup ()
{
  Serial.begin (115200);
  Serial.println ("Restarted.");
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
}  // end of setup

void loop ()
{
  Serial.println ("Entered loop ...");
  wdt_reset ();  // give me another second to do stuff (pat the dog)
  while (true) ;   // oops, went into a loop
}  // end of loop


You need a bootloader that doesn't get into a loop if you have the watchdog fire, but that is just an issue of uploading the right bootloader.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Oct 24, 2012, 10:33 am
Quote
The more severe issue is, that the bootloader does not de-activate the watchdog upon reset, so that one can end up with endless resets.

the official ATmega328 bootloader (optiboot) deactivates the watchdog on reset.
The official MEGA bootloader does not.
I'm not sure about Leonardo or Due.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Oct 24, 2012, 10:50 am
Yes, but this is nothing to do with this:

Quote
One issue is, that there is no watchdog function in the Arduino Library that would allow processor-independent implementation of such function; currently, registers have to be set directly.


My example code did not set registers directly.

I believe Optiboot works OK, the older bootloaders may not.

This statement is not supported:

Quote
it seems to be very complicated using Arduino 1.0.1 and the current standard bootloader for the AVR boards to get watchdog functionality working.


It's nothing to do with complexity. It's to do with the bootloader.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Embed on Oct 24, 2012, 08:13 pm
Thank you for the hints...with complicated, I just meant, I spent hours in the internet to find out in principle what to do. I know Arduino aims more on beginners than on hackers, but nevertheless, Arduino is usable for serious applications. And watchdog is missing in the libraries.
Main thing is, I agree, to install the proper bootloader. Why does the pre-installed bootloader not deactivate the watchdog? This should not really be a technical problem, is it?
Anyway, do you have a link to a bootloader that officially supports Arduino Sketches on a Mega 2560? Sorry if this is a silly question, but I have no special knowledge which bootloaders are available and which one to use for which application.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: retrolefty on Oct 24, 2012, 08:28 pm

Thank you for the hints...with complicated, I just meant, I spent hours in the internet to find out in principle what to do. I know Arduino aims more on beginners than on hackers, but nevertheless, Arduino is usable for serious applications. And watchdog is missing in the libraries.

There are other AVR mega chip built-in hardware functions that are not directly supported by the Arduino core libraries. The watchdog is just one of them. Sleep modes, pin change interrupts and others are not directly supported but has had many request to do so. This doesn't prevent a true hacker from accessing them if they study the AVR datasheet and read the existing arduino source code.

Main thing is, I agree, to install the proper bootloader. Why does the pre-installed bootloader not deactivate the watchdog? This should not really be a technical problem, is it?

Because the Arduino core libraries did not use or support WDT functions they saw no need to disable WDT interrupts as part of the bootloader start-up code. Others saw the flaw early on and had posted patched bootloader, such as ADAFRUIT, and made it available for downloading directly to users. The Arduino Co. was slow or reluctant to make the change, and I have no memory of the exact status of current arduino shipping bootloaders are at. I think Uno bootloader supports WDT, but Mega boards still do not?

Anyway, do you have a link to a bootloader that officially supports Arduino Sketches on a Mega 2560? Sorry if this is a silly question, but I have no special knowledge which bootloaders are available and which one to use for which application.

I don't have a link but I pretty sure there is a newer mega2650 bootloader available somewhere that handles the WDT correctly and fixes the !!! bootloader monitor problem.

Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Oct 24, 2012, 10:17 pm

And watchdog is missing in the libraries.


What do you understand by the word "libraries"?

Code: [Select]
#include <avr/wdt.h>
...
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
...
  wdt_reset ();  // give me another second to do stuff (pat the dog)
...



An include file. A line to enable it. A line to pat the dog. Seems to me to be supported.

And if they release a different processor, they adjust those functions to support the new processor. It seems done to me.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 19, 2012, 07:45 pm


And watchdog is missing in the libraries.


What do you understand by the word "libraries"?

Code: [Select]
#include <avr/wdt.h>
...
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
...
  wdt_reset ();  // give me another second to do stuff (pat the dog)
...



An include file. A line to enable it. A line to pat the dog. Seems to me to be supported.

And if they release a different processor, they adjust those functions to support the new processor. It seems done to me.

Nick,
I would disagree that is is anywhere close to "done".
Calling this part of "Arduino" is more than a bit of a stretch.
Yes the sketch itself is not directly touching the hardware but this header and these functions
are clearly not part of Arduino. They are AVR specific.

In his original post, Embed was talking about a "watchdog function in the Arduino Library that would allow processor-independent implementation of such function"

The only way to do that is to have an Arduino function wrapper to do this. Which means
it would need to be part of the core code or part of an Arduino library.
Today that simply does not exist.

While it is possible to easily make watchdog work on certain AVR based boards
by using the AVR specific header file and its proprietary function calls,
that fails to meet the OP's original request of having
a portable function that is processor independent.
To make it work, you have to step outside of Arduino and use
architecture specific proprietary functions.
Because of that, I'd say it isn't "done".




In terms of support by the bootloader. I'd call it's lack of support a bug, or
at least an issue related to an unintended oversight, particularly given
it is usually a line or two of code to fix it with no other s/w impact.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: retrolefty on Nov 19, 2012, 08:11 pm
Quote
In terms of support by the bootloader. I'd call it's lack of support a bug, or
at least an issue related to an unintended oversight, particularly given
it is usually a line or two of code to fix it with no other s/w impact.

--- bill


I would certainly agree with that part as they seemed to have 'fixed it" for some bootloaders (Uno for sure) but left the mega bootloaders still with the "WDT bug' as well the now famous mega bootloader !!! 'feature'.

Lefty
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Nov 19, 2012, 08:12 pm
Quote
"watchdog function in the Arduino Library that would allow processor-independent implementation of such function"

Ah.  It depends on whether that means "processor-independent within the AVR family" or "processor-independent within all CPUs that implement an arduino-like library."  Usually a watchdog is a pretty architecture-dependent thing; not all chips support the same timeouts, some have a "window" of allowed reset times, etc...
The avr-libc watchdog functions are probably OK for within the AVR family...
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 19, 2012, 10:26 pm
question time:

so can I include a watchdog timer in arduino code,
   
it would be nice on a few bit I'm playing with,
   as I'd like the thing to look after itself, go back to a known good state
       i.e. a watch dog.

is there an example set of code that I can compile etc to try out,
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 19, 2012, 11:04 pm


is there an example set of code that I can compile etc to try out,


Look back 4 posts. Nick's post (reply #7) shows how to use watchdog for the AVR.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 19, 2012, 11:44 pm
thanks

saw the code snippet,
   
there was some comment about the right boot loader !
     what is the right boot loader ? I use the Arduino IDE, is that the right boot loader ?
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2012, 12:09 am
The bootloader is code stored in flash on the board.
The IDE can be used to burn/update the bootloader on the board
if you have a ISP programmer.

Depending on which bootloader is on the board, watchdog may or may
not work because the older bootloaders don't properly handle
restarting from a watchdog reset.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: retrolefty on Nov 20, 2012, 02:46 am

thanks

saw the code snippet,
 
there was some comment about the right boot loader !
    what is the right boot loader ? I use the Arduino IDE, is that the right boot loader ?


Depends on what board model you have. The latest R3 Uno is OK as shipped.

Lefty
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 20, 2012, 08:15 am
thank you

so question still stands,

I have the off the shelf Arduino IDE, program via the ide / code on the standard arduino boards,
    standard boards etc,

I guess like,  most people.


do we have an example design I can download, modify and use ?
    are you saying I need a different set of boot loader and programer for the arduino ?

A guide would be good please.


Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2012, 08:46 am

I have the off the shelf Arduino IDE, program via the ide / code on the standard arduino boards,
   standard boards etc,

What does that mean?
There are many different versions of the IDE and many different arduino boards as well
as revisions of those boards.


Quote

do we have an example design I can download, modify and use ?

I haven't seen one, but then I haven't looked. It is pretty simple to use.
Nick provided an outline of the code you need to make watchdog work.
If you want to know more you can consult the AVR libc manual:
http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html (http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html)
What is useful on that page are the other timer timeout defines like
WDTO_2S, WDTO_120MS, etc.. So you can see the other timeout values
available.



Quote

   are you saying I need a different set of boot loader and programer for the arduino ?

Without knowing exactly what you have, it will be impossible to say.

If you have an ISP programmer you can use the 1.x IDE to burn a new optiboot bootloader
into the AVR on your m328 based board that will work with the watchdog timer.
i.e. you can convert your older arduino into a board that will appear to be
an "uno" with optiboot as far as the IDE is concerned.

But depending on what board you have, you may not have to do this.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Nov 20, 2012, 10:40 am
Well, we know "recent UNOs" (perhaps even "all Unos") should have a bootloader that correctly disables the watchdog on RESET.  Note that this means that your sketch will not be able to tell that it was restarted by the watchdog (which is occasionally useful.)

And we know that all Arduino MEGAs currently in production have a bootloader that does NOT disable the watchdog.
There's actually a patch for the MEGA bootloader to fix this; it's just not shipping yet.

Official Duemilanove and earlier 28-pin CPUs may or may not work.
There are some third-party bootloaders (ie AdaBoot) that fixed this.

Typical usage of the watchdog would be to do the wdt_enable (WDTO_1S); in setup(), and then call wdt_reset() at the start of loop().  If the loop execution takes longer than (in this case) one second, the watchdog will reset the chip and it will start over.

I'm not sure how much more of an API you'd be able to wrap around this...
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 20, 2012, 08:05 pm
thank you west fw.

seems from what I read that it could be construed that arduino is not one product.
   
to answer an earlier comment,  I like the mega, and I have the official IDE that comes from the arduino web site,
   as in here

http://arduino.cc/en/Main/Software

would be nice if there was an example I could use, same as we have examples for things like lcd's in the ide.
   
how about an example,

suggestions.
  print out to rs232 message.
    flashes the led, using say delay
     that needs say a key to be pressed on the terminal within say 10 seconds,
            if you press a key, led stays on for a second,
                 if you don't press a key in time,  program resets

just an idea,
   thanks
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 20, 2012, 08:49 pm
I tried testing my simple sketch on page 1 of this thread. It works up to a point, but I must have the "bad" bootloader. Does anyone have a link to one that definitely fixes the watchdog issue? (For the Mega 2560).
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2012, 08:54 pm

to answer an earlier comment,  I like the mega, and I have the official IDE that comes from the arduino web site,
   as in here

http://arduino.cc/en/Main/Software


I think you are not seeing or understanding the need for exactness.

Whether or not the Watchdog reset can be used with your board depends on what you actually have.
More precisely what AVR chip is on your board and what bootloader is installed in that chip.
And if what you have doesn't work, depending on what you have (board, IDE, and ISP programmer),
you can use the IDE or tools that come with the IDE to modify/correct (update)
a bootloader that won't work with watchdog reset to a new bootloader that will.


The information you provided above still does not answer the basic questions of what you actually have
with respect to either s/w or h/w.
Ok, so you "like" the "mega", but is that what you have? And if so, which "mega"?
There is an Arduino "mega" that uses 1280 and one that uses a 2560.
Or do you have some other AVR based Arduino?

And the s/w link you provided, actually has 27 different versions of the IDE that can
be downloaded from that page.
While the latest AVR based s/w version is at the top, that version changes through time
so depending on when you downloaded the "latest" IDE it can be different versions.
The reason that all this is very important is that in the past year there
have been some pretty big changes to the IDE, and the AVR based bootloaders
that ship with the Arduino s/w.

Some of these changes affect whether or not watchdog reset works and how to update
a bootloader that doesn't work with one that does.

--- bill

Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 20, 2012, 09:09 pm

would be nice if there was an example I could use, same as we have examples for things like lcd's in the ide.
 


The exact code I posted on page 1 of this thread works, and does pretty-much what you ask. Here it is again:

Code: [Select]

#include <avr/wdt.h>

void setup ()
{
  Serial.begin (115200);
  Serial.println ("Restarted.");
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
}  // end of setup

void loop ()
{
  Serial.println ("Entered loop ...");
  wdt_reset ();  // give me another second to do stuff (pat the dog)
  while (true) ;   // oops, went into a loop
}  // end of loop


Tested on the Mega2560 board once I replaced the bootloader with this working one:

https://raw.github.com/arduino/Arduino-stk500v2-bootloader/master/goodHexFiles/stk500boot_v2_mega2560.hex

Output:

Code: [Select]

Restarted.
Entered loop ...
Restarted.
Entered loop ...
Restarted.
Entered loop ...
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2012, 09:37 pm
Here is an example sketch that shows how to use watchdog reset as a way to
intentionally reset the board.
While having a way to reset the board under software control can be useful, this example
unlike Nick's example, is not a good example of what watchdog reset is often/normally used for.
When using watchdog in the normal way - as in Nick's example,
best programming practice is *NOT* to do the wdt_reset() in an ISR but in a main loop
as his example shows, since doing it in an ISR will prevent a WDT reset from happening
when the foreground code is stuck in an an unintended loop - which is the entire point of using WDT reset.

--- bill





Code: [Select]
/*
* Demonstration of AVR processor reset under software control
* since the Arduino core library does not provide a function to do this.
* While this resets the AVR chip and all its internal registers,
* it won't reset any of the attached hardware that is external to the AVR chip.
*
* Note:
* This will not work with some of the older versions of the Arduino bootloader
* as they didn't properly initiliaze the AVR chip for watch dog reset & timeouts.
* With some older bootloaders it may cause the bootloader to get stuck in a
* constant reboot until the chip is power cycled.
*
* 2012-17-06  bperrybap - added flush for 1.x arduino to ensure message is sent
*
* May 2011 -  bperrybap@opensource.billsworld.billandterrie.com
*/


void setup()
{
Serial.begin(9600);
Serial.println("setup()");
}

void loop()
{
Serial.println("Top of loop()");
for(int x = 3; x ; x--)
{
Serial.print(x);
delay(1000);
}
Serial.println("(Reseting Arduino)");
#if ARDUINO > 100
// force out the serial data as silly 1.x ALWAYS buffers xmits
// with no way to disable buffering.
Serial.flush();
#endif
resetArduino(); // reset myself
}

/*
* Function to cause the AVR to reset
*/
#include <avr/wdt.h>
void resetArduino()
{
noInterrupts();
wdt_enable(WDTO_15MS);
while(1); // wait to die and be reborn....
}
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 21, 2012, 08:40 am
thank you guys,

Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: FernandoGarcia on Nov 25, 2012, 05:25 pm
I uploaded these examples in my arduino mega 2560 and was a fight to get remove them.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 25, 2012, 05:47 pm

I uploaded these examples in my arduino mega 2560 and was a fight to get remove them.

So you got to see first hand what happens when a bootloader doesn't properly initialize the WDT registers
after a watchdog reset.
Given it is such a simple/easy fix to the bootloader, I don't understand why the Arduino team doesn't
ship an updated bootloader to fix this.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 25, 2012, 08:52 pm
I've provided a link above to the fixed one. Replace the file in your current installation, and do an "burn bootloader".

I'm not sure if the fixed one is the one that ships with the IDE, it should be, one would think.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: stevemeng on Jan 24, 2013, 11:29 am


And watchdog is missing in the libraries.


What do you understand by the word "libraries"?

Code: [Select]
#include <avr/wdt.h>
...
  wdt_enable (WDTO_1S);  // reset after one second, if no "pat the dog" received
...
  wdt_reset ();  // give me another second to do stuff (pat the dog)
...



An include file. A line to enable it. A line to pat the dog. Seems to me to be supported.

And if they release a different processor, they adjust those functions to support the new processor. It seems done to me.


I don't support your words.
If the delay() is used, the watchdog will reset all time.
Reset button don't work. I have to  power off.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: retrolefty on Jan 24, 2013, 04:09 pm
After much searching the web I finally found a optiboot version for the mega1280 board. Tested on two different mega1280 boards a seeeduino and a arduino mega1280. First the the new boards.txt entry to support the new bootloader:

Quote

##############################################################

megao.name=Arduino Mega1280 Optiboot
megao.upload.protocol=arduino
megao.upload.maximum_size=130048
megao.upload.speed=115200
megao.bootloader.low_fuses=0xff
megao.bootloader.high_fuses=0xdc
megao.bootloader.extended_fuses=0xf5
megao.bootloader.path=optiboot
megao.bootloader.file=optiboot_atmega1280.hex
megao.bootloader.unlock_bits=0x3F
megao.bootloader.lock_bits=0x0F
megao.build.mcu=atmega1280
megao.build.f_cpu=16000000L
megao.build.core=arduino
megao.build.variant=mega



And the optiboot hex file  optiboot_atmega1280.hex
I was able to burn this bootloader using the arduinoISP sketch from IDE 1.0.3, but not using my USBtiny hardware programmer, as it does not work with flash sizes >64KB in size.

Quote

:020000000404F6
:020000021000EC
:10FC0000112484B714BE81FFF2D085E08093810077
:10FC100082E08093C00088E18093C10086E08093F9
:10FC2000C20080E18093C4008EE0CBD0279A86E0AA
:10FC300020E33CEF91E0309385002093840096BB55
:10FC4000B09BFECF1F9AA8958150A9F7CC24DD2444
:10FC500099249394A5E0BA2EF1E1AF2EA6D0813479
:10FC600061F4A3D0082FB3D0023811F0013811F499
:10FC700084E001C083E091D08DC0823411F484E12E
:10FC800003C0853419F485E0AAD084C08535A1F479
:10FC90008CD0082F10E089D0E82EFF24FE2CEE2413
:10FCA000E02AF12A8F2D881F8827881F8BBFEE0C32
:10FCB000FF1C8DD067016EC0863521F484E08FD0A3
:10FCC00080E0D9CF843609F042C06FD06ED0082FC3
:10FCD0006CD080E0C81680EED80620F483E0F601F0
:10FCE00087BFE895C0E0D2E060D089930C17E1F7B8
:10FCF000F0E0CF16F0EEDF0620F083E0F60187BFDC
:10FD0000E89565D007B600FCFDCFA601A0E0B2E003
:10FD10002C9130E011968C91119790E0982F8827C4
:10FD2000822B932B1296FA010C0197BEE8951124B1
:10FD30004E5F5F4FF3E0A030BF0751F7F601B7BE4B
:10FD4000E89507B600FCFDCFA7BEE89523C0843731
:10FD5000A1F42BD02AD0E82E28D039D0E6010E2DE0
:10FD6000FE0186911AD021960150D1F70894C11C4A
:10FD7000D11CEA94CE0CD11C0DC0853731F427D0AC
:10FD80008EE10BD087E909D075CF813511F488E079
:10FD900018D01DD080E101D061CF982F8091C00094
:10FDA00085FFFCCF9093C60008958091C00087FF27
:10FDB000FCCF8091C00084FD01C0A8958091C60051
:10FDC0000895E0E6F0E098E1908380830895EDDF08
:10FDD000803219F088E0F5DFFFCF84E1DECF1F939A
:10FDE000182FE3DF1150E9F7F2DF1F91089580E04B
:08FDF000E8DFEE27FF2709946C
:040000031000FC00ED
:00000001FF


And finally a sketch from a poster here on this forum (forgot name, sorry whoever) to test the ability to handle a very short 15 millisec WDT interrupt timeout. Works with Uno but
will 'brick' mega boards with 'stock' bootloaders.
WDT_test.ino
Code: [Select]

// Test sketch to see if WDT interrupts are handled properly by the bootloader

/*
Warning Warning Warning this is a semi-destructive test in that
if your bootloader does not reset WDT interrupts upon starting
it will be forced into a tight loop of bootloader starts/WDT resets
chip/bootloader starts again/lather rinse repeat. One can only
recover to normal operation by reburning the bootloader with
a ICSP programmer. If this sketch runs properly on a chip with a WDT aware bootloader,
you will see continuous serial output on the serial monitor.

Note that current arduino mega boards will fail this test and brick

*/

// Code from arduino forum poster (sorry name not remembered) 1/11/13

#include <avr/wdt.h>

void setup(){
 Serial.begin(57600);
 delay(100);
 Serial.println("Hello world");
 wdt_enable(WDTO_15MS);
}

void loop(){
 Serial.println("I am going to not get stuck..");
 for(int x=0; x<100; x++) {
   wdt_reset();
   x++;
   delay(10);
 }
 wdt_reset();
 Serial.println("I am going to get stuck now..");
 for(int x=0; 1; x++) {
   delay(10);
 }
}



Lefty
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Jan 24, 2013, 09:53 pm

I don't support your words.
If the delay() is used, the watchdog will reset all time.
Reset button don't work. I have to  power off.


It's a bug in the bootloader, not the sketch. If you change the fuse to load the sketch (and bypass the bootloader) it will work correctly. Or, better, get a bootloader that handles the WDT correctly.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Embed on Mar 03, 2013, 06:07 pm
Regarding "
Tested on the Mega2560 board once I replaced the bootloader with this working one:

https://raw.github.com/arduino/Arduino-stk500v2-bootloader/master/goodHexFiles/stk500boot_v2_mega2560.hex
"

I found another bootloader (which seems to be the one installed with the Arduino IDE) under
https://github.com/arduino/Arduino/tree/master/hardware/arduino/
bootloaders/stk500v2/stk500boot_v2_mega2560.hex   

It has the same file name as "this working one", but its size is 103kB versus 21kB for the other.
Obviously, these are different bootloaders, and should have a different version, means e.g. stk500boot_v3_mega2560.hex for the working one. Besides of this, what are the differences between the two, other than watchdog support?
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Embed on Mar 03, 2013, 06:16 pm
Thanks Nick to have solved the problem for the Mega2560 (now only the correctly working bootloader should be delivered with new Mega 2560 and new versions of the IDE).

Meanwhile, I have ordered the new Arduino Due. Has anyone yet successfully used watchdog with this one? Means, does the standard Due bootloader support watchdog, and what are the equivalent lines of code for the SAM3XE8 in the Arduino IDE?
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Mar 03, 2013, 09:10 pm
I don't know, I suggest you post this question in the Due part of the forum.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Mar 04, 2013, 08:53 am
Quote
Obviously, these are different bootloaders, and should have a different version, means e.g. stk500boot_v3_mega2560.hex for the working one.

"Stk500v2" is the name of the protocol supported by this bootloader, the v2 is NOT the version of bootloader itself.
I don't think that there is a separate version number for the bootloader.  Although that WOULD be a good idea.
(Hmm.  It does have the date that the code was compiled:
Bootloader>? CPU stats
Arduino explorer stk500V2 by MLS
Compiled on = Jan 28 2013

)
Quote
but its size is 103kB versus 21kB for the other.

Um?  Not that I could see.
https://github.com/arduino/Arduino/blob/master/hardware/arduino/bootloaders/stk500v2/stk500boot_v2_mega2560.hex shows as "514 lines (513 sloc) 22.989 kb"
While the newer code at https://github.com/arduino/Arduino-stk500v2-bootloader/blob/master/goodHexFiles/stk500boot_v2_mega2560.hex shows as "file 469 lines (468 sloc) 20.964 kb "
Perhaps you were comparing HTML pretty-printed web page against 'raw' file size?
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Mar 04, 2013, 09:23 am

It has the same file name as "this working one", but its size is 103kB versus 21kB for the other.


We have to take that with a grain of salt, as the maximum size of the bootloader on the Mega2560 is 8K bytes (see datasheet, page 330).

The idea that a chip with 256 Kb of program memory would have a 103 Kb bootloader is, if I may say, laughable.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Mar 04, 2013, 09:59 am
Well, size of the .hex file will always be somewhat more than twice the size of the actual code...
20k is a reasonable .hex file size for an 8k bootloader.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Omnimusha on Mar 20, 2013, 02:04 am

hello, I need the strech by bluetooth shit, and I get it, but I must be attentive to press the reset arduino-one.

when bleutooch connects, there squeezed reset and this loads the strech.

I tried to use the wacthdog, when this is connected to a reset,
but I can not load the strech, someone has done something similar to what
comment?


Code: [Select]


#include <avr/wdt.h>

int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
void setup ()
{
  Serial.begin (115200);
    pinMode(led, OUTPUT);

//  Serial.println ("Restarted.");
while(!Serial){
  ;

wdt_reset ();   
wdt_disable();

}  // end of setup

void loop ()
{


analogWrite(led, brightness);   

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }     
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);   





 
}  // end of loop




http://www.ebay.com/itm/Wireless-Bluetooth-Transceiver-Module-RS232-TTL-/200520318414?_trksid=p2047675.m1850&_trkparms=aid%3D222002%26algo%3DSIC.FIT%26ao%3D1%26asc%3D11%26meid%3D6299423035725889752%26pid%3D100011%26prg%3D1005%26rk%3D4%26sd%3D320968741708%26 (http://www.ebay.com/itm/Wireless-Bluetooth-Transceiver-Module-RS232-TTL-/200520318414?_trksid=p2047675.m1850&_trkparms=aid%3D222002%26algo%3DSIC.FIT%26ao%3D1%26asc%3D11%26meid%3D6299423035725889752%26pid%3D100011%26prg%3D1005%26rk%3D4%26sd%3D320968741708%26)
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: richsp51 on Mar 30, 2013, 10:46 pm
Nick,
re: Regarding "
Tested on the Mega2560 board once I replaced the bootloader with this working one:

https://raw.github.com/arduino/Arduino-stk500v2-bootloader/master/goodHexFiles/stk500boot_v2_mega2560.hex"

Do you know if this is the bootloader in the Arduino 1.04 IDE release?

I've been battling dropped ethernet  connection on my webserver application for weeks. It occurs anywhere from hours to days. The application continues to run, but over time, a client will not be able to connect. I have a Mega2560  and want to implement watchdog timer based on your example. The code verifies, but I haven't uploaded it yet to test based on the comments on this post (the need to replace the bootloader)

Is your recommendation of the bootloader you specified still valid? Any help is appreciated.

Thanks,
Rich
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Mar 30, 2013, 11:02 pm
There seem to be two booloader files shipping with 1.04 namely:

Code: [Select]

// File =  Mega2560-prod-firmware-2011-06-29.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 1E 35 14 08 1F 65 7F 8C 96 50 69 9F 19 1E 3D F0

// File =  stk500boot_v2_mega2560.hex
// Loader start: 3E000, length: 8192
// MD5 sum = D9 E6 6B 4E D1 A6 11 2C 61 8F 9B D5 5D 24 E2 13


However neither has the MD5 sum of the one that I found to work namely:

Code: [Select]

// File =  stk500boot_v2_mega2560_fixes_watchdog_problem.hex
// Loader start: 3E000, length: 8192
// MD5 sum = 8A F4 7A 29 43 A0 D8 7C DB ED 09 A3 8F 40 24 1E


I would still recommend the "fixed" one from:


https://raw.github.com/arduino/Arduino-stk500v2-bootloader/master/goodHexFiles/stk500boot_v2_mega2560.hex
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: richsp51 on Mar 30, 2013, 11:24 pm
Nick, thanks for your reply. Ummmm... how do I actually load this? Using the Arduino IDE under tools->Burn Bootloader  ? Obviously from my question I've never done this and would appreciate pointing me in the right direction. Is there documentation you could site? All I could find in  http://arduino.cc/en/Guide/Environment  using IDE tools->Burn Bootloader is:
"Ensure that you've selected the correct board from the Boards menu before burning the bootloader."

Thanks in advance.
Rich
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Mar 30, 2013, 11:36 pm
You need an external device. A board can't burn its own bootloader. If you have a second Arduino one option is the sketch described here:

http://www.gammon.com.au/forum/?id=11635

Using the IDE you also need another device, such as another Arduino with the "Arduino as ISP" sketch on it, or a programmer like USBtinyISP or similar. These are available from places like Adafruit, Sparkfun, eBay, etc.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: richsp51 on Apr 02, 2013, 12:45 am
Nick, thanks for your reply. I do have another Arduino ( Uno, rev 3). I reviewed the sketch you specified in your reply. To other readers... Nick's reply is more then just a sketch, it's a tutorial on how to burn a new bootloader using a second arduino.
Question: before doing this, is there any harm in testing the sketch you listed on page 1 with my existing bootloader? If my bootloader (by chance) were correct, I would get the results listed on page 2 ,your  Reply #22 . If not, what will occur? It won't brick my Mega will it? What happened when you ran with the "bad" bootloader,  Reply #20?
I'm asking because of FernandoGarcia 's reply #25 "I uploaded these examples in my arduino mega 2560 and was a fight to get remove them." as well as RetroLefty's post #29 example code comment: "Note that current arduino mega boards will fail this test and brick". Yikes!
To restate my environment: Mega2560, Arduino IDE 1.0.4

thanks
Rich
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Apr 02, 2013, 02:20 am

Question: before doing this, is there any harm in testing the sketch you listed on page 1 with my existing bootloader? If my bootloader (by chance) were correct, I would get the results listed on page 2 ,your  Reply #22 . If not, what will occur? It won't brick my Mega will it? What happened when you ran with the "bad" bootloader,  Reply #20?


First, there are ways around a "bricked" processor, particularly this type of bricking. The problem is that the sketch runs, sets up the watchdog timer, and the watchdog then interrupts the bootloader.

The trick is to not let the sketch run. You power the board off completely, hold down reset, keep holding down reset you apply power (plug the USB cable in). At this stage the sketch hasn't run (you have reset held down). Now with a free hand (this can be the tricky bit) you start uploading a fresh sketch. As it starts to upload you release reset (and not before), and the bootloader kicks in, without having the watchdog timer enabled.

Or, you simply use things like my bootloader uploader to simply replace the bootloader which has the side-effect of erasing program memory, and thus erasing the problem sketch. So you have two ways around it.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: williamcbc on Oct 11, 2013, 02:42 pm
pls check this:
>>Arduino Mini 328
no delay() befero wdt_enable(), and one wdt_reset() just after wdt_enable() and another wdt_reset() in first line in loop();

like this:
void setup()
{
  wdt_enable(WDTO_4S);
  wdt_reset();
}
void loop()
{
    wdt_reset ();
    while(1)
    {
    //user loop
    }
}
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: gerrievanzyl on Nov 19, 2013, 10:18 pm
Nick,

Is there a source that documents which Arduinos have what bootloader?

Specfically, I would like to know whether a standard Leonardo will work with a watchdog timer?

Thanks so much
Gerrie
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 19, 2013, 11:50 pm
The bootloader in use doesn't really affect whether or not you can use the watchdog timer.

The only problem is some bootloaders (particularly the Mega earlier ones) don't work too well if the watchdog fires while it is booting.

The Leonardo has a watchdog timer.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2013, 12:26 am

The bootloader in use doesn't really affect whether or not you can use the watchdog timer.

The only problem is some bootloaders (particularly the Mega earlier ones) don't work too well if the watchdog fires while it is booting.


I believe this is misleading.
The issue I've seen on the older bootloaders is that if the watchdog actually "fires" (causing a watchdog reset),
the bootloader didn't properly clear the watchdog reset state which
ended up causing an infinite watchdog reset loop.
It did not matter when/where the the initial watchdog fired.
This was definitely true in the m168/m328 bootloader..
It was a simple 1 line fix to the bootloader code but the Arduino team never fixed it.
(I patched all my bootloaders)
The optiboot bootloader, which is now used, does not have this issue.

If the bootloader has this issue, it is not possible to use the watchdog timer
beause if the watchdog ever actually occured (causing a watchdog reset),
the sketch would never restart because of the watchdog reset error in the bootloader.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 20, 2013, 03:48 am
That's what I was trying to say. The watchdog works, but the way the bootloader interacts with it doesn't. This particularly applies to a "reset" watchdog, not an "interrupt" watchdog.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: drjiohnsmith on Nov 20, 2013, 09:29 am
is it just me

or is it sad that the arduino team did not implement the one line fix


Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2013, 09:32 am

is it just me

or is it sad that the arduino team did not implement the one line fix

Kind of,
But the newer optiboot bootloader which is now being used is better since not
only does it not have the issue, but it is smaller so there is now more
room for application code.

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Nov 20, 2013, 09:58 am

is it just me

or is it sad that the arduino team did not implement the one line fix


No software is perfect when it is released. As far as I know recent versions fix known bugs. Known at the time, that is. There is always going to be some lag between when a bug is discovered and when it is fixed. First the bug needs to be confirmed. Then a fix devised. Then the fix tested to make sure it doesn't introduce other, unexpected, side-effects. Then the fixed software is rolled out to the production line that is making the gadgets in the first place.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bperrybap on Nov 20, 2013, 10:48 am


is it just me

or is it sad that the arduino team did not implement the one line fix


No software is perfect when it is released. As far as I know recent versions fix known bugs. Known at the time, that is. There is always going to be some lag between when a bug is discovered and when it is fixed. First the bug needs to be confirmed. Then a fix devised. Then the fix tested to make sure it doesn't introduce other, unexpected, side-effects. Then the fixed software is rolled out to the production line that is making the gadgets in the first place.

Well, at least let's be a little more open/honest here.
There were many bugs/issues (some even self inflicted by the team itself) that the team simply
refused to fix even when it was clearly pointed out and a drop in solution was offered.
More recently things are getting better, but for a few years, things were pretty bad
and got very heated at times.

The best example of a total screw up was when the team consciously and intentionally decided
to break 100% of the existing 3rd party libraries between the final 1.0 release candidate
and the final official 1.0 release.
In normal s/w development practices, you just don't do something like that.
(have years of working code, and then break it all between a release candidate and the final release)
This broke years of compability and stability. The team knew this was going to be an issue
and against many pleas from 3rd party developers proceeded anyway
even though there were some very simple alternatives that offer backward comptibilty to
to the vast majority of existing 3rd party.

Think about that again for a moment. This was not a bug, it was not an "oops" or
an untended consequence. The team knowingly and consciously decided
that it was a good idea to break 100% of all the 3rd party libraries in existence.
They thought that breaking all the libraries was a way to force the 3rd party developers
to all update their code for the new way of doing things.

Even after a couple of years since the 1.0 release, we are still fighting issues with this decision
and yet they still refuse to include some backward compability header files to allow
most of the pre 1.x libraries to work on 1.x

--- bill
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Nov 20, 2013, 11:07 am
Quote
Specfically, I would like to know whether a standard Leonardo will work with a watchdog timer?

The leonardo bootloader DOES include a "wdt_disable()" call, and DOES terminate early (running the user sketch) in the case of a WDT-caused reset, so you should be able to use the WDT however you want in a sketch, without any problems (except that the sketch will not be able to notice that it was restarted by the WDT.)

Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Kralj5 on Jan 09, 2014, 04:06 pm

I tried testing my simple sketch on page 1 of this thread. It works up to a point, but I must have the "bad" bootloader. Does anyone have a link to one that definitely fixes the watchdog issue? (For the Mega 2560).


I am having mega 2560 board with "bad" bootloader. I am asking myself, if I can solve the problem with non-disabled watchdog by bootloader with this trick?
Can I add "wdt_disable();" before a "wdt_enable (WDTO_8S);" in a void setup (WDT will be set to 4 or 8s to ensure that bootloader will load completely)?. Will this trick do the job?
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: nickgammon on Jan 09, 2014, 09:07 pm
I doubt it. My tests showed that with the bad bootloader even a long watchdog time didn't help much. I think it might change the watchdog time-out to something short, the watchdog then immediately fires, it reboots, and it does the whole thing again.

I recommend upgrading the bootloader, it can be done simply enough.

http://www.gammon.com.au/forum/?id=11635
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: westfw on Jan 10, 2014, 07:36 am
Theory (and the datasheet) says that the watchdog control register is reset to the default value of zero (except for the Enable bit) on a reset, which would mean that a freshly reset chip is always operating with the minimum timeout. :-(
I think that's silly; if you're going to remember that the WDT is running, you should remember other things about it as well; but it does make sense from a HW perspective, and seem to match observed behavior.
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: bilica on Mar 06, 2014, 10:36 pm

Does anybody know if the Arduino Yun has a "good" bootloader ? The Yun supposedly has  a Leonardo inside...

Thanks!
Title: Re: Watchdog in Arduino Library - or at least support by bootloader
Post by: Kralj5 on Dec 01, 2014, 11:38 am
Nick,

thanks for the link to your bootloader upgrade program. I have successfully upgraded few mega2560 bootloaders.

I have ProMini 5v 16MHz with ATmega328 chip. Stock bootloader has some problems with watchdog. (if wdt is activated board continuously reset, unless you make power cycle reset).

So i use Nick`s bootloader upgrade program and reprogram Pro Mini with Uno bootloader (i have only this option) The Pro Mini with Uno bootloader is working fine and fully support watchdog.

Can I expect some problem with ProMini with Uno bootloader?