This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Best way to implement micros() function in nrf51822?

I need to implement the micros() function of arduino on nrf51822. Can someone guide me on how it can be done?

This the function for Arduino:

    unsigned long micros() {
    unsigned long m;
    uint8_t oldSREG = SREG, t;
 
    cli();
    m = timer0_overflow_count;
#if defined(TCNT0)
    t = TCNT0;
#elif defined(TCNT0L)
    t = TCNT0L;
#else
    #error TIMER 0 not defined
#endif


#ifdef TIFR0
    if ((TIFR0 & _BV(TOV0)) && (t & 255))
        m++;
#else
    if ((TIFR & _BV(TOV0)) && (t & 255))
        m++;
#endif

    SREG = oldSREG;
 
    return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}

Basically the function returns the number of microseconds since the board started running.