lpc_wwdt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**********************************************************************
  2. * $Id$ lpc_wwdt.c 2011-06-02
  3. *//**
  4. * @file lpc_wwdt.c
  5. * @brief Contains all functions support for Wachtdog Timer
  6. * firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @addtogroup WWDT
  34. * @{
  35. */
  36. #ifdef __BUILD_WITH_EXAMPLE__
  37. #include "lpc_libcfg.h"
  38. #else
  39. #include "lpc_libcfg_default.h"
  40. #endif /* __BUILD_WITH_EXAMPLE__ */
  41. #ifdef _WDT
  42. /* Includes ------------------------------------------------------------------- */
  43. #include "lpc_wwdt.h"
  44. #include "lpc_clkpwr.h"
  45. #include "lpc_pinsel.h"
  46. /* Public Functions ----------------------------------------------------------- */
  47. /** @addtogroup WDT_Public_Functions
  48. * @{
  49. */
  50. /********************************************************************//**
  51. * @brief Set timeout value to Timer Constant register
  52. * (if enable) to generate a WatchDog event if match
  53. *
  54. * @param[in] timeoutVal The value (counter) will write directly to
  55. * Register w/o pre-calc
  56. *
  57. * @return None
  58. *********************************************************************/
  59. int8_t WWDT_SetTimeOutRaw(uint32_t timeoutVal)
  60. {
  61. int8_t retval = 0;
  62. if(timeoutVal < WWDT_TIMEOUT_MIN)
  63. {
  64. timeoutVal = WWDT_TIMEOUT_MIN;
  65. retval = WWDT_FUNC_BAD_PARAM;
  66. }
  67. else if (timeoutVal > WWDT_TIMEOUT_MAX)
  68. {
  69. timeoutVal = WWDT_TIMEOUT_MAX;
  70. retval = WWDT_FUNC_BAD_PARAM;
  71. }
  72. LPC_WDT->TC = timeoutVal;
  73. return retval;
  74. }
  75. /********************************************************************//**
  76. * @brief Set WDT timeout (cal by usec) to TC register
  77. *
  78. * @param[in] timeout The time (usec) to generate watchdog event if
  79. * the watchdog counter reach this value
  80. *
  81. * @return WWDT_FUNC_OK if success
  82. *********************************************************************/
  83. int8_t WWDT_SetTimeOut(uint32_t timeout)
  84. {
  85. return WWDT_SetTimeOutRaw(WDT_GET_FROM_USEC(timeout));
  86. }
  87. /*********************************************************************//**
  88. * @brief Initial for Watchdog function by setting timeout
  89. *
  90. * @param[in] TimeOut time out value, should be in range:
  91. * 2048 .. 134217728
  92. *
  93. * @return WWDT_FUNC_OK if success
  94. **********************************************************************/
  95. int8_t WWDT_Init(uint32_t TimeOut)
  96. {
  97. return WWDT_SetTimeOut(TimeOut);
  98. }
  99. /*********************************************************************//**
  100. * @brief Configure the WatchDog by initialization all the timing
  101. * value for register (Warning value, Window value,...)
  102. *
  103. * @param wdtCfg a the st_Wdt_Config type value
  104. *
  105. * @return None
  106. **********************************************************************/
  107. void WWDT_Configure(st_Wdt_Config wdtCfg)
  108. {
  109. WWDT_SetTimeOut(wdtCfg.wdtTmrConst);
  110. if(wdtCfg.wdtEnable)
  111. {
  112. LPC_WDT->MOD |= WWDT_WDMOD_WDEN;
  113. }
  114. else
  115. {
  116. LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
  117. }
  118. if(wdtCfg.wdtReset)
  119. {
  120. LPC_WDT->MOD |= WWDT_WDMOD_WDRESET;
  121. }
  122. else
  123. {
  124. LPC_WDT->MOD &= ~WWDT_WDMOD_WDRESET;
  125. }
  126. if(wdtCfg.wdtProtect)
  127. {
  128. LPC_WDT->MOD |= WWDT_WDMOD_WDPROTECT;
  129. }
  130. else
  131. {
  132. LPC_WDT->MOD &= ~WWDT_WDMOD_WDPROTECT;
  133. }
  134. WWDT_SetWarning(wdtCfg.wdtWarningVal);
  135. WWDT_SetWindow(wdtCfg.wdtWindowVal);
  136. }
  137. /*********************************************************************//**
  138. * @brief Start WatchDog with specific Timeout
  139. *
  140. * @param TimeOut specific Timeout for WatchDog event
  141. *
  142. * @return WWDT_FUNC_OK if success
  143. **********************************************************************/
  144. int8_t WWDT_Start(uint32_t TimeOut)
  145. {
  146. int8_t retval = WWDT_FUNC_OK;
  147. retval = WWDT_SetTimeOut(TimeOut);
  148. WWDT_Cmd(ENABLE);
  149. return retval;
  150. }
  151. /********************************************************************//**
  152. * @brief Update WDT timeout value and feed
  153. *
  154. * @param[in] WarnTime time to generate watchdog warning interrupt(us)
  155. * should be in range: 2048 .. 134217728
  156. *
  157. * @return None
  158. *********************************************************************/
  159. void WWDT_SetTimerConstant(uint32_t constVal)
  160. {
  161. LPC_WDT->TC = constVal;
  162. }
  163. /*********************************************************************//**
  164. * @brief Enable/Disable WDT function
  165. *
  166. * @param[in] Mode WWDT mode that will be enabled/disabled, should be:
  167. * - WWDT_PROTECT_MODE : protect mode
  168. * - WWDT_RESET_MODE : reset mode
  169. *
  170. * @param[in] NewState new state of protection function, should be:
  171. * - ENABLE: The watchdog reload value can be changed at any time
  172. * - DISABLE: The watchdog reload value can be changed only after
  173. * the counter is below the value of WDWARNINT and WDWINDOW
  174. *
  175. * @return None
  176. **********************************************************************/
  177. void WWDT_SetMode(uint8_t mode, FunctionalState NewState)
  178. {
  179. if (mode == WWDT_PROTECT_MODE )
  180. {
  181. if(NewState == ENABLE)
  182. LPC_WDT->MOD |= WWDT_WDMOD_WDPROTECT;
  183. else
  184. LPC_WDT->MOD &= ~WWDT_WDMOD_WDPROTECT;
  185. }
  186. else if(mode == WWDT_RESET_MODE)
  187. {
  188. if(NewState == ENABLE)
  189. LPC_WDT->MOD |= WWDT_WDMOD_WDRESET;
  190. else
  191. LPC_WDT->MOD &= ~WWDT_WDMOD_WDRESET;
  192. }
  193. }
  194. /*********************************************************************//**
  195. * @brief Enable/Disable WWDT activity
  196. *
  197. * @param[in] NewState To enable/disable the WatchDog
  198. *
  199. * @return None
  200. **********************************************************************/
  201. void WWDT_Enable(FunctionalState NewState)
  202. {
  203. if(NewState == ENABLE)
  204. {
  205. LPC_WDT->MOD |= WWDT_WDMOD_WDEN;
  206. }
  207. else
  208. {
  209. LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
  210. }
  211. }
  212. /*********************************************************************//**
  213. * @brief Enable/Disable WWDT activity. In case of Enable, it will
  214. * do feeding WatchDog for its normal operation.
  215. *
  216. * @param[in] NewState To enable/disable the WatchDog
  217. *
  218. * @return None
  219. **********************************************************************/
  220. void WWDT_Cmd(FunctionalState NewState)
  221. {
  222. if(NewState == ENABLE)
  223. {
  224. LPC_WDT->MOD |= WWDT_WDMOD_WDEN;
  225. //Load the Feed register to start using WDT
  226. WWDT_Feed();
  227. }
  228. else
  229. {
  230. LPC_WDT->MOD &= ~WWDT_WDMOD_WDEN;
  231. }
  232. }
  233. /********************************************************************//**
  234. * @brief Set the warning value to register to generate interrupt
  235. * (if enable) if the watchdog timer matches this value.
  236. *
  237. * @param[in] warnVal The value (counter) will write directly to Warning
  238. * Register w/o pre-calc
  239. *
  240. * @return WWDT_FUNC_OK if success
  241. *********************************************************************/
  242. int8_t WWDT_SetWarningRaw(uint32_t warnVal)
  243. {
  244. int8_t retval = WWDT_FUNC_OK;
  245. if(warnVal < WWDT_WARNINT_MIN)
  246. {
  247. warnVal = WWDT_WARNINT_MIN;
  248. retval = WWDT_FUNC_BAD_PARAM;
  249. }
  250. else if (warnVal > WWDT_WARNINT_MAX)
  251. {
  252. warnVal = WWDT_WARNINT_MAX;
  253. retval = WWDT_FUNC_BAD_PARAM;
  254. }
  255. LPC_WDT->WARNINT = warnVal;
  256. return retval;
  257. }
  258. /********************************************************************//**
  259. * @brief Update WDT warning time (cal by usec) to warning register
  260. *
  261. * @param[in] WarnTime The time (usec) to generate watchdog warning
  262. * interrupt should be in range: 2048 .. 8192
  263. *
  264. * @return WWDT_FUNC_OK if success
  265. *********************************************************************/
  266. int8_t WWDT_SetWarning(uint32_t WarnTime)
  267. {
  268. return WWDT_SetWarningRaw(WDT_GET_FROM_USEC(WarnTime));
  269. }
  270. /********************************************************************//**
  271. * @brief Update WDT Windows value (by counter) to Window Register
  272. * of WatchDog
  273. *
  274. * @param[in] wndVal The value (counter) will write directly to Window
  275. * Register w/o pre-calc
  276. *
  277. * @return WWDT_FUNC_OK if success
  278. *********************************************************************/
  279. int8_t WWDT_SetWindowRaw(uint32_t wndVal)
  280. {
  281. int8_t retval = WWDT_FUNC_OK;
  282. if(wndVal < WWDT_WINDOW_MIN)
  283. {
  284. wndVal = WWDT_WINDOW_MIN;
  285. retval = WWDT_FUNC_BAD_PARAM;
  286. }
  287. else if (wndVal > WWDT_WINDOW_MAX)
  288. {
  289. wndVal = WWDT_WINDOW_MAX;
  290. retval = WWDT_FUNC_BAD_PARAM;
  291. }
  292. LPC_WDT->WINDOW = wndVal;
  293. return retval;
  294. }
  295. /********************************************************************//**
  296. * @brief Update WDT Windows value (by usec) to Window Register
  297. * of WatchDog
  298. *
  299. * @param[in] WindowedTime Expected time (usec) to set watchdog window event
  300. *
  301. * @return WWDT_FUNC_OK if success
  302. *********************************************************************/
  303. int8_t WWDT_SetWindow(uint32_t WindowedTime)
  304. {
  305. return WWDT_SetWindowRaw(WDT_GET_FROM_USEC(WindowedTime));
  306. }
  307. /********************************************************************//**
  308. * @brief Update WDT timeout value for WatchDog event(s)
  309. *
  310. * @param[in] TimeOut Time Out value (usec) to be updated, should be
  311. * in range: 2048 .. 134217728
  312. *
  313. * @return None
  314. *********************************************************************/
  315. void WDT_UpdateTimeOut(uint32_t TimeOut)
  316. {
  317. /* check WDPROTECT,
  318. * if it is enable, wait until the counter is below the value of
  319. * WDWARNINT and WDWINDOW
  320. */
  321. if(LPC_WDT->MOD & (1<<4))
  322. {
  323. while((LPC_WDT->TV <(LPC_WDT->WARNINT & WWDT_WDWARNINT_MASK))\
  324. &&(LPC_WDT->TV <(LPC_WDT->WINDOW & WWDT_WDTC_MASK)));
  325. }
  326. WWDT_SetTimeOut(TimeOut);
  327. }
  328. /********************************************************************//**
  329. * @brief Read WWDT status flag
  330. *
  331. * @param[in] Status kind of status flag that you want to get, should be:
  332. * - WWDT_WARNINT_FLAG: watchdog interrupt flag
  333. * - WWDT_TIMEOUT_FLAG: watchdog time-out flag
  334. *
  335. * @return Time out flag status of WDT
  336. *********************************************************************/
  337. FlagStatus WWDT_GetStatus (uint8_t Status)
  338. {
  339. if(Status == WWDT_WARNINT_FLAG)
  340. {
  341. return ((FlagStatus)(LPC_WDT->MOD & (1<<3)));
  342. }
  343. else if (Status == WWDT_TIMEOUT_FLAG)
  344. {
  345. return ((FlagStatus)(LPC_WDT->MOD & (1<<2)));
  346. }
  347. return RESET;
  348. }
  349. /********************************************************************//**
  350. * @brief Clear WWDT status flag
  351. *
  352. * @param[in] Status kind of status flag that you want to get, should be:
  353. * - WWDT_WARNINT_FLAG: watchdog interrupt flag
  354. * - WWDT_TIMEOUT_FLAG: watchdog time-out flag
  355. *
  356. * @return None
  357. *********************************************************************/
  358. void WWDT_ClearStatusFlag (uint8_t flag)
  359. {
  360. if(flag == WWDT_WARNINT_FLAG)
  361. {
  362. // Write 1 to this bit to clear itself
  363. LPC_WDT->MOD |= WWDT_WDMOD_WDINT;
  364. }
  365. else if(flag == WWDT_TIMEOUT_FLAG)
  366. {
  367. // Write 0 to this bit to clear itself
  368. LPC_WDT->MOD &= ~ WWDT_WDMOD_WDTOF;
  369. }
  370. }
  371. /********************************************************************//**
  372. * @brief Clear WDT Time out flag
  373. * @param None
  374. * @return None
  375. *********************************************************************/
  376. void WWDT_ClrTimeOutFlag (void)
  377. {
  378. LPC_WDT->MOD &= ~ WWDT_WDMOD_WDTOF;
  379. }
  380. /********************************************************************//**
  381. * @brief Following the standard sequence to Feed the WatchDog Timer
  382. *
  383. * @param None
  384. *
  385. * @return None
  386. *********************************************************************/
  387. void WWDT_FeedStdSeq (void)
  388. {
  389. LPC_WDT->FEED = 0xAA;
  390. LPC_WDT->FEED = 0x55;
  391. }
  392. /********************************************************************//**
  393. * @brief After set WDTEN, call this function to start Watchdog
  394. * or reload the Watchdog timer
  395. *
  396. * @param None
  397. *
  398. * @return None
  399. *********************************************************************/
  400. void WWDT_Feed (void)
  401. {
  402. // Disable irq interrupt
  403. __disable_irq();
  404. WWDT_FeedStdSeq();
  405. // Then enable irq interrupt
  406. __enable_irq();
  407. }
  408. /********************************************************************//**
  409. * @brief Get the current value of WDT
  410. *
  411. * @param None
  412. *
  413. * @return Current value of WDT
  414. *********************************************************************/
  415. uint32_t WWDT_GetCurrentCount(void)
  416. {
  417. return LPC_WDT->TV;
  418. }
  419. /**
  420. * @}
  421. */
  422. #endif /*_WDT*/
  423. /**
  424. * @}
  425. */
  426. /* --------------------------------- End Of File ------------------------------ */