Compiler Optimization

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

I am trying to use the built in library delay.h and getting the following warning:
(using AVR Studio 4.13 and winavr-20070525)

c:/winavr-20070525/bin/../avr/include/util/delay.h:89:3: warning: #warning "Compiler optimizations disabled; functions from  won't work as designed"

Although it's giving a warning that it may not work as desired but the delay functions of delay.h are working properly in this case.

I had gone through some of the posts and found that changing the complier optimization from -O0 to -0s and adding the library libm.a would remove this.

After doing this it removes the warning but the program doesn't work as intended..i.e. the flow of the program changes..

So, is there anything i am missing out while adding the library libm.a??

Many Thanks,
Surya

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Source code?

Stealing Proteus doesn't make you an engineer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ArnoldB wrote:
Source code?
what part of the source code you require??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The part of the source code that no longer works when you turn on optimizations.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am working on the GSM module sim300D and i need to set the powerkey for a short duration to get the module working
In -O0 this part of code works fine but after changing it to -Os it does not work

#define PWR_KEY_ON	PORTD |=  (1<<7)					
#define PWR_KEY_OFF	PORTD &= ~(1<<7)
#define MODULE_RESET_ON     PORTB |=  (1<<4)                    
#define MODULE_RESET_OFF    PORTB &= ~(1<<4)  

int main(void)
{
MODULE_RESET_ON;

   CPU_INIT();	// Initialization routines
    
    PWR_KEY_ON;
   for(yL=0;yL<=150;yL++)
   {
	for(i=0;i<=60000;i++);   
   }
   PWR_KEY_OFF;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I presume that your nested for loop is intended to be pure time-wasting.

In which case replace with:

  _delay_ms(10);  // 10 ms
or:
  for (i = 0; i < 1000; i++) _delay_ms(10); // 10 secs

In other words use a specific time. If you change the compiler or AVR crystal, you will still get the same delay time.

A "feature" of earlier _delay_ms() was that it did not like large arguments. My example would work with any version.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks David, i tried it but it does not work.. it behaves the same as before with -Os but works fine with -O0

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My example should work with any vintage of avr-gcc.

Have you #included ?

Note that your "it works with -O0" does not mean that _delay_ms(10) is actually 10ms. It will probably be a bit longer. If you use the macros correctly you will get exact delays. ( except for time spent in any ISRs)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Have you #included ?

Certainly looks like it in the first post:

util/delay.h:89:3: warning: #warning "Compiler optimizations disabled; functions from  won't work as designed"

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why using avr-gcc -O0 optimisation should be avoided(as said in the FAQ above)??

I am not able to find why my project does not work with -Os optimization.

Moreover the warning I am getting with -O0 that:

util/delay.h:89:3: warning: #warning "Compiler optimizations disabled; functions from  won't work as designed" 

seems to me that its not true cause using the delay function from #include e.g. _delay_ms(300) gives a correct delay of approx 300ms.

So, would it be okie to continue with the -O0 and ignore the warning??

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The warning is most likly important for short delays, because there the time to call the funktions can depend on optimization. At 300 ms an extra delay of a few cycles does not matter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For a delay of 300 ms the delay is close to 5% longer (13 ms) without optimization, and a trivial test program grows from 136 byte of flash to 3146 bytes..

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okie.. Many thanks..!!