watchdog.c 20 KB

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