root/nano-RK/projects/basic_sem/main.c

Revision 243, 4.3 kB (checked in by agr, 10 months ago)

projects license update, remove docs

Line 
1 /******************************************************************************
2 *  Nano-RK, a real-time operating system for sensor networks.
3 *  Copyright (C) 2007, Real-Time and Multimedia Lab, Carnegie Mellon University
4 *  All rights reserved.
5 *
6 *  This is the Open Source Version of Nano-RK included as part of a Dual
7 *  Licensing Model. If you are unsure which license to use please refer to:
8 *  http://www.nanork.org/nano-RK/wiki/Licensing
9 *
10 *  This program is free software: you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License as published by
12 *  the Free Software Foundation, version 2.0 of the License.
13 *
14 *  This program is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 *
22 *  Contributing Authors (specific to this file):
23 *  Zane Starr
24 *******************************************************************************/
25
26
27 #include <nrk.h>
28 #include <include.h>
29 #include <ulib.h>
30 #include <stdio.h>
31 #include <avr/sleep.h>
32 #include <hal.h>
33 #include <nrk_error.h>
34 #include <nrk_events.h>
35 #include <nrk_timer.h>
36
37
38 NRK_STK Stack1[NRK_APP_STACKSIZE];
39 nrk_task_type TaskOne;
40 void Task1(void);
41
42 NRK_STK Stack2[NRK_APP_STACKSIZE];
43 nrk_task_type TaskTwo;
44 void Task2 (void);
45
46 void nrk_create_taskset();
47
48 nrk_sem_t *my_semaphore;
49
50 int
51 main ()
52 {
53   uint8_t t;
54   nrk_setup_ports();
55   nrk_setup_uart(UART_BAUDRATE_115K2);
56
57   printf( "Starting up...\r\n" );
58
59   nrk_init();
60
61   nrk_led_clr(ORANGE_LED);
62   nrk_led_clr(BLUE_LED);
63   nrk_led_set(GREEN_LED);
64   nrk_led_clr(RED_LED);
65  
66   nrk_time_set(0,0);
67   nrk_create_taskset ();
68
69   my_semaphore = nrk_sem_create(1,4);
70   if(my_semaphore==NULL) nrk_kprintf( PSTR("Error creating sem\r\n" ));
71   nrk_start();
72  
73   return 0;
74 }
75
76
77 void Task1()
78 {
79 uint16_t cnt;
80 int8_t v;
81
82 printf( "My node's address is %d\r\n",NODE_ADDR );
83
84   printf( "Task1 PID=%d\r\n",nrk_get_pid());
85   cnt=0;
86   while(1) {
87         nrk_led_toggle(ORANGE_LED);
88         printf( "Task1 cnt=%d\r\n",cnt );
89         nrk_kprintf( PSTR("Task1 accessing semaphore\r\n"));
90         v = nrk_sem_pend(my_semaphore);
91         if(v==NRK_ERROR) nrk_kprintf( PSTR("T1 error pend\r\n"));
92         nrk_kprintf( PSTR("Task1 holding semaphore\r\n"));
93         // wait some time inside semaphore to show the effect
94         nrk_wait_until_next_period();
95         v = nrk_sem_post(my_semaphore);
96         if(v==NRK_ERROR) nrk_kprintf( PSTR("T1 error post\r\n"));
97         nrk_kprintf( PSTR("Task1 released semaphore\r\n"));
98         nrk_wait_until_next_period();
99         cnt++;
100         }
101 }
102
103 void Task2()
104 {
105   uint8_t cnt;
106   int8_t v;
107
108   printf( "Task2 PID=%d\r\n",nrk_get_pid());
109   cnt=0;
110   while(1) {
111         nrk_led_toggle(ORANGE_LED);
112         printf( "Task1 cnt=%d\r\n",cnt );
113         nrk_kprintf( PSTR("Task2 accessing semaphore\r\n"));
114         v = nrk_sem_pend(my_semaphore);
115         if(v==NRK_ERROR) nrk_kprintf( PSTR("T2 error pend\r\n"));
116         nrk_kprintf( PSTR("Task2 holding semaphore\r\n"));
117         // wait some time inside semaphore to show the effect
118         nrk_wait_until_next_period();
119         v = nrk_sem_post(my_semaphore);
120         if(v==NRK_ERROR) nrk_kprintf( PSTR("T2 error post\r\n"));
121         nrk_kprintf( PSTR("Task2 released semaphore\r\n"));
122         nrk_wait_until_next_period();
123         cnt++;
124         }
125
126
127
128 }
129 void
130 nrk_create_taskset()
131 {
132   TaskOne.task = Task1;
133   TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE];
134   TaskOne.Pbos = (void *) &Stack1[0];
135   TaskOne.prio = 1;
136   TaskOne.FirstActivation = TRUE;
137   TaskOne.Type = BASIC_TASK;
138   TaskOne.SchType = PREEMPTIVE;
139   TaskOne.period.secs = 0;
140   TaskOne.period.nano_secs = 250*NANOS_PER_MS;
141   TaskOne.cpu_reserve.secs = 0;
142   TaskOne.cpu_reserve.nano_secs =  50*NANOS_PER_MS;
143   TaskOne.offset.secs = 0;
144   TaskOne.offset.nano_secs= 0;
145   nrk_activate_task (&TaskOne);
146
147   TaskTwo.task = Task2;
148   TaskTwo.Ptos = (void *) &Stack2[NRK_APP_STACKSIZE];
149   TaskTwo.Pbos = (void *) &Stack2[0];
150   TaskTwo.prio = 2;
151   TaskTwo.FirstActivation = TRUE;
152   TaskTwo.Type = BASIC_TASK;
153   TaskTwo.SchType = PREEMPTIVE;
154   TaskTwo.period.secs = 3;
155   TaskTwo.period.nano_secs = 0;
156   TaskTwo.cpu_reserve.secs = 0;
157   TaskTwo.cpu_reserve.nano_secs = 100*NANOS_PER_MS;
158   TaskTwo.offset.secs = 0;
159   TaskTwo.offset.nano_secs= 0;
160   nrk_activate_task (&TaskTwo);
161
162
163 }
164
165
166
Note: See TracBrowser for help on using the browser.