Jump to content

Recommended Posts

converted the watchdog example from Tivaware into Energia.

it should explain itself...

I tested this with the connected launchpad TM4C1294

 

5 blinks, resets itself, 5 blinks, on and on....

/*
6.11.2015 Watchdog example
*/

#define LED RED_LED

#include <stdint.h>
#include <stdbool.h>

#include "driverlib/sysctl.h"
#include "driverlib/watchdog.h"

unsigned long oldmillis;
unsigned int  timeout;
   
void setup() {                
  pinMode(LED, OUTPUT);     
  SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG0);
  WatchdogReloadSet(WATCHDOG0_BASE,120000000);
  WatchdogResetEnable(WATCHDOG0_BASE);
}

// the loop routine runs over and over again forever:
void loop() {
  if (millis()>(oldmillis+500)){
    oldmillis=millis();

    digitalWrite(LED, HIGH);   
    delay(100);               
    digitalWrite(LED, LOW); 
   
    timeout++;
    WatchdogReloadSet(WATCHDOG0_BASE,120000000);    //feed the dog, food for a second ;-)
 
 // simulated software crash: dog will not be fed any longer, causing a reset
    if (timeout>5){
      while(1);
    }
  }
}

Share this post


Link to post
Share on other sites

Answering a different thread, I just now discovered this thread. http://forum.43oh.com/topic/9240-watchdog-timer-for-tiva-tm4c1294ncpdt/

 

Below is a bit more extensive example of how to use the watchdog timer to reset the system using interrupts. The idea is that when you press USR_SW1 once, the watchdog will be starved and a system reset is triggered:

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/watchdog.h"
#include "driverlib/rom_map.h"
#include "driverlib/rom.h"

uint8_t feedWatchdog = true;
uint8_t ledState = true;
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 100;   // the debounce time; increase if the output flickers
int lastButtonState = HIGH; // the previous reading from the input pin

void setup()
{
  Serial.begin(115200);
  pinMode(PUSH1, INPUT_PULLUP);
  
  pinMode(GREEN_LED, OUTPUT);
  digitalWrite(GREEN_LED, HIGH);

  // Enable the watchdog peripheral
  MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_WDOG0);
  // Set timeout of watchdog to 2 sec
  MAP_WatchdogReloadSet(WATCHDOG0_BASE, MAP_SysCtlClockGet() * 2);
  // Reset when watchdog expires
  MAP_WatchdogResetEnable(WATCHDOG0_BASE);
  // Register the watchdog interrupt handler
  WatchdogIntRegister(WATCHDOG0_BASE, &WatchdogIntHandler);
  // Enable the watchdog
  MAP_WatchdogEnable(WATCHDOG0_BASE);
  
  // Get and print the reset cause
  if(SysCtlResetCauseGet() & SYSCTL_CAUSE_WDOG0) {
    Serial.println("WatchDog Reset");
  }else if(SysCtlResetCauseGet() & SYSCTL_CAUSE_EXT) {
    Serial.println("External reset");
  }
  
  SysCtlResetCauseClear(SYSCTL_CAUSE_WDOG0 | SYSCTL_CAUSE_EXT);
}

void loop()
{
  // Read the button that triggers starving the Watchdog
  int reading = digitalRead(PUSH1);

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  } 
  
  if (((millis() - lastDebounceTime) > debounceDelay) && lastButtonState == LOW) {
    // If the button has been low for longer than the debounce time, starve the Watchdog.
    feedWatchdog = false;
  }
  
  // save the reading.  Next time through the loop,
  // it'll be the lastButtonState:
  lastButtonState = reading;
}

void WatchdogIntHandler(void)
{
    //
    // If we have been told to stop feeding the watchdog, return immediately
    // without clearing the interrupt.  This will cause the system to reset
    // next time the watchdog interrupt fires.
    //
    if(!feedWatchdog)
    {
        return;
    }

    //
    // Clear the watchdog interrupt.
    //
    WatchdogIntClear(WATCHDOG0_BASE);
    digitalWrite(GREEN_LED, ledState);
    ledState = !ledState;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×