00001 /* ---------------------------------------------------------------------------- */ 00002 /* Atmel Microcontroller Software Support */ 00003 /* SAM Software Package License */ 00004 /* ---------------------------------------------------------------------------- */ 00005 /* Copyright (c) 2015, Atmel Corporation */ 00006 /* */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without */ 00010 /* modification, are permitted provided that the following condition is met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, */ 00013 /* this list of conditions and the disclaimer below. */ 00014 /* */ 00015 /* Atmel's name may not be used to endorse or promote products derived from */ 00016 /* this software without specific prior written permission. */ 00017 /* */ 00018 /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ 00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ 00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ 00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ 00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ 00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ 00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ 00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ 00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ 00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 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 }