#include #include #include #include static struct timespec r; static int period; #define NSEC_PER_SEC 1000000000ULL static inline void timespec_add_us(struct timespec *t, uint64_t d) { d *= 1000; d += t->tv_nsec; while (d >= NSEC_PER_SEC) { d -= NSEC_PER_SEC; t->tv_sec += 1; } t->tv_nsec = d; } static void wait_next_activation(void) { clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &r, NULL); timespec_add_us(&r, period); } static int start_periodic_timer(uint64_t offs, int t) { clock_gettime(CLOCK_REALTIME, &r); timespec_add_us(&r, offs); period = t; return 0; } int main(int argc, char *argv[]) { int res; res = start_periodic_timer(1000000, 50000); if (res < 0) { perror("Start Periodic Timer"); return -1; } while(1) { wait_next_activation(); /* Begin of the Major cycle */ task1(); task2(); task3(); wait_next_activation(); /* 50 */ task1(); wait_next_activation(); /* 100 */ task1(); task2(); wait_next_activation(); /* 150 */ task1(); task3(); wait_next_activation(); /* 200 */ task1(); task2(); wait_next_activation(); /* 250 */ task1(); } return 0; }