On Arduino due, Is this possible to get the current time in nano seconds? At 84Mhz, I guess it will always be a multiple of ~12ns.

And a perfect solution would read a register/variable rather than call a function. Because I want to avoid the cost of calling a function which is in order of microseconds. what about counting clocks since start of execution. is this possible?

1 Answer 1


Yes, it should be. At least according to the Cortex M3 Technical Reference Manual that I think I downloaded from ARM. (DDI0337E_cortex_m3_r1p1_trm.pdf)

In section 11.5 of said manual, there is a register called DWT_CYCCNT (Data Watchpoint and Trace Cycle Count Register) which is incremented once per clock cycle when the appropriate flag of the DTW_CTRL (DWT Control) register is set.

This flag is unset on reset of the CPU so it needs to be set manually (unless it is done by the Arduino IDE, but checking the header files of the IDE, it doesn't look like it as unlike many other CPU registers, there is no Arduino REG_ define for these registers).

This untested code should be usable as the basis for your own code. I suggest you add some serial initialisation in there and dump the value of currentCycles to the serial log to test it. I am away from my hardware at the moment so can only test that it compiles.

#define REG_DWT_CTRL (*(unsigned int*)0xE0001000U)      /* Data Watchpoint and Trace Control Register */
#define REG_DWT_CYCCNT (*(unsigned int*)0xE0001004U)    /* Data Watchpoint and Trace Cycle Count Register */

#define CYCCNTENA ( 1 << 0 )                            /* DWT CTRL Cycle Count Enable Bit */

void setup()

void loop()
    unsigned int currentCycles = REG_DWT_CYCCNT;

Edit: Note that the counter is a 32 bit register, so at 84 MHz it will reset every 51.13 seconds. It is a read only register so writing to it will have no effect (you cannot manually reset it that way).

  • You probably mean (*(volatile unsigned int*)...). Commented Sep 29, 2016 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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