wdt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. //*****************************************************************************
  2. //
  3. // wdt.c
  4. //
  5. // Driver for the Watchdog Timer Module.
  6. //
  7. // Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
  8. //
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions
  12. // are met:
  13. //
  14. // Redistributions of source code must retain the above copyright
  15. // notice, this list of conditions and the following disclaimer.
  16. //
  17. // Redistributions in binary form must reproduce the above copyright
  18. // notice, this list of conditions and the following disclaimer in the
  19. // documentation and/or other materials provided with the
  20. // distribution.
  21. //
  22. // Neither the name of Texas Instruments Incorporated nor the names of
  23. // its contributors may be used to endorse or promote products derived
  24. // from this software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. //
  38. //*****************************************************************************
  39. //*****************************************************************************
  40. //
  41. //! \addtogroup WDT_Watchdog_Timer_api
  42. //! @{
  43. //
  44. //*****************************************************************************
  45. #include "inc/hw_ints.h"
  46. #include "inc/hw_memmap.h"
  47. #include "inc/hw_types.h"
  48. #include "inc/hw_wdt.h"
  49. #include "debug.h"
  50. #include "interrupt.h"
  51. #include "wdt.h"
  52. //*****************************************************************************
  53. //
  54. //! Determines if the watchdog timer is enabled.
  55. //!
  56. //! \param ulBase is the base address of the watchdog timer module.
  57. //!
  58. //! This will check to see if the watchdog timer is enabled.
  59. //!
  60. //! \return Returns \b true if the watchdog timer is enabled, and \b false
  61. //! if it is not.
  62. //
  63. //*****************************************************************************
  64. tBoolean
  65. WatchdogRunning(unsigned long ulBase)
  66. {
  67. //
  68. // Check the arguments.
  69. //
  70. ASSERT((ulBase == WDT_BASE));
  71. //
  72. // See if the watchdog timer module is enabled, and return.
  73. //
  74. return(HWREG(ulBase + WDT_O_CTL) & WDT_CTL_INTEN);
  75. }
  76. //*****************************************************************************
  77. //
  78. //! Enables the watchdog timer.
  79. //!
  80. //! \param ulBase is the base address of the watchdog timer module.
  81. //!
  82. //! This will enable the watchdog timer counter and interrupt.
  83. //!
  84. //! \note This function will have no effect if the watchdog timer has
  85. //! been locked.
  86. //!
  87. //! \sa WatchdogLock(), WatchdogUnlock()
  88. //!
  89. //! \return None.
  90. //
  91. //*****************************************************************************
  92. void
  93. WatchdogEnable(unsigned long ulBase)
  94. {
  95. //
  96. // Check the arguments.
  97. //
  98. ASSERT((ulBase == WDT_BASE));
  99. //
  100. // Enable the watchdog timer module.
  101. //
  102. HWREG(ulBase + WDT_O_CTL) |= WDT_CTL_INTEN;
  103. }
  104. //*****************************************************************************
  105. //
  106. //! Enables the watchdog timer lock mechanism.
  107. //!
  108. //! \param ulBase is the base address of the watchdog timer module.
  109. //!
  110. //! Locks out write access to the watchdog timer configuration registers.
  111. //!
  112. //! \return None.
  113. //
  114. //*****************************************************************************
  115. void
  116. WatchdogLock(unsigned long ulBase)
  117. {
  118. //
  119. // Check the arguments.
  120. //
  121. ASSERT((ulBase == WDT_BASE));
  122. //
  123. // Lock out watchdog register writes. Writing anything to the WDT_O_LOCK
  124. // register causes the lock to go into effect.
  125. //
  126. HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_LOCKED;
  127. }
  128. //*****************************************************************************
  129. //
  130. //! Disables the watchdog timer lock mechanism.
  131. //!
  132. //! \param ulBase is the base address of the watchdog timer module.
  133. //!
  134. //! Enables write access to the watchdog timer configuration registers.
  135. //!
  136. //! \return None.
  137. //
  138. //*****************************************************************************
  139. void
  140. WatchdogUnlock(unsigned long ulBase)
  141. {
  142. //
  143. // Check the arguments.
  144. //
  145. ASSERT((ulBase == WDT_BASE));
  146. //
  147. // Unlock watchdog register writes.
  148. //
  149. HWREG(ulBase + WDT_O_LOCK) = WDT_LOCK_UNLOCK;
  150. }
  151. //*****************************************************************************
  152. //
  153. //! Gets the state of the watchdog timer lock mechanism.
  154. //!
  155. //! \param ulBase is the base address of the watchdog timer module.
  156. //!
  157. //! Returns the lock state of the watchdog timer registers.
  158. //!
  159. //! \return Returns \b true if the watchdog timer registers are locked, and
  160. //! \b false if they are not locked.
  161. //
  162. //*****************************************************************************
  163. tBoolean
  164. WatchdogLockState(unsigned long ulBase)
  165. {
  166. //
  167. // Check the arguments.
  168. //
  169. ASSERT((ulBase == WDT_BASE));
  170. //
  171. // Get the lock state.
  172. //
  173. return((HWREG(ulBase + WDT_O_LOCK) == WDT_LOCK_LOCKED) ? true : false);
  174. }
  175. //*****************************************************************************
  176. //
  177. //! Sets the watchdog timer reload value.
  178. //!
  179. //! \param ulBase is the base address of the watchdog timer module.
  180. //! \param ulLoadVal is the load value for the watchdog timer.
  181. //!
  182. //! This function sets the value to load into the watchdog timer when the count
  183. //! reaches zero for the first time; if the watchdog timer is running when this
  184. //! function is called, then the value will be immediately loaded into the
  185. //! watchdog timer counter. If the \e ulLoadVal parameter is 0, then an
  186. //! interrupt is immediately generated.
  187. //!
  188. //! \note This function will have no effect if the watchdog timer has
  189. //! been locked.
  190. //!
  191. //! \sa WatchdogLock(), WatchdogUnlock(), WatchdogReloadGet()
  192. //!
  193. //! \return None.
  194. //
  195. //*****************************************************************************
  196. void
  197. WatchdogReloadSet(unsigned long ulBase, unsigned long ulLoadVal)
  198. {
  199. //
  200. // Check the arguments.
  201. //
  202. ASSERT((ulBase == WDT_BASE));
  203. //
  204. // Set the load register.
  205. //
  206. HWREG(ulBase + WDT_O_LOAD) = ulLoadVal;
  207. }
  208. //*****************************************************************************
  209. //
  210. //! Gets the watchdog timer reload value.
  211. //!
  212. //! \param ulBase is the base address of the watchdog timer module.
  213. //!
  214. //! This function gets the value that is loaded into the watchdog timer when
  215. //! the count reaches zero for the first time.
  216. //!
  217. //! \sa WatchdogReloadSet()
  218. //!
  219. //! \return None.
  220. //
  221. //*****************************************************************************
  222. unsigned long
  223. WatchdogReloadGet(unsigned long ulBase)
  224. {
  225. //
  226. // Check the arguments.
  227. //
  228. ASSERT((ulBase == WDT_BASE));
  229. //
  230. // Get the load register.
  231. //
  232. return(HWREG(ulBase + WDT_O_LOAD));
  233. }
  234. //*****************************************************************************
  235. //
  236. //! Gets the current watchdog timer value.
  237. //!
  238. //! \param ulBase is the base address of the watchdog timer module.
  239. //!
  240. //! This function reads the current value of the watchdog timer.
  241. //!
  242. //! \return Returns the current value of the watchdog timer.
  243. //
  244. //*****************************************************************************
  245. unsigned long
  246. WatchdogValueGet(unsigned long ulBase)
  247. {
  248. //
  249. // Check the arguments.
  250. //
  251. ASSERT((ulBase == WDT_BASE));
  252. //
  253. // Get the current watchdog timer register value.
  254. //
  255. return(HWREG(ulBase + WDT_O_VALUE));
  256. }
  257. //*****************************************************************************
  258. //
  259. //! Registers an interrupt handler for watchdog timer interrupt.
  260. //!
  261. //! \param ulBase is the base address of the watchdog timer module.
  262. //! \param pfnHandler is a pointer to the function to be called when the
  263. //! watchdog timer interrupt occurs.
  264. //!
  265. //! This function does the actual registering of the interrupt handler. This
  266. //! will enable the global interrupt in the interrupt controller; the watchdog
  267. //! timer interrupt must be enabled via WatchdogEnable(). It is the interrupt
  268. //! handler's responsibility to clear the interrupt source via
  269. //! WatchdogIntClear().
  270. //!
  271. //! \sa IntRegister() for important information about registering interrupt
  272. //! handlers.
  273. //!
  274. //! \note This function will only register the standard watchdog interrupt
  275. //! handler. To register the NMI watchdog handler, use IntRegister()
  276. //! to register the handler for the \b FAULT_NMI interrupt.
  277. //!
  278. //! \return None.
  279. //
  280. //*****************************************************************************
  281. void
  282. WatchdogIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
  283. {
  284. //
  285. // Check the arguments.
  286. //
  287. ASSERT((ulBase == WDT_BASE));
  288. //
  289. // Register the interrupt handler and
  290. // Enable the watchdog timer interrupt.
  291. //
  292. IntRegister(INT_WDT, pfnHandler);
  293. IntEnable(INT_WDT);
  294. }
  295. //*****************************************************************************
  296. //
  297. //! Unregisters an interrupt handler for the watchdog timer interrupt.
  298. //!
  299. //! \param ulBase is the base address of the watchdog timer module.
  300. //!
  301. //! This function does the actual unregistering of the interrupt handler. This
  302. //! function will clear the handler to be called when a watchdog timer
  303. //! interrupt occurs. This will also mask off the interrupt in the interrupt
  304. //! controller so that the interrupt handler no longer is called.
  305. //!
  306. //! \sa IntRegister() for important information about registering interrupt
  307. //! handlers.
  308. //!
  309. //! \note This function will only unregister the standard watchdog interrupt
  310. //! handler. To unregister the NMI watchdog handler, use IntUnregister()
  311. //! to unregister the handler for the \b FAULT_NMI interrupt.
  312. //!
  313. //! \return None.
  314. //
  315. //*****************************************************************************
  316. void
  317. WatchdogIntUnregister(unsigned long ulBase)
  318. {
  319. //
  320. // Check the arguments.
  321. //
  322. ASSERT((ulBase == WDT_BASE));
  323. //
  324. // Disable the interrupt
  325. IntDisable(INT_WDT);
  326. //
  327. // Unregister the interrupt handler.
  328. //
  329. IntUnregister(INT_WDT);
  330. }
  331. //*****************************************************************************
  332. //
  333. //! Gets the current watchdog timer interrupt status.
  334. //!
  335. //! \param ulBase is the base address of the watchdog timer module.
  336. //! \param bMasked is \b false if the raw interrupt status is required and
  337. //! \b true if the masked interrupt status is required.
  338. //!
  339. //! This returns the interrupt status for the watchdog timer module. Either
  340. //! the raw interrupt status or the status of interrupt that is allowed to
  341. //! reflect to the processor can be returned.
  342. //!
  343. //! \return Returns the current interrupt status, where a 1 indicates that the
  344. //! watchdog interrupt is active, and a 0 indicates that it is not active.
  345. //
  346. //*****************************************************************************
  347. unsigned long
  348. WatchdogIntStatus(unsigned long ulBase, tBoolean bMasked)
  349. {
  350. //
  351. // Check the arguments.
  352. //
  353. ASSERT((ulBase == WDT_BASE));
  354. //
  355. // Return either the interrupt status or the raw interrupt status as
  356. // requested.
  357. //
  358. if(bMasked)
  359. {
  360. return(HWREG(ulBase + WDT_O_MIS));
  361. }
  362. else
  363. {
  364. return(HWREG(ulBase + WDT_O_RIS));
  365. }
  366. }
  367. //*****************************************************************************
  368. //
  369. //! Clears the watchdog timer interrupt.
  370. //!
  371. //! \param ulBase is the base address of the watchdog timer module.
  372. //!
  373. //! The watchdog timer interrupt source is cleared, so that it no longer
  374. //! asserts.
  375. //!
  376. //! \note Because there is a write buffer in the Cortex-M3 processor, it may
  377. //! take several clock cycles before the interrupt source is actually cleared.
  378. //! Therefore, it is recommended that the interrupt source be cleared early in
  379. //! the interrupt handler (as opposed to the very last action) to avoid
  380. //! returning from the interrupt handler before the interrupt source is
  381. //! actually cleared. Failure to do so may result in the interrupt handler
  382. //! being immediately reentered (because the interrupt controller still sees
  383. //! the interrupt source asserted).
  384. //!
  385. //! \return None.
  386. //
  387. //*****************************************************************************
  388. void
  389. WatchdogIntClear(unsigned long ulBase)
  390. {
  391. //
  392. // Check the arguments.
  393. //
  394. ASSERT((ulBase == WDT_BASE));
  395. //
  396. // Clear the interrupt source.
  397. //
  398. HWREG(ulBase + WDT_O_ICR) = WDT_INT_TIMEOUT;
  399. }
  400. //*****************************************************************************
  401. //
  402. //! Enables stalling of the watchdog timer during debug events.
  403. //!
  404. //! \param ulBase is the base address of the watchdog timer module.
  405. //!
  406. //! This function allows the watchdog timer to stop counting when the processor
  407. //! is stopped by the debugger. By doing so, the watchdog is prevented from
  408. //! expiring (typically almost immediately from a human time perspective) and
  409. //! resetting the system (if reset is enabled). The watchdog will instead
  410. //! expired after the appropriate number of processor cycles have been executed
  411. //! while debugging (or at the appropriate time after the processor has been
  412. //! restarted).
  413. //!
  414. //! \return None.
  415. //
  416. //*****************************************************************************
  417. void
  418. WatchdogStallEnable(unsigned long ulBase)
  419. {
  420. //
  421. // Check the arguments.
  422. //
  423. ASSERT((ulBase == WDT_BASE));
  424. //
  425. // Enable timer stalling.
  426. //
  427. HWREG(ulBase + WDT_O_TEST) |= WDT_TEST_STALL;
  428. }
  429. //*****************************************************************************
  430. //
  431. //! Disables stalling of the watchdog timer during debug events.
  432. //!
  433. //! \param ulBase is the base address of the watchdog timer module.
  434. //!
  435. //! This function disables the debug mode stall of the watchdog timer. By
  436. //! doing so, the watchdog timer continues to count regardless of the processor
  437. //! debug state.
  438. //!
  439. //! \return None.
  440. //
  441. //*****************************************************************************
  442. void
  443. WatchdogStallDisable(unsigned long ulBase)
  444. {
  445. //
  446. // Check the arguments.
  447. //
  448. ASSERT((ulBase == WDT_BASE));
  449. //
  450. // Disable timer stalling.
  451. //
  452. HWREG(ulBase + WDT_O_TEST) &= ~(WDT_TEST_STALL);
  453. }
  454. //*****************************************************************************
  455. //
  456. // Close the Doxygen group.
  457. //! @}
  458. //
  459. //*****************************************************************************