Exemple #1
0
static void timer_code(void *arg)
{
    long long int t1, t2, t3;
    long int tdiff1, tdiff2, tdiff3;
    char buf[30];

    while (1) {

	/* a simple counter... */
	timer_count++;
	/* print the results every second */
	if (timer_count % 1000 == 0) {
	    t1 = rtapi_get_time();
	    t2 = rtapi_get_time();
	    rtapi_delay(rtapi_delay_max());
	    t3 = rtapi_get_time();
	    tdiff1 = t2 - t1;
	    tdiff2 = t3 - t2;
	    tdiff3 = t3 - t1;
	    long_long_to_dec_str(buf, t1);
	    rtapi_print("T1 = %s\n", buf);
	    long_long_to_dec_str(buf, t2);
	    rtapi_print("T2 = %s\n", buf);
	    long_long_to_dec_str(buf, t3);
	    rtapi_print("T3 = %s\n", buf);
	    rtapi_print("Tdiff1 = %ld\n", tdiff1);
	    rtapi_print("Tdiff2 = %ld\n", tdiff2);
	    rtapi_print("Tdiff3 = %ld\n", tdiff3);
	}
	/* put the task to sleep until the next interrupt */
	rtapi_wait();
    }

    return;
}
Exemple #2
0
/* rtapi_app_main() is expanded to init_module() via a macro in
   rtapi_app.h */
int rtapi_app_main(void)
{
    int retval;
    int timer_prio;
    long period;

    module = rtapi_init("TIMERTASK");
    if (module < 0) {
	rtapi_print("timertask init: rtapi_init returned %d\n", module);
	return -1;
    }

    /* is timer started? if so, what period? */
    period = rtapi_clock_set_period(0);
    if (period == 0) {
	/* not running, start it */
	rtapi_print("timertask init: starting timer with period %ld\n",
	    TIMER_PERIOD_NSEC);
	period = rtapi_clock_set_period(TIMER_PERIOD_NSEC);
	if (period < 0) {
	    rtapi_print
		("timertask init: rtapi_clock_set_period failed with %ld\n",
		period);
	    rtapi_exit(module);
	    return -1;
	}
    }
    /* make sure period <= desired period (allow 1% roundoff error) */
    if (period > (TIMER_PERIOD_NSEC + (TIMER_PERIOD_NSEC / 100))) {
	/* timer period too long */
	rtapi_print("timertask init: clock period too long: %ld\n", period);
	rtapi_exit(module);
	return -1;
    }
    rtapi_print("timertask init: desired clock %ld, actual %ld\n",
	TIMER_PERIOD_NSEC, period);

    /* set the task priority to second lowest, since we only have one task */
    timer_prio = rtapi_prio_next_higher(rtapi_prio_lowest());

    /* create the timer task */
    /* the second arg is an abitrary int that is passed to the timer task on
       the first iterration */
    timer_task = rtapi_task_new(timer_code, 0 /* arg */ , timer_prio, module,
	TIMER_STACKSIZE, RTAPI_NO_FP);
    if (timer_task < 0) {
	/* See rtapi.h for the error codes returned */
	rtapi_print("timertask init: rtapi_task_new returned %d\n",
	    timer_task);
	rtapi_exit(module);
	return -1;
    }
    /* start the task running */
    retval = rtapi_task_start(timer_task, TASK_PERIOD_NSEC);
    if (retval < 0) {
	rtapi_print("timertask init: rtapi_task_start returned %d\n", retval);
	rtapi_exit(module);
	return -1;
    }
    rtapi_print("timertask init: started timer task\n");
    rtapi_print("timertask init: max delay = %ld\n", rtapi_delay_max());
    return 0;
}
Exemple #3
0
void rtapi_delay(long ns) {
    if(ns > rtapi_delay_max()) ns = rtapi_delay_max();
    struct timespec ts = {0, ns};
    rtapi_clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL, NULL);
}