Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Novice
1,632 Views

Linux timer interval

Hello I want to run a timer with interval of 5 ms. I created a Linux timer and when a sigalrm_handler is called I'm checking elapsed time from a previous call. I'm getting times like: 4163, 4422, 4266, 4443, 4470 4503, 4288 microseconds when I want intervals to be about 5000 microseconds with a least possible error. Here is my code:

# include

# include

# include

# include

static int time_count;

static int counter;

struct itimerval timer={0};

void sigalrm_handler(int signum)

{

Serial.print("SIGALRM received, time: ");

Serial.println(micros()-time_count);

time_count=micros();

counter++;

if(counter > 20) {

timer.it_interval.tv_usec = 0;

timer.it_value.tv_sec = 0;

setitimer(ITIMER_REAL, &timer, NULL);

}

}

void setup() {

// put your setup code here, to run once:

Serial.begin(9600);

/* Initial timeout value */

timer.it_value.tv_sec = 1;

/* We want a repetitive timer */

timer.it_interval.tv_usec = 5000;

/* Register Signal handler

* And register for periodic timer with Kernel*/

signal(SIGALRM, &sigalrm_handler);

setitimer(ITIMER_REAL, &timer, NULL);

time_count = micros();

}

0 Kudos
5 Replies
Highlighted
Community Manager
11 Views

While I am not an expert on these matters, I believe that timing at the microsecond level is impractical for the stated operating system. I had similar timing challenges in using the DHTXX (temperature, humidity) sensors in any practical manner.

Regards.

0 Kudos
Highlighted
Community Manager
11 Views

Hello Michal,

 

 

Thank you for your interest in the Intel Edison board.

 

 

Unfortunately, we cannot comment or debug your code.

 

However, Edison is not running a real time OS and so the desired time precision (ms) may not be possible.

 

 

Regards,

 

Octavian
0 Kudos
Highlighted
Community Manager
11 Views

Hello Michal,

 

 

Do you require any more assistance with this issue?

 

 

Regards,

 

Octavian
0 Kudos
Highlighted
Novice
11 Views

I have made it by creating a function that tuns in a separate thread and in a loop it has a delay providing by a micros() function.

0 Kudos
Highlighted
Valued Contributor I
11 Views

Matha is right. Under load you might get 100ms error. Try f.i. running iperf simultaneously with your code.

With a preempt_rt version of the kernel you might achieve 100us or so.

Nevertheless it is strange that you measure less then 5ms, the latencies should be causing longer periods not shorter.

I have a piece of code that transmits data on fixed intervals. I use timerfd_create(CLOCK_REALTIME, 0) to create file descriptor containing timer events, then select() on the fd. This works for me (and even better on preempt_rt). If you're interested: https://github.com/htot/hs_uart/blob/master/main.c hs_uart/main.c at master · htot/hs_uart · GitHub from line # 147

0 Kudos