Contiki 2.5
watchdog.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * @(#)$Id: watchdog.c,v 1.12 2010/11/12 15:54:41 nifi Exp $
32  */
33 
34 #include "contiki.h"
35 #include "dev/watchdog.h"
36 
37 static int counter = 0;
38 
39 #define PRINT_STACK_ON_REBOOT 0
40 
41 /*---------------------------------------------------------------------------*/
42 #if PRINT_STACK_ON_REBOOT
43 #ifdef CONTIKI_TARGET_SKY
44 static void
45 printchar(char c)
46 {
47  /* Transmit the data. */
48  TXBUF1 = c;
49 
50  /* Loop until the transmission buffer is available. */
51  while((IFG2 & UTXIFG1) == 0);
52 
53 }
54 /*---------------------------------------------------------------------------*/
55 static void
56 hexprint(uint8_t v)
57 {
58  const char hexconv[] = "0123456789abcdef";
59  printchar(hexconv[v >> 4]);
60  printchar(hexconv[v & 0x0f]);
61 }
62 /*---------------------------------------------------------------------------*/
63 static void
64 printstring(char *s)
65 {
66  while(*s) {
67  printchar(*s++);
68  }
69 }
70 #endif /* CONTIKI_TARGET_SKY */
71 #endif /* PRINT_STACK_ON_REBOOT */
72 /*---------------------------------------------------------------------------*/
73 #ifdef __IAR_SYSTEMS_ICC__
74 #pragma vector=WDT_VECTOR
75 __interrupt void
76 #else
77 interrupt(WDT_VECTOR)
78 #endif
79 watchdog_interrupt(void)
80 {
81 #ifdef CONTIKI_TARGET_SKY
82 #if PRINT_STACK_ON_REBOOT
83  uint8_t dummy;
84  static uint8_t *ptr;
85  static int i;
86 
87  ptr = &dummy;
88  printstring("Watchdog reset");
89  printstring("\nStack at $");
90  hexprint(((int)ptr) >> 8);
91  hexprint(((int)ptr) & 0xff);
92  printstring(":\n");
93 
94  for(i = 0; i < 64; ++i) {
95  hexprint(ptr[i]);
96  printchar(' ');
97  if((i & 0x0f) == 0x0f) {
98  printchar('\n');
99  }
100  }
101  printchar('\n');
102 #endif /* PRINT_STACK_ON_REBOOT */
103 #endif /* CONTIKI_TARGET_SKY */
104 
105  watchdog_reboot();
106 }
107 /*---------------------------------------------------------------------------*/
108 void
109 watchdog_init(void)
110 {
111  /* The MSP430 watchdog is enabled at boot-up, so we stop it during
112  initialization. */
113  counter = 0;
114  watchdog_stop();
115 
116  IFG1 &= ~WDTIFG;
117  IE1 |= WDTIE;
118 }
119 /*---------------------------------------------------------------------------*/
120 void
121 watchdog_start(void)
122 {
123  /* We setup the watchdog to reset the device after one second,
124  unless watchdog_periodic() is called. */
125 // printf("WATCHDOG: on\n");
126  counter--;
127  if(counter == 0) {
128  WDTCTL = WDTPW | WDTCNTCL | WDT_ARST_1000 | WDTTMSEL;
129  }
130 }
131 /*---------------------------------------------------------------------------*/
132 void
133 watchdog_periodic(void)
134 {
135  /* This function is called periodically to restart the watchdog
136  timer. */
137  /* if(counter < 0) {*/
138  WDTCTL = (WDTCTL & 0xff) | WDTPW | WDTCNTCL | WDTTMSEL;
139  /* }*/
140 }
141 /*---------------------------------------------------------------------------*/
142 void
143 watchdog_stop(void)
144 {
145  // printf("WATCHDOG: off\n");
146  counter++;
147  if(counter == 1) {
148  WDTCTL = WDTPW | WDTHOLD;
149  }
150 }
151 /*---------------------------------------------------------------------------*/
152 void
153 watchdog_reboot(void)
154 {
155  WDTCTL = 0;
156 }
157 /*---------------------------------------------------------------------------*/