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