Pages: [1]   Go Down
Author Topic: nanos() [time elapsed since program start in nanoseconds]?  (Read 2413 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi everyone!

In my current application I'd like to be able to have access to timing in the 10 to 100 ns range.  So I started digging around in the micros() source code and came up with the following:

Code:
uint32_t nanos( void )
{
    uint32_t ticks ;
    uint32_t count ;

    SysTick->CTRL;
    do {
        ticks = SysTick->VAL;
    } while (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk);

    // GetTickCount() is millis()
    uint32_t load= (SysTick->LOAD + 1 - ticks);
    uint32_t milliS = GetTickCount() * 1000000;
    uint32_t microS =  load/ (SystemCoreClock/1000000) * 1000;
    uint32_t nanoS = load/ (SystemCoreClock/10000000); // these are actually 100s of nanoseconds; 84MHz  = 11.9ns
   
    //Serial.print(milliS); Serial.print(" "); Serial.print(microS); Serial.print(" "); Serial.println(nanoS);
   
    return milliS + microS + nanoS;

}

It's exactly the micros() source code except that on the return I changed the divisions by 1000.  Could someone please comment on whether this is the right approach?

On a side note: isn't division notoriously slow on the Due, so isn't there a better way to optimize this code for performance?
« Last Edit: November 26, 2013, 11:03:49 pm by titous » Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12489
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

multiplication and division are in hardware on the Cortex, multiplication is single cycle,
don't know the figure for division though.
Logged

[ I won't respond to messages, use the forum please ]

Pages: [1]   Go Up
Jump to: