comparison main.c @ 41:1701457e6007

fix tabbing
author Matt Johnston <matt@ucc.asn.au>
date Sat, 23 Jun 2012 22:12:07 +0800
parents 9b5b202129c3
children 96c336896201
comparison
equal deleted inserted replaced
40:9b5b202129c3 41:1701457e6007
124 124
125 // 3.3v power for bluetooth and SD 125 // 3.3v power for bluetooth and SD
126 DDR_LED |= _BV(PIN_LED); 126 DDR_LED |= _BV(PIN_LED);
127 DDR_SHDN |= _BV(PIN_SHDN); 127 DDR_SHDN |= _BV(PIN_SHDN);
128 128
129 // INT0 setup 129 // INT0 setup
130 EIMSK = _BV(INT0); 130 EIMSK = _BV(INT0);
131 // set pullup 131 // set pullup
132 PORTD |= _BV(PD2); 132 PORTD |= _BV(PD2);
133 } 133 }
134 134
135 static void 135 static void
136 set_aux_power(uint8_t on) 136 set_aux_power(uint8_t on)
137 { 137 {
177 // set 2x clock, improves accuracy of UBRR 177 // set 2x clock, improves accuracy of UBRR
178 UCSR0A |= _BV(U2X0); 178 UCSR0A |= _BV(U2X0);
179 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0); 179 UCSR0B = _BV(RXCIE0) | _BV(RXEN0) | _BV(TXEN0);
180 //8N1 180 //8N1
181 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); 181 UCSR0C = _BV(UCSZ01) | _BV(UCSZ00);
182 uart_enabled = 1; 182 uart_enabled = 1;
183 } 183 }
184 184
185 static void 185 static void
186 uart_off() 186 uart_off()
187 { 187 {
188 // Turn of interrupts and disable tx/rx 188 // Turn of interrupts and disable tx/rx
189 UCSR0B = 0; 189 UCSR0B = 0;
190 uart_enabled = 0; 190 uart_enabled = 0;
191 191
192 // Power reduction register 192 // Power reduction register
193 //PRR |= _BV(PRUSART0); 193 //PRR |= _BV(PRUSART0);
194 } 194 }
195 195
196 int 196 int
197 uart_putchar(char c, FILE *stream) 197 uart_putchar(char c, FILE *stream)
198 { 198 {
199 if (!uart_enabled) 199 if (!uart_enabled)
200 { 200 {
201 return EOF; 201 return EOF;
202 } 202 }
203 // XXX could perhaps sleep in the loop for power. 203 // XXX could perhaps sleep in the loop for power.
204 if (c == '\n') 204 if (c == '\n')
205 { 205 {
206 loop_until_bit_is_set(UCSR0A, UDRE0); 206 loop_until_bit_is_set(UCSR0A, UDRE0);
207 UDR0 = '\r'; 207 UDR0 = '\r';
374 } 374 }
375 375
376 static void 376 static void
377 cmd_add_all() 377 cmd_add_all()
378 { 378 {
379 uint8_t id[OW_ROMCODE_SIZE]; 379 uint8_t id[OW_ROMCODE_SIZE];
380 printf_P("Adding all\n"); 380 printf_P("Adding all\n");
381 ow_reset(); 381 ow_reset();
382 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; ) 382 for( uint8_t diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE; )
383 { 383 {
384 diff = ow_rom_search( diff, &id[0] ); 384 diff = ow_rom_search( diff, &id[0] );
385 if( diff == OW_PRESENCE_ERR ) { 385 if( diff == OW_PRESENCE_ERR ) {
386 printf_P( PSTR("No Sensor found\r") ); 386 printf_P( PSTR("No Sensor found\r") );
387 return; 387 return;
388 } 388 }
389 389
390 if( diff == OW_DATA_ERR ) { 390 if( diff == OW_DATA_ERR ) {
391 printf_P( PSTR("Bus Error\r") ); 391 printf_P( PSTR("Bus Error\r") );
392 return; 392 return;
393 } 393 }
394 add_sensor(id); 394 add_sensor(id);
395 } 395 }
396 } 396 }
397 397
398 static void 398 static void
462 } 462 }
463 } 463 }
464 464
465 ISR(INT0_vect) 465 ISR(INT0_vect)
466 { 466 {
467 need_comms = 1; 467 need_comms = 1;
468 blink(); 468 blink();
469 _delay_ms(100); 469 _delay_ms(100);
470 blink(); 470 blink();
471 _delay_ms(100); 471 _delay_ms(100);
472 blink(); 472 blink();
510 510
511 ISR(TIMER2_COMPA_vect) 511 ISR(TIMER2_COMPA_vect)
512 { 512 {
513 TCNT2 = 0; 513 TCNT2 = 0;
514 measure_count ++; 514 measure_count ++;
515 comms_count ++; 515 comms_count ++;
516 516
517 clock_epoch ++; 517 clock_epoch ++;
518 518
519 if (comms_timeout != 0) 519 if (comms_timeout != 0)
520 { 520 {
526 { 526 {
527 measure_count = 0; 527 measure_count = 0;
528 need_measurement = 1; 528 need_measurement = 1;
529 } 529 }
530 530
531 if (comms_count >= COMMS_WAKE) 531 if (comms_count >= COMMS_WAKE)
532 { 532 {
533 comms_count = 0; 533 comms_count = 0;
534 need_comms = 1; 534 need_comms = 1;
535 } 535 }
536 536
537 } 537 }
538 538
539 DWORD get_fattime (void) 539 DWORD get_fattime (void)
540 { 540 {
667 } 667 }
668 668
669 static void 669 static void
670 do_comms() 670 do_comms()
671 { 671 {
672 // turn on bluetooth 672 // turn on bluetooth
673 set_aux_power(1); 673 set_aux_power(1);
674 uart_on(); 674 uart_on();
675 675
676 // write sd card here? same 3.3v regulator... 676 // write sd card here? same 3.3v regulator...
677 677
678 for (comms_timeout = WAKE_SECS; comms_timeout > 0; ) 678 for (comms_timeout = WAKE_SECS; comms_timeout > 0; )
679 { 679 {
680 if (need_measurement) 680 if (need_measurement)
681 { 681 {
682 need_measurement = 0; 682 need_measurement = 0;
683 do_measurement(); 683 do_measurement();
684 } 684 }
688 have_cmd = 0; 688 have_cmd = 0;
689 read_handler(); 689 read_handler();
690 } 690 }
691 691
692 // wait for commands from the master 692 // wait for commands from the master
693 idle_sleep(); 693 idle_sleep();
694 } 694 }
695 695
696 uart_off(); 696 uart_off();
697 set_aux_power(0); 697 set_aux_power(0);
698 } 698 }
699 699
756 { 756 {
757 if (need_measurement) 757 if (need_measurement)
758 { 758 {
759 need_measurement = 0; 759 need_measurement = 0;
760 do_measurement(); 760 do_measurement();
761 continue; 761 continue;
762 } 762 }
763 763
764 if (need_comms) 764 if (need_comms)
765 { 765 {
766 need_comms = 0; 766 need_comms = 0;
767 do_comms(); 767 do_comms();
768 continue; 768 continue;
769 } 769 }
770 770
771 deep_sleep(); 771 deep_sleep();
772 blink(); 772 blink();
773 } 773 }
774 774
775 return 0; /* never reached */ 775 return 0; /* never reached */
776 } 776 }