سؤال

I made simple delay func:

void Delay(__IO uint32_t nCount)
{
  while(nCount--) {}
}

And I want to predict time duration of single execution with some value. I know it's bad idea but I don't need accurate time and it will be not interrupted.

I'm using STM32F405 @ 168 MHz with 8 MHz external crystal.

So far I've noticed that calling it with 0x80 0000 gives me about half second delay.

هل كانت مفيدة؟

المحلول

So instead of having an empty while loop, you should put __no_operation(); in there (2 underscores, not 1). This takes ~29ns per instruction cycle at 168MHz on my board, and it's inserting intrinsic assembly NOP's directly into the code stream, so it will stand-up against any optimizations.

One last note: your loop-counter is __IO, meaning that it's volatile. This means the loop-counter will not be put in a CPU register. You can change that once you put the __no_operation(); line in your loop, because it will protect it from being eliminated by the compiler.

You typically should use a timer, but sometimes we all just need a hack-up :)

-Jesse

نصائح أخرى

The amount of time it takes to execute that function can vary widely depending on your compiler and settings. Since your function does nothing an optimizer would turn this function into a simple bx lr, which takes very little time. If you are able to measure the time then you are not optimizing (and your overall execution of this and other parts of your code will vary even more).

Assuming you solve that problem in a deterministic and repeatable manner, you can get a rough idea of how long it takes to execute by executing it and timing it using a reference clock. the timers in the cortex-m4 are an excellent choice.

Any time you change the way you use this code, turn on a cache, or change the processor clock, change the timing settings on the flash, etc, you will need to re-tune your delay function.

It is far easier to just use one of the timers directly to perform a delay, and the accuracy is improved by quite a bit. Prevents having to continue to maintain the counter loop code and/or calls to it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top