esp_task_wdt.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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; /**< Bitmask of the core whose idle task should be subscribed on initialization where 1 << i means that core i's idle task will be monitored by the TWDT */
  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. This function will subscribe the idle tasks if
  30. * configured to do so. For other tasks, users can subscribe them using esp_task_wdt_add() or esp_task_wdt_add_user().
  31. * This function won't start the timer if no task have been registered yet.
  32. *
  33. * @note esp_task_wdt_init() must only be called after the scheduler is started. Moreover, it must not be called by
  34. * multiple tasks simultaneously.
  35. * @param[in] config Configuration structure
  36. * @return
  37. * - ESP_OK: Initialization was successful
  38. * - ESP_ERR_INVALID_STATE: Already initialized
  39. * - Other: Failed to initialize TWDT
  40. */
  41. esp_err_t esp_task_wdt_init(const esp_task_wdt_config_t *config);
  42. /**
  43. * @brief Reconfigure the Task Watchdog Timer (TWDT)
  44. *
  45. * The function reconfigures the running TWDT. It must already be initialized when this function is called.
  46. *
  47. * @note esp_task_wdt_reconfigure() must not be called by multiple tasks simultaneously.
  48. *
  49. * @param[in] config Configuration structure
  50. *
  51. * @return
  52. * - ESP_OK: Reconfiguring was successful
  53. * - ESP_ERR_INVALID_STATE: TWDT not initialized yet
  54. * - Other: Failed to initialize TWDT
  55. */
  56. esp_err_t esp_task_wdt_reconfigure(const esp_task_wdt_config_t *config);
  57. /**
  58. * @brief Deinitialize the Task Watchdog Timer (TWDT)
  59. *
  60. * This function will deinitialize the TWDT, and unsubscribe any idle tasks. Calling this function whilst other tasks
  61. * are still subscribed to the TWDT, or when the TWDT is already deinitialized, will result in an error code being
  62. * returned.
  63. *
  64. * @note esp_task_wdt_deinit() must not be called by multiple tasks simultaneously.
  65. * @return
  66. * - ESP_OK: TWDT successfully deinitialized
  67. * - Other: Failed to deinitialize TWDT
  68. */
  69. esp_err_t esp_task_wdt_deinit(void);
  70. /**
  71. * @brief Subscribe a task to the Task Watchdog Timer (TWDT)
  72. *
  73. * This function subscribes a task to the TWDT. Each subscribed task must periodically call esp_task_wdt_reset() to
  74. * prevent the TWDT from elapsing its timeout period. Failure to do so will result in a TWDT timeout.
  75. *
  76. * @param task_handle Handle of the task. Input NULL to subscribe the current running task to the TWDT
  77. * @return
  78. * - ESP_OK: Successfully subscribed the task to the TWDT
  79. * - Other: Failed to subscribe task
  80. */
  81. esp_err_t esp_task_wdt_add(TaskHandle_t task_handle);
  82. /**
  83. * @brief Subscribe a user to the Task Watchdog Timer (TWDT)
  84. *
  85. * This function subscribes a user to the TWDT. A user of the TWDT is usually a function that needs to run
  86. * periodically. Each subscribed user must periodically call esp_task_wdt_reset_user() to prevent the TWDT from elapsing
  87. * its timeout period. Failure to do so will result in a TWDT timeout.
  88. *
  89. * @param[in] user_name String to identify the user
  90. * @param[out] user_handle_ret Handle of the user
  91. * @return
  92. * - ESP_OK: Successfully subscribed the user to the TWDT
  93. * - Other: Failed to subscribe user
  94. */
  95. esp_err_t esp_task_wdt_add_user(const char *user_name, esp_task_wdt_user_handle_t *user_handle_ret);
  96. /**
  97. * @brief Reset the Task Watchdog Timer (TWDT) on behalf of the currently running task
  98. *
  99. * This function will reset the TWDT on behalf of the currently running task. Each subscribed task must periodically
  100. * call this function to prevent the TWDT from timing out. If one or more subscribed tasks fail to reset the TWDT on
  101. * their own behalf, a TWDT timeout will occur.
  102. *
  103. * @return
  104. * - ESP_OK: Successfully reset the TWDT on behalf of the currently running task
  105. * - Other: Failed to reset
  106. */
  107. esp_err_t esp_task_wdt_reset(void);
  108. /**
  109. * @brief Reset the Task Watchdog Timer (TWDT) on behalf of a user
  110. *
  111. * This function will reset the TWDT on behalf of a user. Each subscribed user must periodically call this function to
  112. * prevent the TWDT from timing out. If one or more subscribed users fail to reset the TWDT on their own behalf, a TWDT
  113. * timeout will occur.
  114. *
  115. * @param[in] user_handle User handle
  116. * - ESP_OK: Successfully reset the TWDT on behalf of the user
  117. * - Other: Failed to reset
  118. */
  119. esp_err_t esp_task_wdt_reset_user(esp_task_wdt_user_handle_t user_handle);
  120. /**
  121. * @brief Unsubscribes a task from the Task Watchdog Timer (TWDT)
  122. *
  123. * This function will unsubscribe a task from the TWDT. After being unsubscribed, the task should no longer call
  124. * esp_task_wdt_reset().
  125. *
  126. * @param[in] task_handle Handle of the task. Input NULL to unsubscribe the current running task.
  127. * @return
  128. * - ESP_OK: Successfully unsubscribed the task from the TWDT
  129. * - Other: Failed to unsubscribe task
  130. */
  131. esp_err_t esp_task_wdt_delete(TaskHandle_t task_handle);
  132. /**
  133. * @brief Unsubscribes a user from the Task Watchdog Timer (TWDT)
  134. *
  135. * This function will unsubscribe a user from the TWDT. After being unsubscribed, the user should no longer call
  136. * esp_task_wdt_reset_user().
  137. *
  138. * @param[in] user_handle User handle
  139. * @return
  140. * - ESP_OK: Successfully unsubscribed the user from the TWDT
  141. * - Other: Failed to unsubscribe user
  142. */
  143. esp_err_t esp_task_wdt_delete_user(esp_task_wdt_user_handle_t user_handle);
  144. /**
  145. * @brief Query whether a task is subscribed to the Task Watchdog Timer (TWDT)
  146. *
  147. * This function will query whether a task is currently subscribed to the TWDT, or whether the TWDT is initialized.
  148. *
  149. * @param[in] task_handle Handle of the task. Input NULL to query the current running task.
  150. * @return:
  151. * - ESP_OK: The task is currently subscribed to the TWDT
  152. * - ESP_ERR_NOT_FOUND: The task is not subscribed
  153. * - ESP_ERR_INVALID_STATE: TWDT was never initialized
  154. */
  155. esp_err_t esp_task_wdt_status(TaskHandle_t task_handle);
  156. /**
  157. * @brief User ISR callback placeholder
  158. *
  159. * This function is called by task_wdt_isr function (ISR for when TWDT times out). It can be defined in user code to
  160. * handle TWDT events.
  161. *
  162. * @note It has the same limitations as the interrupt function. Do not use ESP_LOGx functions inside.
  163. */
  164. void __attribute__((weak)) esp_task_wdt_isr_user_handler(void);
  165. #ifdef __cplusplus
  166. }
  167. #endif