1 //*****************************************************************************
2 //
3 // watchdog.c - Driver for the Watchdog Timer Module.
4 //
5 // Copyright (c) 2005-2020 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.2.0.295 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup watchdog_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_ints.h"
50 #include "inc/hw_memmap.h"
51 #include "inc/hw_types.h"
52 #include "inc/hw_watchdog.h"
53 #include "driverlib/debug.h"
54 #include "driverlib/interrupt.h"
55 #include "driverlib/watchdog.h"
56 
57 //*****************************************************************************
58 //
59 //! Determines if the watchdog timer is enabled.
60 //!
61 //! \param ui32Base is the base address of the watchdog timer module.
62 //!
63 //! This function checks to see if the watchdog timer is enabled.
64 //!
65 //! \return Returns \b true if the watchdog timer is enabled and \b false
66 //! if it is not.
67 //
68 //*****************************************************************************
69 bool
WatchdogRunning(uint32_t ui32Base)70 WatchdogRunning(uint32_t ui32Base)
71 {
72     //
73     // Check the arguments.
74     //
75     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
76 
77     //
78     // See if the watchdog timer module is enabled, and return.
79     //
80     return(HWREG(ui32Base + WDT_O_CTL) & WDT_CTL_INTEN);
81 }
82 
83 //*****************************************************************************
84 //
85 //! Enables the watchdog timer.
86 //!
87 //! \param ui32Base is the base address of the watchdog timer module.
88 //!
89 //! This function enables the watchdog timer counter and interrupt.
90 //!
91 //! \note This function has no effect if the watchdog timer has been locked.
92 //!
93 //! \return None.
94 //
95 //*****************************************************************************
96 void
WatchdogEnable(uint32_t ui32Base)97 WatchdogEnable(uint32_t ui32Base)
98 {
99     //
100     // Check the arguments.
101     //
102     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
103 
104     //
105     // Enable the watchdog timer module.
106     //
107     HWREG(ui32Base + WDT_O_CTL) |= WDT_CTL_INTEN;
108 }
109 
110 //*****************************************************************************
111 //
112 //! Enables the watchdog timer reset.
113 //!
114 //! \param ui32Base is the base address of the watchdog timer module.
115 //!
116 //! This function enables the capability of the watchdog timer to issue a reset
117 //! to the processor after a second timeout condition.
118 //!
119 //! \note This function has no effect if the watchdog timer has been locked.
120 //!
121 //! \return None.
122 //
123 //*****************************************************************************
124 void
WatchdogResetEnable(uint32_t ui32Base)125 WatchdogResetEnable(uint32_t ui32Base)
126 {
127     //
128     // Check the arguments.
129     //
130     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
131 
132     //
133     // Enable the watchdog reset.
134     //
135     HWREG(ui32Base + WDT_O_CTL) |= WDT_CTL_RESEN;
136 }
137 
138 //*****************************************************************************
139 //
140 //! Disables the watchdog timer reset.
141 //!
142 //! \param ui32Base is the base address of the watchdog timer module.
143 //!
144 //! This function disables the capability of the watchdog timer to issue a
145 //! reset to the processor after a second timeout condition.
146 //!
147 //! \note This function has no effect if the watchdog timer has been locked.
148 //!
149 //! \return None.
150 //
151 //*****************************************************************************
152 void
WatchdogResetDisable(uint32_t ui32Base)153 WatchdogResetDisable(uint32_t ui32Base)
154 {
155     //
156     // Check the arguments.
157     //
158     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
159 
160     //
161     // Disable the watchdog reset.
162     //
163     HWREG(ui32Base + WDT_O_CTL) &= ~(WDT_CTL_RESEN);
164 }
165 
166 //*****************************************************************************
167 //
168 //! Enables the watchdog timer lock mechanism.
169 //!
170 //! \param ui32Base is the base address of the watchdog timer module.
171 //!
172 //! This function locks out write access to the watchdog timer registers.
173 //!
174 //! \return None.
175 //
176 //*****************************************************************************
177 void
WatchdogLock(uint32_t ui32Base)178 WatchdogLock(uint32_t ui32Base)
179 {
180     //
181     // Check the arguments.
182     //
183     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
184 
185     //
186     // Lock out watchdog register writes.  Writing anything to the WDT_O_LOCK
187     // register causes the lock to go into effect.
188     //
189     HWREG(ui32Base + WDT_O_LOCK) = WDT_LOCK_LOCKED;
190 }
191 
192 //*****************************************************************************
193 //
194 //! Disables the watchdog timer lock mechanism.
195 //!
196 //! \param ui32Base is the base address of the watchdog timer module.
197 //!
198 //! This function enables write access to the watchdog timer registers.
199 //!
200 //! \return None.
201 //
202 //*****************************************************************************
203 void
WatchdogUnlock(uint32_t ui32Base)204 WatchdogUnlock(uint32_t ui32Base)
205 {
206     //
207     // Check the arguments.
208     //
209     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
210 
211     //
212     // Unlock watchdog register writes.
213     //
214     HWREG(ui32Base + WDT_O_LOCK) = WDT_LOCK_UNLOCK;
215 }
216 
217 //*****************************************************************************
218 //
219 //! Gets the state of the watchdog timer lock mechanism.
220 //!
221 //! \param ui32Base is the base address of the watchdog timer module.
222 //!
223 //! This function returns the lock state of the watchdog timer registers.
224 //!
225 //! \return Returns \b true if the watchdog timer registers are locked, and
226 //! \b false if they are not locked.
227 //
228 //*****************************************************************************
229 bool
WatchdogLockState(uint32_t ui32Base)230 WatchdogLockState(uint32_t ui32Base)
231 {
232     //
233     // Check the arguments.
234     //
235     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
236 
237     //
238     // Get the lock state.
239     //
240     return((HWREG(ui32Base + WDT_O_LOCK) == WDT_LOCK_LOCKED) ? true : false);
241 }
242 
243 //*****************************************************************************
244 //
245 //! Sets the watchdog timer reload value.
246 //!
247 //! \param ui32Base is the base address of the watchdog timer module.
248 //! \param ui32LoadVal is the load value for the watchdog timer.
249 //!
250 //! This function configures the value to load into the watchdog timer when the
251 //! count reaches zero for the first time; if the watchdog timer is running
252 //! when this function is called, then the value is immediately loaded into the
253 //! watchdog timer counter.  If the \e ui32LoadVal parameter is 0, then an
254 //! interrupt is immediately generated.
255 //!
256 //! \note This function has no effect if the watchdog timer has been locked.
257 //!
258 //! \return None.
259 //
260 //*****************************************************************************
261 void
WatchdogReloadSet(uint32_t ui32Base,uint32_t ui32LoadVal)262 WatchdogReloadSet(uint32_t ui32Base, uint32_t ui32LoadVal)
263 {
264     //
265     // Check the arguments.
266     //
267     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
268 
269     //
270     // Set the load register.
271     //
272     HWREG(ui32Base + WDT_O_LOAD) = ui32LoadVal;
273 }
274 
275 //*****************************************************************************
276 //
277 //! Gets the watchdog timer reload value.
278 //!
279 //! \param ui32Base is the base address of the watchdog timer module.
280 //!
281 //! This function gets the value that is loaded into the watchdog timer when
282 //! the count reaches zero for the first time.
283 //!
284 //! \return None.
285 //
286 //*****************************************************************************
287 uint32_t
WatchdogReloadGet(uint32_t ui32Base)288 WatchdogReloadGet(uint32_t ui32Base)
289 {
290     //
291     // Check the arguments.
292     //
293     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
294 
295     //
296     // Get the load register.
297     //
298     return(HWREG(ui32Base + WDT_O_LOAD));
299 }
300 
301 //*****************************************************************************
302 //
303 //! Gets the current watchdog timer value.
304 //!
305 //! \param ui32Base is the base address of the watchdog timer module.
306 //!
307 //! This function reads the current value of the watchdog timer.
308 //!
309 //! \return Returns the current value of the watchdog timer.
310 //
311 //*****************************************************************************
312 uint32_t
WatchdogValueGet(uint32_t ui32Base)313 WatchdogValueGet(uint32_t ui32Base)
314 {
315     //
316     // Check the arguments.
317     //
318     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
319 
320     //
321     // Get the current watchdog timer register value.
322     //
323     return(HWREG(ui32Base + WDT_O_VALUE));
324 }
325 
326 //*****************************************************************************
327 //
328 //! Registers an interrupt handler for the watchdog timer interrupt.
329 //!
330 //! \param ui32Base is the base address of the watchdog timer module.
331 //! \param pfnHandler is a pointer to the function to be called when the
332 //! watchdog timer interrupt occurs.
333 //!
334 //! This function does the actual registering of the interrupt handler.  This
335 //! function also enables the global interrupt in the interrupt controller; the
336 //! watchdog timer interrupt must be enabled via WatchdogEnable().  It is the
337 //! interrupt handler's responsibility to clear the interrupt source via
338 //! WatchdogIntClear().
339 //!
340 //! \sa IntRegister() for important information about registering interrupt
341 //! handlers.
342 //!
343 //! \note For parts with a watchdog timer module that has the ability to
344 //! generate an NMI instead of a standard interrupt, this function registers
345 //! the standard watchdog interrupt handler.  To register the NMI watchdog
346 //! handler, use IntRegister() to register the handler for the
347 //! \b FAULT_NMI interrupt.
348 //!
349 //! \return None.
350 //
351 //*****************************************************************************
352 void
WatchdogIntRegister(uint32_t ui32Base,void (* pfnHandler)(void))353 WatchdogIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
354 {
355     //
356     // Check the arguments.
357     //
358     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
359 
360     //
361     // Register the interrupt handler.
362     //
363     IntRegister(INT_WATCHDOG_TM4C123, pfnHandler);
364 
365     //
366     // Enable the watchdog timer interrupt.
367     //
368     IntEnable(INT_WATCHDOG_TM4C123);
369 }
370 
371 //*****************************************************************************
372 //
373 //! Unregisters an interrupt handler for the watchdog timer interrupt.
374 //!
375 //! \param ui32Base is the base address of the watchdog timer module.
376 //!
377 //! This function does the actual unregistering of the interrupt handler.  This
378 //! function clears the handler to be called when a watchdog timer interrupt
379 //! occurs.  This function also masks off the interrupt in the interrupt
380 //! controller so that the interrupt handler no longer is called.
381 //!
382 //! \sa IntRegister() for important information about registering interrupt
383 //! handlers.
384 //!
385 //! \note For parts with a watchdog timer module that has the ability to
386 //! generate an NMI instead of a standard interrupt, this function unregisters
387 //! the standard watchdog interrupt handler.  To unregister the NMI watchdog
388 //! handler, use IntUnregister() to unregister the handler for the
389 //! \b FAULT_NMI interrupt.
390 //!
391 //! \return None.
392 //
393 //*****************************************************************************
394 void
WatchdogIntUnregister(uint32_t ui32Base)395 WatchdogIntUnregister(uint32_t ui32Base)
396 {
397     //
398     // Check the arguments.
399     //
400     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
401 
402     //
403     // Disable the interrupt.
404     //
405     IntDisable(INT_WATCHDOG_TM4C123);
406 
407     //
408     // Unregister the interrupt handler.
409     //
410     IntUnregister(INT_WATCHDOG_TM4C123);
411 }
412 
413 //*****************************************************************************
414 //
415 //! Enables the watchdog timer interrupt.
416 //!
417 //! \param ui32Base is the base address of the watchdog timer module.
418 //!
419 //! This function enables the watchdog timer interrupt.
420 //!
421 //! \note This function has no effect if the watchdog timer has been locked.
422 //!
423 //! \return None.
424 //
425 //*****************************************************************************
426 void
WatchdogIntEnable(uint32_t ui32Base)427 WatchdogIntEnable(uint32_t ui32Base)
428 {
429     //
430     // Check the arguments.
431     //
432     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
433 
434     //
435     // Enable the watchdog interrupt.
436     //
437     HWREG(ui32Base + WDT_O_CTL) |= WDT_CTL_INTEN;
438 }
439 
440 //*****************************************************************************
441 //
442 //! Gets the current watchdog timer interrupt status.
443 //!
444 //! \param ui32Base is the base address of the watchdog timer module.
445 //! \param bMasked is \b false if the raw interrupt status is required and
446 //! \b true if the masked interrupt status is required.
447 //!
448 //! This function returns the interrupt status for the watchdog timer module.
449 //! Either the raw interrupt status or the status of interrupt that is allowed
450 //! to reflect to the processor can be returned.
451 //!
452 //! \return Returns the current interrupt status, where a 1 indicates that the
453 //! watchdog interrupt is active, and a 0 indicates that it is not active.
454 //
455 //*****************************************************************************
456 uint32_t
WatchdogIntStatus(uint32_t ui32Base,bool bMasked)457 WatchdogIntStatus(uint32_t ui32Base, bool bMasked)
458 {
459     //
460     // Check the arguments.
461     //
462     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
463 
464     //
465     // Return either the interrupt status or the raw interrupt status as
466     // requested.
467     //
468     if(bMasked)
469     {
470         return(HWREG(ui32Base + WDT_O_MIS));
471     }
472     else
473     {
474         return(HWREG(ui32Base + WDT_O_RIS));
475     }
476 }
477 
478 //*****************************************************************************
479 //
480 //! Clears the watchdog timer interrupt.
481 //!
482 //! \param ui32Base is the base address of the watchdog timer module.
483 //!
484 //! The watchdog timer interrupt source is cleared, so that it no longer
485 //! asserts.
486 //!
487 //! \note Because there is a write buffer in the Cortex-M processor, it may
488 //! take several clock cycles before the interrupt source is actually cleared.
489 //! Therefore, it is recommended that the interrupt source be cleared early in
490 //! the interrupt handler (as opposed to the very last action) to avoid
491 //! returning from the interrupt handler before the interrupt source is
492 //! actually cleared.  Failure to do so may result in the interrupt handler
493 //! being immediately reentered (because the interrupt controller still sees
494 //! the interrupt source asserted). This function has no effect if the watchdog
495 //! timer has been locked.
496 //!
497 //! \return None.
498 //
499 //*****************************************************************************
500 void
WatchdogIntClear(uint32_t ui32Base)501 WatchdogIntClear(uint32_t ui32Base)
502 {
503     //
504     // Check the arguments.
505     //
506     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
507 
508     //
509     // Clear the interrupt source.
510     //
511     HWREG(ui32Base + WDT_O_ICR) = WDT_RIS_WDTRIS;
512 }
513 
514 //*****************************************************************************
515 //
516 //! Sets the type of interrupt generated by the watchdog.
517 //!
518 //! \param ui32Base is the base address of the watchdog timer module.
519 //! \param ui32Type is the type of interrupt to generate.
520 //!
521 //! This function sets the type of interrupt that is generated if the watchdog
522 //! timer expires.  \e ui32Type can be either \b WATCHDOG_INT_TYPE_INT to
523 //! generate a standard interrupt (the default) or \b WATCHDOG_INT_TYPE_NMI to
524 //! generate a non-maskable interrupt (NMI).
525 //!
526 //! When configured to generate an NMI, the watchdog interrupt must still be
527 //! enabled with WatchdogIntEnable(), and it must still be cleared inside the
528 //! NMI handler with WatchdogIntClear().
529 //!
530 //! \note The ability to select an NMI interrupt varies with the Tiva part
531 //! in use.  Please consult the datasheet for the part you are using to
532 //! determine whether this support is available. This function has no effect if
533 //! the watchdog timer has been locked.
534 //!
535 //! \return None.
536 //
537 //*****************************************************************************
538 void
WatchdogIntTypeSet(uint32_t ui32Base,uint32_t ui32Type)539 WatchdogIntTypeSet(uint32_t ui32Base, uint32_t ui32Type)
540 {
541     //
542     // Check the arguments.
543     //
544     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
545     ASSERT((ui32Type == WATCHDOG_INT_TYPE_INT) ||
546            (ui32Type == WATCHDOG_INT_TYPE_NMI));
547 
548     //
549     // Set the interrupt type.
550     //
551     HWREG(ui32Base + WDT_O_CTL) = (HWREG(ui32Base + WDT_O_CTL) &
552                                    ~WDT_CTL_INTTYPE) | ui32Type;
553 }
554 
555 //*****************************************************************************
556 //
557 //! Enables stalling of the watchdog timer during debug events.
558 //!
559 //! \param ui32Base is the base address of the watchdog timer module.
560 //!
561 //! This function allows the watchdog timer to stop counting when the processor
562 //! is stopped by the debugger.  By doing so, the watchdog is prevented from
563 //! expiring (typically almost immediately from a human time perspective) and
564 //! resetting the system (if reset is enabled).  The watchdog instead expires
565 //! after the appropriate number of processor cycles have been executed while
566 //! debugging (or at the appropriate time after the processor has been
567 //! restarted).
568 //!
569 //! \note This function has no effect if the watchdog timer has been locked.
570 //!
571 //! \return None.
572 //
573 //*****************************************************************************
574 void
WatchdogStallEnable(uint32_t ui32Base)575 WatchdogStallEnable(uint32_t ui32Base)
576 {
577     //
578     // Check the arguments.
579     //
580     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
581 
582     //
583     // Enable timer stalling.
584     //
585     HWREG(ui32Base + WDT_O_TEST) |= WDT_TEST_STALL;
586 }
587 
588 //*****************************************************************************
589 //
590 //! Disables stalling of the watchdog timer during debug events.
591 //!
592 //! \param ui32Base is the base address of the watchdog timer module.
593 //!
594 //! This function disables the debug mode stall of the watchdog timer.  By
595 //! doing so, the watchdog timer continues to count regardless of the processor
596 //! debug state.
597 //!
598 //! \note This function has no effect if the watchdog timer has been locked.
599 //!
600 //! \return None.
601 //
602 //*****************************************************************************
603 void
WatchdogStallDisable(uint32_t ui32Base)604 WatchdogStallDisable(uint32_t ui32Base)
605 {
606     //
607     // Check the arguments.
608     //
609     ASSERT((ui32Base == WATCHDOG0_BASE) || (ui32Base == WATCHDOG1_BASE));
610 
611     //
612     // Disable timer stalling.
613     //
614     HWREG(ui32Base + WDT_O_TEST) &= ~(WDT_TEST_STALL);
615 }
616 
617 //*****************************************************************************
618 //
619 // Close the Doxygen group.
620 //! @}
621 //
622 //*****************************************************************************
623