test_wdt.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. uassert_not_null(wdg_dev);
  63. /* Initialize the device */
  64. ret = rt_device_init(wdg_dev);
  65. uassert_int_equal(ret, RT_EOK);
  66. /* Set the watchdog timeout time */
  67. ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  68. uassert_int_equal(ret, RT_EOK);
  69. flag_feed = 1; /* Set feed the dog sign */
  70. /* start watchdog timer */
  71. rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
  72. LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
  73. /* Delay enough time to see if the system will be restarted by the watchdog */
  74. waiting_for_timeout(10);
  75. LOG_I("Thanks for feeding me, I'm still alive!\n");
  76. /* If you feed the dog successfully, you will have a chance to come here and close the gate dog. */
  77. rt_device_close(wdg_dev);
  78. LOG_I("Watchdog is closed!\n");
  79. LOG_I("Test Done with Feed!\n");
  80. return;
  81. }
  82. static void test_wdt_nofeed(void)
  83. {
  84. rt_err_t ret = RT_EOK;
  85. /* Find the watchdog device according to the name and obtain the handle */
  86. wdg_dev = rt_device_find(IWDG_DEVICE_NAME);
  87. uassert_not_null(wdg_dev);
  88. /* Initialize the device */
  89. ret = rt_device_init(wdg_dev);
  90. uassert_int_equal(ret, RT_EOK);
  91. /* Set the watchdog timeout time */
  92. ret = rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
  93. uassert_int_equal(ret, RT_EOK);
  94. flag_feed = 0; /* Do not feed the dog */
  95. rt_device_control(wdg_dev, KD_DEVICE_CTRL_WDT_START, RT_NULL);
  96. LOG_I("Watchdog Timer [%s] is launched!\n", IWDG_DEVICE_NAME);
  97. /* Delay long enough and the system should reboot due to watchdog timeout. */
  98. LOG_I("Oops, I am so hungary and will be killed in seconds!\n");
  99. waiting_for_timeout(10);
  100. LOG_I("SHOULD NOT SEE THIS PRINT!\n");
  101. return;
  102. }
  103. static void test_wdt (void)
  104. {
  105. UTEST_UNIT_RUN(test_wdt_feed);
  106. UTEST_UNIT_RUN(test_wdt_nofeed);
  107. }
  108. static rt_err_t utest_init(void)
  109. {
  110. flag_feed = 0;
  111. rt_thread_idle_sethook(idle_hook);
  112. return RT_EOK;
  113. }
  114. UTEST_TC_EXPORT(test_wdt, "wdt", utest_init, NULL, 10);