Author Topic: How to setup a Watchdog timer?  (Read 3611 times)

hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
How to setup a Watchdog timer?
« on: June 26, 2007, 05:33:53 AM »
I want to setup a Watchdog timer when the program start so that if something wrong in the program (or ethernet board), it can automatically reset the ethernet board.

How to do???

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #1 on: June 26, 2007, 02:53:18 PM »
create a global flag 

 int Alive;

In time interrupt add
Code: [Select]
 
  // RESET BOARD IF MAIN CYCLE IS NOT RUNNING
  if (Alive==1)   // Check if Main is Running
    {Alive = 0;}
   else
    {Alive = Alive + 2;}
   if (Alive > 250)  // check 1 sec of refresh
    {Reset();}

and in main add somewhere
Code: [Select]
    Alive = 1;

modtro2

  • Administrator
  • Hero Member
  • *****
  • Posts: 536
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #2 on: June 26, 2007, 05:35:00 PM »
As you might know the PIC microcontrollers have built in hardware watchdog timer peripherals. They have to be configured with the configuration bits. When using the bootloader on the SBC65EC and SBC68EC boards, the configuration bits can not be changed. The configuration bits are programmed together with the bootloader. When updating the firmware (or writing your own code) via the Modtronix Network Bootloader, you can not change the configuration bits. The configuration bits on both the SBC65EC and SBC68EC boards have been set up so the watchdog timer can be enabled by using the SWDTEN bit.

On the SBC65EC the watchdog timer has been configured as follows:
Watchdog disabled (SWDTEN controlled), Watchdog prescaler = 128. This gives: min=435ms, typ=512ms, max=589ms

On the SBC68EC the watchdog timer has been configured as follows:
Watchdog disabled (SWDTEN controlled), Watchdog prescaler = 32. This gives: min=224ms, typ=576ms, max=1056ms

This means, that by setting the SWDTEN bit in the WDTCON register, the watchdog timer will be enabled. After enabling the WDT, the CLRWDT() function must be called at a regular basis. If not called for 435ms (min) for the SBC65EC, or 224mm (min) for the SBC68EC, the board will reset. This has already been done in the Modtronix Web Server code via the FAST_USER_PROCESS() macro in the "projdefs.h" file. This FAST_USER_PROCESS() macro is called in many places in the code where the program could delay for more then the WDT configured times.

hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #3 on: June 26, 2007, 09:40:22 PM »
In time interrupt add

Code: [Select]
 
  // RESET BOARD IF MAIN CYCLE IS NOT RUNNING
  if (Alive==1)   // Check if Main is Running
    {Alive = 0;}
   else
    {Alive = Alive + 2;}
   if (Alive > 250)  // check 1 sec of refresh
    {Reset();}

 :? I want to know where should i add the above code? In which .c file? I don't know what does it mean by time interrupt.

and in main add somewhere
Code: [Select]
    Alive = 1;

 :? does it mean i add Alvie = 1 in mxwebsrvr.c, inside the main loop?

Thank you.

hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #4 on: June 26, 2007, 09:44:22 PM »

This means, that by setting the SWDTEN bit in the WDTCON register, the watchdog timer will be enabled. After enabling the WDT, the CLRWDT() function must be called at a regular basis. If not called for 435ms (min) for the SBC65EC, or 224mm (min) for the SBC68EC, the board will reset. This has already been done in the Modtronix Web Server code via the FAST_USER_PROCESS() macro in the "projdefs.h" file. This FAST_USER_PROCESS() macro is called in many places in the code where the program could delay for more then the WDT configured times.

 :? I am using SBC65EC, i want to know how to set the SWDTEN bit in the WDTCON register? And how to call the CLRWDT() function at a regular basis?

I have put the below codes in InitializeBoard() Function, but the board still die (the LED become dim and program stop) and cannot automatically reset.

Code: [Select]
WDTCON = 0b00000001;

Since i am just a beginner, i hope that you can talk more detailed. Thank you very much for your help!
« Last Edit: June 26, 2007, 11:06:54 PM by hung »

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #5 on: June 27, 2007, 03:19:51 PM »
in mxwebsrvr.c
1)find
Code: [Select]
static void ProcessIO(void);
and add
Code: [Select]
int Alive;

