User avatar
RedDragon
Posts: 13
Joined: Sun Jun 03, 2018 5:57 am
Contact: Website

Time measurement

Sun Apr 28, 2019 10:51 am

I am working on an RC meter. It's need four digit time measurement. I have tried nanosleep, interrupt routines, but all that fail with three digit repetability.

As an alternative IRQ I would like to use DMA initiated by GPIO. Sounds crazy, but if the RX periferies selected to the GPIO can initiate a transfer from a free running counter. It is teoretical, any suggestion, advice example program appritiated!

User avatar
joan
Posts: 14016
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Time measurement

Sun Apr 28, 2019 11:07 am

In effect that is how pigpio works. You found that using the mouse cursor (which also uses DMA) introduced jitter. The same jitter will be present on any DMA based solution.

jahboater
Posts: 4453
Joined: Wed Feb 04, 2015 6:38 pm

Re: Time measurement

Sun Apr 28, 2019 11:14 am

Just for interest, take a look at the POSIX clocks:-

Code: Select all

static uint64_t
counter( void )
{
  struct timespec now;
  clock_gettime( CLOCK_MONOTONIC, &now );
  return (uint64_t)now.tv_sec * 1000000000U + (uint64_t)now.tv_nsec;
}
This returns nano-seconds as a 64-bit unsigned number, monotonically increasing, probably since system boot.
The actual resolution looks like microseconds.
Call it twice to measure time of course.

There is also CLOCK_MONOTONIC_RAW (constant rate, totally unaffected by NTP micro-adjustments) and CLOCK_REALTIME (wallclock time in nsec, but of couse subject to clock adjustments and dst etc).

See "man clock_gettime" for all the details.

User avatar
RedDragon
Posts: 13
Joined: Sun Jun 03, 2018 5:57 am
Contact: Website

Re: Time measurement

Sun Apr 28, 2019 12:14 pm

Thank you for replies! I tried POSIX time stamp, too, with the same results. I think joan has right, there is a jitter between GPIO change and reading the time stamp. The time stamp must be always correct in all kind of solution, but GPIO change -> reading jitter should be avoided somehow or stabilized to a constatnt delay. Eg. the first interrupt rutin the processor checks should be mine, than continue with the usual order not to froze the system. I am afraid that is not soluble on arm. If you had any idea, how to stabilize the jitter, I would highly aprriciated.

Return to “C/C++”