Update to new tree.
[bertos.git] / drv / wdt.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Bernardo Innocenti <bernie@develer.com>
36  *
37  * \brief Watchdog interface
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.12  2007/06/07 14:35:12  batt
43  *#* Merge from project_ks.
44  *#*
45  *#* Revision 1.11  2006/07/19 12:56:26  bernie
46  *#* Convert to new Doxygen style.
47  *#*
48  *#* Revision 1.10  2006/05/18 00:38:42  bernie
49  *#* Work around missing ARCH_FREERTOS symbol.
50  *#*
51  *#* Revision 1.9  2006/03/22 09:49:09  bernie
52  *#* Add FreeRTOS support.
53  *#*
54  *#* Revision 1.8  2006/02/20 02:02:29  bernie
55  *#* Port to Qt 4.1.
56  *#*
57  *#* Revision 1.7  2005/11/27 03:58:40  bernie
58  *#* Add POSIX timer emulator.
59  *#*
60  *#* Revision 1.6  2005/11/27 03:03:08  bernie
61  *#* Add Qt support hack.
62  *#*
63  *#* Revision 1.5  2005/11/04 16:20:02  bernie
64  *#* Fix reference to README.devlib in header.
65  *#*
66  *#* Revision 1.4  2005/04/12 01:37:17  bernie
67  *#* Prevent warning when watchdog is disabled.
68  *#*
69  *#* Revision 1.3  2005/04/11 19:10:28  bernie
70  *#* Include top-level headers from cfg/ subdir.
71  *#*
72  *#* Revision 1.2  2004/11/16 21:02:07  bernie
73  *#* Make driver optional; mark AVR specific parts as such.
74  *#*
75  *#* Revision 1.1  2004/10/26 08:34:47  bernie
76  *#* New DevLib module.
77  *#*
78  *#*/
79 #ifndef DRV_WDT_H
80 #define DRV_WDT_H
81
82 #include <appconfig.h>
83 #include <cfg/compiler.h> // INLINE
84 #include <cfg/arch_config.h>
85
86 /* Configury sanity check */
87 #if !defined(CONFIG_WATCHDOG) || (CONFIG_WATCHDOG != 0 && CONFIG_WATCHDOG != 1)
88         #error CONFIG_WATCHDOG must be defined to either 0 or 1
89 #endif
90
91 #if CONFIG_WATCHDOG
92         #include <cpu/cpu.h>
93         #include <cfg/os.h>
94
95         #if OS_QT
96                 #if _QT < 4
97                         #include <qapplication.h>
98                 #else
99                         #include <QtGui/QApplication>
100                 #endif
101         #elif OS_POSIX
102                 #include <sys/select.h>
103         #elif CPU_AVR
104                 #include <avr/io.h>
105                 #include <cfg/macros.h> // BV()
106                 #if CPU_AVR_ATMEGA1281  // Name is different in atmega1281
107                         #define WDTCR WDTCSR
108                 #endif
109         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
110                 #include <task.h> /* taskYIELD() */
111         #else
112                 #error unknown CPU
113         #endif
114 #endif /* CONFIG_WATCHDOG */
115
116 /**
117  * Reset the watchdog timer.
118  */
119 INLINE void wdt_reset(void)
120 {
121 #if CONFIG_WATCHDOG
122         #if OS_QT
123                 // Let Qt handle events
124                 ASSERT(qApp);
125                 qApp->processEvents();
126         #elif OS_POSIX
127                 static struct timeval tv = { 0, 0 };
128                 select(0, NULL, NULL, NULL, &tv);
129         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
130                 vTaskDelay(1);
131         #elif CPU_AVR
132                 __asm__ __volatile__ ("wdr");
133         #else
134                 #error unknown CPU
135         #endif
136 #endif /* CONFIG_WATCHDOG */
137 }
138
139 /**
140  * Set watchdog timer timeout.
141  *
142  * \param timeout  0: 16.3ms, 7: 2.1s
143  */
144 INLINE void wdt_init(uint8_t timeout)
145 {
146 #if CONFIG_WATCHDOG
147         #if OS_QT
148                 // Create a dummy QApplication object
149                 if (!qApp)
150                 {
151                         int argc;
152                         new QApplication(argc, (char **)NULL);
153                 }
154                 (void)timeout;
155         #elif OS_POSIX
156                 (void)timeout; // NOP
157         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
158                 /* nop */
159         #elif CPU_AVR
160                 WDTCR |= BV(WDCE) | BV(WDE);
161                 WDTCR = timeout;
162         #else
163                 #error unknown CPU
164         #endif
165 #else
166         (void)timeout;
167 #endif /* CONFIG_WATCHDOG */
168 }
169
170 INLINE void wdt_start(void)
171 {
172 #if CONFIG_WATCHDOG
173         #if OS_QT
174                 // NOP
175         #elif OS_POSIX
176                 // NOP
177         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
178                 /* nop */
179         #elif CPU_AVR
180                 WDTCR |= BV(WDE);
181         #else
182                 #error unknown CPU
183         #endif
184 #endif /* CONFIG_WATCHDOG */
185 }
186
187 INLINE void wdt_stop(void)
188 {
189 #if CONFIG_WATCHDOG
190         #if OS_QT
191                 // NOP
192         #elif OS_POSIX
193                 // NOP
194         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
195                 /* nop */
196         #elif CPU_AVR
197                 WDTCR |= BV(WDCE) | BV(WDE);
198                 WDTCR &= ~BV(WDE);
199         #else
200                 #error unknown CPU
201         #endif
202 #endif /* CONFIG_WATCHDOG */
203 }
204
205 #endif /* DRV_WDT_H */