GeppettoLab
Active member
Hi,
I'm triyng to add a watchdog timer to my project using a Teensy LC.
I followed the instruction of this thread https://forum.pjrc.com/threads/25370-Teensy-3-0-Watchdog-Timer using this code
and also using the Basic Usage example of the Adafruit_SleepyDog Library in the Paul's version https://github.com/PaulStoffregen/Adafruit_SleepyDog
but both doesn't works.
The first one start but the LED remain high and the sketch freeze after WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
Using the Sleepy library the device seems to disconnect it repeteadly every second even just including the library without enabling the timer.
Instead, both is perfectly working on Teensy 3.2 and 3.5.
There is some explanation that I missed reading the thread for the LC?
Thanks
Luca
I'm triyng to add a watchdog timer to my project using a Teensy LC.
I followed the instruction of this thread https://forum.pjrc.com/threads/25370-Teensy-3-0-Watchdog-Timer using this code
Code:
// This sample program shows how to use the watch dog timer (WDT).
// When program starts, it turns LED off for 1 seconds.
// It then turns LED on, and pauses for 5 seconds letting you know it is ready to start.
// WDT is initialized, and loop starts. It will flash led and reset wdt for the first 10 secs.
// After that, it stops resetting wdt. This causes the WDT to reboot and the cycle starts over.
const int led = 13;
bool ledState = false;
unsigned long timer;
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
// Indicate we are starting over by hold led off for 1s
ledState = false;
digitalWrite(led, ledState);
delay(1000UL);
// Indicate we are in setup by hold LED on
ledState = true;
digitalWrite(led, ledState);
delay(5000UL);
// Setup WDT
noInterrupts(); // don't allow interrupts while setting up WDOG
WDOG_UNLOCK = WDOG_UNLOCK_SEQ1; // unlock access to WDOG registers
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
delayMicroseconds(1); // Need to wait a bit..
// for this demo, we will use 1 second WDT timeout (e.g. you must reset it in < 1 sec or a boot occurs)
WDOG_TOVALH = 0x006d;
WDOG_TOVALL = 0xdd00;
// This sets prescale clock so that the watchdog timer ticks at 7.2MHz
WDOG_PRESC = 0x400;
// Set options to enable WDT. You must always do this as a SINGLE write to WDOG_CTRLH
WDOG_STCTRLH |= WDOG_STCTRLH_ALLOWUPDATE |
WDOG_STCTRLH_WDOGEN | WDOG_STCTRLH_WAITEN |
WDOG_STCTRLH_STOPEN | WDOG_STCTRLH_CLKSRC;
interrupts();
}
void loop() {
timer = millis() + 10000UL; // length of time we will reset WDT
while (true) {
ledState = !ledState;
digitalWrite(led, ledState);
delay(100UL);
if (millis() < timer) { // Have we timed out yet?
noInterrupts(); // No - reset WDT
WDOG_REFRESH = 0xA602;
WDOG_REFRESH = 0xB480;
interrupts();
}
} // while
}
Code:
// Adafruit Watchdog Library Basic Usage Example
//
// Simple example of how to use the watchdog library.
//
// Author: Tony DiCola
#include <Adafruit_SleepyDog.h>
void setup() {
Serial.begin(115200);
while(!Serial); // wait for Arduino Serial Monitor (native USB boards)
Serial.println("Adafruit Watchdog Library Demo!");
Serial.println();
// First a normal example of using the watchdog timer.
// Enable the watchdog by calling Watchdog.enable() as below.
// This will turn on the watchdog timer with a ~4 second timeout
// before reseting the Arduino. The estimated actual milliseconds
// before reset (in milliseconds) is returned.
// Make sure to reset the watchdog before the countdown expires or
// the Arduino will reset!
int countdownMS = Watchdog.enable(4000);
Serial.print("Enabled the watchdog with max countdown of ");
Serial.print(countdownMS, DEC);
Serial.println(" milliseconds!");
Serial.println();
// Now loop a few times and periodically reset the watchdog.
Serial.println("Looping ten times while resetting the watchdog...");
for(int i = 1; i <= 10; ++i) {
Serial.print("Loop #"); Serial.println(i, DEC);
delay(1000);
// Reset watchdog with every loop to make sure the sketch keeps running.
// If you comment out this call watch what happens in about 4 iterations!
Watchdog.reset();
}
Serial.println();
// Disable the watchdog entirely by calling Watchdog.disable();
Watchdog.disable();
// Finally demonstrate the watchdog resetting by enabling it for a shorter
// period of time and waiting a long time without a reset. Notice you can
// pass a _maximum_ countdown time (in milliseconds) to the enable call.
// The library will try to use that value as the countdown, but it might
// pick a smaller value if the hardware doesn't support it. The actual
// countdown value will be returned so you can see what it is.
countdownMS = Watchdog.enable(4000);
Serial.print("Get ready, the watchdog will reset in ");
Serial.print(countdownMS, DEC);
Serial.println(" milliseconds!");
Serial.println();
delay(countdownMS+1000);
// Execution will never get here because the watchdog resets the Arduino!
}
void loop() {
// We'll never actually get to the loop because the watchdog will reset in
// the setup function.
Serial.println("You shouldn't see this message.");
delay(1000);
}
The first one start but the LED remain high and the sketch freeze after WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
Using the Sleepy library the device seems to disconnect it repeteadly every second even just including the library without enabling the timer.
Instead, both is perfectly working on Teensy 3.2 and 3.5.
There is some explanation that I missed reading the thread for the LC?
Thanks
Luca