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

Implementing 64-bit Epoch Time Using Timer

I have an application that requires that timestamps be 64-bit epoch to the millisecond.  I'm currently using a Timer peripheral to track 1ms and increment a 64-bit counter.  I was concerned reading the value in an atomic way so I am currently disabling all interrupts when a read is performed but this seems to be pretty heavy handed. 

I saw this post https://devzone.nordicsemi.com/f/nordic-q-a/27597/extending-rtc-beyond-24-bits and it seem likes it's better just to disable the timer interrupt.

I noticed the driver API also clears the current timer value when the interrupts are enabled.  Is there a reason for this?

Is there are better way to achieve the atomic read than my current implementation?

Thanks,

Parents
  • Hi Darren

    For efficiency reasons I would suggest using a longer interval before you update your variable, and do a timer capture to get the 'lower bits' of the timestamp. 

    As an example if you configure the timer to run at a 1MHz frequency (prescaler = 4) you can reload it every second, and get the milliseconds (and microseconds if needed) by doing a capture on the timer. 

    To get the current timer value you could do the following:

    - Disable timer interrupt
    - Capture the current timer value to a separate CC register
    - Add the captured value to your variable counting the seconds, and return this as the current time
    - Enable timer interrupt

    Then in the interrupt you essentially need to increment the second variable, and increment the capture register by a million to schedule the next interrupt.

    Best regards
    Torbjørn

Reply
  • Hi Darren

    For efficiency reasons I would suggest using a longer interval before you update your variable, and do a timer capture to get the 'lower bits' of the timestamp. 

    As an example if you configure the timer to run at a 1MHz frequency (prescaler = 4) you can reload it every second, and get the milliseconds (and microseconds if needed) by doing a capture on the timer. 

    To get the current timer value you could do the following:

    - Disable timer interrupt
    - Capture the current timer value to a separate CC register
    - Add the captured value to your variable counting the seconds, and return this as the current time
    - Enable timer interrupt

    Then in the interrupt you essentially need to increment the second variable, and increment the capture register by a million to schedule the next interrupt.

    Best regards
    Torbjørn

Children
Related