1 /*
2  * @brief Windowed Watchdog Timer (WWDT) example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2013
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 
34 /*****************************************************************************
35  * Private types/enumerations/variables
36  ****************************************************************************/
37 
38 /*****************************************************************************
39  * Public types/enumerations/variables
40  ****************************************************************************/
41 
42 /*****************************************************************************
43  * Private functions
44  ****************************************************************************/
45 
46 /*****************************************************************************
47  * Public functions
48  ****************************************************************************/
49 /**
50  * @brief	watchdog timer Interrupt Handler
51  * @return	Nothing
52  * @note	Handles watchdog timer warning and timeout events
53  */
WDT_IRQHandler(void)54 void WDT_IRQHandler(void)
55 {
56 	uint32_t wdtStatus = Chip_WWDT_GetStatus(LPC_WWDT);
57 
58 	Board_LED_Toggle(0);
59 
60 	/* The chip will reset before this happens, but if the WDT doesn't
61 	   have WWDT_WDMOD_WDRESET enabled, this will hit once */
62 	if (wdtStatus & WWDT_WDMOD_WDTOF) {
63 		/* A watchdog feed didn't occur prior to window timeout */
64 		Chip_WWDT_UnsetOption(LPC_WWDT, WWDT_WDMOD_WDEN);	/* Stop WDT */
65 		Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF);
66 		Chip_WWDT_Start(LPC_WWDT);	/* Needs restart */
67 	}
68 
69 	/* Handle warning interrupt */
70 	if (wdtStatus & WWDT_WDMOD_WDINT) {
71 		/* A watchdog feed didn't occur prior to warning timeout */
72 		Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDINT);
73 		Chip_WWDT_Feed(LPC_WWDT);
74 	}
75 }
76 
77 /**
78  * @brief	Application Main entry point
79  * @return	Nothing (This will not return)
80  */
main(void)81 int main(void)
82 {
83 	uint32_t wdtFreq;
84 
85 	SystemCoreClockUpdate();
86 	Board_Init();
87 	Board_LED_Set(0, false);
88 
89 	/* Enable the WDT oscillator */
90 	Chip_SYSCTL_PowerUp(SYSCTL_POWERDOWN_WDTOSC_PD);
91 
92 	/* The WDT divides the input frequency into it by 4 */
93 	wdtFreq = Chip_Clock_GetWDTOSCRate() / 4;
94 
95 	/* Initialize WWDT (also enables WWDT clock) */
96 	Chip_WWDT_Init(LPC_WWDT);
97 
98 	/* Set watchdog feed time constant to approximately 2s
99 	   Set watchdog warning time to 512 ticks after feed time constant
100 	   Set watchdog window time to 3s */
101 	Chip_WWDT_SetTimeOut(LPC_WWDT, wdtFreq * 2);
102 	Chip_WWDT_SetWarning(LPC_WWDT, 512);
103 	Chip_WWDT_SetWindow(LPC_WWDT, wdtFreq * 3);
104 
105 	/* Configure WWDT to reset on timeout */
106 	Chip_WWDT_SetOption(LPC_WWDT, WWDT_WDMOD_WDRESET);
107 
108 	/* Clear watchdog warning and timeout interrupts */
109 	Chip_WWDT_ClearStatusFlag(LPC_WWDT, WWDT_WDMOD_WDTOF | WWDT_WDMOD_WDINT);
110 
111 	/* Power everything up except for ADC, USB and temp sensor on wake up from
112 	   deep sleep. */
113 	Chip_SYSCTL_SetWakeup(~(SYSCTL_SLPWAKE_IRCOUT_PD | SYSCTL_SLPWAKE_IRC_PD |
114 							SYSCTL_SLPWAKE_FLASH_PD | SYSCTL_SLPWAKE_SYSOSC_PD |
115 							SYSCTL_SLPWAKE_SYSPLL_PD | SYSCTL_SLPWAKE_WDTOSC_PD));
116 	/* Allow WDT to wake from deep sleep. */
117 	Chip_SYSCTL_EnableERP0PeriphWakeup(SYSCTL_ERP0_WAKEUP_WDTINT);
118 
119 	/* Clear and enable watchdog interrupt */
120 	NVIC_ClearPendingIRQ(WWDT_IRQn);
121 	NVIC_EnableIRQ(WWDT_IRQn);
122 
123 	/* Start watchdog */
124 	Chip_WWDT_Start(LPC_WWDT);
125 
126 	/* Sleep until WDT needs servicing */
127 	while (1) {
128 		LPC_PWRD_API->power_mode_configure(PMU_DEEP_SLEEP, ~PMU_PD_WDOSC);
129 		__WFI();
130 	}
131 
132 	return 0;
133 }
134