/******************************************************************************************************************* *File Name : watchdog.c *Description : the file is driver for watchdog *Date : 2008/06/16 *Author : yuanguiyou --History: ------------------------------------------------------------------------------------------------------------------ *change info: *******************************************************************************************************************/ #include #include #include "watchdog.h" /*! * \addtogroup xgNutArchAvrDevWatchDog */ /*@{*/ /*! * \brief Watchdog oscillator frequency. */ #ifndef WDT_FREQ #define WDT_FREQ 1165000 #endif static u16 nested; static u08 wdt_div; /*! * \brief Start the AVR hardware watch dog timer. * * For portability, applications should use the platform independent * \ref xgWatchDog "Watchdog Driver API". * * \param ms Desired watchdog timeout in milliseconds. * * \return The actual watchdog timeout. */ u32 WatchDogStart(u32 ms) { u32 ticks; wdt_reset(); ticks = ((WDT_FREQ / 1000UL) * ms) >> 14; for (wdt_div = 0; wdt_div < 7 && ticks; wdt_div++) { ticks >>= 1; } wdt_enable(wdt_div); nested = 1; return (16384UL << wdt_div) / (WDT_FREQ / 1000UL); } /*! * \brief Re-start the AVR hardware watch dog timer. * * For portability, applications should use the platform independent * \ref xgWatchDog "Watchdog Driver API". */ void WatchDogRestart(void) { wdt_reset(); } /*! * \brief Disable the AVR hardware watch dog timer. * * For portability, applications should use the platform independent * \ref xgWatchDog "Watchdog Driver API". */ void WatchDogDisable(void) { if (nested) { nested++; } wdt_disable(); } /*! * \brief Enable the AVR hardware watch dog timer. * * For portability, applications should use the platform independent * \ref xgWatchDog "Watchdog Driver API". */ void WatchDogEnable(void) { if (nested > 1 && --nested == 1) { wdt_enable(wdt_div); } } /*@}*/