esp_task_wdt.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include <stdbool.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/task.h"
  10. #include "esp_err.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /**
  15. * @brief Task Watchdog Timer (TWDT) configuration structure
  16. */
  17. typedef struct {
  18. uint32_t timeout_ms; /**< TWDT timeout duration in milliseconds */
  19. uint32_t idle_core_mask; /**< Mask of the cores who's idle task should be subscribed on initialization */
  20. bool trigger_panic; /**< Trigger panic when timeout occurs */
  21. } esp_task_wdt_config_t;
  22. /**
  23. * @brief Task Watchdog Timer (TWDT) user handle
  24. */
  25. typedef struct esp_task_wdt_user_handle_s * esp_task_wdt_user_handle_t;
  26. /**
  27. * @brief Initialize the Task Watchdog Timer (TWDT)
  28. *
  29. * This function configures and initializes the TWDT. If the TWDT is already initialized when this function is called,
  30. * this function will update the TWDT's current configuration. This funciton will also subscribe the idle tasks if
  31. * configured to do so. For other tasks, users can subscribe them using esp_task_wdt_add() or esp_task_wdt_add_user().
  32. *
  33. * @note esp_task_wdt_init() must only be called after the scheduler is started
  34. * @param[in] config Configuration structure
  35. * @return
  36. * - ESP_OK: Initialization was successful
  37. * - Other: Failed to initialize TWDT
  38. */
  39. esp_err_t esp_task_wdt_init(const esp_task_wdt_config_t *config);
  40. /**
  41. * @brief Deinitialize the Task Watchdog Timer (TWDT)
  42. *
  43. * This function will deinitialize the TWDT, and unsubscribe any idle tasks. Calling this function whilst other tasks
  44. * are still subscribed to the TWDT, or when the TWDT is already deinitialized, will result in an error code being
  45. * returned.
  46. *
  47. * @return
  48. * - ESP_OK: TWDT successfully deinitialized
  49. * - Other: Failed to deinitialize TWDT
  50. */
  51. esp_err_t esp_task_wdt_deinit(void);
  52. /**
  53. * @brief Subscribe a task to the Task Watchdog Timer (TWDT)
  54. *
  55. * This function subscribes a task to the TWDT. Each subscribed task must periodically call esp_task_wdt_reset() to
  56. * prevent the TWDT from elapsing its timeout period. Failure to do so will result in a TWDT timeout.
  57. *
  58. * @param task_handle Handle of the task. Input NULL to subscribe the current running task to the TWDT
  59. * @return
  60. * - ESP_OK: Successfully subscribed the task to the TWDT
  61. * - Other: Failed to subscribe task
  62. */
  63. esp_err_t esp_task_wdt_add(TaskHandle_t task_handle);
  64. /**
  65. * @brief Subscribe a user to the Task Watchdog Timer (TWDT)
  66. *
  67. * This function subscribes a user to the TWDT. A user of the TWDT is usually a function that needs to run
  68. * periodically. Each subscribed user must periodically call esp_task_wdt_reset_user() to prevent the TWDT from elapsing
  69. * its timeout period. Failure to do so will result in a TWDT timeout.
  70. *
  71. * @param[in] user_name String to identify the user
  72. * @param[out] user_handle_ret Handle of the user
  73. * @return
  74. * - ESP_OK: Successfully subscribed the user to the TWDT
  75. * - Other: Failed to subscribe user
  76. */
  77. esp_err_t esp_task_wdt_add_user(const char *user_name, esp_task_wdt_user_handle_t *user_handle_ret);
  78. /**
  79. * @brief Reset the Task Watchdog Timer (TWDT) on behalf of the currently running task
  80. *
  81. * This function will reset the TWDT on behalf of the currently running task. Each subscribed task must periodically
  82. * call this function to prevent the TWDT from timing out. If one or more subscribed tasks fail to reset the TWDT on
  83. * their own behalf, a TWDT timeout will occur.
  84. *
  85. * @return
  86. * - ESP_OK: Successfully reset the TWDT on behalf of the currently running task
  87. * - Other: Failed to reset
  88. */
  89. esp_err_t esp_task_wdt_reset(void);
  90. /**
  91. * @brief Reset the Task Watchdog Timer (TWDT) on behalf of a user
  92. *
  93. * This function will reset the TWDT on behalf of a user. Each subscribed user must periodically call this function to
  94. * prevent the TWDT from timing out. If one or more subscribed users fail to reset the TWDT on their own behalf, a TWDT
  95. * timeout will occur.
  96. *
  97. * @param[in] user_handle User handle
  98. * - ESP_OK: Successfully reset the TWDT on behalf of the user
  99. * - Other: Failed to reset
  100. */
  101. esp_err_t esp_task_wdt_reset_user(esp_task_wdt_user_handle_t user_handle);
  102. /**
  103. * @brief Unsubscribes a task from the Task Watchdog Timer (TWDT)
  104. *
  105. * This function will unsubscribe a task from the TWDT. After being unsubscribed, the task should no longer call
  106. * esp_task_wdt_reset().
  107. *
  108. * @param[in] task_handle Handle of the task. Input NULL to unsubscribe the current running task.
  109. * @return
  110. * - ESP_OK: Successfully unsubscribed the task from the TWDT
  111. * - Other: Failed to unsubscribe task
  112. */
  113. esp_err_t esp_task_wdt_delete(TaskHandle_t task_handle);
  114. /**
  115. * @brief Unsubscribes a user from the Task Watchdog Timer (TWDT)
  116. *
  117. * This function will unsubscribe a user from the TWDT. After being unsubscribed, the user should no longer call
  118. * esp_task_wdt_reset_user().
  119. *
  120. * @param[in] user_handle User handle
  121. * @return
  122. * - ESP_OK: Successfully unsubscribed the user from the TWDT
  123. * - Other: Failed to unsubscribe user
  124. */
  125. esp_err_t esp_task_wdt_delete_user(esp_task_wdt_user_handle_t user_handle);
  126. /**
  127. * @brief Query whether a task is subscribed to the Task Watchdog Timer (TWDT)
  128. *
  129. * This function will query whether a task is currently subscribed to the TWDT, or whether the TWDT is initialized.
  130. *
  131. * @param[in] task_handle Handle of the task. Input NULL to query the current running task.
  132. * @return:
  133. * - ESP_OK: The task is currently subscribed to the TWDT
  134. * - ESP_ERR_NOT_FOUND: The task is not subscribed
  135. * - ESP_ERR_INVALID_STATE: TWDT was never initialized
  136. */
  137. esp_err_t esp_task_wdt_status(TaskHandle_t task_handle);
  138. #ifdef __cplusplus
  139. }
  140. #endif