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