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 /* Watchdog routines for the AVR */
34 
35 /* The default timeout of 2 seconds works on most MCUs.
36  * It should be disabled during sleep (unless used for wakeup) since
37  * it draws significant current (~5 uamp on 1284p, 20x the MCU consumption).
38  *
39  * Note the wdt is not properly simulated in AVR Studio 4 Simulator 1:
40  * On many devices calls to wdt_reset will have no effect, and a wdt reboot will occur.
41  * The MCUSR will not show the cause of a wdt reboot.
42  * A 1MHz clock is assumed; at 8MHz timeout occurs 8x faster than it should.
43  * Simulator 2 is supposed to work on supported devices (not atmega128rfa1),
44  * but neither it nor Studio 5 beta do any resets on the 1284p.
45  *
46  * Setting WATCHDOG_CONF_TIMEOUT -1 will disable the WDT.
47  */
48 //#define WATCHDOG_CONF_TIMEOUT -1
49 
50 #ifndef WATCHDOG_CONF_TIMEOUT
51 #define WATCHDOG_CONF_TIMEOUT WDTO_2S
52 #endif
53 
54  /* While balancing start and stop calls is a good idea, an imbalance will cause
55  * resets that can take a lot of time to track down.
56  * Some low power protocols may cause this.
57  * The default is no balance; define WATCHDOG_CONF_BALANCE 1 to override.
58  */
59 #ifndef WATCHDOG_CONF_BALANCE
60 #define WATCHDOG_CONF_BALANCE 0
61 #endif
62 
63 #include "dev/watchdog.h"
64 #include <avr/wdt.h>
65 #include <avr/interrupt.h>
66 
67 /* MCUSR is a deprecated name but older avr-libc versions may define it */
68 #if !defined (MCUCSR)
69 # if defined (MCUSR)
70 # define MCUCSR MCUSR
71 # endif
72 #endif
73 
74 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
75 static int stopped = 0;
76 #endif
77 
78 /*---------------------------------------------------------------------------*/
79 void
80 watchdog_init(void)
81 {
82 /* Clear startup bit and disable the wdt, whether or not it will be used.
83  Random code may have caused the last reset.
84  */
85  MCUCSR&=~(1<<WDRF);
86  wdt_disable();
87 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
88  stopped = 1;
89 #endif
90 }
91 /*---------------------------------------------------------------------------*/
92 void
93 watchdog_start(void)
94 {
95 #if WATCHDOG_CONF_TIMEOUT >= 0
96 #if WATCHDOG_CONF_BALANCE
97  stopped--;
98  if(!stopped)
99 #endif
100  wdt_enable(WATCHDOG_CONF_TIMEOUT);
101 #endif
102 }
103 /*---------------------------------------------------------------------------*/
104 void
105 watchdog_periodic(void)
106 {
107 #if WATCHDOG_CONF_TIMEOUT >= 0
108 #if WATCHDOG_CONF_BALANCE
109  if(!stopped)
110 #endif
111  wdt_reset();
112 #endif
113 }
114 /*---------------------------------------------------------------------------*/
115 void
116 watchdog_stop(void)
117 {
118 #if WATCHDOG_CONF_TIMEOUT >= 0
119 #if WATCHDOG_CONF_BALANCE
120  stopped++;
121 #endif
122  wdt_disable();
123 #endif
124 }
125 /*---------------------------------------------------------------------------*/
126 void
127 watchdog_reboot(void)
128 {
129  cli();
130  wdt_enable(WDTO_15MS); //wd on,250ms
131  while(1); //loop
132 }
133 /*---------------------------------------------------------------------------*/
134 #if 0
135 /* Not all AVRs implement the wdt interrupt */
136 ISR(WDT_vect)
137 {
138 }
139 #endif
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