drv_wdt.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-18 ylz0923 first version
  9. */
  10. #include <board.h>
  11. #include <rtdevice.h>
  12. #include <nrfx_wdt.h>
  13. #ifdef RT_USING_WDT
  14. struct nrf5x_wdt_obj
  15. {
  16. rt_watchdog_t watchdog;
  17. nrfx_wdt_t wdt;
  18. nrfx_wdt_channel_id ch;
  19. rt_uint16_t is_start;
  20. };
  21. static struct nrf5x_wdt_obj nrf5x_wdt = {
  22. .wdt = NRFX_WDT_INSTANCE(0),
  23. };
  24. static nrfx_wdt_config_t nrf5x_wdt_cfg = NRFX_WDT_DEFAULT_CONFIG;
  25. static struct rt_watchdog_ops ops;
  26. static rt_err_t wdt_init(rt_watchdog_t *wdt)
  27. {
  28. return RT_EOK;
  29. }
  30. static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
  31. {
  32. switch (cmd)
  33. {
  34. /* feed the watchdog */
  35. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  36. nrfx_wdt_feed(&nrf5x_wdt.wdt);
  37. break;
  38. /* set watchdog timeout */
  39. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  40. nrf5x_wdt_cfg.reload_value = (rt_uint32_t)arg * 1000;
  41. break;
  42. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  43. *((rt_uint32_t*)arg) = nrf5x_wdt_cfg.reload_value;
  44. break;
  45. case RT_DEVICE_CTRL_WDT_START:
  46. if (nrf5x_wdt.is_start != 1)
  47. {
  48. nrfx_wdt_init(&nrf5x_wdt.wdt, &nrf5x_wdt_cfg, RT_NULL);
  49. nrfx_wdt_channel_alloc(&nrf5x_wdt.wdt, &nrf5x_wdt.ch);
  50. nrfx_wdt_enable(&nrf5x_wdt.wdt);
  51. nrf5x_wdt.is_start = 1;
  52. }
  53. break;
  54. default:
  55. return -RT_ERROR;
  56. }
  57. return RT_EOK;
  58. }
  59. int rt_wdt_init(void)
  60. {
  61. nrf5x_wdt.is_start = 0;
  62. ops.init = &wdt_init;
  63. ops.control = &wdt_control;
  64. nrf5x_wdt.watchdog.ops = &ops;
  65. /* register watchdog device */
  66. if (rt_hw_watchdog_register(&nrf5x_wdt.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
  67. {
  68. return -RT_ERROR;
  69. }
  70. return RT_EOK;
  71. }
  72. INIT_BOARD_EXPORT(rt_wdt_init);
  73. static int wdt_sample(int argc, char *argv[])
  74. {
  75. rt_device_t wdt = rt_device_find("wdt");
  76. rt_device_init(wdt);
  77. rt_device_control(wdt, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, (void *)2);
  78. rt_device_control(wdt, RT_DEVICE_CTRL_WDT_START, RT_NULL);
  79. }
  80. MSH_CMD_EXPORT(wdt_sample, wdt sample);
  81. #endif /* RT_USING_WDT */