1

I use Raspian on the Raspberry B+ to get 1700 nsec (+- 10%) pulses on a GPIO output. Thus, I need a high-resolution wallclock timer. There are several references to clock_gettime for high-resolution timing (e.g. 1, 2). However, I get 1. only a microsecond resolution and 2. not sufficient minimum time with this short code:

int start_time, current_time, elapsed_time;
struct timespec resolution;

clock_gettime(CLOCK_MONOTONIC, &resolution);
start_time = Resolution.tv_nsec;

clock_gettime(CLOCK_MONOTONIC, &resolution);
current_time = resolution.tv_nsec;

elapsed_time = current_time - start_time;
if(elapsed_time < 0) {
  elapsed_time = elapsed_time + 1000000000; //in case clock_gettime wraps around
}

printf("%i\n", elapsed_time);

The result is 3000 (nanoseconds), i.e. even this shortest possible piece of code takes too much time. If I add some time-consuming code, the next greater result is 4000.

How I can I get a wallclock timer that will result in at least 100 nsec resolution and a smallest possible time of less than 1700 nsec? That the Raspberry Pi can do faster (100 nsec pulses with WiringPi) shows the GPIO Benchmark. I am aware that additional electronics (monoflop) can help me but I hope to solve the problem in a simpler way. Thank you.

Community
  • 1
  • 1
Michael Westwort
  • 864
  • 8
  • 22
  • 2
    My gut says you're never going to achieve this kind of timing with software-driven GPIO in Linux (or any non-RTOS) – Jonathon Reinhart Feb 28 '15 at 22:31
  • But just switching the GPIO on and off as in the Benchmark (with one software command each) is much faster. How can it be that processing a software command to switch on/off the GPIO takes just 100 nanoseconds while querying the clock (which seems a very basic command to me and which might be optimised for time-consumption) takes microseconds? – Michael Westwort Feb 28 '15 at 22:47
  • Adding a piece of code that consumes some time (just as a `for` loop) gives reproducible results - but not as reliably reproducible as using a timer, of course. By the way: I expected a "high-resolution" timer on the fast and close-to-hardware language C to give better results than just microseconds. – Michael Westwort Feb 28 '15 at 22:54
  • On Intel, you could use RDTSC to read the infamous Time_Stamp_Counter. I don't know if there is anything equivalent on your system. – chqrlie Mar 01 '15 at 02:39
  • There seems to be: http://stackoverflow.com/questions/3247373/how-to-measure-program-execution-time-in-arm-cortex-a8-processor – chqrlie Mar 01 '15 at 02:41

0 Answers0