8abba4791f9accaaf5f9193a62398c06add9c55e
[bertos.git] / drv / wdt.h
1 /**
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \version $Id$
9  *
10  * \author Bernardo Innocenti <bernie@develer.com>
11  *
12  * \brief Watchdog interface
13  */
14
15 /*#*
16  *#* $Log$
17  *#* Revision 1.12  2007/06/07 14:35:12  batt
18  *#* Merge from project_ks.
19  *#*
20  *#* Revision 1.11  2006/07/19 12:56:26  bernie
21  *#* Convert to new Doxygen style.
22  *#*
23  *#* Revision 1.10  2006/05/18 00:38:42  bernie
24  *#* Work around missing ARCH_FREERTOS symbol.
25  *#*
26  *#* Revision 1.9  2006/03/22 09:49:09  bernie
27  *#* Add FreeRTOS support.
28  *#*
29  *#* Revision 1.8  2006/02/20 02:02:29  bernie
30  *#* Port to Qt 4.1.
31  *#*
32  *#* Revision 1.7  2005/11/27 03:58:40  bernie
33  *#* Add POSIX timer emulator.
34  *#*
35  *#* Revision 1.6  2005/11/27 03:03:08  bernie
36  *#* Add Qt support hack.
37  *#*
38  *#* Revision 1.5  2005/11/04 16:20:02  bernie
39  *#* Fix reference to README.devlib in header.
40  *#*
41  *#* Revision 1.4  2005/04/12 01:37:17  bernie
42  *#* Prevent warning when watchdog is disabled.
43  *#*
44  *#* Revision 1.3  2005/04/11 19:10:28  bernie
45  *#* Include top-level headers from cfg/ subdir.
46  *#*
47  *#* Revision 1.2  2004/11/16 21:02:07  bernie
48  *#* Make driver optional; mark AVR specific parts as such.
49  *#*
50  *#* Revision 1.1  2004/10/26 08:34:47  bernie
51  *#* New DevLib module.
52  *#*
53  *#*/
54 #ifndef DRV_WDT_H
55 #define DRV_WDT_H
56
57 #include <appconfig.h>
58 #include <cfg/compiler.h> // INLINE
59 #include <cfg/arch_config.h>
60
61 /* Configury sanity check */
62 #if !defined(CONFIG_WATCHDOG) || (CONFIG_WATCHDOG != 0 && CONFIG_WATCHDOG != 1)
63         #error CONFIG_WATCHDOG must be defined to either 0 or 1
64 #endif
65
66 #if CONFIG_WATCHDOG
67         #include <cfg/cpu.h>
68         #include <cfg/os.h>
69
70         #if OS_QT
71                 #if _QT < 4
72                         #include <qapplication.h>
73                 #else
74                         #include <QtGui/QApplication>
75                 #endif
76         #elif OS_POSIX
77                 #include <sys/select.h>
78         #elif CPU_AVR
79                 #include <avr/io.h>
80                 #include <cfg/macros.h> // BV()
81                 #if CPU_AVR_ATMEGA1281  // Name is different in atmega1281
82                         #define WDTCR WDTCSR
83                 #endif
84         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
85                 #include <task.h> /* taskYIELD() */
86         #else
87                 #error unknown CPU
88         #endif
89 #endif /* CONFIG_WATCHDOG */
90
91 /**
92  * Reset the watchdog timer.
93  */
94 INLINE void wdt_reset(void)
95 {
96 #if CONFIG_WATCHDOG
97         #if OS_QT
98                 // Let Qt handle events
99                 ASSERT(qApp);
100                 qApp->processEvents();
101         #elif OS_POSIX
102                 static struct timeval tv = { 0, 0 };
103                 select(0, NULL, NULL, NULL, &tv);
104         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
105                 vTaskDelay(1);
106         #elif CPU_AVR
107                 __asm__ __volatile__ ("wdr");
108         #else
109                 #error unknown CPU
110         #endif
111 #endif /* CONFIG_WATCHDOG */
112 }
113
114 /**
115  * Set watchdog timer timeout.
116  *
117  * \param timeout  0: 16.3ms, 7: 2.1s
118  */
119 INLINE void wdt_init(uint8_t timeout)
120 {
121 #if CONFIG_WATCHDOG
122         #if OS_QT
123                 // Create a dummy QApplication object
124                 if (!qApp)
125                 {
126                         int argc;
127                         new QApplication(argc, (char **)NULL);
128                 }
129                 (void)timeout;
130         #elif OS_POSIX
131                 (void)timeout; // NOP
132         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
133                 /* nop */
134         #elif CPU_AVR
135                 WDTCR |= BV(WDCE) | BV(WDE);
136                 WDTCR = timeout;
137         #else
138                 #error unknown CPU
139         #endif
140 #else
141         (void)timeout;
142 #endif /* CONFIG_WATCHDOG */
143 }
144
145 INLINE void wdt_start(void)
146 {
147 #if CONFIG_WATCHDOG
148         #if OS_QT
149                 // NOP
150         #elif OS_POSIX
151                 // NOP
152         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
153                 /* nop */
154         #elif CPU_AVR
155                 WDTCR |= BV(WDE);
156         #else
157                 #error unknown CPU
158         #endif
159 #endif /* CONFIG_WATCHDOG */
160 }
161
162 INLINE void wdt_stop(void)
163 {
164 #if CONFIG_WATCHDOG
165         #if OS_QT
166                 // NOP
167         #elif OS_POSIX
168                 // NOP
169         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
170                 /* nop */
171         #elif CPU_AVR
172                 WDTCR |= BV(WDCE) | BV(WDE);
173                 WDTCR &= ~BV(WDE);
174         #else
175                 #error unknown CPU
176         #endif
177 #endif /* CONFIG_WATCHDOG */
178 }
179
180 #endif /* DRV_WDT_H */