4

I am started to program the RPI without any OS, I would like to create a delay function

.globl noop
noop:
    nop

extern void noop();

void delayUS(uint32 useconds) {
    uint32 i;
    uint32 lpnr = useconds / MCUFREQ * ...;
    for (i = 0; i < lpnr; ++i)
        noop();
}

I would like to know how can I create an accurate delay?

2
  • if you want to program the bare metal, you'd better stick with microcontrollers at the beginning. there is plenty of info on the net.
    – lenik
    Nov 10 '13 at 23:47
  • May I ask, what compiler chain you are using? Is there a howto somewhere? Oct 28 '14 at 13:32
2

According to this Datasheet of the ARM1176JZF-S CPU core (Chapter 16.17), the nop instruction takes 2 CPU cycles.

So it would be seconds / (MCUFREQ * .5). To complete your initialization line:

uint32 lpnr = useconds / (MCUFREQ * .5 * 1E6);

I think this is true, if the branch prediction was correct, because the introduction of chapter 16 talks about best case information. If the branch prediction was not right, the first nop in the loop could take a bit longer (maybe 1-2 more cycles?). In this case, the delay would be somewhere in the lower two digits nanoseconds longer than intended. But maybe I am wrong and branch prediction is not relevant for noops.

It also looks like, you don't need to care about the Intelligent Energy Management feature of the CPU, because the BCM2835 SoC does not support it with an Intelligent Energy Controller. Like mentioned in chapter 9 and 10.4, this would change your processor frequency dynamically.

Update: It might also be interesting to use the timers of the BCM2835 SoC for timing stuff, but this is only interesting if you are doing asynchronous things because it does not seems like you could switch to some power saving mode, when waiting for the timer to go off. The timers are documented in chapter 14 of the BCM2835 ARM Peripherals Datasheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.