Mercurial > templog
annotate main.c @ 18:bf733e8e8cf0
Add INT0 button
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 22 May 2012 21:27:50 +0800 |
parents | 54b0fda9cba7 |
children | f1016b151689 |
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> | |
13 | 9 #include <stddef.h> |
1 | 10 #include <avr/io.h> |
11 #include <avr/interrupt.h> | |
12 #include <avr/sleep.h> | |
7 | 13 #include <util/delay.h> |
6 | 14 #include <avr/pgmspace.h> |
13 | 15 #include <avr/eeprom.h> |
0 | 16 #include <util/crc16.h> |
17 | |
12 | 18 // for DWORD of get_fattime() |
3 | 19 #include "integer.h" |
12 | 20 |
21 #include "simple_ds18b20.h" | |
13 | 22 #include "onewire.h" |
3 | 23 |
0 | 24 // configuration params |
25 // - measurement interval | |
26 // - transmit interval | |
27 // - bluetooth params | |
28 // - number of sensors (and range?) | |
29 | |
1 | 30 // 1 second. we have 1024 prescaler, 32768 crystal. |
2 | 31 #define SLEEP_COMPARE 32 |
14 | 32 #define MEASURE_WAKE 10 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
33 |
13 | 34 #define VALUE_NOSENSOR -9000 |
35 #define VALUE_BROKEN -8000 | |
36 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
37 #define COMMS_WAKE 3600 |
2 | 38 |
10 | 39 #define BAUD 19200 |
7 | 40 #define UBRR ((F_CPU)/8/(BAUD)-1) |
41 | |
42 #define PORT_LED PORTC | |
43 #define DDR_LED DDRC | |
44 #define PIN_LED PC4 | |
1 | 45 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
46 #define PORT_SHDN PORTD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
47 #define DDR_SHDN DDRD |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
48 #define PIN_SHDN PD7 |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
49 |
13 | 50 #define NUM_MEASUREMENTS 100 |
51 #define MAX_SENSORS 5 | |
6 | 52 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
53 // fixed at 8, have a shorter name |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
54 #define ID_LEN OW_ROMCODE_SIZE |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
55 |
10 | 56 int uart_putchar(char c, FILE *stream); |
14 | 57 static void long_delay(int ms); |
58 | |
0 | 59 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
60 _FDEV_SETUP_WRITE); | |
61 | |
13 | 62 static uint16_t n_measurements = 0; |
63 // stored as decidegrees | |
14 | 64 static int16_t measurements[NUM_MEASUREMENTS][MAX_SENSORS]; |
0 | 65 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
66 // boolean flags |
7 | 67 static uint8_t need_measurement = 0; |
68 static uint8_t need_comms = 0; | |
69 static uint8_t comms_done = 0; | |
1 | 70 |
7 | 71 static uint8_t readpos = 0; |
0 | 72 static char readbuf[30]; |
73 | |
7 | 74 static uint8_t measure_count = 0; |
75 static uint16_t comms_count = 0; | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
76 |
13 | 77 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ |
78 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) | |
79 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) | |
80 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) | |
81 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } | |
82 | |
83 #define EXPECT_MAGIC 0x67c9 | |
84 | |
85 struct __attribute__ ((__packed__)) __eeprom_data { | |
86 uint16_t magic; | |
87 uint8_t n_sensors; | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
88 uint8_t sensor_id[MAX_SENSORS][ID_LEN]; |
13 | 89 }; |
90 | |
8 | 91 #define DEBUG(str) printf_P(PSTR(str)) |
92 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
93 static void deep_sleep(); |
1 | 94 |
18 | 95 static void |
96 chip_setup() | |
97 { | |
98 // INT0 setup | |
99 EIMSK = _BV(INT0); | |
100 // set pullup | |
101 PORTD |= _BV(PD2); | |
102 } | |
103 | |
104 | |
0 | 105 static void |
7 | 106 uart_on() |
0 | 107 { |
8 | 108 // Power reduction register |
109 //PRR &= ~_BV(PRUSART0); | |
110 | |
0 | 111 // baud rate |
7 | 112 UBRR0H = (unsigned char)(UBRR >> 8); |
113 UBRR0L = (unsigned char)UBRR; | |
114 // set 2x clock, improves accuracy of UBRR | |
115 UCSR0A |= _BV(U2X0); | |
6 | 116 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
0 | 117 //8N1 |
7 | 118 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
6 | 119 } |
120 | |
121 static void | |
122 uart_off() | |
123 { | |
124 // Turn of interrupts and disable tx/rx | |
125 UCSR0B = 0; | |
126 | |
127 // Power reduction register | |
8 | 128 //PRR |= _BV(PRUSART0); |
0 | 129 } |
130 | |
10 | 131 int |
0 | 132 uart_putchar(char c, FILE *stream) |
133 { | |
10 | 134 // XXX should sleep in the loop for power. |
8 | 135 if (c == '\n') |
136 { | |
10 | 137 loop_until_bit_is_set(UCSR0A, UDRE0); |
138 UDR0 = '\r';; | |
8 | 139 } |
2 | 140 loop_until_bit_is_set(UCSR0A, UDRE0); |
141 UDR0 = c; | |
10 | 142 if (c == '\r') |
143 { | |
144 loop_until_bit_is_set(UCSR0A, UDRE0); | |
145 UDR0 = '\n';; | |
146 } | |
0 | 147 return 0; |
148 } | |
149 | |
150 static void | |
151 cmd_fetch() | |
152 { | |
153 uint16_t crc = 0; | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
154 uint8_t n_sensors; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
155 eeprom_read(n_sensors, n_sensors); |
13 | 156 |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
157 printf_P(PSTR("%d sensors\n"), n_measurements); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
158 for (uint8_t s = 0; s < n_sensors; s++) |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
159 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
160 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
161 printf_P(PSTR("%d : "), s); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
162 eeprom_read_to(id, sensor_id[s], ID_LEN); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
163 printhex(id, ID_LEN); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
164 putchar('\n'); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
165 for (uint8_t i = 0; i < ID_LEN; i++) |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
166 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
167 crc = _crc_ccitt_update(crc, id[i]); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
168 } |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
169 } |
6 | 170 printf_P(PSTR("%d measurements\n"), n_measurements); |
14 | 171 for (uint16_t n = 0; n < n_measurements; n++) |
0 | 172 { |
14 | 173 printf_P(PSTR("%3d :"), n); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
174 for (uint8_t s = 0; s < n_sensors; s++) |
13 | 175 { |
14 | 176 printf_P(PSTR(" %6d"), measurements[n][s]); |
177 crc = _crc_ccitt_update(crc, measurements[n][s]); | |
13 | 178 } |
179 putchar('\n'); | |
0 | 180 } |
6 | 181 printf_P(PSTR("CRC : %d\n"), crc); |
0 | 182 } |
183 | |
184 static void | |
185 cmd_clear() | |
186 { | |
7 | 187 n_measurements = 0; |
188 printf_P(PSTR("Cleared\n")); | |
0 | 189 } |
190 | |
191 static void | |
192 cmd_btoff() | |
193 { | |
7 | 194 printf_P(PSTR("Turning off\n")); |
195 _delay_ms(50); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
196 PORTD |= _BV(PIN_SHDN); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
197 comms_done = 1; |
0 | 198 } |
199 | |
200 static void | |
12 | 201 cmd_measure() |
202 { | |
203 printf_P(PSTR("Measuring\n")); | |
204 need_measurement = 1; | |
205 } | |
206 | |
207 static void | |
208 cmd_sensors() | |
209 { | |
210 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
14 | 211 printf_P(PSTR("All sensors, ret %d, waiting...\n"), ret); |
212 long_delay(DS18B20_TCONV_12BIT); | |
12 | 213 simple_ds18b20_read_all(); |
214 } | |
215 | |
13 | 216 // 0 on success |
217 static uint8_t | |
218 get_hex_string(const char *hex, uint8_t *out, uint8_t size) | |
219 { | |
220 uint8_t upper; | |
221 uint8_t o; | |
222 for (uint8_t i = 0, z = 0; o < size; i++) | |
223 { | |
224 uint8_t h = hex[i]; | |
225 if (h >= 'A' && h <= 'F') | |
226 { | |
227 // lower case | |
228 h += 0x20; | |
229 } | |
230 uint8_t nibble; | |
231 if (h >= '0' && h <= '9') | |
232 { | |
233 nibble = h - '0'; | |
234 } | |
235 else if (h >= 'a' && h <= 'f') | |
236 { | |
237 nibble = 10 + h - 'a'; | |
238 } | |
239 else if (h == ' ' || h == ':') | |
240 { | |
241 continue; | |
242 } | |
243 else | |
244 { | |
245 printf_P(PSTR("Bad hex 0x%x '%c'\n"), hex[i], hex[i]); | |
246 return 1; | |
247 } | |
248 | |
249 if (z % 2 == 0) | |
250 { | |
251 upper = nibble << 4; | |
252 } | |
253 else | |
254 { | |
255 out[o] = upper | nibble; | |
256 o++; | |
257 } | |
258 | |
259 z++; | |
260 } | |
261 | |
262 if (o != size) | |
263 { | |
264 printf_P(PSTR("Short hex\n")); | |
265 return 1; | |
266 } | |
267 return 0; | |
268 } | |
269 | |
270 static void | |
271 add_sensor(uint8_t *id) | |
272 { | |
273 uint8_t n; | |
274 eeprom_read(n, n_sensors); | |
275 if (n < MAX_SENSORS) | |
276 { | |
277 cli(); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
278 eeprom_write_from(id, sensor_id[n], ID_LEN); |
13 | 279 n++; |
280 eeprom_write(n, n_sensors); | |
281 sei(); | |
282 printf_P(PSTR("Added sensor %d : "), n); | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
283 printhex(id, ID_LEN); |
13 | 284 putchar('\n'); |
285 } | |
286 else | |
287 { | |
288 printf_P(PSTR("Too many sensors\n")); | |
289 } | |
290 } | |
291 | |
292 static void | |
293 cmd_add_all() | |
294 { | |
295 uint8_t id[OW_ROMCODE_SIZE]; | |
296 printf_P("Adding all\n"); | |
297 ow_reset(); | |
298 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) | |
299 { | |
300 diff = ow_rom_search( diff, &id[0] ); | |
301 if( diff == OW_PRESENCE_ERR ) { | |
302 printf_P( PSTR("No Sensor found\r") ); | |
303 return; | |
304 } | |
305 | |
306 if( diff == OW_DATA_ERR ) { | |
307 printf_P( PSTR("Bus Error\r") ); | |
308 return; | |
309 } | |
310 add_sensor(id); | |
311 } | |
312 } | |
313 | |
314 static void | |
315 cmd_add_sensor(const char* hex_addr) | |
316 { | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
317 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
318 uint8_t ret = get_hex_string(hex_addr, id, ID_LEN); |
13 | 319 if (ret) |
320 { | |
321 return; | |
322 } | |
323 add_sensor(id); | |
324 | |
325 } | |
326 | |
327 static void | |
328 cmd_init() | |
329 { | |
330 printf_P(PSTR("Resetting sensor list\n")); | |
331 uint8_t zero = 0; | |
332 cli(); | |
333 eeprom_write(zero, n_sensors); | |
334 sei(); | |
335 printf_P(PSTR("Done.\n")); | |
336 } | |
337 | |
338 static void | |
339 check_first_startup() | |
340 { | |
341 uint16_t magic; | |
342 eeprom_read(magic, magic); | |
343 if (magic != EXPECT_MAGIC) | |
344 { | |
345 printf_P(PSTR("First boot, looking for sensors...\n")); | |
346 cmd_init(); | |
347 cmd_add_all(); | |
14 | 348 cli(); |
349 magic = EXPECT_MAGIC; | |
350 eeprom_write(magic, magic); | |
351 sei(); | |
13 | 352 } |
353 } | |
354 | |
12 | 355 static void |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
356 cmd_toggle() |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
357 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
358 PORT_SHDN ^= _BV(PIN_SHDN); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
359 printf_P(PSTR("toggling power 3.3v %d\n"), PORT_SHDN & _BV(PIN_SHDN)); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
360 } |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
361 |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
362 static void |
0 | 363 read_handler() |
364 { | |
6 | 365 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
0 | 366 { |
367 cmd_fetch(); | |
368 } | |
6 | 369 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
0 | 370 { |
371 cmd_clear(); | |
372 } | |
6 | 373 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
0 | 374 { |
375 cmd_btoff(); | |
376 } | |
12 | 377 else if (strcmp_P(readbuf, PSTR("measure")) == 0) |
378 { | |
379 cmd_measure(); | |
380 } | |
381 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) | |
382 { | |
383 cmd_sensors(); | |
384 } | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
385 else if (strcmp_P(readbuf, PSTR("toggle")) == 0) |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
386 { |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
387 cmd_toggle(); |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
388 } |
13 | 389 else if (strncmp_P(readbuf, PSTR("adds "), strlen("adds ")) == 0) |
390 { | |
391 cmd_add_sensor(readbuf + strlen("adds ")); | |
392 } | |
393 else if (strcmp_P(readbuf, PSTR("addall"))== 0) | |
394 { | |
395 cmd_add_all(); | |
396 } | |
397 else if (strcmp_P(readbuf, PSTR("init")) == 0) | |
398 { | |
399 cmd_init(); | |
400 } | |
0 | 401 else |
402 { | |
6 | 403 printf_P(PSTR("Bad command\n")); |
0 | 404 } |
405 } | |
406 | |
18 | 407 ISR(INT0_vwct) |
408 { | |
409 need_comms = 1; | |
410 } | |
411 | |
412 | |
2 | 413 ISR(USART_RX_vect) |
0 | 414 { |
2 | 415 char c = UDR0; |
12 | 416 uart_putchar(c, NULL); |
417 if (c == '\r') | |
0 | 418 { |
419 readbuf[readpos] = '\0'; | |
420 read_handler(); | |
421 readpos = 0; | |
422 } | |
423 else | |
424 { | |
425 readbuf[readpos] = c; | |
426 readpos++; | |
427 if (readpos >= sizeof(readbuf)) | |
428 { | |
429 readpos = 0; | |
430 } | |
431 } | |
432 } | |
433 | |
2 | 434 ISR(TIMER2_COMPA_vect) |
1 | 435 { |
14 | 436 TCNT2 = 0; |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
437 measure_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
438 comms_count ++; |
12 | 439 printf("measure_count %d\n", measure_count); |
14 | 440 if (measure_count >= MEASURE_WAKE) |
1 | 441 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
442 measure_count = 0; |
12 | 443 printf("need_measurement = 1\n"); |
1 | 444 need_measurement = 1; |
445 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
446 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
447 if (comms_count == COMMS_WAKE) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
448 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
449 comms_count = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
450 need_comms = 1; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
451 } |
1 | 452 } |
453 | |
3 | 454 DWORD get_fattime (void) |
455 { | |
456 return 0; | |
457 } | |
458 | |
1 | 459 static void |
460 deep_sleep() | |
461 { | |
462 // p119 of manual | |
2 | 463 OCR2A = SLEEP_COMPARE; |
464 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 465 |
466 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
467 sleep_mode(); | |
468 } | |
469 | |
470 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
471 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
472 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
473 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
474 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
475 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
476 |
14 | 477 #if 0 |
478 // untested | |
6 | 479 static void |
480 do_adc_335() | |
481 { | |
8 | 482 //PRR &= ~_BV(PRADC); |
6 | 483 |
484 ADMUX = _BV(ADLAR); | |
485 | |
486 // ADPS2 = /16 prescaler, 62khz at 1mhz clock | |
487 ADCSRA = _BV(ADEN) | _BV(ADPS2); | |
488 | |
489 // measure value | |
490 ADCSRA |= _BV(ADSC); | |
491 loop_until_bit_is_clear(ADCSRA, ADSC); | |
492 uint8_t low = ADCL; | |
493 uint8_t high = ADCH; | |
494 uint16_t f_measure = low + (high << 8); | |
495 | |
496 // set to measure 1.1 reference | |
497 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
498 ADCSRA |= _BV(ADSC); | |
499 loop_until_bit_is_clear(ADCSRA, ADSC); | |
500 uint8_t low_11 = ADCL; | |
501 uint8_t high_11 = ADCH; | |
502 uint16_t f_11 = low_11 + (high_11 << 8); | |
503 | |
504 float res_volts = 1.1 * f_measure / f_11; | |
505 | |
506 // 10mV/degree | |
507 // scale to 1/5 degree units above 0C | |
508 int temp = (res_volts - 2.73) * 500; | |
13 | 509 // XXX fixme |
510 //measurements[n_measurements] = temp; | |
6 | 511 // XXX something if it hits the limit |
512 | |
513 // measure AVR internal temperature against 1.1 ref. | |
514 ADMUX = _BV(ADLAR) | _BV(MUX3) | _BV(REFS1) | _BV(REFS0); | |
515 ADCSRA |= _BV(ADSC); | |
516 loop_until_bit_is_clear(ADCSRA, ADSC); | |
517 uint16_t res_internal = ADCL; | |
518 res_internal |= ADCH << 8; | |
519 | |
520 float internal_volts = res_internal * (1.1 / 1024.0); | |
521 | |
522 // 1mV/degree | |
523 int internal_temp = (internal_volts - 2.73) * 5000; | |
13 | 524 // XXX fixme |
525 //internal_measurements[n_measurements] = internal_temp; | |
6 | 526 |
527 printf_P("measure %d: external %d, internal %d, 1.1 %d\n", | |
528 n_measurements, temp, internal_temp, f_11); | |
529 | |
530 n_measurements++; | |
8 | 531 //PRR |= _BV(PRADC); |
6 | 532 } |
14 | 533 #endif |
6 | 534 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
535 static void |
1 | 536 do_measurement() |
537 { | |
13 | 538 uint8_t n_sensors; |
14 | 539 printf("do_measurement\n"); |
13 | 540 eeprom_read(n_sensors, n_sensors); |
14 | 541 printf("do_measurement sensors %d\n", n_sensors); |
13 | 542 |
543 uint8_t ret = simple_ds18b20_start_meas(NULL); | |
14 | 544 printf_P(PSTR("Read all sensors, ret %d, waiting...\n"), ret); |
13 | 545 _delay_ms(DS18B20_TCONV_12BIT); |
546 | |
547 if (n_measurements == NUM_MEASUREMENTS) | |
548 { | |
14 | 549 printf_P(PSTR("Measurements .overflow\n")); |
13 | 550 n_measurements = 0; |
551 } | |
6 | 552 |
14 | 553 for (uint8_t s = 0; s < MAX_SENSORS; s++) |
13 | 554 { |
555 int16_t decicelsius; | |
14 | 556 if (s >= n_sensors) |
13 | 557 { |
558 decicelsius = VALUE_NOSENSOR; | |
559 } | |
560 else | |
561 { | |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
562 uint8_t id[ID_LEN]; |
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
563 eeprom_read_to(id, sensor_id[s], ID_LEN); |
13 | 564 |
565 uint8_t ret = simple_ds18b20_read_decicelsius(id, &decicelsius); | |
566 if (ret != DS18X20_OK) | |
567 { | |
568 decicelsius = VALUE_BROKEN; | |
569 } | |
570 } | |
14 | 571 measurements[n_measurements][s] = decicelsius; |
13 | 572 } |
14 | 573 n_measurements++; |
12 | 574 //do_adc_335(); |
1 | 575 } |
576 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
577 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
578 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
579 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
580 need_comms = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
581 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
582 // turn on bluetooth |
7 | 583 uart_on(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
584 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
585 // 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
|
586 |
12 | 587 printf("ready> \n"); |
588 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
589 comms_done = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
590 for (;;) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
591 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
592 if (comms_done) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
593 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
594 break; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
595 } |
6 | 596 |
597 if (need_measurement) | |
598 { | |
14 | 599 need_measurement = 0; |
600 printf("measure from do_comms\n"); | |
6 | 601 do_measurement(); |
602 } | |
603 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
604 idle_sleep(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
605 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
606 |
6 | 607 uart_off(); |
608 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
609 // turn off bluetooth |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
610 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
611 |
7 | 612 static void |
613 blink() | |
614 { | |
615 PORT_LED &= ~_BV(PIN_LED); | |
9 | 616 _delay_ms(1); |
7 | 617 PORT_LED |= _BV(PIN_LED); |
618 } | |
619 | |
620 static void | |
621 long_delay(int ms) | |
622 { | |
623 int iter = ms / 100; | |
624 | |
625 for (int i = 0; i < iter; i++) | |
626 { | |
627 _delay_ms(100); | |
628 } | |
629 } | |
630 | |
8 | 631 ISR(BADISR_vect) |
632 { | |
14 | 633 //uart_on(); |
8 | 634 printf_P(PSTR("Bad interrupt\n")); |
635 } | |
636 | |
9 | 637 static void |
638 set_2mhz() | |
639 { | |
640 cli(); | |
641 CLKPR = _BV(CLKPCE); | |
642 // divide by 4 | |
643 CLKPR = _BV(CLKPS1); | |
644 sei(); | |
645 } | |
646 | |
0 | 647 int main(void) |
648 { | |
9 | 649 set_2mhz(); |
650 | |
7 | 651 DDR_LED |= _BV(PIN_LED); |
15
54b0fda9cba7
Add shutdown handling, print sensors in "fetch" output
Matt Johnston <matt@ucc.asn.au>
parents:
14
diff
changeset
|
652 DDR_SHDN |= _BV(PIN_SHDN); |
8 | 653 blink(); |
7 | 654 |
6 | 655 stdout = &mystdout; |
7 | 656 uart_on(); |
657 | |
6 | 658 fprintf_P(&mystdout, PSTR("hello %d\n"), 12); |
13 | 659 |
660 check_first_startup(); | |
661 | |
8 | 662 uart_off(); |
0 | 663 |
6 | 664 // turn off everything except timer2 |
8 | 665 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
666 | |
667 // for testing | |
668 uart_on(); | |
669 | |
12 | 670 sei(); |
8 | 671 |
1 | 672 // set up counter2. |
673 // COM21 COM20 Set OC2 on Compare Match (p116) | |
674 // WGM21 Clear counter on compare | |
14 | 675 //TCCR2A = _BV(COM2A1) | _BV(COM2A0) | _BV(WGM21); |
676 // toggle on match | |
677 TCCR2A = _BV(COM2A0); | |
1 | 678 // CS22 CS21 CS20 clk/1024 |
2 | 679 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
1 | 680 // set async mode |
681 ASSR |= _BV(AS2); | |
14 | 682 TCNT2 = 0; |
683 OCR2A = SLEEP_COMPARE; | |
8 | 684 // interrupt |
685 TIMSK2 = _BV(OCIE2A); | |
0 | 686 |
6 | 687 for (;;) |
688 { | |
12 | 689 do_comms(); |
6 | 690 } |
12 | 691 |
0 | 692 for(;;){ |
693 /* insert your main loop code here */ | |
1 | 694 if (need_measurement) |
695 { | |
14 | 696 need_measurement = 0; |
1 | 697 do_measurement(); |
14 | 698 // testing |
699 cmd_fetch(); | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
700 continue; |
1 | 701 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
702 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
703 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
704 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
705 do_comms(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
706 continue; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
707 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
708 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
709 deep_sleep(); |
9 | 710 blink(); |
10 | 711 printf("."); |
0 | 712 } |
12 | 713 |
0 | 714 return 0; /* never reached */ |
715 } |