Using the Internal Watchdog Timer

I have an application that will ultimately run unattended in a remote location. Are there any examples of how to use the internal Atmel watchdog timer in the '328?

My first attempt had things timing out to where I had to re-flash the boot loader, etc.

Tim

http://www.nongnu.org/avr-libc/user-manual/group__avr__watchdog.html

also please pay attention to:

Note that for newer devices (ATmega88 and newer, effectively any AVR that has the option to also generate interrupts), the watchdog timer remains active even after a system reset (except a power-on condition), using the fastest prescaler value (approximately 15 ms). It is therefore required to turn off the watchdog early during program startup

Note that for newer devices (ATmega88 and newer, effectively any AVR that has the option to also generate interrupts), the watchdog timer remains active even after a system reset (except a power-on condition), using the fastest prescaler value (approximately 15 ms). It is therefore required to turn off the watchdog early during program startup

This most likely explains the problem I had. My system would never get out of the bootloader except for the very first time the program ran.

Has anyone ever modified a boot loader so it shuts off the WDT before it does anything else? That 15 mS is a killer.

Tim

i recommend you add the code underneath that quote to be added to the bootloader code, then recompile the bootloader code to fix this issue.

Okay. A new adventure! Off to find the bootloader source...

These things are really addictive.

maybe you should also edit your hardware/cores/arduino/main.cxx file and add the same thing

also, keep in mind, it looks like the bootloader will not boot if the reset is triggered by a watchdog timer

also, keep in mind, it looks like the bootloader will not boot if the reset is triggered by a watchdog timer

This could be more trouble than it is worth by the time it's all said and done. I'm also considering a full uP supervisor IC to watch for low voltage, etc. If I don't go that route perhaps once the whole project is fleshed out I'll just make it straight AVR-GCC code and not use the Arduino bootloader. Hope saying that is heresy here.

Tim

This could be more trouble than it is worth by the time it's all said and done.

That's probably right in your situation. However if would be nice if the WDT function stuff could be made 'Arduino safe' and understandable so that us software challenged types could actually use it without bricking our bootloaders. :wink:

Perhaps a nice library project for someone to contribute for us, hint hint anyone?

Lefty

That's probably right in your situation. However if would be nice if the WDT function stuff could be made 'Arduino safe' and understandable so that us software challenged types could actually use it without bricking our bootloaders. Wink

It's certainly a doable project. I'm pretty new to the Arduino environment and have never looked at the bootloader. I've been programming embedded systems for many years.

"If it were me..." I'd probably look at making a replacement boot loader that would first go and run the sketch whether the reset was caused by a watchdog timeout or not. Then rather than ever disabling the watchdog (Bad ju ju -- Murphy's Law says the system will only hang during those few milliseconds when you have the WDT disabled!) I'd see if you could just insert calls to the AVR-GCC wdt_reset() function in the bootloader so that even at the fastest watchdog reset period you would get past the bootloader and into the user's code. From there on it's up to the user as usual.

If the watchdog isn't being used calls to wdt_reset() wouldn't have any ill effects. Ideally this would get incorporated into the standard bootloader so with newer boards it would be a non issue.

If you're interested, Jack Ganssle has a great paper on watchdog timers in general and how to service them in software so that they only get reset when things are going well. You can download or read a copy here: Designing Great Watchdog Timers for Embedded Systems. The paper has example code that works very well with the GCC compiler and I've used his techniques in quite a few projects with great success.

Tim

Designing Great Watchdog Timers for Embedded Systems.

Wow, what a great and well written article. I worked in an oil refinery before retirement and was aware of watchdog functions in our process control systems.

Older systems we had mostly always used simple dedicated external hardware component based designs at first, but later designs morphed into two independent systems per operating areas, the control system (DCS) and a independent Safety Shutdown System (SSS). The SSS used triple redundant PLC type processors, independent internal and external UPS systems for power and were very very costly.

However unplanned plant outages could exceed $1MM/24 hour day, and danger of loss of life due to fire or explosions justified the expense, the plant must be placed in a safe state in any foreseen control situation. We would get safety system trips from time to time due to human error, but that was part of the cost of a safety first philosophy.

Frequent testing (usually monthly) of the safety system inputs were part of the normal operations and maintenance procedures. My job supporting these system made me very paranoid at times and the responsibility could be stressful. However I did survive to retirement and enjoying life. :wink:

Lefty