What is extern volatile pointer.
extern volatile uint32 *ptr;
Here, what will be the behavior of *ptr? What does this actually mean?
<
extern keyword is used to declare a global variable which is defined somewhere else (that means its defined in some other .c file).
For example consider in a project two .c files a.c and b.c are there. In that a global variable is defined in a.c, and that variable can be accessed in all the functions which are defined in that file. If we want to access that same global variable in our second file b.c, then that variable should be declared as extern in b.c
a.c file is given below
int flag = 0;
int main()
{
.......
func1();
printf("\nflag value is %d\n", flag).
.......
}
b.c file is given below
extern int flag;
void func1();
{
.....
flag = 10;
.....
}
volatile keyword is used to inform the compiler to avoid doing any sort of optimization while generating executable instructions.
int flag = 0;
int main()
{
while(flag == 0);
printf("\nflag value is %d\n", flag);
return 0;
}
Consider the above program, all compiler will optimize the while(flag == 0); as while(1);. Because in the code there is no where flag value gets updated before that while loop. So if that variable value gets updated by some other hardware then it will never get reflected in the program execution. So if we declare that variable as volatile like below, compiler will not perform any optimization for that variable, and the behviour of that program will be as intended.
volatile int flag = 0;
But if there is no way for the value of program's variable is getting updated by other hardware, then no need to declare that variable as volatile. Because for valatile variables, CPU needs to perform I/O operation for each instructions which is accessing that variable. This impact in performance needs to be consider for a variable which will never get updated by other hardwares.