drv_wdt.c 6.0 KB

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