Page 1 of 1

Microsecond or fraction of microsecond delay

Posted: Sun Mar 11, 2018 1:44 pm
by Vader_Mester
So whats the easiest way in the ESP32 to put in a delay of lets say 0.5 uSeconds?

Thanks for the answer in advance!

Vader[BEN]

Re: Microsecond or fraction of microsecond delay

Posted: Sun Mar 11, 2018 5:22 pm
by kolban
If this were my puzzle, I'd probably follow this line of logic.

The CPU is executing at a constant processor clock rate ... lets call it C hz. That means that it ticks at C times per second or, each clock tick is 1/C seconds. Let us now assume that 1/C is faster than the delay you want to wait. For example, 1us = 1 / 1000000 of a second = 1MHZ clock rate. So your clock processor clock should be > 1 MHZ (which it is ... don't know the exact number off the top of my head).

In ESP32 assembler, there is a special register called CCOUNT that increments (internally and in the hardware) every processor clock cycle.

What this means is that each time CCOUNT increments, we know that 1/C seconds have passed. Now you can write some code that would (loosely) do the following.

Code: Select all

function delay(period) {
   start = CCOUNT;
   while (CCOUNT - start < period) {
      // do nothing
   }
}[
This would busy wait (waste) the cycles, but it should work. You might also have to disable some of the interrupts if you need absolutely delays no matter what. For example, you may be preempted by FreeRTOS.

Re: Microsecond or fraction of microsecond delay

Posted: Sun Mar 11, 2018 5:49 pm
by Vader_Mester
Thanks for this idea. I might check this.

Vader[BEN]