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;


Wednesday, September 16, 2009

FIQ and IRQ interrupt

The difference between FIQ and IRQ interrupt system in any microprocessor...Eg:ARM926EJ

Ans1. A method of performing a fast interrupt in a digital data processor having the capability of handling more than one interrupt is provided. When a fast interrupt request is received a flag is set and the program counter and condition code registers are stored on a stack. At the end of the interrupt servicing routine the return from interrupt instructions retrieves the condition code register which contains the status of the digital data processor and checks to see whether the flag has been set or not. If the flag is set it indicates that a fast interrupt was serviced and therefore only the program counter is unstacked.

In other words, an FIQ is just a higher priority interrupt request, that is prioritized by disabling IRQ and other FIQ handlers during request servicing. Therefore, no other interrupts can occur during the processing of the active FIQ interrupt.

Ans 2. An FIQ interrupt is higher priority than an IRQ, it has its own mode (and therefore r13, r14 and spsr) and mask bit (CPSR[6]), which allows it to be taken while an IRQ is being handled - the processor can store enough state to enable the programmer to return to the IRQ handler after handling the FIQ. Further, the extra banked registers and the address of the FIQ vector
allows the programmer to build systems for which the interrupt latency for an FIQ is less than for an IRQ.

In a typical system there will be one FIQ (the highest priority or most critical interrupt) sources and a number of IRQ sources, arbitrated by an interrupt handler.

Please check out my another post in the same blog.

UART port and a serial port in a computer

What is the difference, if any, between a UART port and a serial port in a computer?

UART is the abbreviation of Universal Asynchronous Receiver Transmitter, the name of the chip that enables the computer to communicate via a serial line (eg. RS-232, RS-485, RS-422).

The serial port is the RS-232 interface (internally connected to the UART) of the computer.

Regarding interrupt based communication

We have a simple architecture :

  1. Main chip (arm9 based)
  2. PIC controller

The PIC communicates to ARM via an interrupt based I2C communication protocol for transfer of data. Inside the interrupt we signal a task which reads the data from the I2C layer (bus).

In case the data is limited we usually won't have much problem to read the data and send it to upper layer. In case this data is very huge the interrupt will be tied for a long time.

how to avoid the same? ...or can we a different solution?


Ans 1: Have some kind of 'worker thread', sometimes called a kernel thread, whose job it is to pull data out of the I2C interface and buffer it, hand it off to other parts of your system, etc. Use the interrupt routine only to un-block the kernel thread. That way, if there are other duties the system has to perform, it is not prevented from doing so by the interrupt handler, and you still get your data in from your device in a timely manner.

Ans 2: You shouldn't read a complete packet in one execution of the interrupt routine. Depending on the hardware support you should handle one sample/bit/byte, store data in a buffer and only signal the task when the packet is complete.

Why I couldnt work with the gcc compiler without ‘\n’ in printf?

I have written a printf() statement like below:

printf("hello\n");

this works fine when built using Linux' gcc compiler. However if I write

printf("hello");

the print doesn't appear on the screen. There is some buffering mechanism it seems?


Answer :

Even if buffering isn't a problem, if you don't print the newline your shell's prompt might be clobbering the output.

you are for example using gcc in a unix shell and at the end of your program do printf("hello") it won't print a newline before your shells prompt is displayed. The prompt will be printed on that same line, sometimes overwriting the entire line depending on kind of prompt you have set up.


Sunday, September 6, 2009

C skills : tutor 5: StrStr Library function





char *strstr(char *string1, char *string2);


Description:

The strstr function locates the first occurrence of the string string2 in the string string1 and returns a pointer to the beginning of the first occurrence.

Return Value

The strstr function returns a pointer within string1 that points to a string identical to string2. If no such sub string exists in src a null pointer is returned.



/* user defined strstr */

#include "stdio.h"


char * strstr_ud(char *, char *);

int main()
{

char s1 [] = "This House is Nice";
char s2 [] = "My Car is White";

printf("Returned String 1: %s\n", strstr_ud(s1, "House"));
printf("Returned String 2: %s\n", strstr_ud(s2, "Car"));
return 0;

}

