comparison main.c @ 54:0d3d14af55c2

add "awake" and "reset" functions
author Matt Johnston <matt@ucc.asn.au>
date Sun, 24 Jun 2012 22:30:34 +0800
parents c3f5e02c1c42
children 8e897a682208
comparison
equal deleted inserted replaced
53:56b1f02f470c 54:0d3d14af55c2
5 #include <avr/interrupt.h> 5 #include <avr/interrupt.h>
6 #include <avr/sleep.h> 6 #include <avr/sleep.h>
7 #include <util/delay.h> 7 #include <util/delay.h>
8 #include <avr/pgmspace.h> 8 #include <avr/pgmspace.h>
9 #include <avr/eeprom.h> 9 #include <avr/eeprom.h>
10 #include <avr/wdt.h>
10 #include <util/crc16.h> 11 #include <util/crc16.h>
11 12
12 // for DWORD of get_fattime() 13 // for DWORD of get_fattime()
13 #include "integer.h" 14 #include "integer.h"
14 15
113 114
114 // Very first setup 115 // Very first setup
115 static void 116 static void
116 setup_chip() 117 setup_chip()
117 { 118 {
119 cli();
120
121 // stop watchdog timer (might have been used to cause a reset)
122 wdt_reset();
123 MCUSR &= ~_BV(WDRF);
124 WDTCSR |= _BV(WDCE) | _BV(WDE);
125 WDTCSR = 0;
126
118 // Set clock to 2mhz 127 // Set clock to 2mhz
119 cli();
120 CLKPR = _BV(CLKPCE); 128 CLKPR = _BV(CLKPCE);
121 // divide by 4 129 // divide by 4
122 CLKPR = _BV(CLKPS1); 130 CLKPR = _BV(CLKPS1);
123 131
124 // enable pullups 132 // enable pullups
292 _delay_ms(100); 300 _delay_ms(100);
293 comms_timeout = 0; 301 comms_timeout = 0;
294 } 302 }
295 303
296 static void 304 static void
305 cmd_awake()
306 {
307 comms_timeout = WAKE_SECS;
308 printf_P(PSTR("awake %hu\n"), WAKE_SECS);
309 }
310
311 static void
312 cmd_reset()
313 {
314 printf_P(PSTR("reset\n"));
315 _delay_ms(100);
316 cli(); // disable interrupts
317 wdt_enable(WDTO_15MS); // enable watchdog
318 while(1); // wait for watchdog to reset processor
319 }
320
321 static void
297 cmd_measure() 322 cmd_measure()
298 { 323 {
299 printf_P(PSTR("measuring\n")); 324 printf_P(PSTR("measuring\n"));
300 need_measurement = 1; 325 need_measurement = 1;
301 } 326 }
464 } 489 }
465 else if (strcmp_P(readbuf, PSTR("addall"))== 0) 490 else if (strcmp_P(readbuf, PSTR("addall"))== 0)
466 { 491 {
467 cmd_add_all(); 492 cmd_add_all();
468 } 493 }
494 else if (strcmp_P(readbuf, PSTR("awake"))== 0)
495 {
496 cmd_awake();
497 }
469 else if (strcmp_P(readbuf, PSTR("init")) == 0) 498 else if (strcmp_P(readbuf, PSTR("init")) == 0)
470 { 499 {
471 cmd_init(); 500 cmd_init();
472 } 501 }
502 else if (strcmp_P(readbuf, PSTR("reset")) == 0)
503 {
504 cmd_reset();
505 }
473 else 506 else
474 { 507 {
475 printf_P(PSTR("Bad command\n")); 508 printf_P(PSTR("Bad command\n"));
476 } 509 }
477 } 510 }
478 511
479 ISR(INT0_vect) 512 ISR(INT0_vect)
480 { 513 {
481 need_comms = 1; 514 need_comms = 1;
515 comms_timeout = WAKE_SECS;
482 blink(); 516 blink();
483 _delay_ms(100); 517 _delay_ms(100);
484 blink(); 518 blink();
485 } 519 }
486 520
726 { 760 {
727 //uart_on(); 761 //uart_on();
728 printf_P(PSTR("Bad interrupt\n")); 762 printf_P(PSTR("Bad interrupt\n"));
729 } 763 }
730 764
731
732 int main(void) 765 int main(void)
733 { 766 {
734 setup_chip(); 767 setup_chip();
735 blink(); 768 blink();
736 769