test_wdt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*
  7. * Refer to https://www.rt-thread.org/document/api/iwdg_sample_8c-example.html
  8. * This is a watchdog device test routine, the program searches for the watchdog
  9. * device by device name, then initializes the device and sets the watchdog
  10. * device overflow time.
  11. *
  12. * "test_wdt_feed" tests that the watchdog timer will not time out when the
  13. * watchdog is fed and will not cause the system to restart.
  14. *
  15. * "test_wdt_nofeed" tests that the watchdog timer will time out when the
  16. * watchdog is not fed and cause the system to restart.
  17. */
  18. #include <rtthread.h>
  19. #include <rtdevice.h>
  20. #include "../interdrv/wdt/drv_wdt.h"
  21. #include "utest.h"
  22. /* Default watchdog device name */
  23. /* If WDT0 and WDT1 both enabled, we use WDT0 */
  24. #ifdef BSP_USING_WDT0
  25. #define IWDG_DEVICE_NAME "wdt0"
  26. #elif defined(BSP_USING_WDT1)
  27. #define IWDG_DEVICE_NAME "wdt1"
  28. #else
  29. #error "No watchdog device defined!"
  30. #endif
  31. /* Watchdog device handle */
  32. static rt_device_t wdg_dev;
  33. /*
  34. * Dog feeding flag, 1 means feeding the dog, 0 means not feeding the dog.
  35. * This flag is used in the idle thread callback function.
  36. * If the dog is not fed, the system will restart due to watchdog timeout.
  37. */
  38. static int flag_feed;
  39. /* Overflow time, in seconds. */
  40. static rt_uint32_t timeout = 3;
  41. static void idle_hook(void)
  42. {
  43. /* Feed the dog in the callback function of the idle thread */
  44. if (flag_feed)
  45. rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
  46. /* Do not print, otherwise the screen will display too much. */
  47. }
  48. static void waiting_for_timeout(int seconds)
  49. {
  50. LOG_I("Waiting for watchdog timer time-out...\n");
  51. for (int i = 0; i < seconds; i++)
  52. {
  53. rt_thread_mdelay(1000);
  54. LOG_I(".");
  55. }
  56. }
  57. static void test_wdt_feed(void)
  58. {
  59. rt_err_t ret = RT_EOK;
  60. /* Find the watchdog device according to the name and obtain the handle */
  61. wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
  62. if (!wdg_dev)
  63. {
  64. LOG_E("find %s failed!\n", IWDG_DEVICE_NAME);
  65. return;
  66. }
  67. /* Initialize the device */
  68. ret = rt_device_init(wdg_dev);
  69. if (ret != RT_EOK)
  70. {
  71. LOG_E("initialize %s failed!\n", IWDG_DEVICE_NAME);
  72. return;
  73. }
  74. /* Set the watchdog timeout time */
  75. ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  76. if (ret != RT_EOK)
  77. {
  78. LOG_E("set %s timeout failed!\n", IWDG_DEVICE_NAME);
  79. return;
  80. }
  81. flag_feed = 1; /* Set feed the dog sign */
  82. /* start watchdog timer */
  83. rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
  84. LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
  85. /* Delay enough time to see if the system will be restarted by the watchdog */
  86. waiting_for_timeout(10);
  87. LOG_I("Thanks for feeding me, I'm still alive!\n");
  88. /* If you feed the dog successfully, you will have a chance to come here and close the gate dog. */
  89. rt_device_close(wdg_dev);
  90. LOG_I("Watchdog is closed!\n");
  91. LOG_I("Test Done with Feed!\n");
  92. return;
  93. }
  94. static void test_wdt_nofeed(void)
  95. {
  96. rt_err_t ret = RT_EOK;
  97. /* Find the watchdog device according to the name and obtain the handle */
  98. wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
  99. if (!wdg_dev)
  100. {
  101. LOG_E("find %s failed!\n", IWDG_DEVICE_NAME);
  102. return;
  103. }
  104. /* Initialize the device */
  105. ret = rt_device_init(wdg_dev);
  106. if (ret != RT_EOK)
  107. {
  108. LOG_E("initialize %s failed!\n", IWDG_DEVICE_NAME);
  109. return;
  110. }
  111. /* Set the watchdog timeout time */
  112. ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  113. if (ret != RT_EOK)
  114. {
  115. LOG_E("set %s timeout failed!\n", IWDG_DEVICE_NAME);
  116. return;
  117. }
  118. flag_feed = 0; /* Do not feed the dog */
  119. rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
  120. LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
  121. /* Delay long enough and the system should reboot due to watchdog timeout. */
  122. LOG_I("Oops, I am so hungary and will be killed in seconds!\n");
  123. waiting_for_timeout(10);
  124. LOG_I("SHOULD NOT SEE THIS PRINT!\n");
  125. return;
  126. }
  127. static void test_wdt (void)
  128. {
  129. UTEST_UNIT_RUN(test_wdt_feed);
  130. UTEST_UNIT_RUN(test_wdt_nofeed);
  131. }
  132. static rt_err_t utest_init(void)
  133. {
  134. flag_feed = 0;
  135. rt_thread_idle_sethook(idle_hook);
  136. return RT_EOK;
  137. }
  138. UTEST_TC_EXPORT(test_wdt, "wdt", utest_init, NULL, 10);