Go Down

Topic: Watchdog in Arduino Library - or at least support by bootloader (Read 33963 times) previous topic - next topic

retrolefty


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

drjiohnsmith

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.



bperrybap

#17
Nov 20, 2012, 08:46 am Last Edit: Nov 20, 2012, 08:48 am by bperrybap Reason: 1

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

westfw

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

drjiohnsmith

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

Nick Gammon

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).
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bperrybap


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


Nick Gammon


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 ...
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bperrybap

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


FernandoGarcia

I uploaded these examples in my arduino mega 2560 and was a fight to get remove them.
Conheça uma nova opção Open Source para controlar seu aquário: www.ferduino.com

bperrybap


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

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics



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.

retrolefty

#29
Jan 24, 2013, 04:09 pm Last Edit: Jan 24, 2013, 04:14 pm by retrolefty Reason: 1
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

Go Up