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  * @(#)$Id: watchdog.c,v 1.12 2010/11/12 15:54:41 nifi Exp $
00032  */
00033 
00034 #include "contiki.h"
00035 #include "dev/watchdog.h"
00036 #include "isr_compat.h"
00037 
00038 static int counter = 0;
00039 
00040 #define PRINT_STACK_ON_REBOOT 0
00041 
00042 /*---------------------------------------------------------------------------*/
00043 #if PRINT_STACK_ON_REBOOT
00044 #ifdef CONTIKI_TARGET_SKY
00045 static void
00046 printchar(char c)
00047 {
00048   /* Transmit the data. */
00049   TXBUF1 = c;
00050 
00051   /* Loop until the transmission buffer is available. */
00052   while((IFG2 & UTXIFG1) == 0);
00053 
00054 }
00055 /*---------------------------------------------------------------------------*/
00056 static void
00057 hexprint(uint8_t v)
00058 {
00059   const char hexconv[] = "0123456789abcdef";
00060   printchar(hexconv[v >> 4]);
00061   printchar(hexconv[v & 0x0f]);
00062 }
00063 /*---------------------------------------------------------------------------*/
00064 static void
00065 printstring(char *s)
00066 {
00067   while(*s) {
00068     printchar(*s++);
00069   }
00070 }
00071 #endif /* CONTIKI_TARGET_SKY */
00072 #endif /* PRINT_STACK_ON_REBOOT */
00073 /*---------------------------------------------------------------------------*/
00074 ISR(WDT, watchdog_interrupt)
00075 {
00076 #ifdef CONTIKI_TARGET_SKY
00077 #if PRINT_STACK_ON_REBOOT
00078   uint8_t dummy;
00079   static uint8_t *ptr;
00080   static int i;
00081 
00082   ptr = &dummy;
00083   printstring("Watchdog reset");
00084   printstring("\nStack at $");
00085   hexprint(((int)ptr) >> 8);
00086   hexprint(((int)ptr) & 0xff);
00087   printstring(":\n");
00088 
00089   for(i = 0; i < 64; ++i) {
00090     hexprint(ptr[i]);
00091     printchar(' ');
00092     if((i & 0x0f) == 0x0f) {
00093       printchar('\n');
00094     }
00095   }
00096   printchar('\n');
00097 #endif /* PRINT_STACK_ON_REBOOT */
00098 #endif /* CONTIKI_TARGET_SKY */
00099 
00100   watchdog_reboot();
00101 }
00102 /*---------------------------------------------------------------------------*/
00103 void
00104 watchdog_init(void)
00105 {
00106   /* The MSP430 watchdog is enabled at boot-up, so we stop it during
00107      initialization. */
00108   counter = 0;
00109   watchdog_stop();
00110 #if CONTIKI_TARGET_WISMOTE
00111   SFRIFG1 &= ~WDTIFG;
00112   SFRIE1 |= WDTIE;
00113 #else
00114   IFG1 &= ~WDTIFG;
00115   IE1 |= WDTIE;
00116 #endif
00117 }
00118 /*---------------------------------------------------------------------------*/
00119 void
00120 watchdog_start(void)
00121 {
00122   /* We setup the watchdog to reset the device after one second,
00123      unless watchdog_periodic() is called. */
00124   counter--;
00125   if(counter == 0) {
00126     WDTCTL = WDTPW | WDTCNTCL | WDT_ARST_1000 | WDTTMSEL;
00127   }
00128 }
00129 /*---------------------------------------------------------------------------*/
00130 void
00131 watchdog_periodic(void)
00132 {
00133   /* This function is called periodically to restart the watchdog
00134      timer. */
00135   /*  if(counter < 0) {*/
00136     WDTCTL = (WDTCTL & 0xff) | WDTPW | WDTCNTCL | WDTTMSEL;
00137     /*  }*/
00138 }
00139 /*---------------------------------------------------------------------------*/
00140 void
00141 watchdog_stop(void)
00142 {
00143   counter++;
00144   if(counter == 1) {
00145     WDTCTL = WDTPW | WDTHOLD;
00146   }
00147 }
00148 /*---------------------------------------------------------------------------*/
00149 void
00150 watchdog_reboot(void)
00151 {
00152   WDTCTL = 0;
00153 }
00154 /*---------------------------------------------------------------------------*/