watchdog.c 17 KB

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