LPCOpen Platform
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 
65 /*****************************************************************************
66  * Private types/enumerations/variables
67  ****************************************************************************/
68 
69 static volatile int wdtFeedState;
70 static volatile bool On = false, On1 = false;
71 
72 /*****************************************************************************
73  * Public types/enumerations/variables
74  ****************************************************************************/
75 
76 /*****************************************************************************
77  * Private functions
78  ****************************************************************************/
79 
80 /* WDT interrupt handler */
81 static wdt_handle(void) {
82  uint32_t wdtStatus = Chip_WWDT_GetStatus();
83 
84  On = (bool) !On;
85 
86  /* The chip will reset before this happens, but if the WDT doesn't
87  have WWDT_WDMOD_WDRESET enabled, this will hit once */
88  if (wdtStatus & WWDT_WDMOD_WDTOF) {
89  /* A watchdog feed didn't occur prior to window timeout */
90  Chip_WWDT_UnsetOption(WWDT_WDMOD_WDEN); /* Stop WDT */
91  Chip_WWDT_ClearStatusFlag(WWDT_WDMOD_WDTOF);
92 
93  if (wdtFeedState == 2) {
94  Chip_WWDT_Start(); /* Needs restart */
95  }
96  }
97 
98  /* Handle warning interrupt */
99  if (wdtStatus & WWDT_WDMOD_WDINT) {
100  /* A watchdog feed didn't occur prior to warning timeout */
101  Chip_WWDT_ClearStatusFlag(WWDT_WDMOD_WDINT);
102 
103  if (wdtFeedState == 1) {
104  Chip_WWDT_Feed();
105  }
106  }
107 }
108 
109 /*****************************************************************************
110  * Public functions
111  ****************************************************************************/
112 
118 void EVRT_IRQHandler(void)
119 {
120  On1 = !On1;
121 
122  /* Clear watchdog timer interrupt in event router (ok to clear before
123  watchdog clear on edge based event router events) */
125 
126  wdt_handle();
127 }
128 
134 void WDT_IRQHandler(void)
135 {
136  wdt_handle();
137 }
138 
144 void SysTick_Handler(void)
145 {
146  if (wdtFeedState == 0) {
147  On = (bool) !On;
148  Chip_WWDT_Feed();
149  }
150 }
151 
156 int main(void)
157 {
158  uint8_t ch;
159 
160  Board_Init();
161  Board_LED_Init();
162 
163  /* Watchdog will be fed on each watchdog interrupt */
164  wdtFeedState = 0;
165 
166  /* Initialize WWDT and event router */
167  Chip_WWDT_Init();
168  Chip_EVRT_Init();
169 
170  /* Set watchdog feed time constant to 0.1s
171  Set watchdog warning time to 512 ticks after feed time constant
172  Set watchdog window time to 0.9s */
176 
177  /* Configure WWDT to reset on timeout */
179 
180  /* Clear watchdog warning and timeout interrupts */
182 
183  /* Initiate EVRT, route interrupt signal from WWDT to EVRT,
184  enable EVRT WWDT interrupt */
187 
188  /* Start watchdog */
189  Chip_WWDT_Start();
190 
191  /* Setup Systick for a 10Hz tick rate. Systick clock is clocked at
192  CPU core clock speed */
193  SysTick_Config(Chip_Clock_GetRate(CLK_MX_MXCORE) / 20);
194 
195  /* Enable watchdog interrupt */
196  NVIC_EnableIRQ(WWDT_IRQn);
197 
198  /* Watchdog test options */
199  DEBUGOUT("Press '1' to enable watchdog feed on systick interrupt\n\r");
200  DEBUGOUT("Press '2' to enable watchdog feed on warning interrupt\n\r");
201  DEBUGOUT("Press '3' to disable watchdog feed (will reset device)\n\r");
202  DEBUGOUT("Press '4' to switch from WDT interrupt to event router handler\n\r");
203 
204  while (1) {
205  do {
206  ch = DEBUGIN();
207  Board_LED_Set(0, On);
208  Board_LED_Set(1, On1);
209  } while ((ch < '1') || (ch > '4'));
210 
211  switch (ch) {
212  case '1':
213  default:
214  wdtFeedState = 0;
215  DEBUGOUT("Watchdog feed on systick interrupt\r\n");
216  break;
217 
218  case '2':
219  wdtFeedState = 1;
220  DEBUGOUT("Watchdog feed on warning interrupt\r\n");
221  break;
222 
223  case '3':
224  wdtFeedState = 2;
225  DEBUGOUT("No feeding - board will reset\r\n");
226  break;
227 
228  case '4':
229  DEBUGOUT("using event router instead of WDT interrupt\r\n");
230  NVIC_DisableIRQ(WWDT_IRQn);
231  NVIC_EnableIRQ(EVENTROUTER_IRQn);
232  break;
233  }
234  }
235 }
236