Initial (nonworking) draft of preemptive task switching
[bertos.git] / bertos / drv / wdt.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id$
34  *
35  * \author Bernie Innocenti <bernie@codewiz.org>
36  *
37  * \brief Watchdog interface
38  */
39
40 #ifndef DRV_WDT_H
41 #define DRV_WDT_H
42
43 #include "cfg/cfg_wdt.h"
44 #include <cfg/compiler.h> // INLINE
45 #include "cfg/cfg_arch.h"
46
47 /* Configury sanity check */
48 #if !defined(CONFIG_WATCHDOG) || (CONFIG_WATCHDOG != 0 && CONFIG_WATCHDOG != 1)
49         #error CONFIG_WATCHDOG must be defined to either 0 or 1
50 #endif
51
52 #if CONFIG_WATCHDOG
53         #include <cpu/detect.h>
54         #include <cfg/os.h>
55
56         #if OS_QT
57                         #include <QtGui/QApplication>
58         #elif OS_POSIX
59                 #include <sys/select.h>
60         #elif CPU_AVR
61                 #include <avr/io.h>
62                 #include <cfg/macros.h> // BV()
63                 #if CPU_AVR_ATMEGA1281  // Name is different in atmega1281
64                         #define WDTCR WDTCSR
65                 #endif
66         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
67                 #include <task.h> /* taskYIELD() */
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 defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
87                 vTaskDelay(1);
88         #elif CPU_AVR
89                 __asm__ __volatile__ ("wdr");
90         #else
91                 #error unknown CPU
92         #endif
93 #endif /* CONFIG_WATCHDOG */
94 }
95
96 /**
97  * Set watchdog timer timeout.
98  *
99  * \param timeout  0: 16.3ms, 7: 2.1s
100  */
101 INLINE void wdt_init(uint8_t timeout)
102 {
103 #if CONFIG_WATCHDOG
104         #if OS_QT
105                 // Create a dummy QApplication object
106                 if (!qApp)
107                 {
108                         int argc;
109                         new QApplication(argc, (char **)NULL);
110                 }
111                 (void)timeout;
112         #elif OS_POSIX
113                 (void)timeout; // NOP
114         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
115                 /* nop */
116         #elif CPU_AVR
117                 WDTCR |= BV(WDCE) | BV(WDE);
118                 WDTCR = timeout;
119         #else
120                 #error unknown CPU
121         #endif
122 #else
123         (void)timeout;
124 #endif /* CONFIG_WATCHDOG */
125 }
126
127 INLINE void wdt_start(void)
128 {
129 #if CONFIG_WATCHDOG
130         #if OS_QT
131                 // NOP
132         #elif OS_POSIX
133                 // NOP
134         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
135                 /* nop */
136         #elif CPU_AVR
137                 WDTCR |= BV(WDE);
138         #else
139                 #error unknown CPU
140         #endif
141 #endif /* CONFIG_WATCHDOG */
142 }
143
144 INLINE void wdt_stop(void)
145 {
146 #if CONFIG_WATCHDOG
147         #if OS_QT
148                 // NOP
149         #elif OS_POSIX
150                 // NOP
151         #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
152                 /* nop */
153         #elif CPU_AVR
154                 WDTCR |= BV(WDCE) | BV(WDE);
155                 WDTCR &= ~BV(WDE);
156         #else
157                 #error unknown CPU
158         #endif
159 #endif /* CONFIG_WATCHDOG */
160 }
161
162 #endif /* DRV_WDT_H */