6 #include <native/task.h>
7 #include <native/timer.h>
8 #include <native/sem.h>
15 RT_TASK demo_task
[NTASKS
];
18 // set new baseperiod for the clock :
19 // - clock only increments counter when a base period is passed
20 // - the unit of the clock counter is called a jiffie, which in fact
21 // represents that a baseperiod is passed
22 // e.g. If 10 baseperiods are passed, the clock gives an increase
24 #define BASEPERIOD 1e6 // baseperiod in ns.
26 // when base period is set, all times in the api are expressed in jiffies
27 #define EXECTIME 200 // execution time in jiffies
28 #define SPINTIME 10 // spin time in jiffies
32 rt_task_set_mode(0, T_RRB
, NULL
);
35 int num
= *(int *)arg
;
37 RT_TASK_INFO curtaskinfo
;
39 rt_printf("Task: %d\n", num
);
41 rt_sem_p(&mysync
, TM_INFINITE
);
43 // let the task run RUNTIME(=200) jiffies in steps of SPINTIME(=20) jiffies
45 while(runtime
< EXECTIME
) {
46 rt_timer_spin(SPINTIME
*BASEPERIOD
); // spin cpu doing nothing
47 // note: rt_timer_spin function does not accept jiffies only nanoseconds
48 // deviates from timing conventions throughout the Xenomai API
50 runtime
= runtime
+ SPINTIME
;
52 rt_printf("Running Task: %d at time: %d\n", num
, runtime
);
54 rt_printf("End Task: %d\n", num
);
63 // semaphore to sync task startup on
64 rt_sem_create(&mysync
, "MySemaphore", 0, S_FIFO
);
66 // change to period mode because round robin does not work
68 rt_timer_set_mode(BASEPERIOD
);// set tick period
69 for(i
= 0; i
< NTASKS
; i
++) {
70 rt_printf("start task: %d\n", i
);
71 sprintf(str
, "task%d", i
);
72 rt_task_create(&demo_task
[i
], str
, 0, 50, 0);
73 rt_task_slice(&demo_task
[i
], 1);
74 rt_task_start(&demo_task
[i
], &demo
, &i
);
76 rt_printf("wake up all tasks\n");
77 rt_sem_broadcast(&mysync
);
81 /* Avoids memory swapping for this program */
82 mlockall(MCL_CURRENT
| MCL_FUTURE
);
84 /* Perform auto-init of rt_print buffers if the task doesn't do so */
85 rt_print_auto_init(1);
88 int main(int argc
, char* argv
[])
90 printf("\nType CTRL-C to end this program\n\n" );
92 // code to set things to run xenomai
98 // wait for CTRL-c is typed to end the program