1 /**********************************************************************
\r
2 * $Id$ lpc18xx_wwdt.c 2011-06-02
\r
4 * @file lpc18xx_wwdt.c
\r
5 * @brief Contains all functions support for WDT firmware library
\r
8 * @date 02. June. 2011
\r
9 * @author NXP MCU SW Application Team
\r
11 * Copyright(C) 2011, NXP Semiconductor
\r
12 * All rights reserved.
\r
14 ***********************************************************************
\r
15 * Software that is described herein is for illustrative purposes only
\r
16 * which provides customers with programming information regarding the
\r
17 * products. This software is supplied "AS IS" without any warranties.
\r
18 * NXP Semiconductors assumes no responsibility or liability for the
\r
19 * use of the software, conveys no license or title under any patent,
\r
20 * copyright, or mask work right to the product. NXP Semiconductors
\r
21 * reserves the right to make changes in the software without
\r
22 * notification. NXP Semiconductors also make no representation or
\r
23 * warranty that such application will be suitable for the specified
\r
24 * use without further testing or modification.
\r
25 **********************************************************************/
\r
27 /* Peripheral group ----------------------------------------------------------- */
\r
28 /** @addtogroup WWDT
\r
32 /* Includes ------------------------------------------------------------------- */
\r
33 #include "lpc18xx_wwdt.h"
\r
35 /* If this source file built with example, the LPC18xx FW library configuration
\r
36 * file in each example directory ("lpc18xx_libcfg.h") must be included,
\r
37 * otherwise the default FW library configuration file must be included instead
\r
39 #ifdef __BUILD_WITH_EXAMPLE__
\r
40 #include "lpc18xx_libcfg.h"
\r
42 #include "lpc18xx_libcfg_default.h"
\r
43 #endif /* __BUILD_WITH_EXAMPLE__ */
\r
48 void WWDT_SetTimeOut(uint32_t timeout);
\r
50 /*********************************************************************//**
\r
51 * @brief Update WDT timeout value and feed
\r
52 * @param[in] timeout WDT timeout (us)
\r
54 **********************************************************************/
\r
55 void WWDT_SetTimeOut(uint32_t timeout)
\r
57 uint32_t timeoutVal;
\r
59 timeoutVal = WDT_GET_FROM_USEC(timeout);
\r
61 if(timeoutVal < WWDT_TIMEOUT_MIN)
\r
63 timeoutVal = WWDT_TIMEOUT_MIN;
\r
65 else if (timeoutVal > WWDT_TIMEOUT_MAX)
\r
67 timeoutVal = WWDT_TIMEOUT_MAX;
\r
70 LPC_WWDT->TC = timeoutVal;
\r
72 /* Public Functions ----------------------------------------------------------- */
\r
73 /** @addtogroup WDT_Public_Functions
\r
77 /*********************************************************************//**
\r
78 * @brief Initial for Watchdog function
\r
81 **********************************************************************/
\r
82 void WWDT_Init(void)
\r
84 LPC_WWDT->MOD = 0; // Clear time out and interrupt flags
\r
85 LPC_WWDT->TC = WWDT_TIMEOUT_MIN; // Reset time out
\r
86 LPC_WWDT->WARNINT= 0; // Reset warning value
\r
87 LPC_WWDT->WINDOW = WWDT_WINDOW_MAX; // Reset window value
\r
90 /********************************************************************//**
\r
91 * @brief Update WDT timeout value and feed
\r
92 * @param[in] TimeOut TimeOut value to be updated, should be in range:
\r
95 *********************************************************************/
\r
96 void WDT_UpdateTimeOut(uint32_t TimeOut)
\r
99 * if it is enable, wait until the counter is below the value of
\r
100 * WDWARNINT and WDWINDOW
\r
102 if(LPC_WWDT->MOD & (1<<4))
\r
104 while((LPC_WWDT->TV <(LPC_WWDT->WARNINT & WWDT_WDWARNINT_MASK))\
\r
105 &&(LPC_WWDT->TV <(LPC_WWDT->WINDOW & WWDT_WDTC_MASK)));
\r
108 WWDT_SetTimeOut(TimeOut);
\r
110 /********************************************************************//**
\r
111 * @brief After set WDTEN, call this function to start Watchdog
\r
112 * or reload the Watchdog timer
\r
115 *********************************************************************/
\r
116 void WWDT_Feed (void)
\r
118 LPC_WWDT->FEED = 0xAA;
\r
120 LPC_WWDT->FEED = 0x55;
\r
123 /********************************************************************//**
\r
124 * @brief Update WDT timeout value and feed
\r
125 * @param[in] WarnTime time to generate watchdog warning interrupt(us)
\r
126 * should be in range: 2048 .. 8192
\r
128 *********************************************************************/
\r
129 void WWDT_SetWarning(uint32_t WarnTime)
\r
133 warnVal = WDT_GET_FROM_USEC(WarnTime);
\r
135 if(warnVal <= WWDT_WARNINT_MIN)
\r
137 warnVal = WWDT_WARNINT_MIN;
\r
139 else if (warnVal >= WWDT_WARNINT_MAX)
\r
141 warnVal = WWDT_WARNINT_MAX;
\r
144 LPC_WWDT->WARNINT = warnVal;
\r
147 /********************************************************************//**
\r
148 * @brief Update WDT timeout value and feed
\r
149 * @param[in] WindowedTime expected time to set watchdog window event(us)
\r
151 *********************************************************************/
\r
152 void WWDT_SetWindow(uint32_t WindowedTime)
\r
156 wndVal = WDT_GET_FROM_USEC(WindowedTime);
\r
158 if(wndVal <= WWDT_WINDOW_MIN)
\r
160 wndVal = WWDT_WINDOW_MIN;
\r
162 else if (wndVal >= WWDT_WINDOW_MAX)
\r
164 wndVal = WWDT_WINDOW_MAX;
\r
167 LPC_WWDT->WINDOW = wndVal;
\r
169 /*********************************************************************//**
\r
170 * @brief Enable/Disable WWDT activity
\r
173 **********************************************************************/
\r
174 void WWDT_Configure(st_Wdt_Config wdtCfg)
\r
176 WWDT_SetTimeOut(wdtCfg.wdtTmrConst);
\r
178 if(wdtCfg.wdtReset)
\r
180 LPC_WWDT->MOD |= WWDT_WDMOD_WDRESET;
\r
184 LPC_WWDT->MOD &= ~WWDT_WDMOD_WDRESET;
\r
187 if(wdtCfg.wdtProtect)
\r
189 LPC_WWDT->MOD |= WWDT_WDMOD_WDPROTECT;
\r
193 LPC_WWDT->MOD &= ~WWDT_WDMOD_WDPROTECT;
\r
197 /*********************************************************************//**
\r
198 * @brief Enable WWDT activity
\r
201 **********************************************************************/
\r
202 void WWDT_Start(void)
\r
204 LPC_WWDT->MOD |= WWDT_WDMOD_WDEN;
\r
208 /********************************************************************//**
\r
209 * @brief Read WWDT status flag
\r
210 * @param[in] Status kind of status flag that you want to get, should be:
\r
211 * - WWDT_WARNINT_FLAG: watchdog interrupt flag
\r
212 * - WWDT_TIMEOUT_FLAG: watchdog time-out flag
\r
213 * @return Time out flag status of WDT
\r
214 *********************************************************************/
\r
215 FlagStatus WWDT_GetStatus (uint8_t Status)
\r
217 if(Status == WWDT_WARNINT_FLAG)
\r
219 return ((FlagStatus)(LPC_WWDT->MOD & (1<<3)));
\r
221 else if (Status == WWDT_TIMEOUT_FLAG)
\r
223 return ((FlagStatus)(LPC_WWDT->MOD & (1<<2)));
\r
225 return (FlagStatus)RESET;
\r
228 /********************************************************************//**
\r
229 * @brief Read WWDT status flag
\r
230 * @param[in] Status kind of status flag that you want to get, should be:
\r
231 * - WWDT_WARNINT_FLAG: watchdog interrupt flag
\r
232 * - WWDT_TIMEOUT_FLAG: watchdog time-out flag
\r
233 * @return Time out flag status of WDT
\r
234 *********************************************************************/
\r
235 void WWDT_ClearStatusFlag (uint8_t flag)
\r
237 if(flag == WWDT_WARNINT_FLAG)
\r
239 // Write 1 to this bit to clear itself
\r
240 LPC_WWDT->MOD |= WWDT_WDMOD_WDINT;
\r
242 else if(flag == WWDT_TIMEOUT_FLAG)
\r
244 // Write 0 to this bit to clear itself
\r
245 LPC_WWDT->MOD &= ~ WWDT_WDMOD_WDTOF;
\r
249 /********************************************************************//**
\r
250 * @brief Get the current value of WDT
\r
252 * @return current value of WDT
\r
253 *********************************************************************/
\r
254 uint32_t WWDT_GetCurrentCount(void)
\r
256 return LPC_WWDT->TV;
\r
268 /* --------------------------------- End Of File ------------------------------ */
\r