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