2017-10-09 06:07:30 -04:00
|
|
|
/* Task_Watchdog Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2022-04-14 06:56:02 -04:00
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
2017-10-09 06:07:30 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-04-14 06:56:02 -04:00
|
|
|
#include "esp_err.h"
|
2017-10-09 06:07:30 -04:00
|
|
|
#include "esp_task_wdt.h"
|
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
#define TWDT_TIMEOUT_MS 3000
|
|
|
|
#define TASK_RESET_PERIOD_MS 2000
|
|
|
|
#define MAIN_DELAY_MS 10000
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
static volatile bool run_loop;
|
|
|
|
static esp_task_wdt_user_handle_t func_a_twdt_user_hdl;
|
|
|
|
static esp_task_wdt_user_handle_t func_b_twdt_user_hdl;
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
static void func_a(void)
|
|
|
|
{
|
|
|
|
esp_task_wdt_reset_user(func_a_twdt_user_hdl);
|
|
|
|
}
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
static void func_b(void)
|
2017-10-09 06:07:30 -04:00
|
|
|
{
|
2022-04-14 06:56:02 -04:00
|
|
|
esp_task_wdt_reset_user(func_b_twdt_user_hdl);
|
2017-10-09 06:07:30 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
void task_func(void *arg)
|
2017-10-09 06:07:30 -04:00
|
|
|
{
|
2022-04-14 06:56:02 -04:00
|
|
|
// Subscribe this task to TWDT, then check if it is subscribed
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_status(NULL));
|
|
|
|
|
|
|
|
// Subscribe func_a and func_b as users of the the TWDT
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_add_user("func_a", &func_a_twdt_user_hdl));
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_add_user("func_b", &func_b_twdt_user_hdl));
|
|
|
|
|
|
|
|
printf("Subscribed to TWDT\n");
|
|
|
|
|
|
|
|
while (run_loop) {
|
|
|
|
// Reset the task and each user periodically
|
|
|
|
/*
|
|
|
|
Note: Comment out any one of the calls below to trigger the TWDT
|
|
|
|
*/
|
|
|
|
esp_task_wdt_reset();
|
|
|
|
func_a();
|
|
|
|
func_b();
|
|
|
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(TASK_RESET_PERIOD_MS));
|
2017-10-09 06:07:30 -04:00
|
|
|
}
|
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
// Unsubscribe this task, func_a, and func_b
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_delete_user(func_a_twdt_user_hdl));
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_delete_user(func_b_twdt_user_hdl));
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_delete(NULL));
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
printf("Unsubscribed from TWDT\n");
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
// Notify main task of deletion
|
|
|
|
xTaskNotifyGive((TaskHandle_t)arg);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
2017-10-09 06:07:30 -04:00
|
|
|
|
2022-04-14 06:56:02 -04:00
|
|
|
void app_main(void)
|
|
|
|
{
|
2022-07-07 02:54:15 -04:00
|
|
|
#if !CONFIG_ESP_TASK_WDT_INIT
|
2022-04-14 06:56:02 -04:00
|
|
|
// If the TWDT was not initialized automatically on startup, manually intialize it now
|
|
|
|
esp_task_wdt_config_t twdt_config = {
|
|
|
|
.timeout_ms = TWDT_TIMEOUT_MS,
|
2023-12-13 17:32:53 -05:00
|
|
|
.idle_core_mask = (1 << CONFIG_FREERTOS_NUMBER_OF_CORES) - 1, // Bitmask of all cores
|
2022-04-14 06:56:02 -04:00
|
|
|
.trigger_panic = false,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_init(&twdt_config));
|
|
|
|
printf("TWDT initialized\n");
|
2022-07-07 02:54:15 -04:00
|
|
|
#endif // CONFIG_ESP_TASK_WDT_INIT
|
2022-04-14 06:56:02 -04:00
|
|
|
|
|
|
|
// Create a task
|
|
|
|
run_loop = true;
|
|
|
|
xTaskCreatePinnedToCore(task_func, "task", 2048, xTaskGetCurrentTaskHandle(), 10, NULL, 0);
|
|
|
|
|
|
|
|
// Let the created task run for a while
|
|
|
|
printf("Delay for %d seconds\n", MAIN_DELAY_MS/1000);
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(MAIN_DELAY_MS));
|
|
|
|
|
|
|
|
// Stop the created task
|
|
|
|
run_loop = false;
|
|
|
|
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
|
|
|
|
2022-07-07 02:54:15 -04:00
|
|
|
#if !CONFIG_ESP_TASK_WDT_INIT
|
2022-04-14 06:56:02 -04:00
|
|
|
// If we manually initialized the TWDT, deintialize it now
|
|
|
|
ESP_ERROR_CHECK(esp_task_wdt_deinit());
|
|
|
|
printf("TWDT deinitialized\n");
|
2022-07-07 02:54:15 -04:00
|
|
|
#endif // CONFIG_ESP_TASK_WDT_INIT
|
2022-04-14 06:56:02 -04:00
|
|
|
printf("Example complete\n");
|
2017-10-09 06:07:30 -04:00
|
|
|
}
|