Optimization problem

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

Optimization problem

Jump to solution
2,455 Views
neuzzo
Contributor II

I have a problem with this code when activate optimization:

inside main i have this cycle, that waiting a trigger of interrupt to proceed:

for(;;)

{

WaitTrigger();

..

...

}

 

void WaitTrigger(void)

{

  while (!appTrigger)

   ;

  appTrigger = FALSE;

}

 

and appTrigger  is forced TRUE by interrupt.

 

with -O0 working fine, but if I insert -O1 stop work.

 

I can disable the optimization in this area, or other idea to enable the optimization?

Labels (1)
Tags (1)
1 Solution
1,779 Views
MJW
NXP Employee
NXP Employee

Hello,

make sure you declared all variables shared between main application code and an ISR as "volatile", e.g. like this:

volatile unsigned int appTrigger;

Short explanation: "volatile" tells the compiler to not make any assumptions about the content of the variable. That is essential for all variables that are used in a (for the compiler) non-deterministic way (i.e. ISR).

HTH,

MJW

View solution in original post

4 Replies
1,780 Views
MJW
NXP Employee
NXP Employee

Hello,

make sure you declared all variables shared between main application code and an ISR as "volatile", e.g. like this:

volatile unsigned int appTrigger;

Short explanation: "volatile" tells the compiler to not make any assumptions about the content of the variable. That is essential for all variables that are used in a (for the compiler) non-deterministic way (i.e. ISR).

HTH,

MJW

1,779 Views
TICS_Fiona
NXP Employee
NXP Employee

Hello Riccardo

To workaround the problem, you may use compiler pragma to disable the optimization locally for this ISR.

To dig into the problem, we need to know the CodeWarrior version you're using.

For Classic CodeWarrior, please:

1)Start the IDE and click on Help | About Freescale(Metrowerks) CodeWarrior.

2)Click on Installed Products

3)Provide us all info displayed. Or you can save them in a txt file.

For Eclipse CodeWarrior, please:

1)Start the IDE and click on Help | About CodeWarrior Development Studio.

2)Provide us the info displayed.

And an example project will be greatly appreciated.

Best Regards

Fiona Kuang

Technical Information & Commercial Support

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

1,779 Views
neuzzo
Contributor II

Dear Fiona,

thanks for the replay.

The CodeWarrior version is 10.6,

The MCU used are MKE02Z64.

I have create the grid of software with PE.

0 Kudos
Reply
1,779 Views
TICS_Fiona
NXP Employee
NXP Employee

Hello neuzzo

CodeWarrior v10.6 is an old version, please upgrade to CW10.6.6.

Please first install CW10.6.4 which can be downloaded from:

  1. www.freescale.com/cwmcu10

Its update v10.6.5 and v10.6.6 are also available at the same link.

Further information and the release notes are available on the following Freescale community page:

https://community.freescale.com/docs/DOC-143655

To workaround the problem, you may use compiler pragma to disable the optimization locally for this ISR. For GCC compiler, it should be :

#pragma GCC optimize ("O0")

For details, please refer to the link below:

http://stackoverflow.com/questions/2219829/how-to-prevent-gcc-optimizing-some-statements-in-c

Hope this helps!

Best Regards

Fiona Kuang

Technical Information & Commercial Support

-----------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply