Added ex09b
[des2015.git] / natanael / ex09 / ex09a2.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 RT_INTR keypress;
10 RT_TASK key_isr;
11 RT_TASK dummy;
12 #define KEYBOARD_IRQ 1
13
14 void key_handler(void *arg)
15 {
16 int count = 0;
17 int nr_interrupts_waiting;
18
19 RT_TASK *curtask;
20 RT_TASK_INFO curtaskinfo;
21
22 while(1) {
23 //rt_printf("%d\n",count++);
24 nr_interrupts_waiting = rt_intr_wait(&keypress,TM_INFINITE);
25 if (nr_interrupts_waiting>0)
26 {
27 // inquire current task
28 curtask=rt_task_self();
29 rt_task_inquire(curtask,&curtaskinfo);
30 rt_printf("Current priority of handler task: %d \n", curtaskinfo.cprio);
31 }
32 }
33 }
34
35 void dummy_task(void *arg) {
36 while(1) { rt_printf("Sleeping\n");
37 rt_task_sleep(2e9);
38 rt_printf("Spinning\n");
39 rt_timer_spin(3e9);
40 }
41 }
42
43 //startup code
44 void startup()
45 {
46 rt_intr_create(&keypress, NULL, KEYBOARD_IRQ, I_PROPAGATE);
47
48 rt_task_create(&key_isr, NULL,0,50,0);
49 rt_task_start(&key_isr, &key_handler, NULL);
50
51 // Higher priority
52 rt_task_create(&dummy, NULL,0,53,0);
53 rt_task_start(&dummy, &dummy_task, NULL);
54 }
55
56 void init_xenomai() {
57 /* Avoids memory swapping for this program */
58 mlockall(MCL_CURRENT|MCL_FUTURE);
59
60 /* Perform auto-init of rt_print buffers if the task doesn't do so */
61 rt_print_auto_init(1);
62 }
63
64 int main(int argc, char* argv[])
65 {
66 printf("\nType CTRL-C to end this program\n\n" );
67
68 // code to set things to run xenomai
69 init_xenomai();
70
71 //startup code
72 startup();
73
74 // wait for CTRL-c is typed to end the program
75 pause();
76 }