2)find
Code: [Select]
    //TMR0 is used for the ticks
    if (INTCON_TMR0IF)
    {
        TickUpdate();
        #if defined(STACK_USE_SLIP)
        MACISR();
        #endif
        INTCON_TMR0IF = 0;
    }
and after TickUpdate(); add
Code: [Select]
  // RESET BOARD IF MAIN CYCLE IS NOT RUNNING
  if (Alive==1)   // Check if Main is Running
    {Alive = 0;}
   else
    {Alive = Alive + 2;}
   if (Alive > 250)  // check 1 sec of refresh
    {Reset();}
3)find
Code: [Select]
    /*
     * Once all items are initialized, go into infinite loop and let
     * stack items execute their tasks.
     * If application needs to perform its own task, it should be
     * done at the end of while loop.
     * Note that this is a "co-operative mult-tasking" mechanism
     * where every task performs its tasks (whether all in one shot
     * or part of it) and returns so that other tasks can do their
     * job.
     * If a task needs very long time to do its job, it must broken
     * down into smaller pieces so that other tasks can have CPU time.
     */
    while(1)
    {
 
and add
Code: [Select]
Alive = 1;

Done.

hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #6 on: June 27, 2007, 10:28:15 PM »
in mxwebsrvr.c
1)find
Code: [Select]
static void ProcessIO(void);
and add
Code: [Select]
int Alive;

2)find
Code: [Select]
    //TMR0 is used for the ticks
    if (INTCON_TMR0IF)
    {
        TickUpdate();
        #if defined(STACK_USE_SLIP)
        MACISR();
        #endif
        INTCON_TMR0IF = 0;
    }
and after TickUpdate(); add
Code: [Select]
  // RESET BOARD IF MAIN CYCLE IS NOT RUNNING
  if (Alive==1)   // Check if Main is Running
    {Alive = 0;}
   else
    {Alive = Alive + 2;}
   if (Alive > 250)  // check 1 sec of refresh
    {Reset();}


I cannot define int Alive in ProcessIO because error ocuur, saying that the variable is not found.

When I define it as global variable and follow the step you said, the board cannot work properly, I think there are something wrong... :oops:


hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #7 on: June 27, 2007, 10:38:58 PM »

On the SBC65EC the watchdog timer has been configured as follows:
Watchdog disabled (SWDTEN controlled), Watchdog prescaler = 128. This gives: min=435ms, typ=512ms, max=589ms

This means, that by setting the SWDTEN bit in the WDTCON register, the watchdog timer will be enabled. After enabling the WDT, the CLRWDT() function must be called at a regular basis. If not called for 435ms (min) for the SBC65EC, or 224mm (min) for the SBC68EC, the board will reset. This has already been done in the Modtronix Web Server code via the FAST_USER_PROCESS() macro in the "projdefs.h" file. This FAST_USER_PROCESS() macro is called in many places in the code where the program could delay for more then the WDT configured times.

 :? How can i change the reset time for Watchdog Timer?? Does it means by default, the reset time for SBC65EC is 224 mins??

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #8 on: June 28, 2007, 01:54:41 PM »
Quote

I cannot define int Alive in ProcessIO because error ocuur, saying that the variable is not found.

When I define it as global variable and follow the step you said, the board cannot work properly, I think there are something wrong... :oops:


NOT INSIDE ProcessIO function, but just after declaration at begin of file!!! If you declare inside function, it's not visible outside!
 
Anyway it works since last year in my board. If you need something different follow a different way.

Regards

hung

  • Sr. Member
  • ****
  • Posts: 26
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #9 on: June 29, 2007, 09:56:16 PM »
By changing the refresh time from (Alive > 250) to (Alive>25000), I successfully added the watchdog timer.

Thank you very much OmarZ!  :-)

OmarZ

  • Hero Member
  • *****
  • Posts: 243
    • View Profile
Re: How to setup a Watchdog timer?
« Reply #10 on: June 30, 2007, 04:17:40 AM »
By changing the refresh time from (Alive > 250) to (Alive>25000), I successfully added the watchdog timer.

Thank you very much OmarZ!  :-)
yes, 250 is good for me, but if you need longer time, increase it as you like.
Regards