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