drv_wdt.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-9 fanwenl 1st version
  9. */
  10. #include <rtdevice.h>
  11. #include "wm_watchdog.h"
  12. #ifdef BSP_USING_WDT
  13. static rt_err_t wm_wdg_init(rt_watchdog_t *wdt)
  14. {
  15. /*init for 10S*/
  16. tls_watchdog_init(1000000);
  17. tls_watchdog_stop();
  18. return RT_EOK;
  19. }
  20. static rt_err_t wm_wdg_control(rt_watchdog_t *wdt, int cmd, void *arg)
  21. {
  22. uint64_t timeout_us = 0;
  23. switch (cmd)
  24. {
  25. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  26. timeout_us = *((rt_uint32_t *)arg) * 1000000;
  27. if (timeout_us >= 0xFFFFFFFF)
  28. timeout_us = 0xFFFFFFFF;
  29. tls_watchdog_set_timeout((rt_uint32_t)timeout_us);
  30. break;
  31. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  32. timeout_us = tls_watchdog_get_timeout();
  33. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  34. break;
  35. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  36. timeout_us = tls_watchdog_get_timeleft();
  37. *((rt_uint32_t *)arg) = timeout_us / 1000000;
  38. break;
  39. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  40. tls_watchdog_clr();
  41. break;
  42. case RT_DEVICE_CTRL_WDT_START:
  43. tls_watchdog_start();
  44. break;
  45. case RT_DEVICE_CTRL_WDT_STOP:
  46. tls_watchdog_stop();
  47. break;
  48. default:
  49. return RT_EIO;
  50. }
  51. return RT_EOK;
  52. }
  53. static const struct rt_watchdog_ops wm_wdg_pos =
  54. {
  55. wm_wdg_init,
  56. wm_wdg_control,
  57. };
  58. static rt_watchdog_t wm_wdg;
  59. int wm_hw_wdg_init(void)
  60. {
  61. wm_wdg.ops = &wm_wdg_pos;
  62. rt_hw_watchdog_register(&wm_wdg, "wdg", 0, RT_NULL);
  63. return RT_EOK;
  64. }
  65. INIT_DEVICE_EXPORT(wm_hw_wdg_init);
  66. #endif /*BSP_USING_WDT */