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 # warning *** MCUCSR not defined, using MCUSR instead ***
71 # define MCUCSR MCUSR
72 # endif
73 #endif
74 
75 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
76 static int stopped = 0;
77 #endif
78 
79 /*---------------------------------------------------------------------------*/
80 void
81 watchdog_init(void)
82 {
83 /* Clear startup bit and disable the wdt, whether or not it will be used.
84  Random code may have caused the last reset.
85  */
86  MCUCSR&=~(1<<WDRF);
87  wdt_disable();
88 #if WATCHDOG_CONF_BALANCE && WATCHDOG_CONF_TIMEOUT >= 0
89  stopped = 1;
90 #endif
91 }
92 /*---------------------------------------------------------------------------*/
93 void
94 watchdog_start(void)
95 {
96 #if WATCHDOG_CONF_TIMEOUT >= 0
97 #if WATCHDOG_CONF_BALANCE
98  stopped--;
99  if(!stopped)
100 #endif
101  wdt_enable(WATCHDOG_CONF_TIMEOUT);
102 #endif
103 }
104 /*---------------------------------------------------------------------------*/
105 void
106 watchdog_periodic(void)
107 {
108 #if WATCHDOG_CONF_TIMEOUT >= 0
109 #if WATCHDOG_CONF_BALANCE
110  if(!stopped)
111 #endif
112  wdt_reset();
113 #endif
114 }
115 /*---------------------------------------------------------------------------*/
116 void
117 watchdog_stop(void)
118 {
119 #if WATCHDOG_CONF_TIMEOUT >= 0
120 #if WATCHDOG_CONF_BALANCE
121  stopped++;
122 #endif
123  wdt_disable();
124 #endif
125 }
126 /*---------------------------------------------------------------------------*/
127 void
129 {
130  cli();
131  wdt_enable(WDTO_15MS); //wd on,250ms
132  while(1); //loop
133 }
134 /*---------------------------------------------------------------------------*/
135 #if 0
136 /* Not all AVRs implement the wdt interrupt */
137 ISR(WDT_vect)
138 {
139 }
140 #endif
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
void watchdog_reboot(void)
Keeps control until the WDT throws a reset signal.
Definition: watchdog.c:128
void watchdog_stop(void)
In watchdog mode, the WDT can not be stopped.
Definition: watchdog.c:58
void watchdog_periodic(void)
Writes the WDT clear sequence.
Definition: watchdog.c:64
void watchdog_init(void)
Copyright (c) 2014, Analog Devices, Inc.
Definition: watchdog.c:42