#include #include #include #include #define MINOR_CYCLE 50 #define MAJOR_CYCLE 300 static uint8_t scheduling_table[] = {7, 1, 3, 5, 3, 1}; 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; int t = 0; res = start_periodic_timer(1000000, MINOR_CYCLE * 1000); if (res < 0) { perror("Start Periodic Timer"); return -1; } while(1) { uint8_t scheduled_tasks; wait_next_activation(); scheduled_tasks = scheduling_table[t / MINOR_CYCLE]; if (scheduled_tasks & 0x01) { task1(); } if (scheduled_tasks & 0x02) { task2(); } if (scheduled_tasks & 0x04) { task3(); } t = (t + MINOR_CYCLE) % MAJOR_CYCLE; } return 0; }