wdt.h

Go to the documentation of this file.
00001 
00040 #ifndef DRV_WDT_H
00041 #define DRV_WDT_H
00042 
00043 #include "cfg/cfg_wdt.h"
00044 #include <cfg/compiler.h> // INLINE
00045 #include "cfg/cfg_arch.h"
00046 
00047 /* Configury sanity check */
00048 #if !defined(CONFIG_WATCHDOG) || (CONFIG_WATCHDOG != 0 && CONFIG_WATCHDOG != 1)
00049     #error CONFIG_WATCHDOG must be defined to either 0 or 1
00050 #endif
00051 
00052 #if CONFIG_WATCHDOG
00053     #include <cpu/detect.h>
00054     #include <cfg/os.h>
00055 
00056     #if OS_QT
00057             #include <QtGui/QApplication>
00058     #elif OS_POSIX
00059         #include <sys/select.h>
00060     #elif CPU_AVR
00061         #include <avr/io.h>
00062         #include <cfg/macros.h> // BV()
00063         #if CPU_AVR_ATMEGA1281  // Name is different in atmega1281
00064             #define WDTCR WDTCSR
00065         #endif
00066     #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
00067         #include <task.h> /* taskYIELD() */
00068     #else
00069         #error unknown CPU
00070     #endif
00071 #endif /* CONFIG_WATCHDOG */
00072 
00076 INLINE void wdt_reset(void)
00077 {
00078 #if CONFIG_WATCHDOG
00079     #if OS_QT
00080         // Let Qt handle events
00081         ASSERT(qApp);
00082         qApp->processEvents();
00083     #elif OS_POSIX
00084         static struct timeval tv = { 0, 0 };
00085         select(0, NULL, NULL, NULL, &tv);
00086     #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
00087         vTaskDelay(1);
00088     #elif CPU_AVR
00089         __asm__ __volatile__ ("wdr");
00090     #else
00091         #error unknown CPU
00092     #endif
00093 #endif /* CONFIG_WATCHDOG */
00094 }
00095 
00101 INLINE void wdt_init(uint8_t timeout)
00102 {
00103 #if CONFIG_WATCHDOG
00104     #if OS_QT
00105         // Create a dummy QApplication object
00106         if (!qApp)
00107         {
00108             int argc;
00109             new QApplication(argc, (char **)NULL);
00110         }
00111         (void)timeout;
00112     #elif OS_POSIX
00113         (void)timeout; // NOP
00114     #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
00115         /* nop */
00116     #elif CPU_AVR
00117         WDTCR |= BV(WDCE) | BV(WDE);
00118         WDTCR = timeout;
00119     #else
00120         #error unknown CPU
00121     #endif
00122 #else
00123     (void)timeout;
00124 #endif /* CONFIG_WATCHDOG */
00125 }
00126 
00127 INLINE void wdt_start(void)
00128 {
00129 #if CONFIG_WATCHDOG
00130     #if OS_QT
00131         // NOP
00132     #elif OS_POSIX
00133         // NOP
00134     #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
00135         /* nop */
00136     #elif CPU_AVR
00137         WDTCR |= BV(WDE);
00138     #else
00139         #error unknown CPU
00140     #endif
00141 #endif /* CONFIG_WATCHDOG */
00142 }
00143 
00144 INLINE void wdt_stop(void)
00145 {
00146 #if CONFIG_WATCHDOG
00147     #if OS_QT
00148         // NOP
00149     #elif OS_POSIX
00150         // NOP
00151     #elif defined(ARCH_FREERTOS) && (ARCH & ARCH_FREERTOS)
00152         /* nop */
00153     #elif CPU_AVR
00154         WDTCR |= BV(WDCE) | BV(WDE);
00155         WDTCR &= ~BV(WDE);
00156     #else
00157         #error unknown CPU
00158     #endif
00159 #endif /* CONFIG_WATCHDOG */
00160 }
00161 
00162 #endif /* DRV_WDT_H */