<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/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/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/25881/segger-embedded-studio-with-gcc-volatile-problem</link><description>Hi, 
 I use SES import my keil project, and my keel project work well. With SES, i meed a problem, that is , i have a global variable like this: 
 uint32_t g_ulMainLoopCounter = 0;
 
 and i decrease it in timmer isr: 
 if (g_ulMainLoopCounter &amp;gt; 0</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 13 Oct 2017 01:50:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/25881/segger-embedded-studio-with-gcc-volatile-problem" /><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101935?ContentTypeID=1</link><pubDate>Fri, 13 Oct 2017 01:50:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b5cad12f-7b94-4894-a2b6-78000268c239</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;I got it. Now i know how to modify my code. Thank you very much!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101934?ContentTypeID=1</link><pubDate>Fri, 13 Oct 2017 01:47:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c26d3aee-4702-47e5-b846-0202148d4530</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;many read mean different threads but if you have the write in a different thread, volatile is required. If all are in the same thread, no volatile needed.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101933?ContentTypeID=1</link><pubDate>Fri, 13 Oct 2017 01:41:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:513f0261-405a-4b6c-b756-ccd709d843da</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;I am wandering how to understand &amp;quot;many readers&amp;quot;, i think different threads are &amp;quot;many readers&amp;quot;， but does different functions in one thread  also means &amp;quot;many readers&amp;quot;?  If so,does it mean every global variable should use &amp;#39;volatile&amp;#39; qualifier except only one function in one thread read it? I think this is unreasonable and i hope only different threads are &amp;quot;many readers&amp;quot; and different functions in one thread  are not &amp;quot;many readers&amp;quot;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101928?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 15:51:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1026e856-98c3-4e64-92a9-499d7cde4962</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;If you only have one writer and many readers, volatile is sufficient.  If you have multiple writers then atomic access is required.  No matter which compiler you are using.  You may not get the issue now with Keil does not mean you will not get it later with optimization turn on or with newer version because this is a C language thing not compiler thing.  The behaviour of GCC complies with it. That is why you get the issue.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101932?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 15:17:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:519f5da1-c5a6-4666-9da7-29cb283a79d4</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;Yes, i agree with you about this, and i understand what the effect of &amp;quot;volatile&amp;quot; is. But, in my program, there are lots of variables which used like  &amp;quot;g_ulUartTimeoutCounter&amp;quot; , for example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ((!g_ulMainLoopCounter) &amp;amp;&amp;amp; (!g_tMainLoopInterface.iap_state))
{
    //do something
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When i use keil MDK, it works well，and when i use SES, the code in this &amp;quot;if&amp;quot; segment never be executioned, even &amp;quot;g_ulMainLoopCounter&amp;quot;  used &amp;#39;volatile&amp;#39; qualifier, because the member &amp;quot;iap_state&amp;quot; of struct &amp;quot;g_tMainLoopInterface&amp;quot; is not &amp;#39;volatile&amp;#39;, and i change the value of &amp;#39;iap_state&amp;#39; in other function(not isr).&lt;/p&gt;
&lt;p&gt;So, if i want use SES(or other IDE used gcc),must i check every variable in my program and determine if the variable is needed use  &amp;#39;volatile&amp;#39; qualifier(it&amp;#39;s very difficult i think)?  Is there any easy solutions?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101930?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 14:47:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5fee0b0c-4a2c-43f5-abf4-3af3c2948e51</guid><dc:creator>Ian</dc:creator><description>&lt;p&gt;Yes, &amp;quot;g_ulUartTimeoutCounter&amp;quot; is a global variable, in the isr, check if  &amp;quot;g_ulUartTimeoutCounter&amp;quot; is not zero, decrease it, in main function,  wait until &amp;quot;g_ulUartTimeoutCounter&amp;quot; decrease  to zero.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101931?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 13:50:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:547370bc-10dc-4085-bc77-534664c0f7c4</guid><dc:creator>Nguyen Hoan Hoang</dc:creator><description>&lt;p&gt;Did you mean it worked by adding volatile in front ?  That is the correct behaviour.  There is no bug with GCC but might be a bug in Keil if it is compiled with full optimization.  The volatile key word tells the compiler to always load data from memory.  This is for this type of multi-access to guarantee that the value is loaded before comparison.  Without it, optimization will preload the value in register and use locally in the loop.  Hence when the interrupt updates the value it will not reflect in the main loop. So you will loop endlessly.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: segger embedded studio with gcc volatile problem</title><link>https://devzone.nordicsemi.com/thread/101929?ContentTypeID=1</link><pubDate>Thu, 12 Oct 2017 13:04:17 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46dd0a33-0358-47de-9c8d-921afc3776f2</guid><dc:creator>Aleksei Vegner</dc:creator><description>&lt;p&gt;I&amp;#39;m a little bit confused by your description. You are decreasing &amp;quot;g_ulUartTimeoutCounter&amp;quot; variable in the isr but the loop in main has &amp;quot;while (g_ulMainLoopCounter)&amp;quot;. Is it right?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>