LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
watchdog.c
Go to the documentation of this file.
1 /*
2  * @brief WWDT example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "board.h"
33 
70 /*****************************************************************************
71  * Private types/enumerations/variables
72  ****************************************************************************/
73 
74 /* Comment this define to let the watchdog timeout. In this case, the board
75  will continuously drop via to the WDT warning. */
76 #define DISABLE_WDT_TIMEOUT
77 
78 /*****************************************************************************
79  * Public types/enumerations/variables
80  ****************************************************************************/
81 
82 /*****************************************************************************
83  * Private functions
84  ****************************************************************************/
85 
86 /*****************************************************************************
87  * Public functions
88  ****************************************************************************/
89 
95 void WDT_IRQHandler(void)
96 {
98 
99 #if defined(CHIP_LPC11CXX)
100  if (wdtStatus & WWDT_WDMOD_WDTOF) {
101  Board_LED_Toggle(0);
102  while(Chip_WWDT_GetStatus(LPC_WWDT) & WWDT_WDMOD_WDTOF) {
103  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF);
104  }
105  Chip_WWDT_Start(LPC_WWDT); /* Needs restart */
106  }
107 #else
108  Board_LED_Toggle(0);
109 
110  /* The chip will reset before this happens, but if the WDT doesn't
111  have WWDT_WDMOD_WDRESET enabled, this will hit once */
112  if (wdtStatus & WWDT_WDMOD_WDTOF) {
113  /* A watchdog feed didn't occur prior to window timeout */
114  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF);
115  Chip_WWDT_Start(LPC_WWDT); /* Needs restart */
116  }
117 
118  /* Handle warning interrupt */
119  if (wdtStatus & WWDT_WDMOD_WDINT) {
120  /* A watchdog feed didn't occur prior to warning timeout */
121  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDINT);
123  }
124 #endif
125 }
126 
132 void SysTick_Handler(void)
133 {
134 #if !defined(DISABLE_WDT_TIMEOUT)
136 #endif
137 }
138 
143 int main(void)
144 {
145  uint32_t wdtFreq;
146 
147  /* Board Setup */
148  Board_Init();
149 
150  /* Initialize WWDT (also enables WWDT clock) */
152 
153  /* Prior to initializing the watchdog driver, the clocking for the
154  watchdog must be enabled. This example uses the watchdog oscillator
155  set at a 50KHz (1Mhz / 20) clock rate. */
158 
159  /* The WDT divides the input frequency into it by 4 */
160  wdtFreq = Chip_Clock_GetWDTOSCRate() / 4;
161 
162  /* LPC1102/4, LPC11XXLV, and LPC11CXX devices select the watchdog
163  clock source from the SYSCLK block, while LPC11AXX, LPC11EXX, and
164  LPC11UXX devices select the clock as part of the watchdog block. */
165  /* Select watchdog oscillator for WDT clock source */
166 #if defined(CHIP_LPC110X) || defined(CHIP_LPC11XXLV) || defined(CHIP_LPC11CXX) || defined(CHIP_LPC11EXX)
167  Chip_Clock_SetWDTClockSource(SYSCTL_WDTCLKSRC_WDTOSC, 1);
168 #else
169  Chip_WWDT_SelClockSource(LPC_WWDT, WWDT_CLKSRC_WATCHDOG_WDOSC);
170 #endif
171 
172  Board_LED_Set(0, false);
173 
174  /* Set watchdog feed time constant to approximately 2s
175  Set watchdog warning time to 512 ticks after feed time constant
176  Set watchdog window time to 3s */
177  Chip_WWDT_SetTimeOut(LPC_WWDT, wdtFreq * 2);
178 #if !defined(CHIP_LPC11CXX)
180  Chip_WWDT_SetWindow(LPC_WWDT, wdtFreq * 3);
181 #endif
182 
183 #if !defined(CHIP_LPC11CXX)
184  /* Configure WWDT to reset on timeout */
186 #endif
187 
188  /* Setup Systick to feed the watchdog timer. This needs to be done
189  * at a rate faster than the WDT warning. */
190  SysTick_Config(Chip_Clock_GetSystemClockRate() / 50);
191 
192  /* Clear watchdog warning and timeout interrupts */
193 #if !defined(CHIP_LPC11CXX)
195 #else
197 #endif
198 
199  /* Clear and enable watchdog interrupt */
200  NVIC_ClearPendingIRQ(WDT_IRQn);
201  NVIC_EnableIRQ(WDT_IRQn);
202 
203  /* Start watchdog */
205 
206  /* Idle while waiting */
207  while (1) {
208  __WFI();
209  }
210 }
211