task_watchdog_example_main.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 <stdio.h>
  8. #include <stdlib.h>
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/task.h"
  11. #include "esp_task_wdt.h"
  12. #define TWDT_TIMEOUT_S 3
  13. #define TASK_RESET_PERIOD_S 2
  14. /*
  15. * Macro to check the outputs of TWDT functions and trigger an abort if an
  16. * incorrect code is returned.
  17. */
  18. #define CHECK_ERROR_CODE(returned, expected) ({ \
  19. if(returned != expected){ \
  20. printf("TWDT ERROR\n"); \
  21. abort(); \
  22. } \
  23. })
  24. static TaskHandle_t task_handles[portNUM_PROCESSORS];
  25. //Callback for user tasks created in app_main()
  26. void reset_task(void *arg)
  27. {
  28. //Subscribe this task to TWDT, then check if it is subscribed
  29. CHECK_ERROR_CODE(esp_task_wdt_add(NULL), ESP_OK);
  30. CHECK_ERROR_CODE(esp_task_wdt_status(NULL), ESP_OK);
  31. while(1){
  32. //reset the watchdog every 2 seconds
  33. CHECK_ERROR_CODE(esp_task_wdt_reset(), ESP_OK); //Comment this line to trigger a TWDT timeout
  34. vTaskDelay(pdMS_TO_TICKS(TASK_RESET_PERIOD_S * 1000));
  35. }
  36. }
  37. void app_main(void)
  38. {
  39. printf("Initialize TWDT\n");
  40. //Initialize or reinitialize TWDT
  41. CHECK_ERROR_CODE(esp_task_wdt_init(TWDT_TIMEOUT_S, false), ESP_OK);
  42. //Subscribe Idle Tasks to TWDT if they were not subscribed at startup
  43. #ifndef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
  44. esp_task_wdt_add(xTaskGetIdleTaskHandleForCPU(0));
  45. #endif
  46. #if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 && !CONFIG_FREERTOS_UNICORE
  47. esp_task_wdt_add(xTaskGetIdleTaskHandleForCPU(1));
  48. #endif
  49. //Create user tasks and add them to watchdog
  50. for(int i = 0; i < portNUM_PROCESSORS; i++){
  51. xTaskCreatePinnedToCore(reset_task, "reset task", 1024, NULL, 10, &task_handles[i], i);
  52. }
  53. printf("Delay for 10 seconds\n");
  54. vTaskDelay(pdMS_TO_TICKS(10000)); //Delay for 10 seconds
  55. printf("Unsubscribing and deleting tasks\n");
  56. //Delete and unsubscribe Users Tasks from Task Watchdog, then unsubscribe idle task
  57. for(int i = 0; i < portNUM_PROCESSORS; i++){
  58. vTaskDelete(task_handles[i]); //Delete user task first (prevents the resetting of an unsubscribed task)
  59. CHECK_ERROR_CODE(esp_task_wdt_delete(task_handles[i]), ESP_OK); //Unsubscribe task from TWDT
  60. CHECK_ERROR_CODE(esp_task_wdt_status(task_handles[i]), ESP_ERR_NOT_FOUND); //Confirm task is unsubscribed
  61. //unsubscribe idle task
  62. CHECK_ERROR_CODE(esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(i)), ESP_OK); //Unsubscribe Idle Task from TWDT
  63. CHECK_ERROR_CODE(esp_task_wdt_status(xTaskGetIdleTaskHandleForCPU(i)), ESP_ERR_NOT_FOUND); //Confirm Idle task has unsubscribed
  64. }
  65. //Deinit TWDT after all tasks have unsubscribed
  66. CHECK_ERROR_CODE(esp_task_wdt_deinit(), ESP_OK);
  67. CHECK_ERROR_CODE(esp_task_wdt_status(NULL), ESP_ERR_INVALID_STATE); //Confirm TWDT has been deinitialized
  68. printf("Complete\n");
  69. }