drv_wdt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-20 tyustli the first version.
  9. * 2019-07-15 Magicoe The first version for LPC55S6x, timeout unit is S not mS
  10. *
  11. */
  12. #include <rtthread.h>
  13. #ifdef BSP_USING_WDT
  14. #if !defined(BSP_USING_WDT)
  15. #error "Please define at least one BSP_USING_WDOGx"
  16. #endif
  17. #define LOG_TAG "drv.wdt"
  18. #include <drv_log.h>
  19. #include "drv_wdt.h"
  20. #include <rthw.h>
  21. #include "rtdevice.h"
  22. #if defined(BSP_USING_WDT)
  23. #include "fsl_wwdt.h"
  24. struct lpc_wdt_obj
  25. {
  26. rt_watchdog_t watchdog;
  27. rt_uint16_t is_start;
  28. };
  29. static struct lpc_wdt_obj lpc_wdt;
  30. static wwdt_config_t WWDT1_config =
  31. {
  32. /* Enable the watch dog */
  33. .enableWwdt = true,
  34. /* Disable the watchdog timeout reset */
  35. .enableWatchdogReset = false,
  36. /* Disable the watchdog protection for updating the timeout value */
  37. .enableWatchdogProtect = false,
  38. /* Windowing is not in effect */
  39. .windowValue = 0xFFFFFFU,
  40. /* Set the timeout value to the max */
  41. .timeoutValue = 0xFFFFFFU,
  42. /* No warning is provided */
  43. .warningValue = 0,
  44. /* Set clock frequency. */
  45. .clockFreq_Hz = 0U,
  46. };
  47. void WDT_BOD_IRQHandler(void)
  48. {
  49. uint32_t wdtStatus = WWDT_GetStatusFlags(WWDT);
  50. /* The chip will reset before this happens */
  51. if (wdtStatus & kWWDT_TimeoutFlag)
  52. {
  53. /* A watchdog feed didn't occur prior to window timeout */
  54. /* Stop WDT */
  55. WWDT_Disable(WWDT);
  56. WWDT_ClearStatusFlags(WWDT, kWWDT_TimeoutFlag);
  57. /* Needs restart */
  58. WWDT_Enable(WWDT);
  59. }
  60. /* Handle warning interrupt */
  61. if (wdtStatus & kWWDT_WarningFlag)
  62. {
  63. /* A watchdog feed didn't occur prior to warning timeout */
  64. WWDT_ClearStatusFlags(WWDT, kWWDT_WarningFlag);
  65. /* User code. User can do urgent case before timeout reset.
  66. * IE. user can backup the ram data or ram log to flash.
  67. * the period is set by config.warningValue, user need to
  68. * check the period between warning interrupt and timeout.
  69. */
  70. }
  71. }
  72. static rt_err_t lpc_wwdt_close(rt_watchdog_t *wdt)
  73. {
  74. rt_uint32_t level;
  75. WWDT_Type *base;
  76. base = (WWDT_Type *)wdt->parent.user_data;
  77. level = rt_hw_interrupt_disable();
  78. WWDT_Disable(base);
  79. rt_hw_interrupt_enable(level);
  80. return RT_EOK;
  81. }
  82. static rt_err_t lpc_wwdt_open(rt_watchdog_t *wdt, rt_uint16_t oflag)
  83. {
  84. WWDT_Type *base;
  85. base = (WWDT_Type *)wdt->parent.user_data;
  86. rt_uint32_t level;
  87. level = rt_hw_interrupt_disable();
  88. WWDT_Enable(base);
  89. rt_hw_interrupt_enable(level);
  90. return RT_EOK;
  91. }
  92. static rt_err_t lpc_wwdt_init(rt_watchdog_t *wdt)
  93. {
  94. WWDT_Type *base;
  95. base = (WWDT_Type *)wdt->parent.user_data;
  96. /* Enable FRO 1M clock for WWDT module. */
  97. SYSCON->CLOCK_CTRL |= SYSCON_CLOCK_CTRL_FRO1MHZ_CLK_ENA_MASK;
  98. /* Set clock divider for WWDT clock source. */
  99. CLOCK_SetClkDiv(kCLOCK_DivWdtClk, 1U, true);
  100. WWDT_GetDefaultConfig(&WWDT1_config);
  101. /*
  102. * Set watchdog feed time constant to approximately 4s
  103. * Set watchdog warning time to 512 ticks after feed time constant
  104. * Set watchdog window time to 1s
  105. */
  106. /* The WDT divides the input frequency into it by 4 */
  107. WWDT1_config.timeoutValue = (CLOCK_GetWdtClkFreq() / 4) * 4;
  108. WWDT1_config.warningValue = 512;
  109. WWDT1_config.windowValue = (CLOCK_GetWdtClkFreq() / 4) * 1;
  110. /* Configure WWDT to reset on timeout */
  111. WWDT1_config.enableWatchdogReset = true;
  112. /* Setup watchdog clock frequency(Hz). */
  113. WWDT1_config.clockFreq_Hz = CLOCK_GetWdtClkFreq();
  114. WWDT_Init(base, &WWDT1_config);
  115. lpc_wwdt_close(wdt);
  116. return RT_EOK;
  117. }
  118. static rt_err_t lpc_wwdt_refresh(rt_watchdog_t *wdt)
  119. {
  120. WWDT_Type *base;
  121. base = (WWDT_Type *)wdt->parent.user_data;
  122. rt_uint32_t level;
  123. level = rt_hw_interrupt_disable();
  124. WWDT_Refresh(base);
  125. rt_hw_interrupt_enable(level);
  126. return RT_EOK;
  127. }
  128. /**
  129. * @function control wdog
  130. *
  131. * @param
  132. * wdt whick wdog used
  133. * cmd control wdog options
  134. * args argument of conrtol
  135. * @retval rt_err_t the status of control result
  136. *
  137. * @attention wdog1 is can not get left time(register not exist) and wdogs unit is seconds
  138. *
  139. */
  140. static rt_err_t lpc_wwdt_control(rt_watchdog_t *wdt, int cmd, void *args)
  141. {
  142. RT_ASSERT(wdt != NULL);
  143. WWDT_Type *base;
  144. base = (WWDT_Type *)wdt->parent.user_data;
  145. switch(cmd)
  146. {
  147. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  148. {
  149. *(uint16_t *)args = WWDT1_config.timeoutValue;
  150. }
  151. break;
  152. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  153. {
  154. RT_ASSERT(*(uint16_t *)args != 0);
  155. WWDT1_config.timeoutValue = (CLOCK_GetWdtClkFreq() / 4) * (*(uint16_t *)args) * 2;
  156. WWDT1_config.warningValue = 512;
  157. WWDT1_config.windowValue = (CLOCK_GetWdtClkFreq() / 4) * (*(uint16_t *)args) * 2 / 4;
  158. base->TC = WWDT_TC_COUNT(WWDT1_config.timeoutValue);
  159. base->WINDOW = WWDT_WINDOW_WINDOW(WWDT1_config.windowValue);
  160. base->WARNINT = WWDT_WARNINT_WARNINT(WWDT1_config.warningValue);
  161. WWDT_Refresh(base);
  162. lpc_wwdt_close(wdt);
  163. }
  164. break;
  165. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  166. {
  167. lpc_wwdt_refresh(wdt);
  168. }
  169. break;
  170. case RT_DEVICE_CTRL_WDT_START:
  171. {
  172. lpc_wwdt_open(wdt, *(rt_uint32_t *)args);
  173. }
  174. break;
  175. case RT_DEVICE_CTRL_WDT_STOP:
  176. {
  177. lpc_wwdt_close(wdt);
  178. }
  179. break;
  180. default:
  181. return -RT_EINVAL;
  182. }
  183. return RT_EOK;
  184. }
  185. static struct rt_watchdog_ops lpc_wwdt_ops =
  186. {
  187. .init = lpc_wwdt_init,
  188. .control = lpc_wwdt_control,
  189. };
  190. #endif /* BSP_USING_WDT */
  191. int rt_hw_wdt_init(void)
  192. {
  193. rt_err_t ret = RT_EOK;
  194. #if defined (BSP_USING_WDT)
  195. lpc_wdt.watchdog.ops = &lpc_wwdt_ops;
  196. ret = rt_hw_watchdog_register(&lpc_wdt.watchdog, "wdt", RT_DEVICE_FLAG_RDWR, WWDT);
  197. if (ret != RT_EOK)
  198. {
  199. LOG_E("rt device register failed %d", ret);
  200. }
  201. #endif /* BSP_USING_WDT */
  202. return ret;
  203. }
  204. INIT_DEVICE_EXPORT(rt_hw_wdt_init);
  205. #endif /* BSP_USING_WDT */