Added 10 a b real time result
[des2015.git] / natanael / ex10 / ex10a.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <unistd.h>
4 #include <sys/mman.h>
5 #include <native/intr.h>
6 #include <sys/io.h>
7 #include <native/task.h>
8
9 const RTIME period = 1e5;
10 const int nsamples = 10000;
11
12 RT_TASK task;
13 RTIME write_time;
14 RTIME arr_write_time[10000];
15 RTIME time_diff[10000];
16
17 void write_RTIMES(char * filename, unsigned int number_of_values,
18 RTIME *time_values){
19 unsigned int n=0;
20 FILE *file;
21 file = fopen(filename,"w");
22 while (n<number_of_values) {
23 fprintf(file,"%u;%llu\n",n,time_values[n]);
24 n++;
25 }
26 fclose(file);
27 }
28
29 void do_task(void *arg)
30 {
31 RT_TASK *curtask;
32 RT_TASK_INFO curtaskinfo;
33 int i;
34
35 // inquire current task
36 curtask=rt_task_self();
37 rt_task_inquire(curtask,&curtaskinfo);
38
39 rt_task_set_periodic(NULL, TM_NOW, period);
40
41 for(i=0; i<nsamples; i++)
42 {
43 write_time = rt_timer_read();
44 arr_write_time[i] = write_time;
45
46 rt_task_wait_period(NULL);
47
48 time_diff[i] = rt_timer_read() - write_time;
49 }
50 write_RTIMES("time_diff.csv",nsamples,time_diff);
51
52 }
53
54 //startup code
55 void startup()
56 {
57 rt_task_create(&task, NULL,0,50,0);
58 rt_task_start(&task, &do_task, NULL);
59 }
60
61 void init_xenomai() {
62 /* Avoids memory swapping for this program */
63 mlockall(MCL_CURRENT|MCL_FUTURE);
64
65 /* Perform auto-init of rt_print buffers if the task doesn't do so */
66 rt_print_auto_init(1);
67 }
68
69 int main(int argc, char* argv[])
70 {
71 printf("\nType CTRL-C to end this program\n\n" );
72
73 // code to set things to run xenomai
74 init_xenomai();
75
76 //startup code
77 startup();
78
79 // wait for CTRL-c is typed to end the program
80 pause();
81 }