Contiki 2.6

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 //Not all AVR toolchains alias MCUSR to the older MSUSCR name
00068 //#if defined (__AVR_ATmega8__) || defined (__AVR_ATmega8515__) || defined (__AVR_ATmega16__)
00069 #if !defined (MCUSR) && defined (MCUCSR)
00070 #warning *** MCUSR not defined, using MCUCSR instead ***
00071 #define MCUSR MCUCSR
00072 #endif
00073 
00074 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
00075 static int stopped = 0;
00076 #endif
00077 
00078 /*---------------------------------------------------------------------------*/
00079 void
00080 watchdog_init(void)
00081 {
00082 /*  Clear startup bit and disable the wdt, whether or not it will be used.
00083     Random code may have caused the last reset.
00084  */
00085         MCUSR&=~(1<<WDRF);
00086     wdt_disable();
00087 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
00088         stopped = 1;
00089 #endif
00090 }
00091 /*---------------------------------------------------------------------------*/
00092 void
00093 watchdog_start(void)
00094 {
00095 #if WATCHDOG_CONF_TIMEOUT >= 0
00096 #if WATCHDOG_CONF_BALANCE
00097         stopped--;
00098         if(!stopped)
00099 #endif
00100                 wdt_enable(WATCHDOG_CONF_TIMEOUT);
00101 #endif  
00102 }
00103 /*---------------------------------------------------------------------------*/
00104 void
00105 watchdog_periodic(void)
00106 {
00107 #if WATCHDOG_CONF_TIMEOUT >= 0
00108 #if WATCHDOG_CONF_BALANCE
00109         if(!stopped)
00110 #endif
00111                 wdt_reset();
00112 #endif
00113 }
00114 /*---------------------------------------------------------------------------*/
00115 void
00116 watchdog_stop(void)
00117 {
00118 #if WATCHDOG_CONF_TIMEOUT >= 0
00119 #if WATCHDOG_CONF_BALANCE
00120         stopped++;
00121 #endif
00122         wdt_disable();
00123 #endif
00124 }
00125 /*---------------------------------------------------------------------------*/
00126 void
00127 watchdog_reboot(void)
00128 {
00129         cli();
00130         wdt_enable(WDTO_15MS); //wd on,250ms 
00131         while(1); //loop
00132 }
00133 /*---------------------------------------------------------------------------*/
00134 #if 0
00135 /* Not all AVRs implement the wdt interrupt */
00136 ISR(WDT_vect)
00137 {
00138 }
00139 #endif