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