# segger embedded studio with gcc volatile problem [closed]

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 > 0)
{
g_ulMainLoopCounter--;
}


in my main function, i wait until 'g_ulMainLoopCounter' decrease to 0:

int main(void)
{
system_init();                                      //init timer etc...

g_ulMainLoopCounter = 500;
while (g_ulMainLoopCounter)
{
}

....
}


now the problem is , my code after 'while' will never be executioned, even 'g_ulMainLoopCounter' decrease to 0. This work well in keil.

Then, if i use 'volatile' qualifier for 'g_ulMainLoopCounter', it works, the code then like this:

volatile uint32_t g_ulMainLoopCounter = 0;


My optimization level set to none, means do not optimize my code.

I know use 'volatile' qualifier is a better way, but 'g_ulMainLoopCounter' is just only a example, there are lots of variables which used like "g_ulMainLoopCounter" (means multi-access variable, change the value in a function or isr and comparison in other function)in my program, must i check every variable in my program and determine if the variable is needed use 'volatile' qualifier? If so, i think that's too difficult.Is there any easy solutions?

edit retag reopen delete

### Closed as "the question is answered, right answer was accepted" by Ian at 2017-10-13 05:33:20 +0100

Sort by » oldest newest most voted

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.

more

I'm a little bit confused by your description. You are decreasing "g_ulUartTimeoutCounter" variable in the isr but the loop in main has "while (g_ulMainLoopCounter)". Is it right?

more

Yes, "g_ulUartTimeoutCounter" is a global variable, in the isr, check if "g_ulUartTimeoutCounter" is not zero, decrease it, in main function, wait until "g_ulUartTimeoutCounter" decrease to zero.

( 2017-10-12 16:47:45 +0100 )editconvert to answer

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.

more

Yes, i agree with you about this, and i understand what the effect of "volatile" is. But, in my program, there are lots of variables which used like "g_ulUartTimeoutCounter" , for example:

if ((!g_ulMainLoopCounter) && (!g_tMainLoopInterface.iap_state))
{
//do something
}


When i use keil MDK, it works well，and when i use SES, the code in this "if" segment never be executioned, even "g_ulMainLoopCounter" used 'volatile' qualifier, because the member "iap_state" of struct "g_tMainLoopInterface" is not 'volatile', and i change the value of 'iap_state' in other function(not isr).

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 'volatile' qualifier(it's very difficult i think)? Is there any easy solutions?

( 2017-10-12 17:17:48 +0100 )editconvert to answer

I am wandering how to understand "many readers", i think different threads are "many readers"， but does different functions in one thread also means "many readers"? If so,does it mean every global variable should use 'volatile' qualifier except only one function in one thread read it? I think this is unreasonable and i hope only different threads are "many readers" and different functions in one thread are not "many readers".

( 2017-10-13 03:41:27 +0100 )editconvert to answer

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.

( 2017-10-13 03:47:09 +0100 )editconvert to answer

I got it. Now i know how to modify my code. Thank you very much!

( 2017-10-13 03:50:27 +0100 )editconvert to answer

## Recent blog posts

• ### The world's smallest breakout board compatible BTLE module: Automate your curtains for less than \$90 with BluChip!

Posted 2017-12-07 09:10:36 by Jeevan Anga
• ### Join Jumper's free beta for a Virtual nRF52832 device to streamline your R&D process

Posted 2017-11-27 12:53:04 by Yaniv Nis
• ### PSG-NORDIC Channel in YouTube

Posted 2017-11-27 11:08:04 by Mugelan
• ### Job Offer: nRF / Embedded Developer in Stuttgart, Germany

Posted 2017-11-20 11:46:20 by Marius Heil
• ### Estudando Projetos do SDK 10 para nRF5x com Eclipse Oxygen (Portuguese)

Posted 2017-11-12 00:08:55 by Carlos Delfino

## Recent questions

• ### Do I need to define an Analog input as such in NRF52?

Posted 2017-12-11 15:43:09 by ndarkness
• ### FDS & fstorage extra information

Posted 2017-12-11 14:34:42 by Flinn92

Posted 2017-12-11 13:58:20 by ToasTer86
• ### How can I change radio frequency?

Posted 2017-12-11 13:56:17 by RoomBee
• ### having problem in services

Posted 2017-12-11 13:19:44 by karthikeyan