TVG

Watchdog Timer for TIVA TM4C1294ncpdt

13 posts in this topic

Can someone provide a code for implementing a watchdog timer to autoreset in TIVA TM4C1294ncpdt using energia?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Did you include any files for the above code? @@yosh

 

No ... on a 2553 the sketch above is working as it is. No additional includes etc. ...

Share this post


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

Share this post


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
TVG likes this

Share this post


Link to post
Share on other sites

@@yosh Ok, It worked with 2553, I get the following error with TIVA:
 

sketch_jan07b.cpp: In function 'void loop()':
sketch_jan07b.cpp:14:12: error: 'WDTCTL' was not declared in this scope

Share this post


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

Fmilburn likes this

Share this post


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/

bluehash likes this

Share this post


Link to post
Share on other sites

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?

 

energia likes this

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