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

Revision 243, 4.5 kB (checked in by agr, 1 year 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 *******************************************************************************/
23
24
25 #include <nrk.h>
26 #include <include.h>
27 #include <ulib.h>
28 #include <stdio.h>
29 #include <avr/sleep.h>
30 #include <hal.h>
31 #include <nrk_error.h>
32 #include <nrk_timer.h>
33 #include <nrk_reserve.h>
34 #include <nrk_stack_check.h>
35
36
37 NRK_STK Stack1[NRK_APP_STACKSIZE];
38 nrk_task_type TaskOne;
39 void Task1 (void);
40
41 NRK_STK Stack2[NRK_APP_STACKSIZE];
42 nrk_task_type TaskTwo;
43 void Task2 (void);
44
45
46 void nrk_create_taskset ();
47
48 int main ()
49 {
50   uint8_t t;
51   nrk_setup_ports ();
52   nrk_setup_uart (UART_BAUDRATE_115K2);
53
54   printf ("Starting up...\r\n");
55
56   nrk_init ();
57
58   nrk_led_clr (ORANGE_LED);
59   nrk_led_clr (BLUE_LED);
60   nrk_led_set (GREEN_LED);
61   nrk_led_clr (RED_LED);
62
63   nrk_time_set (0, 0);
64   nrk_create_taskset ();
65   nrk_start ();
66
67   return 0;
68 }
69
70 void my_error ()
71 {
72   nrk_kprintf (PSTR ("DOH, Reserve error handler called...\r\n"));
73 }
74
75 void Task1 ()
76 {
77   uint16_t cnt;
78   int8_t v;
79   int8_t my_rsv;
80   nrk_time_t t;
81
82   my_rsv = nrk_reserve_create ();
83   if (my_rsv == NRK_ERROR)
84     nrk_kprintf (PSTR ("ERROR creating reserve\r\n"));
85   t.secs = 10;
86   t.nano_secs = 0;
87 // create a 10 second reserve with 10 accesses
88 // my_error is called when the reserve is violated
89   v = nrk_reserve_set (my_rsv, &t, 10, &my_error);
90   if (v == NRK_ERROR)
91     nrk_kprintf (PSTR ("ERROR setting reserve\r\n"));
92
93   printf ("My node's address is %d\r\n", NODE_ADDR);
94
95   printf ("Task1 PID=%d\r\n", nrk_get_pid ());
96   cnt = 0;
97   while (1) {
98     nrk_led_toggle (ORANGE_LED);
99     printf ("Task1 cnt=%d\r\n", cnt);
100     v = nrk_reserve_consume (my_rsv);
101     if (v == NRK_ERROR)
102       nrk_kprintf (PSTR ("Task 1: ERROR consuming reserve\r\n"));
103     v = nrk_reserve_get (my_rsv);
104     printf ("Reserve value: %d\r\n", v);
105     nrk_wait_until_next_period ();
106     cnt++;
107   }
108 }
109
110 void Task2 ()
111 {
112   uint8_t cnt;
113   int8_t my_other_rsv, v;
114   nrk_time_t t;
115
116   my_other_rsv = nrk_reserve_create ();
117   if (my_other_rsv == NRK_ERROR)
118     nrk_kprintf (PSTR ("ERROR creating reserve\r\n"));
119   t.secs = 10;
120   t.nano_secs = 0;
121   // create a 10 second reserve with 10 accesses
122   // No function is called when the reserve is violated
123   v = nrk_reserve_set (my_other_rsv, &t, 10, NULL);
124   if (v == NRK_ERROR)
125     nrk_kprintf (PSTR ("ERROR setting reserve\r\n"));
126
127   printf ("Task2 PID=%d\r\n", nrk_get_pid ());
128   cnt = 0;
129   while (1) {
130     // This will silently fail half of the time
131     if (nrk_reserve_consume (my_other_rsv) != NRK_ERROR) {
132       nrk_led_toggle (BLUE_LED);
133       printf ("Task2 cnt=%d\r\n", cnt);
134       cnt++;
135     }
136     nrk_wait_until_next_period ();
137   }
138 }
139
140
141 void nrk_create_taskset ()
142 {
143   TaskOne.task = Task1;
144   TaskOne.Ptos = (void *) &Stack1[NRK_APP_STACKSIZE];
145   TaskOne.Pbos = (void *) &Stack1[0];
146   TaskOne.prio = 1;
147   TaskOne.FirstActivation = TRUE;
148   TaskOne.Type = BASIC_TASK;
149   TaskOne.SchType = PREEMPTIVE;
150   TaskOne.period.secs = 0;
151   TaskOne.period.nano_secs = 500 * NANOS_PER_MS;
152   TaskOne.cpu_reserve.secs = 0;
153   TaskOne.cpu_reserve.nano_secs = 50 * NANOS_PER_MS;
154   TaskOne.offset.secs = 0;
155   TaskOne.offset.nano_secs = 0;
156   nrk_activate_task (&TaskOne);
157
158   TaskTwo.task = Task2;
159   TaskTwo.Ptos = (void *) &Stack2[NRK_APP_STACKSIZE];
160   TaskTwo.Pbos = (void *) &Stack2[0];
161   TaskTwo.prio = 2;
162   TaskTwo.FirstActivation = TRUE;
163   TaskTwo.Type = BASIC_TASK;
164   TaskTwo.SchType = PREEMPTIVE;
165   TaskTwo.period.secs = 0;
166   TaskTwo.period.nano_secs = 500 * NANOS_PER_MS;
167   TaskTwo.cpu_reserve.secs = 0;
168   TaskTwo.cpu_reserve.nano_secs = 100 * NANOS_PER_MS;
169   TaskTwo.offset.secs = 0;
170   TaskTwo.offset.nano_secs = 0;
171   nrk_activate_task (&TaskTwo);
172
173
174 }
Note: See TracBrowser for help on using the browser.