Thread: Watchdog Timers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    3

    Watchdog Timers

    Hi, I'm very new to Programming, C in particular, so please be gentle! I am trying to understand watchdog timers since it is part of my course, and I'm not too sure that I can really grasp it. I have some example code that was written by the lecturer, and I was hoping that someone would be able to put some of it into really stupid person's terms.

    Basically, we have to create an application that executes a single task:

    1. This task creates a timer and sets it for 10 seconds.
    2. Then this task periodically checks the value of some flag.
    3. Once the timer expires, its call-back function should set the flag informing the main
    task that the timer has expired.
    4. Once the main task detects the flag set, it should print an appropriate message to the
    user.

    OK so below is the code that the lecturer has written:

    Code:
    char flag = 0;		/* Flag telling when the timer goes off */
    WDOG_ID timerID;	/* Timer ID */
    
    
    /* This is the timer callback function
     * It gets called when the timer expires 
     */
    void timerCallback() {
        /* Set the flag */
        flag = 1;
    }
    
    
    /* This function is executed by the main task
     * It sets the timer, then check the value of the flag once per second
     * to see when the timer expires and prints the corresponding message
     */
    STATUS main() {
        /* Create the watchdog timer */
        if ((timerID = wdCreate()) == NULL) {
            printf("Cannot create timer!!! Terminating this task...");
            exit(0);
        }
    
        /* Start the timer to expire in 10 seconds and call function "timerCallback" */
        if (wdStart(timerID, 10 * sysClkRateGet(), (FUNCPTR)timerCallback, 0) == ERROR) {
            printf("Cannot start the timer!!! Terminating...");
            exit(0);
        }
    
        /* Check the flag value once per second */
        while (1) {
            printf("Checking the flag value...");
            if (flag != 0) {
               printf("Timer expired!!!\n");
               break;
            }
            printf("Still waiting for the timer\n");
            taskDelay(1 * sysClkRateGet());
        }   
    }
    1. First of all, I do not understand the CHAR FLAG = 0 line. What exactly does this mean? Does it mean that the initial value of the flag is 0 until the timerCallBack function is called (at which time it becomes a 1)?

    2. Next I am not too sure about the main function. At first I thought it was a standard line of code for creating a watchdog timer, but why then is it instructed to print that the timer cannot be created? ALSO is exit(0) similar to RETURN (0)?

    3. OK understand wdstart (delay time, number of ticks to delay, C function to call and argument), but I don't understand the ==ERROR bit. Is ERROR a standard C keyword then? And if so, is this line just testing the condition of wdstart to see if an error condition exists and if so, the following line is printed?

    I'm happy with the last bit of code.

    I would really appreciate it if someone could help me out because I'm really struggling to understand C. I know that most of you probably think this is Mickey Mouse stuff, but I would be really grateful for any help anyone can offer.

    I've checked this forum for any clues as well as the vxWorks Kernel Programmers' Guide, and understand a bit, but not everything..... PLEASE HELP!!

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by el_timmo View Post
    I've checked this forum for any clues as well as the vxWorks Kernel Programmers' Guide, and understand a bit, but not everything..... PLEASE HELP!!
    Read this book.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    thanks a lot for the quick reply, having a look now!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by el_timmo View Post
    I would really appreciate it if someone could help me out because I'm really struggling to understand C. I know that most of you probably think this is Mickey Mouse stuff, but I would be really grateful for any help anyone can offer.
    I'm pretty sure most people won't think it's "mickey mouse stuff" but it is first week beginner stuff. So unless this is your first week... and even if it is ... I strongly recommend you hit the books and do some actual studying on C... There a tons of tutorials and ebooks and proabaly just as many real books you can read about this stuff in.

    C Book Recommendations

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Question 3: Read the documentation

    RETURNS

    OK, or ERROR if the watchdog timer cannot be started.
    OK and ERROR are probably #defines somewhere in a header file.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    3
    thanks for your input everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Efficient timer mechanism for many timers
    By ShwangShwing in forum C Programming
    Replies: 9
    Last Post: 07-28-2009, 11:52 AM
  2. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  3. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 04:45 PM
  4. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM