watchdog.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  */
00032 
00033 /* Watchdog routines for the AVR */
00034 
00035 /* The default timeout of 2 seconds works on most MCUs.
00036  * It should be disabled during sleep (unless used for wakeup) since
00037  * it draws significant current (~5 uamp on 1284p, 20x the MCU consumption).
00038  *
00039  * Note the wdt is not properly simulated in AVR Studio 4 Simulator 1:
00040  *   On many devices calls to wdt_reset will have no effect, and a wdt reboot will occur.
00041  *   The MCUSR will not show the cause of a wdt reboot.
00042  *   A 1MHz clock is assumed; at 8MHz timeout occurs 8x faster than it should.
00043  * Simulator 2 is supposed to work on supported devices (not atmega128rfa1),
00044  * but neither it nor Studio 5 beta do any resets on the 1284p.
00045  *
00046  * Setting WATCHDOG_CONF_TIMEOUT -1 will disable the WDT.
00047  */
00048 //#define WATCHDOG_CONF_TIMEOUT -1
00049 
00050 #ifndef WATCHDOG_CONF_TIMEOUT
00051 #define WATCHDOG_CONF_TIMEOUT WDTO_2S
00052 #endif
00053 
00054  /* While balancing start and stop calls is a good idea, an imbalance will cause
00055   * resets that can take a lot of time to track down.
00056   * Some low power protocols may cause this.
00057   * The default is no balance; define WATCHDOG_CONF_BALANCE 1 to override.
00058   */
00059 #ifndef WATCHDOG_CONF_BALANCE
00060 #define WATCHDOG_CONF_BALANCE 0
00061 #endif
00062 
00063 #include "dev/watchdog.h"
00064 #include <avr/wdt.h>
00065 #include <avr/interrupt.h>
00066 
00067 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
00068 static int stopped = 0;
00069 #endif
00070 
00071 /*---------------------------------------------------------------------------*/
00072 void
00073 watchdog_init(void)
00074 {
00075 /*  Clear startup bit and disable the wdt, whether or not it will be used.
00076     Random code may have caused the last reset.
00077  */
00078         MCUSR&=~(1<<WDRF);
00079     wdt_disable();
00080 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
00081         stopped = 1;
00082 #endif
00083 }
00084 /*---------------------------------------------------------------------------*/
00085 void
00086 watchdog_start(void)
00087 {
00088 #if WATCHDOG_CONF_TIMEOUT >= 0
00089 #if WATCHDOG_CONF_BALANCE
00090         stopped--;
00091         if(!stopped)
00092 #endif
00093                 wdt_enable(WATCHDOG_CONF_TIMEOUT);
00094 #endif  
00095 }
00096 /*---------------------------------------------------------------------------*/
00097 void
00098 watchdog_periodic(void)
00099 {
00100 #if WATCHDOG_CONF_TIMEOUT >= 0
00101 #if WATCHDOG_CONF_BALANCE
00102         if(!stopped)
00103 #endif
00104                 wdt_reset();
00105 #endif
00106 }
00107 /*---------------------------------------------------------------------------*/
00108 void
00109 watchdog_stop(void)
00110 {
00111 #if WATCHDOG_CONF_TIMEOUT >= 0
00112 #if WATCHDOG_CONF_BALANCE
00113         stopped++;
00114 #endif
00115         wdt_disable();
00116 #endif
00117 }
00118 /*---------------------------------------------------------------------------*/
00119 void
00120 watchdog_reboot(void)
00121 {
00122         cli();
00123         wdt_enable(WDTO_15MS); //wd on,250ms 
00124         while(1); //loop
00125 }
00126 /*---------------------------------------------------------------------------*/
00127 #if 0
00128 /* Not all AVRs implement the wdt interrupt */
00129 ISR(WDT_vect)
00130 {
00131 }
00132 #endif

Generated on Mon Apr 11 14:23:37 2011 for Contiki 2.5 by  doxygen 1.6.1