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!!