while(1) not eternal

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

while(1) not eternal

301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Brinkand on Thu Feb 25 07:24:41 MST 2010
This keeps bugging me:

 
ADC_local_counter = 0;
   

  while(1)
  {ADC_local_counter++; };


[COLOR=black][FONT=&quot]ADC_local_counter ends at 1! I would suspect it to keep incrementing during execution, but it only runs once. Any hints?
[/FONT][/COLOR]
 


Below example works as expected:

[COLOR=black][FONT=&quot]
[/FONT][/COLOR]
ADC_local_counter = 0;
     
  while(ADC_local_counter <100)
  {ADC_local_counter++; };
   
    ADC_local_counter++;
  
ADC_local_counter ends at 101 as expected.
0 Kudos
5 Replies

291 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by bladerunner on Sun Feb 28 05:21:05 MST 2010

Quote: CodeRedSupport
No - I meant that the compiler will optimise out the incrementing of your local variable - as it can see that the code does not make any other use of the variable.

The while loop itself will remain - encoded as a branch to self - ie a branch to the location of the branch instruction itself.

If you want to keep the code that increments the variable, then make the variable "volatile". This instructs the compiler that "external factors" may access the variable and will hence prevent the compiler removing the code that increments it within your while(1) loop.

If you look at the main.c that is created by the LPCXpresso Project Wizard, this is exactly what the code this contains does.

Regards,

CodeRedSupport



Yes, turn off the optimization or use the volatile qualifier. Compiler optimization and embedded controllers are uneasy bedfellows :)
0 Kudos

291 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Brinkand on Fri Feb 26 00:57:17 MST 2010
Thank You very much for the excellent answer.

Volatile solved the issue. Everything now works as I intend.

I was actually using the default blinky optimization of -O1.
0 Kudos

291 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Feb 25 15:36:05 MST 2010
No - I meant that the compiler will optimise out the incrementing of your local variable - as it can see that the code does not make any other use of the variable.

The while loop itself will remain - encoded as a branch to self - ie a branch to the location of the branch instruction itself.

If you want to keep the code that increments the variable, then make the variable "volatile". This instructs the compiler that "external factors" may access the variable and will hence prevent the compiler removing the code that increments it within your while(1) loop.

If you look at the main.c that is created by the LPCXpresso Project Wizard, this is exactly what the code this contains does.

Regards,

CodeRedSupport
0 Kudos

291 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by rkiryanov on Thu Feb 25 11:25:36 MST 2010

Quote: CodeRedSupport
What is happening is that the compiler is, quite rightly, optimising the update to your local away, as it has detected that nothing is actually making use of it!



Do you mean that gcc optimized infinite useless loop to "nothing"? It should opmimize to __asm("b ."), but not to "nothing".
0 Kudos

291 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Feb 25 09:35:03 MST 2010
What optimisation level are you using? A release build / -O2 ??

What is happening is that the compiler is, quite rightly, optimising the update to your local away, as it has detected that nothing is actually making use of it!

Try marking your local as "volatile".

Note that when you see apparently strange behaviour like this, it can be quite useful to look at the actually code generated. In this case, you will almost certainly see a "branch to self instruction".

For details of how to disassemble whilst debugging, please see:
  http://lpcxpresso.code-red-tech.com/LPCXpresso/node/28

And for details of how to disassemble whilst building, please see:
  http://lpcxpresso.code-red-tech.com/LPCXpresso/node/29

Regards,

CodeRedSupport.
0 Kudos