c - How to prevent GCC from optimizing out a busy wait loop? -


I want to write C code firmware for Atmel AVR microcontrollers. I will compile it through GCC. Also, I want to enable compiler optimization ( -Os or -O2 ), because I do not see any reason not to enable them, and they Possibly a better assembly method will generate faster than manually writing the assembly

But I do not want to customize a small piece of code. I have been delaying execution of a function for some time, and in this way I wanted to write a do-not-loop for a while. There is no need to be accurate, just wait for some time.

  / * How to optimize other code while not optimizing? * / Unsigned char i, j; j = 0; While (-J) {i = 0; while I); }   

Since memory access to AVR is very slow, so I want the i and j to be placed in the CPU registers.


UPDATE: I could get a better idea to use those functions, however, this question became valid and interesting even though I got more from it.

The document mentions the GCC:

noinline This function attributes prevent a function from being considered for inlining. If there are no side-effects of the function, there are other optimizations other than inlineing, so that the function calls can be customized, although the function call is live. In order to keep away from optimizing such calls, asm ("");

I got an interesting idea from this ... NOP Instructions on the internal loop, I tried to add empty assembly code there, Like:

  Unsigned four I, J; j = 0; While (-J) {i = 0; While (- I) ASM (""); }   

And it worked! That loop has not been optimized, and no additional nop instructions have been inserted.

What's more, if you use instability , then the GCC will store those variables in RAM and ldd and std Add a group of to copy them to temporary registers. On the other hand, this approach does not use unstable and does not produce such an upper part.


Update: If you are compiling the code by using -ansi or -std , you With the asm keyword, replace __ asm __ , such as.

In addition, you can also use __ asm__ __volatile __ ("") .

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -