Pages: [1] 2 3 4   Go Down
Author Topic: Watchdog in Arduino Library - or at least support by bootloader  (Read 25158 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2883
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Per Arduino ad Astra

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
... it seems to be very complicated using Arduino 1.0.1 ...

Doesn't seem complicated to me:

Code:
#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.
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16542
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And watchdog is missing in the libraries.

What do you understand by the word "libraries"?

Code:
#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.
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2346
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And watchdog is missing in the libraries.

What do you understand by the word "libraries"?

Code:
#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
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16542
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 106
Posts: 6378
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 327
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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,
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2346
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

Offline Offline
Sr. Member
****
Karma: 3
Posts: 327
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Dallas, TX USA
Offline Offline
Edison Member
*
Karma: 47
Posts: 2346
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1] 2 3 4   Go Up
Jump to: