My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lpc17xx_wdt.c 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**********************************************************************
  2. * $Id$ lpc17xx_wdt.c 2010-05-21
  3. *//**
  4. * @file lpc17xx_wdt.c
  5. * @brief Contains all functions support for WDT firmware library
  6. * on LPC17xx
  7. * @version 2.0
  8. * @date 21. May. 2010
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2010, 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 WDT
  34. * @{
  35. */
  36. /* Includes ------------------------------------------------------------------- */
  37. #include "lpc17xx_wdt.h"
  38. #include "lpc17xx_clkpwr.h"
  39. #include "lpc17xx_pinsel.h"
  40. /* If this source file built with example, the LPC17xx FW library configuration
  41. * file in each example directory ("lpc17xx_libcfg.h") must be included,
  42. * otherwise the default FW library configuration file must be included instead
  43. */
  44. #ifdef __BUILD_WITH_EXAMPLE__
  45. #include "lpc17xx_libcfg.h"
  46. #else
  47. #include "lpc17xx_libcfg_default.h"
  48. #endif /* __BUILD_WITH_EXAMPLE__ */
  49. #ifdef _WDT
  50. /* Private Functions ---------------------------------------------------------- */
  51. static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout);
  52. /********************************************************************//**
  53. * @brief Set WDT time out value and WDT mode
  54. * @param[in] clk_source select Clock source for WDT device
  55. * @param[in] timeout value of time-out for WDT (us)
  56. * @return None
  57. *********************************************************************/
  58. static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout)
  59. {
  60. uint32_t pclk_wdt = 0;
  61. uint32_t tempval = 0;
  62. switch ((WDT_CLK_OPT) clk_source)
  63. {
  64. case WDT_CLKSRC_IRC:
  65. pclk_wdt = 4000000;
  66. // Calculate TC in WDT
  67. tempval = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
  68. // Check if it valid
  69. if (tempval >= WDT_TIMEOUT_MIN)
  70. {
  71. LPC_WDT->WDTC = tempval;
  72. return SUCCESS;
  73. }
  74. break;
  75. case WDT_CLKSRC_PCLK:
  76. // Get WDT clock with CCLK divider = 4
  77. pclk_wdt = SystemCoreClock / 4;
  78. // Calculate TC in WDT
  79. tempval = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
  80. if (tempval >= WDT_TIMEOUT_MIN)
  81. {
  82. CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
  83. LPC_WDT->WDTC = (uint32_t) tempval;
  84. return SUCCESS;
  85. }
  86. // Get WDT clock with CCLK divider = 2
  87. pclk_wdt = SystemCoreClock / 2;
  88. // Calculate TC in WDT
  89. tempval = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
  90. if (tempval >= WDT_TIMEOUT_MIN)
  91. {
  92. CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_2);
  93. LPC_WDT->WDTC = (uint32_t) tempval;
  94. return SUCCESS;
  95. }
  96. // Get WDT clock with CCLK divider = 1
  97. pclk_wdt = SystemCoreClock;
  98. // Calculate TC in WDT
  99. tempval = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
  100. if (tempval >= WDT_TIMEOUT_MIN)
  101. {
  102. CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_1);
  103. LPC_WDT->WDTC = (uint32_t) tempval;
  104. return SUCCESS;
  105. }
  106. break ;
  107. case WDT_CLKSRC_RTC:
  108. pclk_wdt = 32768;
  109. // Calculate TC in WDT
  110. tempval = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
  111. // Check if it valid
  112. if (tempval >= WDT_TIMEOUT_MIN)
  113. {
  114. LPC_WDT->WDTC = (uint32_t) tempval;
  115. return SUCCESS;
  116. }
  117. break;
  118. // Error parameter
  119. default:
  120. break;
  121. }
  122. return ERROR;
  123. }
  124. /* End of Private Functions --------------------------------------------------- */
  125. /* Public Functions ----------------------------------------------------------- */
  126. /** @addtogroup WDT_Public_Functions
  127. * @{
  128. */
  129. /*********************************************************************//**
  130. * @brief Initial for Watchdog function
  131. * Clock source = RTC ,
  132. * @param[in] ClkSrc Select clock source, should be:
  133. * - WDT_CLKSRC_IRC: Clock source from Internal RC oscillator
  134. * - WDT_CLKSRC_PCLK: Selects the APB peripheral clock (PCLK)
  135. * - WDT_CLKSRC_RTC: Selects the RTC oscillator
  136. * @param[in] WDTMode WDT mode, should be:
  137. * - WDT_MODE_INT_ONLY: Use WDT to generate interrupt only
  138. * - WDT_MODE_RESET: Use WDT to generate interrupt and reset MCU
  139. * @return None
  140. **********************************************************************/
  141. void WDT_Init (WDT_CLK_OPT ClkSrc, WDT_MODE_OPT WDTMode)
  142. {
  143. CHECK_PARAM(PARAM_WDT_CLK_OPT(ClkSrc));
  144. CHECK_PARAM(PARAM_WDT_MODE_OPT(WDTMode));
  145. CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
  146. //Set clock source
  147. LPC_WDT->WDCLKSEL &= ~WDT_WDCLKSEL_MASK;
  148. LPC_WDT->WDCLKSEL |= ClkSrc;
  149. //Set WDT mode
  150. if (WDTMode == WDT_MODE_RESET){
  151. LPC_WDT->WDMOD |= WDT_WDMOD(WDTMode);
  152. }
  153. }
  154. /*********************************************************************//**
  155. * @brief Start WDT activity with given timeout value
  156. * @param[in] TimeOut WDT reset after timeout if it is not feed
  157. * @return None
  158. **********************************************************************/
  159. void WDT_Start(uint32_t TimeOut)
  160. {
  161. uint32_t ClkSrc;
  162. ClkSrc = LPC_WDT->WDCLKSEL;
  163. ClkSrc &=WDT_WDCLKSEL_MASK;
  164. WDT_SetTimeOut(ClkSrc,TimeOut);
  165. //enable watchdog
  166. LPC_WDT->WDMOD |= WDT_WDMOD_WDEN;
  167. WDT_Feed();
  168. }
  169. /********************************************************************//**
  170. * @brief Read WDT Time out flag
  171. * @param[in] None
  172. * @return Time out flag status of WDT
  173. *********************************************************************/
  174. FlagStatus WDT_ReadTimeOutFlag (void)
  175. {
  176. return ((FlagStatus)((LPC_WDT->WDMOD & WDT_WDMOD_WDTOF) >>2));
  177. }
  178. /********************************************************************//**
  179. * @brief Clear WDT Time out flag
  180. * @param[in] None
  181. * @return None
  182. *********************************************************************/
  183. void WDT_ClrTimeOutFlag (void)
  184. {
  185. LPC_WDT->WDMOD &=~WDT_WDMOD_WDTOF;
  186. }
  187. /********************************************************************//**
  188. * @brief Update WDT timeout value and feed
  189. * @param[in] TimeOut TimeOut value to be updated
  190. * @return None
  191. *********************************************************************/
  192. void WDT_UpdateTimeOut ( uint32_t TimeOut)
  193. {
  194. uint32_t ClkSrc;
  195. ClkSrc = LPC_WDT->WDCLKSEL;
  196. ClkSrc &=WDT_WDCLKSEL_MASK;
  197. WDT_SetTimeOut(ClkSrc,TimeOut);
  198. WDT_Feed();
  199. }
  200. /********************************************************************//**
  201. * @brief After set WDTEN, call this function to start Watchdog
  202. * or reload the Watchdog timer
  203. * @param[in] None
  204. *
  205. * @return None
  206. *********************************************************************/
  207. void WDT_Feed (void)
  208. {
  209. // Disable irq interrupt
  210. __disable_irq();
  211. LPC_WDT->WDFEED = 0xAA;
  212. LPC_WDT->WDFEED = 0x55;
  213. // Then enable irq interrupt
  214. __enable_irq();
  215. }
  216. /********************************************************************//**
  217. * @brief Get the current value of WDT
  218. * @param[in] None
  219. * @return current value of WDT
  220. *********************************************************************/
  221. uint32_t WDT_GetCurrentCount(void)
  222. {
  223. return LPC_WDT->WDTV;
  224. }
  225. /**
  226. * @}
  227. */
  228. #endif /* _WDT */
  229. /**
  230. * @}
  231. */
  232. /* --------------------------------- End Of File ------------------------------ */