Jump to content
43oh

Watchdog Timer for TIVA TM4C1294ncpdt


Recommended Posts

Maybe try

WDTCTL = 0;

Example sketch working on a G2553 ... don't know if also working on your device:

void setup() {
  Serial.begin(9600);
  Serial.println("START");
}

int i=0;

void loop() {
  
  Serial.println(i++);
  delay(500);
  if(i==5) WDTCTL = 0 ;
}
Link to post
Share on other sites

 

@@yosh I tried compiling the above code with g2553 still it gives me an error:

 

 

sketch_jan07b.cpp:0:1: error: 'WDTCTL' does not name a type

 

 

Just this code:

void setup() {
  Serial.begin(9600);
  Serial.println("START");
}

int i=0;

void loop() {
  
  Serial.println(i++);
  delay(500);
  if(i==5) WDTCTL = 0 ;
}

First code segment (WDTCTL = 0) was just for information. So at least the code above should compile for G2553 ...

 

Edit: You can have a look HERE too. The example is more TM4C specific ...

Edited by yosh
Link to post
Share on other sites

also, you can use the ROM_ version of those TivaWare API calls and save a tiny amount of flash/code space.  Most of the driverlib API ships within the chip's ROM to save you some space.

 

Full documentation on TivaWare's Peripheral Driver Library, where those watchdog config functions came from: http://www.ti.com/lit/pdf/spmu298

Link to post
Share on other sites

Below is an example of how to use the watchdog timer to reset the system. 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;
}

Also see this thread: http://forum.43oh.com/topic/8984-watchdog/

Link to post
Share on other sites
  • 3 months later...
  • 4 months later...

Depends on how fast you're running the core clock.

 

Example: For a clock frequency of 120 MHz, (120,000,000 Hz) it will take 120,000,000 'ticks' to elapse 1 second of real world time. 

 

All you're doing with 

WatchdogReloadSet(WATCHDOG0_BASE, MAP_SysCtlClockGet() * 2);

is setting the load value of the watchdog timer, which runs at the system clock frequency. A watchdog is geared more towards recovery from catastrophic failure in embedded systems. I would recommend using a software reset for as many cases as you can muster. Otherwise, you could set up a basic timing system like the following: (Note that I use floating point on my Tiva 'cuz... Well why not! Haven't run into memory issues yet...)

 

volatile uint32_t AppStartTime;  // Application Start time in microseconds
volatile uint32_t AppEndTime;  // Application End time in microseconds
volatile uint32_t AppRuntime;  // Application Run time in microseconds
// Take Application Start Time (Whenever you feel is necessary, you could do it in setup() or loop() if you wanted)
AppStartTime = millis();


// Blah blah program code here ...


// Take Application End Time
AppEndTime = millis();


// Buffer for snprintf
char ptimeBuf[6]; 


// Calc Run Time
AppRuntime = AppEndTime - AppStartTime;


// Convert to Seconds
float32_t AppRuntimeSecs = AppRuntime / 1000.0;  


// Parse Float into Char Buffer
snprintf(ptimeBuf, 6, "%.2f", AppRuntimeSecs);


// This is a custom wrapper I made combining Serial.print() and UARTprintf() functionality... And then some. 
// Probably just use UARTprintf() in place of term.println()
// Also note the ANSI terminal escape codes. I like coloring output. I set up some eclipse hotkeys to make for 
// some groovy/easy function timing. Colors really help with tons of output.
term.println("Complete Program Time (Main Loop Entry -> Finish: \e[38;5;220;1;4m%s Seconds\033[m", ptimeBuf);

Anyhow, if your entire application takes for instance an average of 30 seconds, and the code is frozen, why not set the watchdog at something like 1 min. 30 secs?

 

Link to post
Share on other sites
  • 3 years later...

@energia

can i ask a question about the watchdog code that you have posted.

Would mind telling me how the WatchdogIntHandler interrupt function was called ? I understand that feedwatchdog variable is use to determine the state within the WatchdogIntHandler function if the watchdog should reset or not. But i am kinda of confused by how the WatchdogIntHandler interrupt function is being called ? Is it through the watchdogreloadset() where once 2s is up, it will automatically trigger WatchdogIntHandler function ?

  // Register the watchdog interrupt handler
  WatchdogIntRegister(WATCHDOG0_BASE, &WatchdogIntHandler);
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;
}

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...