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.