1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "freertos/FreeRTOS.h"
18 #include "freertos/task.h"
19 #include "esp_err.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 /**
26   * @brief  Initialize the Task Watchdog Timer (TWDT)
27   *
28   * This function configures and initializes the TWDT. If the TWDT is already
29   * initialized when this function is called, this function will update the
30   * TWDT's timeout period and panic configurations instead. After initializing
31   * the TWDT, any task can elect to be watched by the TWDT by subscribing to it
32   * using esp_task_wdt_add().
33   *
34   * @param[in]  timeout     Timeout period of TWDT in seconds
35   * @param[in]  panic       Flag that controls whether the panic handler will be
36   *                         executed when the TWDT times out
37   *
38   * @return
39   *     - ESP_OK: Initialization was successful
40   *     - ESP_ERR_NO_MEM: Initialization failed due to lack of memory
41   *
42   * @note   esp_task_wdt_init() must only be called after the scheduler
43   *         started
44   */
45 esp_err_t esp_task_wdt_init(uint32_t timeout, bool panic);
46 
47 /**
48  * @brief   Deinitialize the Task Watchdog Timer (TWDT)
49  *
50  * This function will deinitialize the TWDT. Calling this function whilst tasks
51  * are still subscribed to the TWDT, or when the TWDT is already deinitialized,
52  * will result in an error code being returned.
53  *
54  * @return
55  *      - ESP_OK: TWDT successfully deinitialized
56  *      - ESP_ERR_INVALID_STATE: Error, tasks are still subscribed to the TWDT
57  *      - ESP_ERR_NOT_FOUND: Error, TWDT has already been deinitialized
58  */
59 esp_err_t esp_task_wdt_deinit(void);
60 
61 /**
62   * @brief  Subscribe a task to the Task Watchdog Timer (TWDT)
63   *
64   * This function subscribes a task to the TWDT. Each subscribed task must
65   * periodically call esp_task_wdt_reset() to prevent the TWDT from elapsing its
66   * timeout period. Failure to do so will result in a TWDT timeout. If the task
67   * being subscribed is one of the Idle Tasks, this function will automatically
68   * enable esp_task_wdt_reset() to called from the Idle Hook of the Idle Task.
69   * Calling this function whilst the TWDT is uninitialized or attempting to
70   * subscribe an already subscribed task will result in an error code being
71   * returned.
72   *
73   * @param[in]  handle  Handle of the task. Input NULL to subscribe the current
74   *                     running task to the TWDT
75   *
76   * @return
77   *     - ESP_OK: Successfully subscribed the task to the TWDT
78   *     - ESP_ERR_INVALID_ARG: Error, the task is already subscribed
79   *     - ESP_ERR_NO_MEM: Error, could not subscribe the task due to lack of
80   *                       memory
81   *     - ESP_ERR_INVALID_STATE: Error, the TWDT has not been initialized yet
82   */
83 esp_err_t esp_task_wdt_add(TaskHandle_t handle);
84 
85 /**
86   * @brief  Reset the Task Watchdog Timer (TWDT) on behalf of the currently
87   *         running task
88   *
89   * This function will reset the TWDT on behalf of the currently running task.
90   * Each subscribed task must periodically call this function to prevent the
91   * TWDT from timing out. If one or more subscribed tasks fail to reset the
92   * TWDT on their own behalf, a TWDT timeout will occur. If the IDLE tasks have
93   * been subscribed to the TWDT, they will automatically call this function from
94   * their idle hooks. Calling this function from a task that has not subscribed
95   * to the TWDT, or when the TWDT is uninitialized will result in an error code
96   * being returned.
97   *
98   * @return
99   *     - ESP_OK: Successfully reset the TWDT on behalf of the currently
100   *               running task
101   *     - ESP_ERR_NOT_FOUND: Error, the current running task has not subscribed
102   *                          to the TWDT
103   *     - ESP_ERR_INVALID_STATE: Error, the TWDT has not been initialized yet
104   */
105 esp_err_t esp_task_wdt_reset(void);
106 
107 /**
108   * @brief  Unsubscribes a task from the Task Watchdog Timer (TWDT)
109   *
110   * This function will unsubscribe a task from the TWDT. After being
111   * unsubscribed, the task should no longer call esp_task_wdt_reset(). If the
112   * task is an IDLE task, this function will automatically disable the calling
113   * of esp_task_wdt_reset() from the Idle Hook. Calling this function whilst the
114   * TWDT is uninitialized or attempting to unsubscribe an already unsubscribed
115   * task from the TWDT will result in an error code being returned.
116   *
117   * @param[in]  handle  Handle of the task. Input NULL to unsubscribe the
118   *                     current running task.
119   *
120   * @return
121   *     - ESP_OK: Successfully unsubscribed the task from the TWDT
122   *     - ESP_ERR_INVALID_ARG: Error, the task is already unsubscribed
123   *     - ESP_ERR_INVALID_STATE: Error, the TWDT has not been initialized yet
124   */
125 esp_err_t esp_task_wdt_delete(TaskHandle_t handle);
126 
127 /**
128   * @brief   Query whether a task is subscribed to the Task Watchdog Timer (TWDT)
129   *
130   * This function will query whether a task is currently subscribed to the TWDT,
131   * or whether the TWDT is initialized.
132   *
133   * @param[in]  handle  Handle of the task. Input NULL to query the current
134   *                     running task.
135   *
136   * @return:
137   *     - ESP_OK: The task is currently subscribed to the TWDT
138   *     - ESP_ERR_NOT_FOUND: The task is currently not subscribed to the TWDT
139   *     - ESP_ERR_INVALID_STATE: The TWDT is not initialized, therefore no tasks
140   *                              can be subscribed
141   */
142 esp_err_t esp_task_wdt_status(TaskHandle_t handle);
143 
144 
145 #ifdef __cplusplus
146 }
147 #endif
148