changeset 47:13a68734348b

Add watchdog timer.
author darius@Inchoate
date Mon, 20 Oct 2008 22:22:15 +1030
parents fb272cb82bcb
children 94f71ddb0cda
files testavr.c
diffstat 1 files changed, 42 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/testavr.c	Mon Oct 20 22:21:21 2008 +1030
+++ b/testavr.c	Mon Oct 20 22:22:15 2008 +1030
@@ -34,6 +34,7 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <util/delay.h>
+#include <avr/wdt.h>
 
 #include "cons.h"
 #include "1wire.h"
@@ -42,8 +43,24 @@
 #endif
 #include "tempctrl.h"
 
+/*
+ * Mirror of the MCUCSR register, taken early during startup.
+ */
+uint8_t mcucsr __attribute__((section(".noinit")));
+
 void		process_cmd(void);
 
+/*
+ * Read out and reset MCUCSR early during startup.
+ */
+void handle_mcucsr(void)
+    __attribute__((section(".init3")))
+    __attribute__((naked));
+void handle_mcucsr(void) {
+    mcucsr = MCUCSR;
+    MCUCSR = 0;
+}
+
 int
 main(void) {
     /* Disable interrupts while we frob stuff */
@@ -84,29 +101,34 @@
     printf_P(PSTR("\r\n\r\n===============\r\n"
 		  "Inited!\r\n\r\n"));
 
+    if ((mcucsr & _BV(PORF)) == _BV(PORF))
+	printf_P(PSTR("Power on reset\r\n"));
+
+    if ((mcucsr & _BV(EXTRF)) == _BV(EXTRF))
+	printf_P(PSTR("External reset\r\n"));
+
+    if ((mcucsr & _BV(BORF)) == _BV(BORF))
+	printf_P(PSTR("Brown-out reset\r\n"));
+
+    if ((mcucsr & _BV(WDRF)) == _BV(WDRF))
+	printf_P(PSTR("Watchdog reset\r\n"));
+
+    if ((mcucsr & _BV(JTRF)) == _BV(JTRF))
+	printf_P(PSTR("JTAG reset\r\n"));
+
     tempctrl_init();
     
     /* Ready to go! */
     sei();
 
-#if 0
-    DDRA = 0xff;
-    DDRC = 0xff;
-    while (1) {
-	printf_P(PSTR("1\r\n"));
-	PORTA = 0xff;
-	printf_P(PSTR("2\r\n"));
-	PORTC = 0x00;
-	printf_P(PSTR("3\r\n"));
-	_delay_us(1);
-	printf_P(PSTR("4\r\n"));
-	PORTA = 0x80;
-	printf_P(PSTR("5\r\n"));
-	PORTC = 0xff;
-	printf_P(PSTR("6\r\n"));
-    }
-#endif    
-    
+    /*
+     * Enable the watchdog with the largest prescaler.  Will cause a
+     * watchdog reset after approximately 2 s @ Vcc = 5 V
+     *
+     * Gets reset in the loop below and in the tempctrl.c timer IRQ
+     */
+    wdt_enable(WDTO_2S);
+
 #ifdef WITHUSB
     printf_P(PSTR("Calling usb_init\r\n"));
     usb_init();
@@ -118,6 +140,8 @@
     
     /* Wait for user input or an "interrupt" */
     while (1) {
+	wdt_reset();
+
 	tempctrl_update();
 	
 	if (cmd.state == 255) {