Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Why Volatile keyword in Embedded C?

Status
Not open for further replies.

Surender Reddy

Full Member level 2
Joined
Jan 28, 2012
Messages
130
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Location
Bangalore, India
Activity points
2,189
Hi,
What is the necessity of using volatile keyword for embedded applications?
Can any guide me with some example?
 

Easyrider83

Advanced Member level 5
Joined
Oct 11, 2011
Messages
1,608
Helped
373
Reputation
746
Reaction score
361
Trophy points
1,363
Location
Tallinn, Estonia
Activity points
8,577
Use this keyword within variable that accessed inside and outside interrupt at the same time.
 

Surender Reddy

Full Member level 2
Joined
Jan 28, 2012
Messages
130
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Location
Bangalore, India
Activity points
2,189
Use this keyword within variable that accessed inside and outside interrupt at the same time.

even the global variable also does the same without volatile.
what is the important of volatile?
How volatile keyword avoid optimization techniques by the compiler for a variable?
 

Easyrider83

Advanced Member level 5
Joined
Oct 11, 2011
Messages
1,608
Helped
373
Reputation
746
Reaction score
361
Trophy points
1,363
Location
Tallinn, Estonia
Activity points
8,577
Just lets compilier fully copy the stack including this variable before enter interrupt routine.
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
assume you have an timer interrupt handler which sest a global variable every second, e.g.
Code:
volatile int second=0; 	// set every second 

void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag														
second=1;
}

int main(void)
{
...
startTimer();
second=0;
while(!second);      // wait 1 second
    ...                // code executed after a second

   
}
in main second is tested in the while() statement which executes every second

the problem is that if second is not declared volatile an optimising compiler will look at
Code:
second=0;
while(!second) ;     // wait 1 second
see that second is initialised to 0 and remove the while() statement - thus whole sections of code can be removed
the volatile keyword tells the compiler that the variable may be altered outside the current context and code should not be optimised
for more detail see
https://en.cppreference.com/w/cpp/language/cv
 
Last edited:

andre_teprom

Super Moderator
Staff member
Joined
Nov 7, 2006
Messages
9,237
Helped
1,151
Reputation
2,321
Reaction score
1,127
Trophy points
1,403
Location
Brazil
Activity points
53,767
even the global variable also does the same without volatile.
what is the important of volatile?

It is a good practice to put the variables inside the routines where are supposed to be used.
This makes the program more organized, avoiding spread a big list of variables at the main().
 

Surender Reddy

Full Member level 2
Joined
Jan 28, 2012
Messages
130
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Location
Bangalore, India
Activity points
2,189
Thanks for the reply....
Is there any separate memory for these volatile variables? where these are stored?

volatile doesn't allow to make perform optimization by compiler on the volatile variables, which increase the length of the code (some).
This is one drawback of volatile.
I am right?

assume you have an timer interrupt handler which sest a global variable every second, e.g.
Code:
volatile int second=0; 	// set every second 

void __attribute__((__interrupt__, __shadow__)) _T1Interrupt(void)
{
/* Interrupt Service Routine code goes here */
IFS0bits.T1IF = 0; 							//Reset Timer1 interrupt flag														
second=1;
}

int main(void)
{
...
startTimer();
second=0;
while(!second);      // wait 1 second
    ...                // code executed after a second

   
}
in main second is tested in the while() statement which executes every second

the problem is that if second is not declared volatile an optimising compiler will look at
Code:
second=0;
while(!second) ;     // wait 1 second
see that second is initialised to 0 and remove the while() statement - thus whole sections of code can be removed
the volatile keyword tells the compiler that the variable may be altered outside the current context and code should not be optimised
for more detail see
https://en.cppreference.com/w/cpp/language/cv
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
Thanks for the reply....
Is there any separate memory for these volatile variables? where these are stored?

volatile doesn't allow to make perform optimization by compiler on the volatile variables, which increase the length of the code (some).
This is one drawback of volatile.
I am right?
I assume volatile variables would be allocated on the heap along with other global variables - you could always look at memory maps to check
you would only use volatile in situations where you don't want optimisation, e.g. global variables used to pass information between interrupt routines and other parts of the program
 

Surender Reddy

Full Member level 2
Joined
Jan 28, 2012
Messages
130
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,298
Location
Bangalore, India
Activity points
2,189
Global variables are allocated in the data memory but you said heap.
Ok, I will check with memory maps for volatile variables.

Anyway, thanks a lot, you cleared my doubt.....



I assume volatile variables would be allocated on the heap along with other global variables - you could always look at memory maps to check
you would only use volatile in situations where you don't want optimisation, e.g. global variables used to pass information between interrupt routines and other parts of the program
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
Global variables are allocated in the data memory but you said heap.
Ok, I will check with memory maps for volatile variables.

Anyway, thanks a lot, you cleared my doubt.....
both heap and satck are what you would call data memory
1. heap for permanent (during execution of the program) variables such as global external variables and static variables (defined inside a function) - also dynamic storage allocated by functions such as malloc and calloc
2. stack for temporary data such as automatic variables defined inside function and the information pushed onto the stack during a function call

have a look at
https://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap14/subsection2.1.1.8.html
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top