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 #define FEED_ON_SYSTICKINT 0
74 #define FEED_ON_WARNINT 1
75 #define NO_FEED 2
76 
77 static volatile int wdtFeedState;
78 static volatile bool On = false;
79 
80 /*****************************************************************************
81  * Public types/enumerations/variables
82  ****************************************************************************/
83 
84 /*****************************************************************************
85  * Private functions
86  ****************************************************************************/
87 
88 /* WDT interrupt handler */
89 static void wdt_handle(void) {
91 
92  On = (bool) !On;
93 
94  /* The chip will reset before this happens, but if the WDT doesn't
95  have WWDT_MOD_WDRESET enabled, this will hit once */
96  if (wdtStatus & WWDT_WDMOD_WDTOF) {
97  /* A watchdog feed didn't occur prior to window timeout */
98  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF);
99 
100  if (wdtFeedState == NO_FEED) {
101  Chip_WWDT_Start(LPC_WWDT); /* Needs restart */
102  }
103  }
104 #if !defined(CHIP_LPC175X_6X)
105  /* Handle warning interrupt */
106  if (wdtStatus & WWDT_WDMOD_WDINT) {
107  /* A watchdog feed didn't occur prior to warning timeout */
108  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDINT);
109 
110  if (wdtFeedState == FEED_ON_WARNINT) {
112  }
113  }
114 #endif
115 }
116 
117 /*****************************************************************************
118  * Public functions
119  ****************************************************************************/
120 
126 void WDT_IRQHandler(void)
127 {
128  wdt_handle();
129 }
130 
136 void SysTick_Handler(void)
137 {
139  On = (bool) !On;
141  }
142 }
143 
148 int main(void)
149 {
150  uint8_t ch;
151  uint32_t wdtFreq;
152 
153  Board_Init();
154 
155  /* Watchdog will be fed on each watchdog interrupt */
157 
158  /* Initialize WWDT and event router */
160 
161 #if defined(WATCHDOG_CLKSEL_SUPPORT)
162  Chip_WWDT_SelClockSource(LPC_WWDT, WWDT_CLKSRC_WATCHDOG_PCLK);
163  wdtFreq = Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_WDT) / 4;
164 #else
165  wdtFreq = WDT_OSC;
166 #endif
167 
168  /* Set watchdog feed time constant to 0.1s */
169  Chip_WWDT_SetTimeOut(LPC_WWDT, wdtFreq / 10);
170 #if !defined(CHIP_LPC175X_6X)
171  /* Set watchdog warning time to 512 ticks after feed time constant
172  Set watchdog window time to 0.9s */
174  Chip_WWDT_SetWindow(LPC_WWDT, wdtFreq - (wdtFreq / 10));
175 #endif
177  /* Configure WWDT to reset on timeout */
179 
180  /* Clear watchdog warning and timeout interrupts */
181  Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF | WWDT_WDMOD_WDINT);
182 
183  /* Start watchdog */
185 
186  /* Setup Systick for a 20Hz tick rate. Systick clock is clocked at
187  CPU core clock speed */
188  SysTick_Config(Chip_Clock_GetSystemClockRate() / 20);
189 
190  /* Enable watchdog interrupt */
191  NVIC_ClearPendingIRQ(WDT_IRQn);
192  NVIC_EnableIRQ(WDT_IRQn);
193 
194  /* Watchdog test options */
195  DEBUGOUT("Press '1' to enable watchdog feed on systick interrupt\n\r");
196 #if !defined(CHIP_LPC175X_6X)
197  DEBUGOUT("Press '2' to enable watchdog feed on warning interrupt\n\r");
198 #endif
199  DEBUGOUT("Press '3' to disable watchdog feed (will reset device)\n\r");
200 
201  while (1) {
202  do {
203  ch = DEBUGIN();
204  Board_LED_Set(0, On);
205  } while ((ch < '1') || (ch > '4'));
206 
207  switch (ch) {
208  case '1':
209  default:
211  DEBUGOUT("Watchdog feed on systick interrupt\r\n");
212  break;
213 #if !defined(CHIP_LPC175X_6X)
214  case '2':
216  DEBUGOUT("Watchdog feed on warning interrupt\r\n");
217  break;
218 #endif
219  case '3':
221  DEBUGOUT("No feeding - board will reset\r\n");
222  break;
223  }
224  }
225 }
226