<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>keywords volatile  in an  interrupt routine</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/41168/keywords-volatile-in-an-interrupt-routine</link><description> 
In this webpage : http://www.keil.com/support/man/docs/gsac/gsac_timer.htm 

The question is why two variables one use volatile but another not
?? 
The two variables is &amp;quot;TimeTick&amp;quot; and &amp;quot;clock_1s&amp;quot;. 
Who can tell me why,i am very confused. thank you !</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: keywords volatile  in an  interrupt routine</title><link>https://community.arm.com/thread/82976?ContentTypeID=1</link><pubDate>Sun, 20 Dec 2015 23:19:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6b8d5139-e509-425a-9f4a-515129946785</guid><dc:creator>Yi zhifei</dc:creator><description>&lt;p&gt;&lt;p&gt;
Thank you very much for you answer, i understand. thank you&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: keywords volatile  in an  interrupt routine</title><link>https://community.arm.com/thread/69453?ContentTypeID=1</link><pubDate>Sat, 19 Dec 2015 03:47:20 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:19346e0b-8c4c-42b4-89e4-bc3f5026e3e0</guid><dc:creator>edPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
It seems like someone have made a mistake.&lt;/p&gt;

&lt;p&gt;
It would be more reasonable to reverse which of the two variables
that has the volatile keyword.&lt;/p&gt;

&lt;p&gt;
If TimeTick is only used by the ISR, then it doesn&amp;#39;t need the
volatile keyword. If some other part of the code is also making use
of TimeTick, then it should have the volatile keyword.&lt;/p&gt;

&lt;p&gt;
But clock_1s seems like it is modified by the ISR, with the
intention that the main loop should look at it and perform some
action whenever it gets set. So this variable should be volatile.&lt;/p&gt;

&lt;p&gt;
Volatile should be used whenever there is some &amp;quot;magic&amp;quot; task switch
where an ISR or task may step in at an arbitrary time and modify a
variable that some other task (or the main loop) also will be using.
This makes sure the compiler inserts code to always read the variable
value directly from RAM instead of keeping a copy of the variable in
some register.&lt;/p&gt;

&lt;p&gt;
If the main loop has a copy of clock_1s cached in a register, then
that register will not get a new value when the ISR gets activated
and increment the master value of clock_1s.&lt;/p&gt;

&lt;p&gt;
The ISR itself doesn&amp;#39;t need the volatile keywords for normal
variables, since the compiler will always have to retrieve the value
from RAM after the ISR has been activated.&lt;/p&gt;

&lt;p&gt;
For memory-mapped special function registers, i.e. magical memory
cells that may change value without the code accessing them, then the
volatile keyword is always needed - both ISR code and normal code
must then perform a new read whenever the variable is accessed.&lt;/p&gt;

&lt;p&gt;
Another thing here:&lt;/p&gt;

&lt;pre&gt;
if (TimeTick++ &amp;gt;= 999) { ... }
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
it would have been nicer to write&lt;/p&gt;

&lt;pre&gt;
if (++TimeTick &amp;gt;= 1000) { ... }
&lt;/pre&gt;

&lt;p&gt;
With post-increment, the compiler needs to load TimeTick. Then
make a copy of it and increment and write down to memory. Then return
to the original value for performing the comparison.&lt;/p&gt;

&lt;p&gt;
So post-increments basically means:&lt;/p&gt;

&lt;pre&gt;
    unsigned tmp1,tmp2;

    tmp1 = TimeTick;
    tmp2 = tmp1;
    tmp2 = tmp2 + 1;
    TimeTick = tmp2;
    if (tmp1 &amp;gt;= 999) { ... }
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
while the pre-increment variant is like:&lt;/p&gt;

&lt;pre&gt;
    unsigned tmp;

    tmp = TimeTick;
    tmp = tmp + 1;
    TimeTick = tmp;
    if (tmp &amp;gt;= 1000) { ... }
&lt;/pre&gt;

&lt;p&gt;
So post-increment/decrement can cost more if the code needs to
both look at the variable and increment/decrement it because of the
need for an extra copy of the variable.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>