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