857f329aa9f1fa1ac3305947e34121d23d950841
[des2015.git] / mart / ex07 / ex07a.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5
6 #include <native/task.h>
7 #include <native/timer.h>
8 #include <native/sem.h>
9
10 #include <rtdk.h>
11 #include <sys/io.h>
12
13 #define NTASKS 3
14
15 RT_TASK demo_task[NTASKS];
16 RT_SEM mysync;
17
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
23 // in 10 jiffies
24 #define BASEPERIOD 1e6 // baseperiod in ns.
25
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
29
30 void demo(void *arg)
31 {
32 RTIME starttime;
33 RTIME runtime;
34 int num = *(int *)arg;
35 RT_TASK *curtask;
36 RT_TASK_INFO curtaskinfo;
37
38 rt_printf("Task : %d\n", num);
39
40 rt_sem_p(&mysync,TM_INFINITE);
41
42 // let the task run RUNTIME(=200) jiffies in steps of SPINTIME(=20) jiffies
43 runtime = 0;
44 while(runtime < EXECTIME) {
45 rt_timer_spin(SPINTIME*BASEPERIOD); // spin cpu doing nothing
46 // note: rt_timer_spin function does not accept jiffies only nanoseconds
47 // deviates from timing conventions throughout the Xenomai API
48
49 runtime += SPINTIME;
50
51 rt_printf("Running Task: %d at time: %d\n", num, runtime);
52 }
53 rt_printf("End Task: %d\n", num);
54 }
55
56 //startup code
57 void startup()
58 {
59 int i;
60 char str[10] ;
61
62 // semaphore to sync task startup on
63 rt_sem_create(&mysync, "MySemaphore", 0, S_FIFO);
64
65 // change to period mode because round robin does not work
66 // in one shot mode
67 rt_timer_set_mode(BASEPERIOD);// set tick period
68 for(i = 0; i < NTASKS; i++) {
69 rt_printf("start task: %d\n", i);
70 sprintf(str, "task%d", i);
71 rt_task_create(&demo_task[i], str, 0, 50, 0);
72 rt_task_start(&demo_task[i], &demo, &i);
73 }
74 rt_printf("wake up all tasks\n");
75 rt_sem_broadcast(&mysync);
76 }
77
78 void init_xenomai() {
79 /* Avoids memory swapping for this program */
80 mlockall(MCL_CURRENT | MCL_FUTURE);
81
82 /* Perform auto-init of rt_print buffers if the task doesn't do so */
83 rt_print_auto_init(1);
84 }
85
86 int main(int argc, char* argv[])
87 {
88 printf("\nType CTRL-C to end this program\n\n" );
89
90 // code to set things to run xenomai
91 init_xenomai();
92
93 //startup code
94 startup();
95
96 // wait for CTRL-c is typed to end the program
97 pause();
98 }