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

Microsecond counter

I need to implement a uint64_t counter that increments every microseconds that I can then read.

I tried to use the timer API and increment the counter in the event handler call back but because it is being called at 1 us rate it never really exits the interrupt handler for very long and I can not do any other work.

Is there another way to do this?

Is there a simple method to just read the "tic" counter (and then just divide by number of tics per us) without setting up a timer?

Parents
  • Hi

    Whenever an interrupt is fired there will be some latency, typically a couple of microseconds, and the timer API might add some additional processing and event forwarding that will increase the latency further. 

    In general it is not recommended to fire off interrupts at such a rapid rate, as it will burn a lot of CPU clock cycles and lead to higher power consumption. 

    I would suggest running a separate timer module at a 1 MHz rate, and do a capture operation on the timer every time you need to read the value. Then the state of the timer will be moved into one of the CC (compare/capture) registers, and you can read out the state from there. 

    At some point you will need to reset the timer, and increment your counter variable, but in principle you don't have to do it more often than every 2^32/1000000 seconds if you run the timer in 32-bit mode (corresponding to an interrupt every 68 minutes). 

    Best regards
    Torbjørn

Reply Children
No Data
Related