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

Revision 389, 3.7 kB (checked in by agr, 6 months ago)

CTR bug fixed in encryption in bmac

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 #include <nrk.h>
25 #include <include.h>
26 #include <ulib.h>
27 #include <stdio.h>
28 #include <avr/sleep.h>
29 #include <hal.h>
30 #include <nrk_error.h>
31 #include <nrk_timer.h>
32 #include <nrk_driver_list.h>
33 #include <nrk_driver.h>
34 #include <adc_driver.h>
35
36
37 NRK_STK Stack1[NRK_APP_STACKSIZE];
38 nrk_task_type TaskOne;
39 void Task1(void);
40
41
42 void nrk_create_taskset();
43 void nrk_register_drivers();
44 uint8_t kill_stack(uint8_t val);
45
46 int
47 main ()
48 {
49   uint8_t t;
50   nrk_setup_ports();
51   nrk_setup_uart(UART_BAUDRATE_115K2);
52
53   printf( "Starting up...\r\n" );
54
55   nrk_init();
56   nrk_time_set(0,0);
57
58   nrk_register_drivers();
59   nrk_create_taskset ();
60   nrk_start();
61  
62   return 0;
63 }
64
65
66 void Task1()
67 {
68 uint16_t cnt;
69 int8_t fd,val,chan;
70 uint16_t buf[8];
71
72 printf( "My node's address is %d\r\n",NODE_ADDR );
73
74   printf( "Task1 PID=%d\r\n",nrk_get_pid());
75
76   // Open ADC device as read
77   fd=nrk_open(ADC_DEV_MANAGER,READ);
78   if(fd==NRK_ERROR) nrk_kprintf( PSTR("Failed to open ADC driver\r\n"));
79  
80   cnt=0;
81   chan=0;
82   val=nrk_set_status(fd,ADC_CHAN,5);
83   if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to set ADC status\r\n" ));
84   while(1) {
85         nrk_led_toggle(BLUE_LED);
86 /*      for(chan=0; chan<8; chan++ )
87         {
88           // Example of setting the ADC channel
89           val=nrk_set_status(fd,ADC_CHAN,chan);
90           if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to set ADC status\r\n" ));
91           val=nrk_read(fd,&buf[chan],2);
92           if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to read ADC\r\n" ));
93
94         }
95         printf( "Task1 " );
96         for(chan=0; chan<8; chan++ )
97         {
98         printf( "chan:%d=%d ",chan,buf[chan]);
99         }
100         nrk_kprintf( PSTR("\r\n" ));
101 */
102           val=nrk_read(fd,&buf[chan],2);
103           if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to read ADC\r\n" ));
104         printf( "%d\r\n",buf[chan]);
105
106         nrk_wait_until_next_period();
107         cnt++;
108         }
109 }
110
111
112 void
113 nrk_create_taskset()
114 {
115   TaskOne.task = Task1;
116   nrk_task_set_stk( &TaskOne, Stack1, NRK_APP_STACKSIZE);
117   TaskOne.prio = 1;
118   TaskOne.FirstActivation = TRUE;
119   TaskOne.Type = BASIC_TASK;
120   TaskOne.SchType = PREEMPTIVE;
121   TaskOne.period.secs = 0;
122   TaskOne.period.nano_secs = 5*NANOS_PER_MS;
123   TaskOne.cpu_reserve.secs = 0;
124   TaskOne.cpu_reserve.nano_secs =  50*NANOS_PER_MS;
125   TaskOne.offset.secs = 0;
126   TaskOne.offset.nano_secs= 0;
127   nrk_activate_task (&TaskOne);
128
129 }
130
131 void nrk_register_drivers()
132 {
133 int8_t val;
134
135 // Register the ADC device driver
136 // Make sure to add:
137 //     #define NRK_MAX_DRIVER_CNT 
138 //     in nrk_cfg.h
139 // Make sure to add:
140 //     SRC += $(ROOT_DIR)/src/drivers/platform/$(PLATFORM_TYPE)/source/adc_driver.c
141 //     in makefile
142 val=nrk_register_driver( &dev_manager_adc,ADC_DEV_MANAGER);
143 if(val==NRK_ERROR) nrk_kprintf( PSTR("Failed to load my ADC driver\r\n") );
144
145 }
146
147
Note: See TracBrowser for help on using the browser.