Mercurial > templog
annotate main.c @ 4:54d369e3d689
Fill out more main.c structure
Add onewire files
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 09 May 2012 00:22:28 +0800 |
parents | 888be1b234b6 |
children | 011160e5b1ce |
rev | line source |
---|---|
0 | 1 /* Name: main.c |
2 * Author: <insert your name here> | |
3 * Copyright: <insert your copyright message here> | |
4 * License: <insert your license reference here> | |
5 */ | |
6 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
1 | 9 #include <avr/io.h> |
10 #include <avr/interrupt.h> | |
11 #include <avr/sleep.h> | |
0 | 12 #include <util/crc16.h> |
13 | |
3 | 14 #include "integer.h" |
15 | |
0 | 16 // configuration params |
17 // - measurement interval | |
18 // - transmit interval | |
19 // - bluetooth params | |
20 // - number of sensors (and range?) | |
21 | |
1 | 22 // 1 second. we have 1024 prescaler, 32768 crystal. |
2 | 23 #define SLEEP_COMPARE 32 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
24 #define MEASURE_WAKE 60 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
25 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
26 #define COMMS_WAKE 3600 |
2 | 27 |
28 #define BAUD 9600 | |
29 #define UBRR ((F_CPU)/16/(BAUD)-1) | |
1 | 30 |
0 | 31 static int uart_putchar(char c, FILE *stream); |
32 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, | |
33 _FDEV_SETUP_WRITE); | |
34 | |
35 static uint8_t n_measurements; | |
36 static uint8_t measurements[500]; | |
37 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
38 // boolean flags |
1 | 39 static uint8_t need_measurement; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
40 static uint8_t need_comms; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
41 static uint8_t comms_done; |
1 | 42 |
0 | 43 static uint8_t readpos; |
44 static char readbuf[30]; | |
45 | |
1 | 46 static uint8_t sec_count; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
47 static uint16_t bt_count; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
48 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
49 static void deep_sleep(); |
1 | 50 |
0 | 51 static void |
2 | 52 uart_init(unsigned int ubrr) |
0 | 53 { |
54 // baud rate | |
2 | 55 UBRR0H = (unsigned char)(ubrr >> 8); |
56 UBRR0L = (unsigned char)ubrr; | |
57 UCSR0B = (1<<RXEN0)|(1<<TXEN0); | |
0 | 58 //8N1 |
2 | 59 UCSR0C = (1<<UMSEL00)|(3<<UCSZ00); |
0 | 60 } |
61 | |
62 static int | |
63 uart_putchar(char c, FILE *stream) | |
64 { | |
65 // XXX should sleep in the loop for power. | |
2 | 66 loop_until_bit_is_set(UCSR0A, UDRE0); |
67 UDR0 = c; | |
0 | 68 return 0; |
69 } | |
70 | |
71 static void | |
72 cmd_fetch() | |
73 { | |
74 uint16_t crc = 0; | |
75 int i; | |
76 printf("%d measurements\n", n_measurements); | |
77 for (i = 0; i < n_measurements; i++) | |
78 { | |
79 printf("%3d : %d\n", i, measurements[i]); | |
1 | 80 crc = _crc_ccitt_update(crc, measurements[i]); |
0 | 81 } |
1 | 82 printf("CRC : %d\n", crc); |
0 | 83 } |
84 | |
85 static void | |
86 cmd_clear() | |
87 { | |
88 } | |
89 | |
90 static void | |
91 cmd_btoff() | |
92 { | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
93 comms_done = 1; |
0 | 94 } |
95 | |
96 static void | |
97 read_handler() | |
98 { | |
99 if (strcmp(readbuf, "fetch") == 0) | |
100 { | |
101 cmd_fetch(); | |
102 } | |
103 else if (strcmp(readbuf, "clear") == 0) | |
104 { | |
105 cmd_clear(); | |
106 } | |
107 else if (strcmp(readbuf, "btoff") == 0) | |
108 { | |
109 cmd_btoff(); | |
110 } | |
111 else | |
112 { | |
113 printf("Bad command\n"); | |
114 } | |
115 } | |
116 | |
2 | 117 ISR(USART_RX_vect) |
0 | 118 { |
2 | 119 char c = UDR0; |
0 | 120 if (c == '\n') |
121 { | |
122 readbuf[readpos] = '\0'; | |
123 read_handler(); | |
124 readpos = 0; | |
125 } | |
126 else | |
127 { | |
128 readbuf[readpos] = c; | |
129 readpos++; | |
130 if (readpos >= sizeof(readbuf)) | |
131 { | |
132 readpos = 0; | |
133 } | |
134 } | |
135 } | |
136 | |
2 | 137 ISR(TIMER2_COMPA_vect) |
1 | 138 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
139 measure_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
140 comms_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
141 if (measure_count == MEASURE_WAKE) |
1 | 142 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
143 measure_count = 0; |
1 | 144 need_measurement = 1; |
145 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
146 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
147 if (comms_count == COMMS_WAKE) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
148 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
149 comms_count = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
150 need_comms = 1; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
151 } |
1 | 152 } |
153 | |
3 | 154 DWORD get_fattime (void) |
155 { | |
156 return 0; | |
157 } | |
158 | |
1 | 159 static void |
160 deep_sleep() | |
161 { | |
162 // p119 of manual | |
2 | 163 OCR2A = SLEEP_COMPARE; |
164 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 165 |
166 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
167 sleep_mode(); | |
168 } | |
169 | |
170 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
171 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
172 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
173 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
174 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
175 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
176 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
177 static void |
1 | 178 do_measurement() |
179 { | |
180 need_measurement = 0; | |
181 } | |
182 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
183 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
184 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
185 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
186 need_comms = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
187 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
188 // turn on bluetooth |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
189 // turn on serial (interrupts) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
190 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
191 // write sd card here? same 3.3v regulator... |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
192 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
193 comms_done = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
194 for (;;) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
195 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
196 if (comms_done) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
197 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
198 break; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
199 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
200 idle_sleep(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
201 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
202 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
203 // turn off bluetooth |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
204 // turn off serial (interrupts) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
205 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
206 |
0 | 207 int main(void) |
208 { | |
2 | 209 uart_init(UBRR); |
0 | 210 |
211 fprintf(&mystdout, "hello %d\n", 12); | |
212 | |
1 | 213 // set up counter2. |
214 // COM21 COM20 Set OC2 on Compare Match (p116) | |
215 // WGM21 Clear counter on compare | |
216 // CS22 CS21 CS20 clk/1024 | |
2 | 217 TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
218 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); | |
1 | 219 // set async mode |
220 ASSR |= _BV(AS2); | |
0 | 221 |
222 for(;;){ | |
223 /* insert your main loop code here */ | |
1 | 224 if (need_measurement) |
225 { | |
226 do_measurement(); | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
227 continue; |
1 | 228 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
229 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
230 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
231 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
232 do_comms(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
233 continue; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
234 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
235 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
236 deep_sleep(); |
0 | 237 } |
238 return 0; /* never reached */ | |
239 } |