watchdog.c 19 KB

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