00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2012, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /** 00031 * \file 00032 * 00033 * Implementation of Watchdog Timer (WDT) controller. 00034 * 00035 */ 00036 00037 /** \addtogroup wdt_module Working with WDT 00038 * \ingroup peripherals_module 00039 * The WDT driver provides the interface to configure and use the WDT 00040 * peripheral. 00041 * 00042 * The WDT can be used to prevent system lock-up if the software becomes 00043 * trapped in a deadlock. It can generate a general reset or a processor 00044 * reset only. It is clocked by slow clock divided by 128. 00045 * 00046 * The WDT is running at reset with 16 seconds watchdog period (slow clock at 00047 * 32.768 kHz) 00048 * and external reset generation enabled. The user must either disable it or 00049 * reprogram it to meet the application requires. 00050 * 00051 * To use the WDT, the user could follow these few steps: 00052 * <ul> 00053 * <li>Enable watchdog with given mode using \ref WDT_Enable(). 00054 * <li>Restart the watchdog using \ref WDT_Restart() within the watchdog period. 00055 * </ul> 00056 * 00057 * For more accurate information, please look at the WDT section of the 00058 * Datasheet. 00059 * 00060 * \note 00061 * The Watchdog Mode Register (WDT_MR) can be written only once.\n 00062 * 00063 * Related files :\n 00064 * \ref wdt.c\n 00065 * \ref wdt.h.\n 00066 */ 00067 /*@{*/ 00068 /*@}*/ 00069 00070 /*--------------------------------------------------------------------------- 00071 * Headers 00072 *---------------------------------------------------------------------------*/ 00073 00074 #include "chip.h" 00075 00076 #include <stdint.h> 00077 00078 /*---------------------------------------------------------------------------- 00079 * Exported functions 00080 *----------------------------------------------------------------------------*/ 00081 00082 /** 00083 * \brief Enable watchdog with given mode. 00084 * 00085 * \note The Watchdog Mode Register (WDT_MR) can be written only once. 00086 * Only a processor reset resets it. 00087 * 00088 * \param dwMode WDT mode to be set 00089 */ 00090 extern void WDT_Enable( Wdt* pWDT, uint32_t dwMode ) 00091 { 00092 pWDT->WDT_MR = dwMode ; 00093 } 00094 00095 /** 00096 * \brief Disable watchdog. 00097 * 00098 * \note The Watchdog Mode Register (WDT_MR) can be written only once. 00099 * Only a processor reset resets it. 00100 */ 00101 extern void WDT_Disable( Wdt* pWDT ) 00102 { 00103 pWDT->WDT_MR = WDT_MR_WDDIS; 00104 } 00105 00106 /** 00107 * \brief Watchdog restart. 00108 */ 00109 extern void WDT_Restart( Wdt* pWDT ) 00110 { 00111 pWDT->WDT_CR = 0xA5000001; 00112 } 00113 00114 /** 00115 * \brief Watchdog get status. 00116 */ 00117 extern uint32_t WDT_GetStatus( Wdt* pWDT ) 00118 { 00119 return (pWDT->WDT_SR & 0x3) ; 00120 } 00121 00122 /** 00123 * \brief Watchdog get period. 00124 * 00125 * \param dwMs desired watchdog period in millisecond. 00126 */ 00127 extern uint32_t WDT_GetPeriod( uint32_t dwMs ) 00128 { 00129 if ( (dwMs < 4) || (dwMs > 16000) ) { 00130 return 0 ; 00131 } 00132 return ((dwMs << 8) / 1000) ; 00133 }