help with a power pc processor based software

i have implemented a powerpc based embedded system in a xilinix vertex-2pro device and trying out an extremely simple program. Here is the code.

#include "xparameters.h" #include "xbasic_types.h" #include "xgpio.h" #include "xstatus.h"

XGpio GpioOutput;

int main() { Xuint32 status; unsigned long i = 0; unsigned long j = 0;

// Initialize the GPIO driver so that it's ready to use, status = XGpio_Initialize(&GpioOutput, XPAR_GENERIC_GPIO_DEVICE_ID); if (status != XST_SUCCESS) return XST_FAILURE; // Set the direction for all signals to be outputs XGpio_SetDataDirection(&GpioOutput, 1, 0x0);

while(1) { XGpio_DiscreteWrite(&GpioOutput, 1, 0xFFFFFFF1); while(i < 10000000) { i++; } XGpio_DiscreteWrite(&GpioOutput, 1, 0xFFFFFFF2); while(j < 10000000) { j++; } i = 0; j = 0;

} return 0; }

As can be seen, this program just blinks two LED's. However what i see is two dimmed LED's. They are dimmed because they blink very fast. It appears like two inner while loops are just skipped. Can anybody guess a reason?

Thanks

Reply to
Manusha
Loading thread data ...

ENERIC_GPIO_DEVICE_ID);

FFFF1);

FFFF2);

Compilers usually optimize out loops like this. You should try using the timer functions to wait for a specific amount of time.

Ed McGettigan

-- Xilinx Inc.

Reply to
Ed McGettigan

_GENERIC_GPIO_DEVICE_ID);

FFFFFF1);

FFFFFF2);

Thanks. I disabled optimizations and now it works fine.

Reply to
Manusha

Sometimes you want delays that are short, perhaps shorter than the unknown overhead of calling some library routine.

I think the recipe to run the compiler in dumb/stupid mode that doesn't do any optomizations like that should be well documented.

--
These are my opinions, not necessarily my employer's.  I hate spam.
Reply to
Hal Murray

Instead of disabling optimizations, try this:

volatile unsigned long i = 0; volatile unsigned long j = 0;

With the 'volatile' qualifier, the compiler is not allowed to optimize away load/stores to that variable. The rest of your code will still be optimized. Disabling all optimization isn't guaranteed to work anyway.

btw, it's more compact to use a 'for' loop, and only a single variable:

for( i = 0; i < 1000000; i++ );

Reply to
Arlet Ottens

No, your program does /not/ work - it is still broken. There is no such thing as code that "works fine with optimisations disabled". You should correct your program - and do that from day 1 on your first blinking light test program.

Ed gave you the best solution - when you want something to run at specific times, use timers (timer functions, timer library calls, RTOS functions, hardware timers, or whatever is appropriate for your platform).

If you want to have a simple loop for a rough delay or for testing, the easiest way is to declare your loop variables to be "volatile" (as "volatile unsigned long int i = 0;"). This is telling your compiler how you want it to treat that variable - it should always read and write it exactly as written in the source code, rather than generating faster or smaller code.

Reply to
David Brown

Which compiler did you use? Smart C compilers do not get rid of empty loops because they are often used to wait for small periods of time. There is a difference between optimising and breaking a program!

--
Failure does not prove something is impossible, failure simply
indicates you are not using the right tools...
nico@nctdevpuntnl (punt=.)
--------------------------------------------------------------
Reply to
Nico Coesel

Why is the compiler breaking the program, when the loop is removed?

When it ignores whitespace, does it break the program too?

Reply to
Marc Jet

In that kind of loop you should always use the qualifier volatile to avoid optimizations as you should do for any hw register mapped in memory...to avoid caching problems...

Carlo

--------------------------------------- Posted through

formatting link

Reply to
carlob

If this optimization breaks your program, it was poorly written in the first place. Use 'volatile' and it can't happen.

Empty loops could be the result of other optimizations, perhaps as a result of some #define settings. For example, you could have code targeted for a multi-CPU environment, with a #define saying how many CPUs you have. For the case that this number equals '1', a lot of code could be redundant, and you'd want the compiler to optimize it away, even if it looks like a delay loop.

Reply to
Arlet Ottens

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.