1 /*
2  *  Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *
8  *    Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *
11  *    Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the
14  *    distribution.
15  *
16  *    Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 //*****************************************************************************
34 //
35 //  wdt.c
36 //
37 //  Driver for the Watchdog Timer Module.
38 //
39 //*****************************************************************************
40 
41 //*****************************************************************************
42 //
43 //! \addtogroup WDT_Watchdog_Timer_api
44 //! @{
45 //
46 //*****************************************************************************
47 
48 #include "inc/hw_ints.h"
49 #include "inc/hw_memmap.h"
50 #include "inc/hw_types.h"
51 #include "inc/hw_wdt.h"
52 #include "debug.h"
53 #include "interrupt.h"
54 #include "wdt.h"
55 
56 //*****************************************************************************
57 //
58 //! Determines if the watchdog timer is enabled.
59 //!
60 //! \param ulBase is the base address of the watchdog timer module.
61 //!
62 //! This will check to see if the watchdog timer is enabled.
63 //!
64 //! \return Returns \b true if the watchdog timer is enabled, and \b false
65 //! if it is not.
66 //
67 //*****************************************************************************
68 tBoolean
WatchdogRunning(unsigned long ulBase)69 WatchdogRunning(unsigned long ulBase)
70 {
71     //
72     // Check the arguments.
73     //
74     ASSERT((ulBase == WDT_BASE));
75 
76     //
77     // See if the watchdog timer module is enabled, and return.
78     //
79     return(HWREG(ulBase + WDT_O_CTL) & WDT_CTL_INTEN);
80 }
81 
82 //*****************************************************************************
83 //
84 //! Enables the watchdog timer.
85 //!
86 //! \param ulBase is the base address of the watchdog timer module.
87 //!
88 //! This will enable the watchdog timer counter and interrupt.
89 //!
90 //! \note This function will have no effect if the watchdog timer has
91 //! been locked.
92 //!
93 //! \sa WatchdogLock(), WatchdogUnlock()
94 //!
95 //! \return None.
96 //
97 //*****************************************************************************
98 void
WatchdogEnable(unsigned long ulBase)99 WatchdogEnable(unsigned long ulBase)
100 {
101     //
102     // Check the arguments.
103     //
104     ASSERT((ulBase == WDT_BASE));
105 
106     //
107     // Enable the watchdog timer module.
108     //
109     HWREG(ulBase + WDT_O_CTL) |= WDT_CTL_INTEN;
110 }
111 
112 //*****************************************************************************
113 //
114 //! Enables the watchdog timer lock mechanism.
115 //!
116 //! \param ulBase is the base address of the watchdog timer module.
117 //!
118 //! Locks out write access to the watchdog timer configuration registers.
119 //!
120 //! \return None.
121 //
122 //*****************************************************************************
123 void
WatchdogLock(unsigned long ulBase)124 WatchdogLock(unsigned long ulBase)
125 {
126     //
127     // Check the arguments.
128     //
129     ASSERT((ulBase == WDT_BASE));
130 
131     //
132     // Lock out watchdog register writes.  Writing anything to the WDT_O_LOCK
133     // register causes the lock to go into effect.
134     //
135     HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_LOCKED;
136 }
137 
138 //*****************************************************************************
139 //
140 //! Disables the watchdog timer lock mechanism.
141 //!
142 //! \param ulBase is the base address of the watchdog timer module.
143 //!
144 //! Enables write access to the watchdog timer configuration registers.
145 //!
146 //! \return None.
147 //
148 //*****************************************************************************
149 void
WatchdogUnlock(unsigned long ulBase)150 WatchdogUnlock(unsigned long ulBase)
151 {
152     //
153     // Check the arguments.
154     //
155     ASSERT((ulBase == WDT_BASE));
156 
157     //
158     // Unlock watchdog register writes.
159     //
160     HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_UNLOCK;
161 }
162 
163 //*****************************************************************************
164 //
165 //! Gets the state of the watchdog timer lock mechanism.
166 //!
167 //! \param ulBase is the base address of the watchdog timer module.
168 //!
169 //! Returns the lock state of the watchdog timer registers.
170 //!
171 //! \return Returns \b true if the watchdog timer registers are locked, and
172 //! \b false if they are not locked.
173 //
174 //*****************************************************************************
175 tBoolean
WatchdogLockState(unsigned long ulBase)176 WatchdogLockState(unsigned long ulBase)
177 {
178     //
179     // Check the arguments.
180     //
181     ASSERT((ulBase == WDT_BASE));
182 
183     //
184     // Get the lock state.
185     //
186     return((HWREG(ulBase + WDT_O_LOCK) == WDT_LOCK_LOCKED) ? true : false);
187 }
188 
189 //*****************************************************************************
190 //
191 //! Sets the watchdog timer reload value.
192 //!
193 //! \param ulBase is the base address of the watchdog timer module.
194 //! \param ulLoadVal is the load value for the watchdog timer.
195 //!
196 //! This function sets the value to load into the watchdog timer when the count
197 //! reaches zero for the first time; if the watchdog timer is running when this
198 //! function is called, then the value will be immediately loaded into the
199 //! watchdog timer counter.  If the \e ulLoadVal parameter is 0, then an
200 //! interrupt is immediately generated.
201 //!
202 //! \note This function will have no effect if the watchdog timer has
203 //! been locked.
204 //!
205 //! \sa WatchdogLock(), WatchdogUnlock(), WatchdogReloadGet()
206 //!
207 //! \return None.
208 //
209 //*****************************************************************************
210 void
WatchdogReloadSet(unsigned long ulBase,unsigned long ulLoadVal)211 WatchdogReloadSet(unsigned long ulBase, unsigned long ulLoadVal)
212 {
213     //
214     // Check the arguments.
215     //
216     ASSERT((ulBase == WDT_BASE));
217 
218     //
219     // Set the load register.
220     //
221     HWREG(ulBase + WDT_O_LOAD) = ulLoadVal;
222 }
223 
224 //*****************************************************************************
225 //
226 //! Gets the watchdog timer reload value.
227 //!
228 //! \param ulBase is the base address of the watchdog timer module.
229 //!
230 //! This function gets the value that is loaded into the watchdog timer when
231 //! the count reaches zero for the first time.
232 //!
233 //! \sa WatchdogReloadSet()
234 //!
235 //! \return None.
236 //
237 //*****************************************************************************
238 unsigned long
WatchdogReloadGet(unsigned long ulBase)239 WatchdogReloadGet(unsigned long ulBase)
240 {
241     //
242     // Check the arguments.
243     //
244     ASSERT((ulBase == WDT_BASE));
245 
246     //
247     // Get the load register.
248     //
249     return(HWREG(ulBase + WDT_O_LOAD));
250 }
251 
252 //*****************************************************************************
253 //
254 //! Gets the current watchdog timer value.
255 //!
256 //! \param ulBase is the base address of the watchdog timer module.
257 //!
258 //! This function reads the current value of the watchdog timer.
259 //!
260 //! \return Returns the current value of the watchdog timer.
261 //
262 //*****************************************************************************
263 unsigned long
WatchdogValueGet(unsigned long ulBase)264 WatchdogValueGet(unsigned long ulBase)
265 {
266     //
267     // Check the arguments.
268     //
269     ASSERT((ulBase == WDT_BASE));
270 
271     //
272     // Get the current watchdog timer register value.
273     //
274     return(HWREG(ulBase + WDT_O_VALUE));
275 }
276 
277 //*****************************************************************************
278 //
279 //! Registers an interrupt handler for watchdog timer interrupt.
280 //!
281 //! \param ulBase is the base address of the watchdog timer module.
282 //! \param pfnHandler is a pointer to the function to be called when the
283 //! watchdog timer interrupt occurs.
284 //!
285 //! This function does the actual registering of the interrupt handler.  This
286 //! will enable the global interrupt in the interrupt controller; the watchdog
287 //! timer interrupt must be enabled via WatchdogEnable().  It is the interrupt
288 //! handler's responsibility to clear the interrupt source via
289 //! WatchdogIntClear().
290 //!
291 //! \sa IntRegister() for important information about registering interrupt
292 //! handlers.
293 //!
294 //! \note This function will only register the standard watchdog interrupt
295 //! handler.  To register the NMI watchdog handler, use IntRegister()
296 //! to register the handler for the \b FAULT_NMI interrupt.
297 //!
298 //! \return None.
299 //
300 //*****************************************************************************
301 void
WatchdogIntRegister(unsigned long ulBase,void (* pfnHandler)(void))302 WatchdogIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
303 {
304     //
305     // Check the arguments.
306     //
307     ASSERT((ulBase == WDT_BASE));
308 
309     //
310     // Register the interrupt handler and
311     // Enable the watchdog timer interrupt.
312     //
313     IntRegister(INT_WDT, pfnHandler);
314     IntEnable(INT_WDT);
315 }
316 
317 //*****************************************************************************
318 //
319 //! Unregisters an interrupt handler for the watchdog timer interrupt.
320 //!
321 //! \param ulBase is the base address of the watchdog timer module.
322 //!
323 //! This function does the actual unregistering of the interrupt handler.  This
324 //! function will clear the handler to be called when a watchdog timer
325 //! interrupt occurs.  This will also mask off the interrupt in the interrupt
326 //! controller so that the interrupt handler no longer is called.
327 //!
328 //! \sa IntRegister() for important information about registering interrupt
329 //! handlers.
330 //!
331 //! \note This function will only unregister the standard watchdog interrupt
332 //! handler.  To unregister the NMI watchdog handler, use IntUnregister()
333 //! to unregister the handler for the \b FAULT_NMI interrupt.
334 //!
335 //! \return None.
336 //
337 //*****************************************************************************
338 void
WatchdogIntUnregister(unsigned long ulBase)339 WatchdogIntUnregister(unsigned long ulBase)
340 {
341     //
342     // Check the arguments.
343     //
344     ASSERT((ulBase == WDT_BASE));
345 
346     //
347     // Disable the interrupt
348     IntDisable(INT_WDT);
349 
350     //
351     // Unregister the interrupt handler.
352     //
353     IntUnregister(INT_WDT);
354 }
355 
356 //*****************************************************************************
357 //
358 //! Gets the current watchdog timer interrupt status.
359 //!
360 //! \param ulBase is the base address of the watchdog timer module.
361 //! \param bMasked is \b false if the raw interrupt status is required and
362 //! \b true if the masked interrupt status is required.
363 //!
364 //! This returns the interrupt status for the watchdog timer module.  Either
365 //! the raw interrupt status or the status of interrupt that is allowed to
366 //! reflect to the processor can be returned.
367 //!
368 //! \return Returns the current interrupt status, where a 1 indicates that the
369 //! watchdog interrupt is active, and a 0 indicates that it is not active.
370 //
371 //*****************************************************************************
372 unsigned long
WatchdogIntStatus(unsigned long ulBase,tBoolean bMasked)373 WatchdogIntStatus(unsigned long ulBase, tBoolean bMasked)
374 {
375     //
376     // Check the arguments.
377     //
378     ASSERT((ulBase == WDT_BASE));
379 
380     //
381     // Return either the interrupt status or the raw interrupt status as
382     // requested.
383     //
384     if(bMasked)
385     {
386         return(HWREG(ulBase + WDT_O_MIS));
387     }
388     else
389     {
390         return(HWREG(ulBase + WDT_O_RIS));
391     }
392 }
393 
394 //*****************************************************************************
395 //
396 //! Clears the watchdog timer interrupt.
397 //!
398 //! \param ulBase is the base address of the watchdog timer module.
399 //!
400 //! The watchdog timer interrupt source is cleared, so that it no longer
401 //! asserts.
402 //!
403 //! \note Because there is a write buffer in the Cortex-M3 processor, it may
404 //! take several clock cycles before the interrupt source is actually cleared.
405 //! Therefore, it is recommended that the interrupt source be cleared early in
406 //! the interrupt handler (as opposed to the very last action) to avoid
407 //! returning from the interrupt handler before the interrupt source is
408 //! actually cleared.  Failure to do so may result in the interrupt handler
409 //! being immediately reentered (because the interrupt controller still sees
410 //! the interrupt source asserted).
411 //!
412 //! \return None.
413 //
414 //*****************************************************************************
415 void
WatchdogIntClear(unsigned long ulBase)416 WatchdogIntClear(unsigned long ulBase)
417 {
418     //
419     // Check the arguments.
420     //
421     ASSERT((ulBase == WDT_BASE));
422 
423     //
424     // Clear the interrupt source.
425     //
426     HWREG(ulBase + WDT_O_ICR) = WDT_INT_TIMEOUT;
427 }
428 
429 //*****************************************************************************
430 //
431 //! Enables stalling of the watchdog timer during debug events.
432 //!
433 //! \param ulBase is the base address of the watchdog timer module.
434 //!
435 //! This function allows the watchdog timer to stop counting when the processor
436 //! is stopped by the debugger.  By doing so, the watchdog is prevented from
437 //! expiring (typically almost immediately from a human time perspective) and
438 //! resetting the system (if reset is enabled).  The watchdog will instead
439 //! expired after the appropriate number of processor cycles have been executed
440 //! while debugging (or at the appropriate time after the processor has been
441 //! restarted).
442 //!
443 //! \return None.
444 //
445 //*****************************************************************************
446 void
WatchdogStallEnable(unsigned long ulBase)447 WatchdogStallEnable(unsigned long ulBase)
448 {
449     //
450     // Check the arguments.
451     //
452     ASSERT((ulBase == WDT_BASE));
453 
454     //
455     // Enable timer stalling.
456     //
457     HWREG(ulBase + WDT_O_TEST) |= WDT_TEST_STALL;
458 }
459 
460 //*****************************************************************************
461 //
462 //! Disables stalling of the watchdog timer during debug events.
463 //!
464 //! \param ulBase is the base address of the watchdog timer module.
465 //!
466 //! This function disables the debug mode stall of the watchdog timer.  By
467 //! doing so, the watchdog timer continues to count regardless of the processor
468 //! debug state.
469 //!
470 //! \return None.
471 //
472 //*****************************************************************************
473 void
WatchdogStallDisable(unsigned long ulBase)474 WatchdogStallDisable(unsigned long ulBase)
475 {
476     //
477     // Check the arguments.
478     //
479     ASSERT((ulBase == WDT_BASE));
480 
481     //
482     // Disable timer stalling.
483     //
484     HWREG(ulBase + WDT_O_TEST) &= ~(WDT_TEST_STALL);
485 }
486 
487 //*****************************************************************************
488 //
489 // Close the Doxygen group.
490 //! @}
491 //
492 //*****************************************************************************
493