Mercurial > templog
annotate main.c @ 13:4838bfcb3504
Add eeprom stuff
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 19 May 2012 23:59:12 +0800 |
parents | 3c27bfbd7f3a |
children | 426fb44ece3f |
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 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
32 #define MEASURE_WAKE 60 |
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 |
13 | 46 #define NUM_MEASUREMENTS 100 |
47 #define MAX_SENSORS 5 | |
6 | 48 |
10 | 49 int uart_putchar(char c, FILE *stream); |
0 | 50 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, |
51 _FDEV_SETUP_WRITE); | |
52 | |
13 | 53 static uint16_t n_measurements = 0; |
54 // stored as decidegrees | |
55 static int16_t measurements[MAX_SENSORS][NUM_MEASUREMENTS]; | |
0 | 56 |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
57 // boolean flags |
7 | 58 static uint8_t need_measurement = 0; |
59 static uint8_t need_comms = 0; | |
60 static uint8_t comms_done = 0; | |
1 | 61 |
7 | 62 static uint8_t readpos = 0; |
0 | 63 static char readbuf[30]; |
64 | |
7 | 65 static uint8_t measure_count = 0; |
66 static uint16_t comms_count = 0; | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
67 |
13 | 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 | |
8 | 82 #define DEBUG(str) printf_P(PSTR(str)) |
83 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
84 static void deep_sleep(); |
1 | 85 |
0 | 86 static void |
7 | 87 uart_on() |
0 | 88 { |
8 | 89 // Power reduction register |
90 //PRR &= ~_BV(PRUSART0); | |
91 | |
0 | 92 // baud rate |
7 | 93 UBRR0H = (unsigned char)(UBRR >> 8); |
94 UBRR0L = (unsigned char)UBRR; | |
95 // set 2x clock, improves accuracy of UBRR | |
96 UCSR0A |= _BV(U2X0); | |
6 | 97 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); |
0 | 98 //8N1 |
7 | 99 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); |
6 | 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 | |
8 | 109 //PRR |= _BV(PRUSART0); |
0 | 110 } |
111 | |
10 | 112 int |
0 | 113 uart_putchar(char c, FILE *stream) |
114 { | |
10 | 115 // XXX should sleep in the loop for power. |
8 | 116 if (c == '\n') |
117 { | |
10 | 118 loop_until_bit_is_set(UCSR0A, UDRE0); |
119 UDR0 = '\r';; | |
8 | 120 } |
2 | 121 loop_until_bit_is_set(UCSR0A, UDRE0); |
122 UDR0 = c; | |
10 | 123 if (c == '\r') |
124 { | |
125 loop_until_bit_is_set(UCSR0A, UDRE0); | |
126 UDR0 = '\n';; | |
127 } | |
0 | 128 return 0; |
129 } | |
130 | |
131 static void | |
132 cmd_fetch() | |
133 { | |
134 uint16_t crc = 0; | |
13 | 135 uint16_t sens; |
136 eeprom_read(sens, n_sensors); | |
137 | |
6 | 138 printf_P(PSTR("%d measurements\n"), n_measurements); |
13 | 139 for (uint16_t i = 0; i < n_measurements; i++) |
0 | 140 { |
13 | 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'); | |
0 | 148 } |
6 | 149 printf_P(PSTR("CRC : %d\n"), crc); |
0 | 150 } |
151 | |
152 static void | |
153 cmd_clear() | |
154 { | |
7 | 155 n_measurements = 0; |
156 printf_P(PSTR("Cleared\n")); | |
0 | 157 } |
158 | |
159 static void | |
160 cmd_btoff() | |
161 { | |
7 | 162 printf_P(PSTR("Turning off\n")); |
163 _delay_ms(50); | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
164 comms_done = 1; |
0 | 165 } |
166 | |
167 static void | |
12 | 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); | |
13 | 178 printf_P(("All sensors, ret %d, waiting...\n"), ret); |
12 | 179 _delay_ms(DS18B20_TCONV_12BIT); |
180 simple_ds18b20_read_all(); | |
181 } | |
182 | |
13 | 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 | |
12 | 319 static void |
0 | 320 read_handler() |
321 { | |
6 | 322 if (strcmp_P(readbuf, PSTR("fetch")) == 0) |
0 | 323 { |
324 cmd_fetch(); | |
325 } | |
6 | 326 else if (strcmp_P(readbuf, PSTR("clear")) == 0) |
0 | 327 { |
328 cmd_clear(); | |
329 } | |
6 | 330 else if (strcmp_P(readbuf, PSTR("btoff")) == 0) |
0 | 331 { |
332 cmd_btoff(); | |
333 } | |
12 | 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 } | |
13 | 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 } | |
0 | 354 else |
355 { | |
6 | 356 printf_P(PSTR("Bad command\n")); |
0 | 357 } |
358 } | |
359 | |
2 | 360 ISR(USART_RX_vect) |
0 | 361 { |
2 | 362 char c = UDR0; |
12 | 363 uart_putchar(c, NULL); |
364 if (c == '\r') | |
0 | 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 | |
2 | 381 ISR(TIMER2_COMPA_vect) |
1 | 382 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
383 measure_count ++; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
384 comms_count ++; |
12 | 385 printf("measure_count %d\n", measure_count); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
386 if (measure_count == MEASURE_WAKE) |
1 | 387 { |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
388 measure_count = 0; |
12 | 389 printf("need_measurement = 1\n"); |
1 | 390 need_measurement = 1; |
391 } | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
392 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
393 if (comms_count == COMMS_WAKE) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
394 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
395 comms_count = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
396 need_comms = 1; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
397 } |
1 | 398 } |
399 | |
3 | 400 DWORD get_fattime (void) |
401 { | |
402 return 0; | |
403 } | |
404 | |
1 | 405 static void |
406 deep_sleep() | |
407 { | |
408 // p119 of manual | |
2 | 409 OCR2A = SLEEP_COMPARE; |
410 loop_until_bit_is_clear(ASSR, OCR2AUB); | |
1 | 411 |
412 set_sleep_mode(SLEEP_MODE_PWR_SAVE); | |
413 sleep_mode(); | |
414 } | |
415 | |
416 static void | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
417 idle_sleep() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
418 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
419 set_sleep_mode(SLEEP_MODE_IDLE); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
420 sleep_mode(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
421 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
422 |
6 | 423 static void |
424 do_adc_335() | |
425 { | |
8 | 426 //PRR &= ~_BV(PRADC); |
6 | 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; | |
13 | 453 // XXX fixme |
454 //measurements[n_measurements] = temp; | |
6 | 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; | |
13 | 468 // XXX fixme |
469 //internal_measurements[n_measurements] = internal_temp; | |
6 | 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++; | |
8 | 475 //PRR |= _BV(PRADC); |
6 | 476 } |
477 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
478 static void |
1 | 479 do_measurement() |
480 { | |
13 | 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 } | |
6 | 493 |
13 | 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 } | |
12 | 514 //do_adc_335(); |
1 | 515 } |
516 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
517 static void |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
518 do_comms() |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
519 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
520 need_comms = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
521 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
522 // turn on bluetooth |
7 | 523 uart_on(); |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
524 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
525 // 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
|
526 |
12 | 527 printf("ready> \n"); |
528 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
529 comms_done = 0; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
530 for (;;) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
531 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
532 if (comms_done) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
533 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
534 break; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
535 } |
6 | 536 |
537 if (need_measurement) | |
538 { | |
539 do_measurement(); | |
540 } | |
541 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
542 idle_sleep(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
543 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
544 |
6 | 545 uart_off(); |
546 | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
547 // turn off bluetooth |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
548 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
549 |
7 | 550 static void |
551 blink() | |
552 { | |
553 PORT_LED &= ~_BV(PIN_LED); | |
9 | 554 _delay_ms(1); |
7 | 555 PORT_LED |= _BV(PIN_LED); |
556 } | |
557 | |
13 | 558 #if 0 |
7 | 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 } | |
13 | 569 #endif |
7 | 570 |
8 | 571 ISR(BADISR_vect) |
572 { | |
573 uart_on(); | |
574 printf_P(PSTR("Bad interrupt\n")); | |
575 } | |
576 | |
9 | 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 | |
0 | 587 int main(void) |
588 { | |
9 | 589 set_2mhz(); |
590 | |
7 | 591 DDR_LED |= _BV(PIN_LED); |
8 | 592 blink(); |
7 | 593 |
6 | 594 stdout = &mystdout; |
7 | 595 uart_on(); |
596 | |
6 | 597 fprintf_P(&mystdout, PSTR("hello %d\n"), 12); |
13 | 598 |
599 check_first_startup(); | |
600 | |
8 | 601 uart_off(); |
0 | 602 |
6 | 603 // turn off everything except timer2 |
8 | 604 //PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); |
605 | |
606 // for testing | |
607 uart_on(); | |
608 | |
12 | 609 sei(); |
8 | 610 |
1 | 611 // set up counter2. |
612 // COM21 COM20 Set OC2 on Compare Match (p116) | |
613 // WGM21 Clear counter on compare | |
13 | 614 TCCR2A = _BV(COM2A1) | _BV(COM2A0);// | _BV(WGM21); |
1 | 615 // CS22 CS21 CS20 clk/1024 |
2 | 616 TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); |
1 | 617 // set async mode |
618 ASSR |= _BV(AS2); | |
8 | 619 // interrupt |
620 TIMSK2 = _BV(OCIE2A); | |
0 | 621 |
6 | 622 for (;;) |
623 { | |
12 | 624 do_comms(); |
6 | 625 } |
12 | 626 |
0 | 627 for(;;){ |
628 /* insert your main loop code here */ | |
1 | 629 if (need_measurement) |
630 { | |
631 do_measurement(); | |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
632 continue; |
1 | 633 } |
4
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
634 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
635 if (need_comms) |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
636 { |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
637 do_comms(); |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
638 continue; |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
639 } |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
640 |
54d369e3d689
Fill out more main.c structure
Matt Johnston <matt@ucc.asn.au>
parents:
3
diff
changeset
|
641 deep_sleep(); |
9 | 642 blink(); |
10 | 643 printf("."); |
0 | 644 } |
12 | 645 |
0 | 646 return 0; /* never reached */ |
647 } |