char * strstr_ud(char * str1, char * str2)
{

char *temp1=NULL;
char *temp2=NULL;

temp2 = str2;
while(*str1++ != '\0')
{

if(*str2 == *str1)
{
temp1 = str1;

while(*str2 != '\0')
{
if(*str2 != *temp1)
{
flag = 1;
}
else
{
flag = 0;
}
str2++;temp1++;
}

if(flag == 0)
{
return str1;
}
else
{
str2 = temp2;
}

}

}

return NULL;

}


Output
House is Nice
Car is White

Friday, September 4, 2009

C skills : tutor 4: Reversal of Linklist using Recursion

#include"stdio.h"

typedef struct node
{
int data;
struct node *next;

} NODE;

NODE * reverse( NODE *);
void print_list(NODE *);
NODE *insert_node(NODE *);

main()
{
NODE * front = NULL;
char ch;


printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
while(1)
{


scanf("%c",&ch);


switch(ch)
{

case '1':front=insert_node(front);
printf("\n====Node is inserted===\n");
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;

case '2':print_list(front);
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;

case '3':front = reverse(front);
printf("\n====Node is reversed===\n");
printf("\nEnter 1:insert,2:print,3:reverse,4:exit\n");
break;
case '4':free(front);
return(0);


};

}

}


NODE * insert_node(NODE * ptr)
{

NODE * newnode ;
int newnode_data;
NODE *temp=ptr;
NODE *front=NULL;


/* INSERT FROM FRONT*/
if(ptr == NULL)
{
ptr = (NODE *)malloc(sizeof(NODE *));
printf("\n=====Enter the data for the first newnode ======\n");
scanf("%d",&newnode_data);
ptr->data = newnode_data;
ptr->next = NULL;
front = ptr;
}
else
{
newnode = (NODE *)malloc(sizeof(NODE *));
printf("\n=====Enter the data for the newnode ======\n");
scanf("%d",&newnode_data);
newnode->data = newnode_data;
newnode->next = ptr;
front = newnode;
}
return front;

}

void print_list(NODE *ptr)
{

NODE *temp_ptr=ptr;
int count=0;

if(temp_ptr == NULL)
{
printf("\nNo list to print \n");
return;
}

while(temp_ptr != NULL)
{

printf("\nnode [%d] : data [%d]\n",count,temp_ptr->data);
temp_ptr=temp_ptr->next;
count++;
}



return;
}


NODE * reverse( NODE * root )
{
NODE * ret_val;

if ( root == NULL || root->next == NULL )
return root;

ret_val = reverse(root->next);

(root -> next)->next = root;
root->next = NULL;

return ret_val;
}

C Skills : Tutor -3 : String Copy

/*string cpy */

#include "stdio.h"

char *stringcpy(char *str_des,const char *str_src);

int main()
{

char str_des[40];
char str_src[20];

printf("\nEnter the first string\n");
scanf("%s",str_des);

printf("\nEnter the second string\n");
scanf("%s",str_src);

stringcpy(str_des,str_src);
printf("\nstring copy is done:The first string is : %s \n", str_des);

return(0);

}

char *stringcpy(char *str_des,const char *str_src)
{
char *temp= str_des;
while(*temp!='\0')temp++;
while(*str_src != '\0')*temp++ = *str_src++;
*temp = '\0';
return str_des;

}

C Skills : Tutor -2 : Pointer declaration

What's wrong with this declaration?

char * t1,t2;


Answer : t1 is a pointer to char and t2 is a char .

if we want both t1 and t2 as pointer to char, below is the declaration for the same:

char *t1,*t2;


P.S : The tutorial only covers few important tricks of the language , This is only for people who have some basic knowledge of the language.

C Skills : Tutor-1 :HelloWorld

C is just Sea , the more I learn, the more I forget ....This is the reason , I thought of writing all about C and try to accumulate all that I know and share with all using the blog....Well!!...My Blog turned technical huh!!!...


This is a simple program in C ..

Say "Hello to the world....."

#include"stdio.h"

int main()
{
printf("hello world ! \n");
return 0;
}

stdio.h is called a header file .......Beginners..come on search in google wats there inside the header file.

Question: What happens when I execute the same code in unix after changing the printf like
printf("hello world !"); ?????



P.S : The tutorial only covers few important tricks of the language , This is only for people who have some basic knowledge of the language.