comparison main.c @ 76:6e47a61edc47

don't store sensors in eeprom, scan at startup instead
author Matt Johnston <matt@ucc.asn.au>
date Tue, 03 Jul 2012 23:12:06 +0800
parents ca08442635ca
children eb532c2a447d
comparison
equal deleted inserted replaced
75:ca08442635ca 76:6e47a61edc47
24 // - number of sensors (and range?) 24 // - number of sensors (and range?)
25 25
26 // 1 second. we have 1024 prescaler, 32768 crystal. 26 // 1 second. we have 1024 prescaler, 32768 crystal.
27 #define SLEEP_COMPARE 32 27 #define SLEEP_COMPARE 32
28 // limited to uint16_t 28 // limited to uint16_t
29 #define MEASURE_WAKE 60 29 #define MEASURE_WAKE 140
30 30
31 #define VALUE_NOSENSOR 0x07D0 // 125 degrees 31 #define VALUE_NOSENSOR 0x07D0 // 125 degrees
32 #define VALUE_BROKEN 0x07D1 // 125.0625 32 #define VALUE_BROKEN 0x07D1 // 125.0625
33 33
34 // limited to uint16_t 34 // limited to uint16_t
35 #define COMMS_WAKE 3600 // XXX testing 35 #define COMMS_WAKE 3600 // XXX testing
36 // limited to uint8_t 36 // limited to uint8_t
37 #define WAKE_SECS 60 // XXX testing 37 #define WAKE_SECS 30 // XXX testing
38 38
39 #define BAUD 19200 39 #define BAUD 19200
40 #define UBRR ((F_CPU)/8/(BAUD)-1) 40 #define UBRR ((F_CPU)/8/(BAUD)-1)
41 41
42 #define PORT_LED PORTC 42 #define PORT_LED PORTC
100 100
101 static uint8_t readpos; 101 static uint8_t readpos;
102 static char readbuf[30]; 102 static char readbuf[30];
103 static uint8_t have_cmd; 103 static uint8_t have_cmd;
104 104
105 uint8_t n_sensors;
106 uint8_t sensor_id[MAX_SENSORS][ID_LEN];
105 107
106 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/ 108 // thanks to http://projectgus.com/2010/07/eeprom-access-with-arduino/
107 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size)) 109 #define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block((dst_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (dst_size))
108 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst)) 110 #define eeprom_read(dst, eeprom_field) eeprom_read_to((&dst), eeprom_field, sizeof(dst))
109 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size)) 111 #define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block((src_p), (void *)offsetof(struct __eeprom_data, eeprom_field), (src_size))
110 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); } 112 #define eeprom_write(src, eeprom_field) { eeprom_write_from(&src, eeprom_field, sizeof(src)); }
111 113
112 #define EXPECT_MAGIC 0x67c9 114 #define EXPECT_MAGIC 0x67c9
113 115
114 struct __attribute__ ((__packed__)) __eeprom_data { 116 struct __attribute__ ((__packed__)) __eeprom_data {
117 // XXX eeprom unused at present
115 uint16_t magic; 118 uint16_t magic;
116 uint8_t n_sensors;
117 uint8_t sensor_id[MAX_SENSORS][ID_LEN];
118 }; 119 };
119 120
120 #define DEBUG(str) printf_P(PSTR(str)) 121 #define DEBUG(str) printf_P(PSTR(str))
121 122
122 static void deep_sleep(); 123 static void deep_sleep();
257 258
258 static void 259 static void
259 cmd_fetch() 260 cmd_fetch()
260 { 261 {
261 crc_out = 0; 262 crc_out = 0;
262 uint8_t n_sensors;
263 eeprom_read(n_sensors, n_sensors);
264
265 uint16_t millivolt_vcc = adc_vcc(); 263 uint16_t millivolt_vcc = adc_vcc();
266 264
267 uint32_t epoch_copy; 265 uint32_t epoch_copy;
268 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) 266 ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
269 { 267 {
286 millivolt_vcc 284 millivolt_vcc
287 ); 285 );
288 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors); 286 fprintf_P(crc_stdout, PSTR("sensors=%u\n"), n_sensors);
289 for (uint8_t s = 0; s < n_sensors; s++) 287 for (uint8_t s = 0; s < n_sensors; s++)
290 { 288 {
291 uint8_t id[ID_LEN];
292 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s); 289 fprintf_P(crc_stdout, PSTR("sensor_id%u="), s);
293 eeprom_read_to(id, sensor_id[s], ID_LEN); 290 printhex(sensor_id[s], ID_LEN, crc_stdout);
294 printhex(id, ID_LEN, crc_stdout);
295 fputc('\n', crc_stdout); 291 fputc('\n', crc_stdout);
296 } 292 }
297 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements); 293 fprintf_P(crc_stdout, PSTR("measurements=%hu\n"), n_measurements);
298 for (uint16_t n = 0; n < n_measurements; n++) 294 for (uint16_t n = 0; n < n_measurements; n++)
299 { 295 {
323 comms_count = 0; 319 comms_count = 0;
324 } 320 }
325 printf_P(PSTR("off:%hu\n"), COMMS_WAKE); 321 printf_P(PSTR("off:%hu\n"), COMMS_WAKE);
326 _delay_ms(100); 322 _delay_ms(100);
327 comms_timeout = 0; 323 comms_timeout = 0;
328 }
329
330 static void
331 cmd_awake()
332 {
333 comms_timeout = WAKE_SECS;
334 printf_P(PSTR("awake %hu\n"), WAKE_SECS);
335 } 324 }
336 325
337 static void 326 static void
338 cmd_reset() 327 cmd_reset()
339 { 328 {
415 return 0; 404 return 0;
416 } 405 }
417 #endif 406 #endif
418 407
419 static void 408 static void
420 add_sensor(uint8_t *id) 409 init_sensors()
421 {
422 uint8_t n;
423 eeprom_read(n, n_sensors);
424 if (n < MAX_SENSORS)
425 {
426 cli();
427 eeprom_write_from(id, sensor_id[n], ID_LEN);
428 n++;
429 eeprom_write(n, n_sensors);
430 sei();
431 printf_P(PSTR("Added sensor %d : "), n);
432 printhex(id, ID_LEN, stdout);
433 putchar('\n');
434 }
435 else
436 {
437 printf_P(PSTR("Too many sensors\n"));
438 }
439 }
440
441 static void
442 cmd_add_all()
443 { 410 {
444 uint8_t id[OW_ROMCODE_SIZE]; 411 uint8_t id[OW_ROMCODE_SIZE];
445 printf_P("Adding all\n"); 412 printf_P(PSTR("init sensors\n"));
446 ow_reset(); 413 ow_reset();
447 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) 414 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
448 { 415 {
449 diff = ow_rom_search( diff, &id[0] ); 416 diff = ow_rom_search( diff, &id[0] );
450 if( diff == OW_PRESENCE_ERR ) { 417 if( diff == OW_PRESENCE_ERR ) {
454 421
455 if( diff == OW_DATA_ERR ) { 422 if( diff == OW_DATA_ERR ) {
456 printf_P( PSTR("Bus Error\r") ); 423 printf_P( PSTR("Bus Error\r") );
457 return; 424 return;
458 } 425 }
459 add_sensor(id); 426
460 } 427 if (n_sensors < MAX_SENSORS)
461 } 428 {
462 429 memcpy(sensor_id[n_sensors], id, ID_LEN);
463 static void 430 printf_P(PSTR("Added sensor %d : "), n_sensors);
464 cmd_init() 431 printhex(id, ID_LEN, stdout);
465 { 432 putchar('\n');
466 printf_P(PSTR("Resetting sensor list\n")); 433 n_sensors++;
467 uint8_t zero = 0; 434 }
468 cli(); 435 else
469 eeprom_write(zero, n_sensors); 436 {
470 sei(); 437 printf_P(PSTR("Too many sensors\n"));
471 printf_P(PSTR("Done.\n")); 438 }
439 }
472 } 440 }
473 441
474 static void 442 static void
475 check_first_startup() 443 check_first_startup()
476 { 444 {
445 #if 0
477 uint16_t magic; 446 uint16_t magic;
478 eeprom_read(magic, magic); 447 eeprom_read(magic, magic);
479 if (magic != EXPECT_MAGIC) 448 if (magic != EXPECT_MAGIC)
480 { 449 {
481 printf_P(PSTR("First boot, looking for sensors...\n")); 450 printf_P(PSTR("First boot, looking for sensors...\n"));
486 cli(); 455 cli();
487 magic = EXPECT_MAGIC; 456 magic = EXPECT_MAGIC;
488 eeprom_write(magic, magic); 457 eeprom_write(magic, magic);
489 sei(); 458 sei();
490 } 459 }
460 #endif
491 } 461 }
492 462
493 static void 463 static void
494 read_handler() 464 read_handler()
495 { 465 {
510 cmd_measure(); 480 cmd_measure();
511 } 481 }
512 else if (strcmp_P(readbuf, PSTR("sensors")) == 0) 482 else if (strcmp_P(readbuf, PSTR("sensors")) == 0)
513 { 483 {
514 cmd_sensors(); 484 cmd_sensors();
515 }
516 else if (strcmp_P(readbuf, PSTR("addall"))== 0)
517 {
518 cmd_add_all();
519 }
520 else if (strcmp_P(readbuf, PSTR("awake"))== 0)
521 {
522 cmd_awake();
523 }
524 else if (strcmp_P(readbuf, PSTR("init")) == 0)
525 {
526 cmd_init();
527 } 485 }
528 else if (strcmp_P(readbuf, PSTR("reset")) == 0) 486 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
529 { 487 {
530 cmd_reset(); 488 cmd_reset();
531 } 489 }
657 } 615 }
658 616
659 static void 617 static void
660 do_measurement() 618 do_measurement()
661 { 619 {
662 uint8_t n_sensors;
663 eeprom_read(n_sensors, n_sensors);
664
665 blink(); 620 blink();
666 621
667 simple_ds18b20_start_meas(NULL); 622 simple_ds18b20_start_meas(NULL);
668 // sleep rather than using a long delay 623 // sleep rather than using a long delay
669 deep_sleep(); 624 deep_sleep();
681 { 636 {
682 reading = VALUE_NOSENSOR; 637 reading = VALUE_NOSENSOR;
683 } 638 }
684 else 639 else
685 { 640 {
686 uint8_t id[ID_LEN]; 641 uint8_t ret = simple_ds18b20_read_raw(sensor_id[s], &reading);
687 eeprom_read_to(id, sensor_id[s], ID_LEN);
688
689 uint8_t ret = simple_ds18b20_read_raw(id, &reading);
690 if (ret != DS18X20_OK) 642 if (ret != DS18X20_OK)
691 { 643 {
692 reading = VALUE_BROKEN; 644 reading = VALUE_BROKEN;
693 } 645 }
694 } 646 }
731 } 683 }
732 684
733 if (have_cmd) 685 if (have_cmd)
734 { 686 {
735 have_cmd = 0; 687 have_cmd = 0;
688 comms_timeout = WAKE_SECS;
736 read_handler(); 689 read_handler();
737 continue; 690 continue;
738 } 691 }
739 692
740 // wait for commands from the master 693 // wait for commands from the master
784 737
785 printf(PSTR("Started.\n")); 738 printf(PSTR("Started.\n"));
786 739
787 check_first_startup(); 740 check_first_startup();
788 741
742 init_sensors();
743
789 uart_off(); 744 uart_off();
790 745
791 // turn off everything except timer2 746 // turn off everything except timer2
792 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC); 747 PRR = _BV(PRTWI) | _BV(PRTIM0) | _BV(PRTIM1) | _BV(PRSPI) | _BV(PRUSART0) | _BV(PRADC);
793 748