task_watchdog_example_main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Task_Watchdog Example
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "sdkconfig.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_err.h"
  13. #include "esp_task_wdt.h"
  14. #define TWDT_TIMEOUT_MS 3000
  15. #define TASK_RESET_PERIOD_MS 2000
  16. #define MAIN_DELAY_MS 10000
  17. static volatile bool run_loop;
  18. static esp_task_wdt_user_handle_t func_a_twdt_user_hdl;
  19. static esp_task_wdt_user_handle_t func_b_twdt_user_hdl;
  20. static void func_a(void)
  21. {
  22. esp_task_wdt_reset_user(func_a_twdt_user_hdl);
  23. }
  24. static void func_b(void)
  25. {
  26. esp_task_wdt_reset_user(func_b_twdt_user_hdl);
  27. }
  28. void task_func(void *arg)
  29. {
  30. // Subscribe this task to TWDT, then check if it is subscribed
  31. ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
  32. ESP_ERROR_CHECK(esp_task_wdt_status(NULL));
  33. // Subscribe func_a and func_b as users of the the TWDT
  34. ESP_ERROR_CHECK(esp_task_wdt_add_user("func_a", &func_a_twdt_user_hdl));
  35. ESP_ERROR_CHECK(esp_task_wdt_add_user("func_b", &func_b_twdt_user_hdl));
  36. printf("Subscribed to TWDT\n");
  37. while (run_loop) {
  38. // Reset the task and each user periodically
  39. /*
  40. Note: Comment out any one of the calls below to trigger the TWDT
  41. */
  42. esp_task_wdt_reset();
  43. func_a();
  44. func_b();
  45. vTaskDelay(pdMS_TO_TICKS(TASK_RESET_PERIOD_MS));
  46. }
  47. // Unsubscribe this task, func_a, and func_b
  48. ESP_ERROR_CHECK(esp_task_wdt_delete_user(func_a_twdt_user_hdl));
  49. ESP_ERROR_CHECK(esp_task_wdt_delete_user(func_b_twdt_user_hdl));
  50. ESP_ERROR_CHECK(esp_task_wdt_delete(NULL));
  51. printf("Unsubscribed from TWDT\n");
  52. // Notify main task of deletion
  53. xTaskNotifyGive((TaskHandle_t)arg);
  54. vTaskDelete(NULL);
  55. }
  56. void app_main(void)
  57. {
  58. #if !CONFIG_ESP_TASK_WDT
  59. // If the TWDT was not initialized automatically on startup, manually intialize it now
  60. esp_task_wdt_config_t twdt_config = {
  61. .timeout_ms = TWDT_TIMEOUT_MS,
  62. .idle_core_mask = (1 << portNUM_PROCESSORS) - 1, // Bitmask of all cores
  63. .trigger_panic = false,
  64. };
  65. ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
  66. printf("TWDT initialized\n");
  67. #endif // CONFIG_ESP_TASK_WDT
  68. // Create a task
  69. run_loop = true;
  70. xTaskCreatePinnedToCore(task_func, "task", 2048, xTaskGetCurrentTaskHandle(), 10, NULL, 0);
  71. // Let the created task run for a while
  72. printf("Delay for %d seconds\n", MAIN_DELAY_MS/1000);
  73. vTaskDelay(pdMS_TO_TICKS(MAIN_DELAY_MS));
  74. // Stop the created task
  75. run_loop = false;
  76. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  77. #if !CONFIG_ESP_TASK_WDT
  78. // If we manually initialized the TWDT, deintialize it now
  79. ESP_ERROR_CHECK(esp_task_wdt_deinit());
  80. printf("TWDT deinitialized\n");
  81. #endif // CONFIG_ESP_TASK_WDT
  82. printf("Example complete\n");
  83. }