Saturday, November 28, 2009

Should we enable or disable watch dog during the startup/boot code executes??

Can Anyone tell me whether we should enable or disable watch dog during the startup/boot code executes?My friend told me that we usually disable watch dog in the boot code. Can anyone one tell me what is the advantage or disadvantage of doing so??


Answers:

1.Usually the WD (watchdog) is enabled after the boot-up procedure, because this is when the program enters its "loop" and periodically kicks the WD. During boot-up, by which I suppose you mean linear initialization of hardware and peripherals, there's much less periodicity in your code and hard to insert a WD kicking cycle.

2.If you're debugging, you want it off or the device will reboot on your when you try to step through code. Otherwise it's up to you. I've seen watchdogs save projects' butts and I've seen watchdogs lead to inadvertent reboot loops that cause customers to clog up the support lines and thus cost the company a ton.

3.It really depends on your project. The watchdog is there to help you ensure that your program won't get "stuck" while executing code. -- If there is a chance that your program may hang during the boot-procedure, it may make sense to incorporate the watchdog there too. That being said, I generally start the watchdog at the end of my boot-up procedures.

4.Production code should always enable the watchdog. Hobby and/or prototype projects are obviously a special case that may not require the watchdog.If the watchdog is enabled during boot, there is a special case which must be considered. Erasing and writing memory take a long time (erasing an entire device may take seconds to complete). So you must insure that your erase and write routines periodically service the watchdog to prevent a reset.

There are some contradictions here with answers which i recieved at stackoverflow.I will update,incase i get some more informations on the same.

A Discussion on Watch Dog Timer


This is a chat transcript of my discussion with one of my friend.

8 minutes
2:41 AM xyz: yes
2:42 AM how r u
where r u
me: i have one question related to wdt
xyz: what is wdt
me: watch dog timer
xyz: ok
2:43 AM me: ya...actually when any hardware boots up....can watchdog reset happen in the boot code itself due to anythin
xyz: can happen
2:44 AM if WDT is not disabled during bootup
Normally the Boot code disables it
I mean the Boot strap code
me: ok...in case wdt is enabled..then if time out happens then the board will power down , i guess
ok
xyz: yes
it is a cold reset
2:45 AM There are two things
Boot strap code
Boot Loader
me: ok
boot loader loads the code ??
xyz: Boot strap code disables it
Boot loader enables it
me: ok
2:46 AM ....one more doubt...can we control the real time clock's registers while boot code excutes?...
xyz: Boot strap ->First Level Boot loader ->Second level boot laoder(optional_->OS->APPLICATION
2:47 AM me: ok
can we control the real time clock's registers while boot code excutes?...
xyz: k
yes
u can make the WDT Count Large enough
2:48 AM so that reset wont happen
me: in the bootstrap code ?? [ I have very little knowledge of boot code ]
xyz: in what ever start up code , u have access to
2:49 AM me: yes....i got it
thanks man for the..help..
xyz: Np
me: ok...chat with u later
byee
2:50 AM xyz: bye

Sunday, November 15, 2009

C Skills : tutor 7 : Function Pointer - Part 2

An Example on Function Pointer


#include "stdio.h"

//Declaration of Function Pointer

int (*funcptr)(int,int); // This is a Global Variable

//Declaration of Functions

int add(int,int);

int sub(int,int);

int mul(int,int);

int (*register_functionpointer(int cmd))(int num1,int num2);//Note this is a function's declaration not a pointer

int main()

{

int(*fptr)(int,int) = NULL;

int cmd,n1,n2,result;

printf("enter the first number\n");

scanf("%d",&n1);

printf("\nenter the second number\n");

scanf("%d",&n2);

printf("\nEnter Command : 1 for Addition,2 for subtraction and 3 for multiplication,4 to exit\n");

scanf("%d",&cmd);

if(cmd == 4)

printf("\nGood Byee....Exit\n");

fptr=register_functionpointer(cmd);

result = fptr(n1,n2);

printf("The result is %d\n",result);

return 0;

}

int (*register_functionpointer(int cmd))(int num1,int num2)

{

switch(cmd)

{

case 1:funcptr = add ;

break;

case 2:funcptr = sub ;

break;

case 3: funcptr = mul;

break;

default:

printf("\ninvalid input\n");

}

return funcptr;

}

int add( int n1,int n2)

{

return(n1+n2);

}

int mul( int n1,int n2)

{

return(n1*n2);

}

int sub( int n1, int n2)

{

if(n1 > n2 )

return(n1-n2);

if(n2 >n1 )

return(n2-n1);

if(n1 == n2)

{

printf("\nBoth numbers are same\n");

return 0;

}

}

Sunday, November 1, 2009

C Skills : tutor 6 : Function Pointers - Part1

Function Pointers are pointers, i.e. variables, which point to the address of a function. You must keep in mind,that a running program gets a certain space in the main-memory. Both, the executable compiled program code and the used variables, are put inside this memory. Thus a function in the program code is, like e.g. a character field, nothing else than an address. It is only important how you, or better your compiler/processor interpret

the memory a pointer points to.

What is “Pointer to function”?Answer : Actually Pointer to Function and Function Pointer are same.


Step1:Declare/Define a Function Pointer

Since a function pointer is nothing else than a pointer variable, it must be defined as usual. In the following example we define two function pointers named pt2Function. They point to functions,
which take one float and two char and return an int.
define a function pointer and initialize to NULL

int (*pt2Function)(float, char, char) = NULL;

We may or may not assign NULL, However Its better.

Step2:Assign an Address to a Function Pointer

It’s quite easy to assign the address of a function to a function pointer. You simply take the name of a suitable and known function or member function. Although it’s optional for most compilers you should use the address operator & infront of the function’s name in order to write portable code.


int funtion1(float a, char b, char c)

{

printf("function1\n");

return (2a+2b+3c) ;

}

pt2Function = function1;