drv_wdt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. * 2022-07-21 Rbb666 first version
  9. */
  10. #include "drv_wdt.h"
  11. #ifdef RT_USING_WDT
  12. //#define DRV_DEBUG
  13. #define LOG_TAG "drv.wdt"
  14. #include <drv_log.h>
  15. cyhal_wdt_t WDT;
  16. static struct ifx_wdt_cfg wdt_cfg =
  17. {
  18. .name = "wdt",
  19. .WDTx = &WDT,
  20. };
  21. static struct ifx_wdt wdt_drv;
  22. static rt_err_t wdt_init(rt_watchdog_t *wdt)
  23. {
  24. return RT_EOK;
  25. }
  26. static rt_err_t wdt_control(rt_watchdog_t *wdt_device, int cmd, void *arg)
  27. {
  28. RT_ASSERT(wdt_device != RT_NULL);
  29. struct ifx_wdt_cfg *cfg;
  30. cfg = wdt_device->parent.user_data;
  31. rt_uint32_t timeout_ms = 0;
  32. switch (cmd)
  33. {
  34. /* feed the watchdog */
  35. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  36. cyhal_wdt_kick(cfg->WDTx);
  37. break;
  38. /* set watchdog timeout */
  39. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  40. {
  41. timeout_ms = *((rt_uint32_t *)arg) * 1000;
  42. rt_uint32_t max_timeout_ms = cyhal_wdt_get_max_timeout_ms();
  43. if (timeout_ms >= max_timeout_ms)
  44. timeout_ms = max_timeout_ms;
  45. /* Initialize the WDT */
  46. int result = cyhal_wdt_init(cfg->WDTx, (rt_uint32_t)timeout_ms);
  47. /* WDT initialization failed. Stop program execution */
  48. RT_ASSERT(result == RT_EOK);
  49. }
  50. break;
  51. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  52. timeout_ms = cyhal_wdt_get_timeout_ms(cfg->WDTx);
  53. *(rt_uint32_t *)arg = timeout_ms / 1000;
  54. break;
  55. case RT_DEVICE_CTRL_WDT_START:
  56. cyhal_wdt_start(cfg->WDTx);
  57. break;
  58. case RT_DEVICE_CTRL_WDT_STOP:
  59. cyhal_wdt_stop(cfg->WDTx);
  60. break;
  61. default:
  62. LOG_W("This command is not supported.");
  63. return -RT_ERROR;
  64. }
  65. return RT_EOK;
  66. }
  67. const static struct rt_watchdog_ops ifx_wdt_ops =
  68. {
  69. wdt_init,
  70. wdt_control
  71. };
  72. int rt_hw_wdt_init(void)
  73. {
  74. wdt_drv.cfg = &wdt_cfg;
  75. wdt_drv.wdt_device.ops = &ifx_wdt_ops;
  76. if (rt_hw_watchdog_register(&wdt_drv.wdt_device, wdt_drv.cfg->name, RT_DEVICE_FLAG_RDWR, wdt_drv.cfg) != RT_EOK)
  77. {
  78. LOG_E("wdt device register failed.");
  79. return -RT_ERROR;
  80. }
  81. LOG_D("wdt device register success.");
  82. return RT_EOK;
  83. }
  84. INIT_BOARD_EXPORT(rt_hw_wdt_init);
  85. #endif /* RT_USING_WDT */