Contiki 3.x
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  */
32 
33 #include "stdio.h"
34 #include "contiki.h"
35 #include "dev/watchdog.h"
36 #include "isr_compat.h"
37 
38 static int counter = 0;
39 
40 #define PRINT_STACK_ON_REBOOT 0
41 
42 /*---------------------------------------------------------------------------*/
43 #if PRINT_STACK_ON_REBOOT
44 #ifdef CONTIKI_TARGET_SKY
45 static void
46 printchar(char c)
47 {
48  /* Transmit the data. */
49  TXBUF1 = c;
50 
51  /* Loop until the transmission buffer is available. */
52  while((IFG2 & UTXIFG1) == 0);
53 
54 }
55 /*---------------------------------------------------------------------------*/
56 static void
57 hexprint(uint8_t v)
58 {
59  const char hexconv[] = "0123456789abcdef";
60  printchar(hexconv[v >> 4]);
61  printchar(hexconv[v & 0x0f]);
62 }
63 /*---------------------------------------------------------------------------*/
64 static void
65 printstring(char *s)
66 {
67  while(*s) {
68  printchar(*s++);
69  }
70 }
71 #endif /* CONTIKI_TARGET_SKY */
72 #endif /* PRINT_STACK_ON_REBOOT */
73 /*---------------------------------------------------------------------------*/
74 ISR(WDT, watchdog_interrupt)
75 {
76 #ifdef CONTIKI_TARGET_SKY
77 #if PRINT_STACK_ON_REBOOT
78  uint8_t dummy;
79  static uint8_t *ptr;
80  static int i;
81 
82  ptr = &dummy;
83  printstring("Watchdog reset");
84  printstring("\nStack at $");
85  hexprint(((int)ptr) >> 8);
86  hexprint(((int)ptr) & 0xff);
87  printstring(":\n");
88 
89  for(i = 0; i < 64; ++i) {
90  hexprint(ptr[i]);
91  printchar(' ');
92  if((i & 0x0f) == 0x0f) {
93  printchar('\n');
94  }
95  }
96  printchar('\n');
97 #endif /* PRINT_STACK_ON_REBOOT */
98 #endif /* CONTIKI_TARGET_SKY */
99 
100  watchdog_reboot();
101 }
102 /*---------------------------------------------------------------------------*/
103 void
105 {
106  /* The MSP430 watchdog is enabled at boot-up, so we stop it during
107  initialization. */
108  counter = 0;
109  watchdog_stop();
110 #if CONTIKI_TARGET_WISMOTE
111  SFRIFG1 &= ~WDTIFG;
112  SFRIE1 |= WDTIE;
113 #else
114  IFG1 &= ~WDTIFG;
115  IE1 |= WDTIE;
116 #endif
117 }
118 /*---------------------------------------------------------------------------*/
119 void
121 {
122  /* We setup the watchdog to reset the device after one second,
123  unless watchdog_periodic() is called. */
124  counter--;
125  if(counter == 0) {
126  WDTCTL = WDTPW | WDTCNTCL | WDT_ARST_1000 | WDTTMSEL;
127  }
128 }
129 /*---------------------------------------------------------------------------*/
130 void
132 {
133  /* This function is called periodically to restart the watchdog
134  timer. */
135  /* if(counter < 0) {*/
136  WDTCTL = (WDTCTL & 0xff) | WDTPW | WDTCNTCL | WDTTMSEL;
137  /* }*/
138 }
139 /*---------------------------------------------------------------------------*/
140 void
142 {
143  counter++;
144  if(counter == 1) {
145  WDTCTL = WDTPW | WDTHOLD;
146  }
147 }
148 /*---------------------------------------------------------------------------*/
149 void
151 {
152  printf("$\n");
153  WDTCTL = 0;
154 }
155 /*---------------------------------------------------------------------------*/
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
Definition: watchdog.c:60
void watchdog_reboot(void)
Keeps control until the WDT throws a reset signal.
Definition: watchdog.c:94
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:85
void watchdog_init(void)
Initialisation function for the WDT.
Definition: watchdog.c:63
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:72