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