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