1 /*
2  * @brief LPC15xx WWDT chip driver
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 #ifndef __WWDT_15XX_H_
33 #define __WWDT_15XX_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /** @defgroup WWDT_15XX CHIP: LPC15xx Windowed Watchdog driver
40  * @ingroup CHIP_15XX_Drivers
41  * @{
42  */
43 
44 /**
45  * @brief Windowed Watchdog register block structure
46  */
47 typedef struct {				/*!< WWDT Structure         */
48 	__IO uint32_t  MOD;			/*!< Watchdog mode register. This register contains the basic mode and status of the Watchdog Timer. */
49 	__IO uint32_t  TC;			/*!< Watchdog timer constant register. This register determines the time-out value. */
50 	__O  uint32_t  FEED;		/*!< Watchdog feed sequence register. Writing 0xAA followed by 0x55 to this register reloads the Watchdog timer with the value contained in WDTC. */
51 	__I  uint32_t  TV;			/*!< Watchdog timer value register. This register reads out the current value of the Watchdog timer. */
52 	__IO uint32_t  RESERVED;
53 	__IO uint32_t  WARNINT;		/*!< Watchdog warning interrupt register. This register contains the Watchdog warning interrupt compare value. */
54 	__IO uint32_t  WINDOW;		/*!< Watchdog timer window register. This register contains the Watchdog window value. */
55 } LPC_WWDT_T;
56 
57 /**
58  * @brief Watchdog Mode register definitions
59  */
60 /** Watchdog Mode Bitmask */
61 #define WWDT_WDMOD_BITMASK          ((uint32_t) 0x1F)
62 /** WWDT interrupt enable bit */
63 #define WWDT_WDMOD_WDEN             ((uint32_t) (1 << 0))
64 /** WWDT interrupt enable bit */
65 #define WWDT_WDMOD_WDRESET          ((uint32_t) (1 << 1))
66 /** WWDT time out flag bit */
67 #define WWDT_WDMOD_WDTOF            ((uint32_t) (1 << 2))
68 /** WDT Time Out flag bit */
69 #define WWDT_WDMOD_WDINT            ((uint32_t) (1 << 3))
70 /** WWDT Protect flag bit */
71 #define WWDT_WDMOD_WDPROTECT        ((uint32_t) (1 << 4))
72 /** WWDT lock bit */
73 #define WWDT_WDMOD_LOCK             ((uint32_t) (1 << 5))
74 
75 /**
76  * @brief	Initialize the Watchdog timer
77  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
78  * @return	None
79  */
80 void Chip_WWDT_Init(LPC_WWDT_T *pWWDT);
81 
82 /**
83  * @brief	Shutdown the Watchdog timer
84  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
85  * @return	None
86  */
Chip_WWDT_DeInit(LPC_WWDT_T * pWWDT)87 STATIC INLINE void Chip_WWDT_DeInit(LPC_WWDT_T *pWWDT)
88 {
89 	Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_WDT);
90 }
91 
92 /**
93  * @brief	Set WDT timeout constant value used for feed
94  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
95  * @param	timeout	: WDT timeout in ticks, between WWDT_TICKS_MIN and WWDT_TICKS_MAX
96  * @return	none
97  */
Chip_WWDT_SetTimeOut(LPC_WWDT_T * pWWDT,uint32_t timeout)98 STATIC INLINE void Chip_WWDT_SetTimeOut(LPC_WWDT_T *pWWDT, uint32_t timeout)
99 {
100 	pWWDT->TC = timeout;
101 }
102 
103 /**
104  * @brief	Feed watchdog timer
105  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
106  * @return	None
107  * @note	If this function isn't called, a watchdog timer warning will occur.
108  * After the warning, a timeout will occur if a feed has happened.
109  */
Chip_WWDT_Feed(LPC_WWDT_T * pWWDT)110 STATIC INLINE void Chip_WWDT_Feed(LPC_WWDT_T *pWWDT)
111 {
112 	pWWDT->FEED = 0xAA;
113 	pWWDT->FEED = 0x55;
114 }
115 
116 /**
117  * @brief	Set WWDT warning interrupt
118  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
119  * @param	timeout	: WDT warning in ticks, between 0 and 1023
120  * @return	None
121  * @note	This is the number of ticks after the watchdog interrupt that the
122  * warning interrupt will be generated.
123  */
Chip_WWDT_SetWarning(LPC_WWDT_T * pWWDT,uint32_t timeout)124 STATIC INLINE void Chip_WWDT_SetWarning(LPC_WWDT_T *pWWDT, uint32_t timeout)
125 {
126 	pWWDT->WARNINT = timeout;
127 }
128 
129 /**
130  * @brief	Set WWDT window time
131  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
132  * @param	timeout	: WDT timeout in ticks, between WWDT_TICKS_MIN and WWDT_TICKS_MAX
133  * @return	None
134  * @note	The watchdog timer must be fed between the timeout from the Chip_WWDT_SetTimeOut()
135  * function and this function, with this function defining the last tick before the
136  * watchdog window interrupt occurs.
137  */
Chip_WWDT_SetWindow(LPC_WWDT_T * pWWDT,uint32_t timeout)138 STATIC INLINE void Chip_WWDT_SetWindow(LPC_WWDT_T *pWWDT, uint32_t timeout)
139 {
140 	pWWDT->WINDOW = timeout;
141 }
142 
143 /**
144  * @brief	Enable watchdog timer options
145  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
146  * @param	options	: An or'ed set of options of values
147  *						WWDT_WDMOD_WDEN, WWDT_WDMOD_WDRESET, and WWDT_WDMOD_WDPROTECT
148  * @return	None
149  * @note	You can enable more than one option at once (ie, WWDT_WDMOD_WDRESET |
150  * WWDT_WDMOD_WDPROTECT), but use the WWDT_WDMOD_WDEN after all other options
151  * are set (or unset) with no other options. If WWDT_WDMOD_LOCK is used, it cannot
152  * be unset.
153  */
Chip_WWDT_SetOption(LPC_WWDT_T * pWWDT,uint32_t options)154 STATIC INLINE void Chip_WWDT_SetOption(LPC_WWDT_T *pWWDT, uint32_t options)
155 {
156 	pWWDT->MOD |= options;
157 }
158 
159 /**
160  * @brief	Disable/clear watchdog timer options
161  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
162  * @param	options	: An or'ed set of options of values
163  *						WWDT_WDMOD_WDEN, WWDT_WDMOD_WDRESET, and WWDT_WDMOD_WDPROTECT
164  * @return	None
165  * @note	You can disable more than one option at once (ie, WWDT_WDMOD_WDRESET |
166  * WWDT_WDMOD_WDTOF).
167  */
Chip_WWDT_UnsetOption(LPC_WWDT_T * pWWDT,uint32_t options)168 STATIC INLINE void Chip_WWDT_UnsetOption(LPC_WWDT_T *pWWDT, uint32_t options)
169 {
170 	pWWDT->MOD &= (~options) & WWDT_WDMOD_BITMASK;
171 }
172 
173 /**
174  * @brief	Enable WWDT activity
175  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
176  * @return	None
177  */
Chip_WWDT_Start(LPC_WWDT_T * pWWDT)178 STATIC INLINE void Chip_WWDT_Start(LPC_WWDT_T *pWWDT)
179 {
180 	Chip_WWDT_SetOption(pWWDT, WWDT_WDMOD_WDEN);
181 	Chip_WWDT_Feed(pWWDT);
182 }
183 
184 /**
185  * @brief	Read WWDT status flag
186  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
187  * @return	Watchdog status, an Or'ed value of WWDT_WDMOD_*
188  */
Chip_WWDT_GetStatus(LPC_WWDT_T * pWWDT)189 STATIC INLINE uint32_t Chip_WWDT_GetStatus(LPC_WWDT_T *pWWDT)
190 {
191 	return pWWDT->MOD;
192 }
193 
194 /**
195  * @brief	Clear WWDT interrupt status flags
196  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
197  * @param	status	: Or'ed value of status flag(s) that you want to clear, should be:
198  *              - WWDT_WDMOD_WDTOF: Clear watchdog timeout flag
199  *              - WWDT_WDMOD_WDINT: Clear watchdog warning flag
200  * @return	None
201  */
202 void Chip_WWDT_ClearStatusFlag(LPC_WWDT_T *pWWDT, uint32_t status);
203 
204 /**
205  * @brief	Get the current value of WDT
206  * @param	pWWDT	: The base of WatchDog Timer peripheral on the chip
207  * @return	current value of WDT
208  */
Chip_WWDT_GetCurrentCount(LPC_WWDT_T * pWWDT)209 STATIC INLINE uint32_t Chip_WWDT_GetCurrentCount(LPC_WWDT_T *pWWDT)
210 {
211 	return pWWDT->TV;
212 }
213 
214 /**
215  * @}
216  */
217 
218 #ifdef __cplusplus
219 }
220 #endif
221 
222 #endif /* __WWDT_15XX_H_ */
223