View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
"volatile" not really used in CCS compiler? |
Posted: Fri Sep 10, 2004 4:40 am |
|
|
Hi,
The way I understood 'volatile' to be used is to inhibit compiler optimization. Thus a global variable in an ISR should have a volatile if it is to be used in other functions.
I have found that I do not have to designate any variables as 'volatile', and things still work fine.
What then would be the use of "volatile" in the CCS compiler? |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Fri Sep 10, 2004 5:47 am |
|
|
Volatile is used to say that this variable may be changed by something that is not apparent in the code running at the moment.
For example lets say I have an ISR routine and I have a global variable...
[code]
UINT done=0;
void isr()
{
done=1;
}
void main()
{
while(!done)
{
}
}
[/done]
In this case the compiler with out knowing that done is set by another thread/process it may look and say well done is not set in the main, but initially set to zero. Then it is not modified in the while loop either. Therefore the compile may optimize the code to be while(1).
Thus any time a variable may change outside of the code you are running it should be a volitale. For example, all peripheral registers should be voltile as they may change outside of code. All hand shaking signals in ISR routines should be volatile as well, like the done above.
Trampas |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Sep 10, 2004 5:55 am |
|
|
Unless CCS made changes recently, volatile can be used when declaring a variable but the compiler just ignores it! |
|
|
valemike Guest
|
|
Posted: Fri Sep 10, 2004 6:53 am |
|
|
I agree with Trampas' explanation. However, like I said in the first post, that even if i don't use volatile for a global var used in an isr, it still works just fine, as if the compiler does no optimization of it whatsoever with or without the volatile keyword specified.
So it is like Mark's observance. Volatile is just ignored, and I would go further to assume that you can imply all variables are inherently volatile. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Sep 10, 2004 6:53 am |
|
|
I think CCS considers everyting to be volitile. The only place where it matters is when a variable is stored in external memory where it could be modified by another processor that this compiler does not know about, but that has access to the same external memory. Rather than fetching the variable to RAM and using it several times a volitile variable should be fetched to RAM from external memory each time it is used. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
|