watchdog.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. //*****************************************************************************
  2. //
  3. // watchdog.c - Driver for the Watchdog Timer Module.
  4. //
  5. // Copyright (c) 2005-2011 Texas Instruments Incorporated. All rights reserved.
  6. // Software License Agreement
  7. //
  8. // Texas Instruments (TI) is supplying this software for use solely and
  9. // exclusively on TI's microcontroller products. The software is owned by
  10. // TI and/or its suppliers, and is protected under applicable copyright
  11. // laws. You may not combine this software with "viral" open-source
  12. // software in order to form a larger program.
  13. //
  14. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  15. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  16. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
  18. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  19. // DAMAGES, FOR ANY REASON WHATSOEVER.
  20. //
  21. // This is part of revision 8049 of the Stellaris Peripheral Driver Library.
  22. //
  23. //*****************************************************************************
  24. //*****************************************************************************
  25. //
  26. //! \addtogroup watchdog_api
  27. //! @{
  28. //
  29. //*****************************************************************************
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_memmap.h"
  32. #include "inc/hw_types.h"
  33. #include "inc/hw_watchdog.h"
  34. #include "driverlib/debug.h"
  35. #include "driverlib/interrupt.h"
  36. #include "driverlib/watchdog.h"
  37. //*****************************************************************************
  38. //
  39. //! Determines if the watchdog timer is enabled.
  40. //!
  41. //! \param ulBase is the base address of the watchdog timer module.
  42. //!
  43. //! This will check to see if the watchdog timer is enabled.
  44. //!
  45. //! \return Returns \b true if the watchdog timer is enabled, and \b false
  46. //! if it is not.
  47. //
  48. //*****************************************************************************
  49. tBoolean
  50. WatchdogRunning(unsigned long ulBase)
  51. {
  52. //
  53. // Check the arguments.
  54. //
  55. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  56. //
  57. // See if the watchdog timer module is enabled, and return.
  58. //
  59. return(HWREG(ulBase + WDT_O_CTL) & WDT_CTL_INTEN);
  60. }
  61. //*****************************************************************************
  62. //
  63. //! Enables the watchdog timer.
  64. //!
  65. //! \param ulBase is the base address of the watchdog timer module.
  66. //!
  67. //! This will enable the watchdog timer counter and interrupt.
  68. //!
  69. //! \note This function will have no effect if the watchdog timer has
  70. //! been locked.
  71. //!
  72. //! \sa WatchdogLock(), WatchdogUnlock()
  73. //!
  74. //! \return None.
  75. //
  76. //*****************************************************************************
  77. void
  78. WatchdogEnable(unsigned long ulBase)
  79. {
  80. //
  81. // Check the arguments.
  82. //
  83. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  84. //
  85. // Enable the watchdog timer module.
  86. //
  87. HWREG(ulBase + WDT_O_CTL) |= WDT_CTL_INTEN;
  88. }
  89. //*****************************************************************************
  90. //
  91. //! Enables the watchdog timer reset.
  92. //!
  93. //! \param ulBase is the base address of the watchdog timer module.
  94. //!
  95. //! Enables the capability of the watchdog timer to issue a reset to the
  96. //! processor upon a second timeout condition.
  97. //!
  98. //! \note This function will have no effect if the watchdog timer has
  99. //! been locked.
  100. //!
  101. //! \sa WatchdogLock(), WatchdogUnlock()
  102. //!
  103. //! \return None.
  104. //
  105. //*****************************************************************************
  106. void
  107. WatchdogResetEnable(unsigned long ulBase)
  108. {
  109. //
  110. // Check the arguments.
  111. //
  112. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  113. //
  114. // Enable the watchdog reset.
  115. //
  116. HWREG(ulBase + WDT_O_CTL) |= WDT_CTL_RESEN;
  117. }
  118. //*****************************************************************************
  119. //
  120. //! Disables the watchdog timer reset.
  121. //!
  122. //! \param ulBase is the base address of the watchdog timer module.
  123. //!
  124. //! Disables the capability of the watchdog timer to issue a reset to the
  125. //! processor upon a second timeout condition.
  126. //!
  127. //! \note This function will have no effect if the watchdog timer has
  128. //! been locked.
  129. //!
  130. //! \sa WatchdogLock(), WatchdogUnlock()
  131. //!
  132. //! \return None.
  133. //
  134. //*****************************************************************************
  135. void
  136. WatchdogResetDisable(unsigned long ulBase)
  137. {
  138. //
  139. // Check the arguments.
  140. //
  141. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  142. //
  143. // Disable the watchdog reset.
  144. //
  145. HWREG(ulBase + WDT_O_CTL) &= ~(WDT_CTL_RESEN);
  146. }
  147. //*****************************************************************************
  148. //
  149. //! Enables the watchdog timer lock mechanism.
  150. //!
  151. //! \param ulBase is the base address of the watchdog timer module.
  152. //!
  153. //! Locks out write access to the watchdog timer configuration registers.
  154. //!
  155. //! \return None.
  156. //
  157. //*****************************************************************************
  158. void
  159. WatchdogLock(unsigned long ulBase)
  160. {
  161. //
  162. // Check the arguments.
  163. //
  164. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  165. //
  166. // Lock out watchdog register writes. Writing anything to the WDT_O_LOCK
  167. // register causes the lock to go into effect.
  168. //
  169. HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_LOCKED;
  170. }
  171. //*****************************************************************************
  172. //
  173. //! Disables the watchdog timer lock mechanism.
  174. //!
  175. //! \param ulBase is the base address of the watchdog timer module.
  176. //!
  177. //! Enables write access to the watchdog timer configuration registers.
  178. //!
  179. //! \return None.
  180. //
  181. //*****************************************************************************
  182. void
  183. WatchdogUnlock(unsigned long ulBase)
  184. {
  185. //
  186. // Check the arguments.
  187. //
  188. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  189. //
  190. // Unlock watchdog register writes.
  191. //
  192. HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_UNLOCK;
  193. }
  194. //*****************************************************************************
  195. //
  196. //! Gets the state of the watchdog timer lock mechanism.
  197. //!
  198. //! \param ulBase is the base address of the watchdog timer module.
  199. //!
  200. //! Returns the lock state of the watchdog timer registers.
  201. //!
  202. //! \return Returns \b true if the watchdog timer registers are locked, and
  203. //! \b false if they are not locked.
  204. //
  205. //*****************************************************************************
  206. tBoolean
  207. WatchdogLockState(unsigned long ulBase)
  208. {
  209. //
  210. // Check the arguments.
  211. //
  212. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  213. //
  214. // Get the lock state.
  215. //
  216. return((HWREG(ulBase + WDT_O_LOCK) == WDT_LOCK_LOCKED) ? true : false);
  217. }
  218. //*****************************************************************************
  219. //
  220. //! Sets the watchdog timer reload value.
  221. //!
  222. //! \param ulBase is the base address of the watchdog timer module.
  223. //! \param ulLoadVal is the load value for the watchdog timer.
  224. //!
  225. //! This function sets the value to load into the watchdog timer when the count
  226. //! reaches zero for the first time; if the watchdog timer is running when this
  227. //! function is called, then the value is immediately loaded into the watchdog
  228. //! timer counter. If the \e ulLoadVal parameter is 0, then an interrupt is
  229. //! immediately generated.
  230. //!
  231. //! \note This function will have no effect if the watchdog timer has
  232. //! been locked.
  233. //!
  234. //! \sa WatchdogLock(), WatchdogUnlock(), WatchdogReloadGet()
  235. //!
  236. //! \return None.
  237. //
  238. //*****************************************************************************
  239. void
  240. WatchdogReloadSet(unsigned long ulBase, unsigned long ulLoadVal)
  241. {
  242. //
  243. // Check the arguments.
  244. //
  245. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  246. //
  247. // Set the load register.
  248. //
  249. HWREG(ulBase + WDT_O_LOAD) = ulLoadVal;
  250. }
  251. //*****************************************************************************
  252. //
  253. //! Gets the watchdog timer reload value.
  254. //!
  255. //! \param ulBase is the base address of the watchdog timer module.
  256. //!
  257. //! This function gets the value that is loaded into the watchdog timer when
  258. //! the count reaches zero for the first time.
  259. //!
  260. //! \sa WatchdogReloadSet()
  261. //!
  262. //! \return None.
  263. //
  264. //*****************************************************************************
  265. unsigned long
  266. WatchdogReloadGet(unsigned long ulBase)
  267. {
  268. //
  269. // Check the arguments.
  270. //
  271. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  272. //
  273. // Get the load register.
  274. //
  275. return(HWREG(ulBase + WDT_O_LOAD));
  276. }
  277. //*****************************************************************************
  278. //
  279. //! Gets the current watchdog timer value.
  280. //!
  281. //! \param ulBase is the base address of the watchdog timer module.
  282. //!
  283. //! This function reads the current value of the watchdog timer.
  284. //!
  285. //! \return Returns the current value of the watchdog timer.
  286. //
  287. //*****************************************************************************
  288. unsigned long
  289. WatchdogValueGet(unsigned long ulBase)
  290. {
  291. //
  292. // Check the arguments.
  293. //
  294. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  295. //
  296. // Get the current watchdog timer register value.
  297. //
  298. return(HWREG(ulBase + WDT_O_VALUE));
  299. }
  300. //*****************************************************************************
  301. //
  302. //! Registers an interrupt handler for watchdog timer interrupt.
  303. //!
  304. //! \param ulBase is the base address of the watchdog timer module.
  305. //! \param pfnHandler is a pointer to the function to be called when the
  306. //! watchdog timer interrupt occurs.
  307. //!
  308. //! This function does the actual registering of the interrupt handler. This
  309. //! will enable the global interrupt in the interrupt controller; the watchdog
  310. //! timer interrupt must be enabled via WatchdogEnable(). It is the interrupt
  311. //! handler's responsibility to clear the interrupt source via
  312. //! WatchdogIntClear().
  313. //!
  314. //! \sa IntRegister() for important information about registering interrupt
  315. //! handlers.
  316. //!
  317. //! \note For parts with a watchdog timer module that has the ability to
  318. //! generate an NMI instead of a standard interrupt, this function will
  319. //! register the standard watchdog interrupt handler. To register the NMI
  320. //! watchdog handler, use IntRegister() to register the handler for the
  321. //! \b FAULT_NMI interrupt.
  322. //!
  323. //! \return None.
  324. //
  325. //*****************************************************************************
  326. void
  327. WatchdogIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  328. {
  329. //
  330. // Check the arguments.
  331. //
  332. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  333. //
  334. // Register the interrupt handler.
  335. //
  336. IntRegister(INT_WATCHDOG, pfnHandler);
  337. //
  338. // Enable the watchdog timer interrupt.
  339. //
  340. IntEnable(INT_WATCHDOG);
  341. }
  342. //*****************************************************************************
  343. //
  344. //! Unregisters an interrupt handler for the watchdog timer interrupt.
  345. //!
  346. //! \param ulBase is the base address of the watchdog timer module.
  347. //!
  348. //! This function does the actual unregistering of the interrupt handler. This
  349. //! function will clear the handler to be called when a watchdog timer
  350. //! interrupt occurs. This will also mask off the interrupt in the interrupt
  351. //! controller so that the interrupt handler no longer is called.
  352. //!
  353. //! \sa IntRegister() for important information about registering interrupt
  354. //! handlers.
  355. //!
  356. //! \note For parts with a watchdog timer module that has the ability to
  357. //! generate an NMI instead of a standard interrupt, this function will
  358. //! unregister the standard watchdog interrupt handler. To unregister the NMI
  359. //! watchdog handler, use IntUnregister() to unregister the handler for the
  360. //! \b FAULT_NMI interrupt.
  361. //!
  362. //! \return None.
  363. //
  364. //*****************************************************************************
  365. void
  366. WatchdogIntUnregister(unsigned long ulBase)
  367. {
  368. //
  369. // Check the arguments.
  370. //
  371. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  372. //
  373. // Disable the interrupt.
  374. //
  375. IntDisable(INT_WATCHDOG);
  376. //
  377. // Unregister the interrupt handler.
  378. //
  379. IntUnregister(INT_WATCHDOG);
  380. }
  381. //*****************************************************************************
  382. //
  383. //! Enables the watchdog timer interrupt.
  384. //!
  385. //! \param ulBase is the base address of the watchdog timer module.
  386. //!
  387. //! Enables the watchdog timer interrupt.
  388. //!
  389. //! \note This function will have no effect if the watchdog timer has
  390. //! been locked.
  391. //!
  392. //! \sa WatchdogLock(), WatchdogUnlock(), WatchdogEnable()
  393. //!
  394. //! \return None.
  395. //
  396. //*****************************************************************************
  397. void
  398. WatchdogIntEnable(unsigned long ulBase)
  399. {
  400. //
  401. // Check the arguments.
  402. //
  403. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  404. //
  405. // Enable the watchdog interrupt.
  406. //
  407. HWREG(ulBase + WDT_O_CTL) |= WDT_CTL_INTEN;
  408. }
  409. //*****************************************************************************
  410. //
  411. //! Gets the current watchdog timer interrupt status.
  412. //!
  413. //! \param ulBase 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 returns the interrupt status for the watchdog timer module. Either
  418. //! the raw interrupt status or the status of interrupt that is allowed to
  419. //! 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. unsigned long
  426. WatchdogIntStatus(unsigned long ulBase, tBoolean bMasked)
  427. {
  428. //
  429. // Check the arguments.
  430. //
  431. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == 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(ulBase + WDT_O_MIS));
  439. }
  440. else
  441. {
  442. return(HWREG(ulBase + WDT_O_RIS));
  443. }
  444. }
  445. //*****************************************************************************
  446. //
  447. //! Clears the watchdog timer interrupt.
  448. //!
  449. //! \param ulBase 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-M3 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).
  462. //!
  463. //! \return None.
  464. //
  465. //*****************************************************************************
  466. void
  467. WatchdogIntClear(unsigned long ulBase)
  468. {
  469. //
  470. // Check the arguments.
  471. //
  472. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  473. //
  474. // Clear the interrupt source.
  475. //
  476. HWREG(ulBase + WDT_O_ICR) = WDT_INT_TIMEOUT;
  477. }
  478. //*****************************************************************************
  479. //
  480. //! Sets the type of interrupt generated by the watchdog.
  481. //!
  482. //! \param ulBase is the base address of the watchdog timer module.
  483. //! \param ulType is the type of interrupt to generate.
  484. //!
  485. //! This function sets the type of interrupt that is generated if the watchdog
  486. //! timer expires. \e ulType can be either \b WATCHDOG_INT_TYPE_INT to
  487. //! generate a standard interrupt (the default) or \b WATCHDOG_INT_TYPE_NMI to
  488. //! generate a non-maskable interrupt (NMI).
  489. //!
  490. //! When configured to generate an NMI, the watchdog interrupt must still be
  491. //! enabled with WatchdogIntEnable(), and it must still be cleared inside the
  492. //! NMI handler with WatchdogIntClear().
  493. //!
  494. //! \note The ability to select an NMI interrupt varies with the Stellaris part
  495. //! in use. Please consult the datasheet for the part you are using to
  496. //! determine whether this support is available.
  497. //!
  498. //! \return None.
  499. //
  500. //*****************************************************************************
  501. void
  502. WatchdogIntTypeSet(unsigned long ulBase, unsigned long ulType)
  503. {
  504. //
  505. // Check the arguments.
  506. //
  507. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  508. ASSERT((ulType == WATCHDOG_INT_TYPE_INT) ||
  509. (ulType == WATCHDOG_INT_TYPE_NMI));
  510. //
  511. // Set the interrupt type.
  512. //
  513. HWREG(ulBase + WDT_O_CTL) =
  514. (HWREG(ulBase + WDT_O_CTL) & ~WDT_CTL_INTTYPE) | ulType;
  515. }
  516. //*****************************************************************************
  517. //
  518. //! Enables stalling of the watchdog timer during debug events.
  519. //!
  520. //! \param ulBase is the base address of the watchdog timer module.
  521. //!
  522. //! This function allows the watchdog timer to stop counting when the processor
  523. //! is stopped by the debugger. By doing so, the watchdog is prevented from
  524. //! expiring (typically almost immediately from a human time perspective) and
  525. //! resetting the system (if reset is enabled). The watchdog will instead
  526. //! expired after the appropriate number of processor cycles have been executed
  527. //! while debugging (or at the appropriate time after the processor has been
  528. //! restarted).
  529. //!
  530. //! \return None.
  531. //
  532. //*****************************************************************************
  533. void
  534. WatchdogStallEnable(unsigned long ulBase)
  535. {
  536. //
  537. // Check the arguments.
  538. //
  539. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  540. //
  541. // Enable timer stalling.
  542. //
  543. HWREG(ulBase + WDT_O_TEST) |= WDT_TEST_STALL;
  544. }
  545. //*****************************************************************************
  546. //
  547. //! Disables stalling of the watchdog timer during debug events.
  548. //!
  549. //! \param ulBase is the base address of the watchdog timer module.
  550. //!
  551. //! This function disables the debug mode stall of the watchdog timer. By
  552. //! doing so, the watchdog timer continues to count regardless of the processor
  553. //! debug state.
  554. //!
  555. //! \return None.
  556. //
  557. //*****************************************************************************
  558. void
  559. WatchdogStallDisable(unsigned long ulBase)
  560. {
  561. //
  562. // Check the arguments.
  563. //
  564. ASSERT((ulBase == WATCHDOG0_BASE) || (ulBase == WATCHDOG1_BASE));
  565. //
  566. // Disable timer stalling.
  567. //
  568. HWREG(ulBase + WDT_O_TEST) &= ~(WDT_TEST_STALL);
  569. }
  570. //*****************************************************************************
  571. //
  572. // Close the Doxygen group.
  573. //! @}
  574. //
  575. //*****************************************************************************