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