当前位置:网站首页>Esp32 esp-idf watchdog twdt
Esp32 esp-idf watchdog twdt
2022-07-06 06:23:00 【Morning breeze】
Chen Tuo 2022/07/02-2022/07/02
1. summary
This example demonstrates how to use the task watchdog timer Task Watchdog Timer (TWDT) The following functions of :
- How to initialize and Uninitialize TWDT
- How to subscribe and unsubscribe TWDT The task of
- How to read and unsubscribe TWDT Users of
- How to make tasks and users reset ( Feed the dog )TWDTs
2. Official routine domestic mirror
https://gitee.com/EspressifSystems/esp-idf/tree/master/examples/system/task_watchdog
3. development environment
《 Use Lexin domestic Gitee Image building ESP32 development environment 》
https://zhuanlan.zhihu.com/p/348106034
https://blog.csdn.net/chentuo2000/article/details/113424934?spm=1001.2014.3001.5501
4. Copy code
- Clone the official routine
Copy the official example project to ESP-IDF Beyond development tools :
cd ~/esp
cp -r ~/esp/esp-idf/examples/system/task_watchdog ~/esp/
- Project tree
cd ~/esp/task_watchdog
tree
5. Build the project
- Refresh esp-idf Environmental Science
get_idf
- Set the target chip
idf.py set-target esp32
- Configuration items
idf.py menuconfig
1) Set flash memory to 4MB
2) Call the emergency handler when the task watchdog times out
preservation , sign out .
- Compile the project
idf.py build
- Burn project
see USB Serial port COM slogan :
burning :
idf.py -p /dev/ttyS3 -b 115200 flash
- Enable monitor
idf.py monitor -p /dev/ttyS3
When the example runs normally , The following output will be observed :
Users can comment out esp_task_wdt_reset() or esp_task_wdt_reset_user() Call to test triggering TWDT,
//CHECK_ERROR_CODE(esp_task_wdt_reset(), ESP_OK);
This will result in the following output :
The watchdog is triggered .
because TWDT The timeout is 3 second :
#define TWDT_TIMEOUT_S 3// TWDT Timeout time
Delay time 10 second :
vTaskDelay(pdMS_TO_TICKS(10000)); // Time delay 10 second
stay 10 Second delay period , The watchdog has 3 Trigger , After that, the program will continue to execute :
6. Restart the program immediately after the watchdog is triggered
If you want to restart after the task watchdog is triggered .
- Set to call the emergency handler when the task watchdog times out
- increase esp_task_wdt_isr_user_handler() function
void esp_task_wdt_isr_user_handler(void)
{
esp_restart();
}
In this way, the program will restart when the watchdog triggers :
7. Complete code
/* Task_Watchdog Examples
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.
*/
#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_task_wdt.h"
#define TWDT_TIMEOUT_S 3 // TWDT Timeout time
#define TASK_RESET_PERIOD_S 2 // Task reset cycle time
/*
* macro , Used for inspection TWDT Output , If the return error code is detected, the program execution will be terminated .
*/
#define CHECK_ERROR_CODE(returned, expected) ({ \
if(returned != expected){ \
printf("TWDT ERROR\n"); \
abort(); \
} \
})
static TaskHandle_t task_handles[portNUM_PROCESSORS];
// Callback by app_main() User tasks created
void reset_task(void *arg)
{
// by TWDT Add tasks , And check dwt Status to see whether to add
CHECK_ERROR_CODE(esp_task_wdt_add(NULL), ESP_OK);
CHECK_ERROR_CODE(esp_task_wdt_status(NULL), ESP_OK);
while(1){
// Every time 2 Reset the watchdog every second
//CHECK_ERROR_CODE(esp_task_wdt_reset(), ESP_OK); // feed a dog . Comment on this line to test the trigger TWDT Overtime
vTaskDelay(pdMS_TO_TICKS(TASK_RESET_PERIOD_S * 1000));
}
}
void esp_task_wdt_isr_user_handler(void)
{
esp_restart();
}
void app_main(void)
{
printf("Initialize TWDT\n");
// Initialize or reinitialize TWDT
CHECK_ERROR_CODE(esp_task_wdt_init(TWDT_TIMEOUT_S, false), ESP_OK);
// If idle tasks are not subscribed at startup , Subscribe to TWDT
#ifndef CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
esp_task_wdt_add(xTaskGetIdleTaskHandleForCPU(0));
#endif
#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 && !CONFIG_FREERTOS_UNICORE
esp_task_wdt_add(xTaskGetIdleTaskHandleForCPU(1));
#endif
// Create user tasks and add them to the watchdog
for(int i = 0; i < portNUM_PROCESSORS; i++){
xTaskCreatePinnedToCore(reset_task, "reset task", 1024, NULL, 10, &task_handles[i], i);
}
printf("Delay for 10 seconds\n");
vTaskDelay(pdMS_TO_TICKS(10000)); // Time delay 10 second
printf("Unsubscribing and deleting tasks\n");
// Delete and unsubscribe user tasks from the task watchdog , Then unsubscribe from idle tasks
for(int i = 0; i < portNUM_PROCESSORS; i++){
vTaskDelete(task_handles[i]); // First delete the user task ( Prevent resetting unsubscribed tasks )
CHECK_ERROR_CODE(esp_task_wdt_delete(task_handles[i]), ESP_OK); // from TWDT Unsubscribe from the task
CHECK_ERROR_CODE(esp_task_wdt_status(task_handles[i]), ESP_ERR_NOT_FOUND); // Confirm that the task has been unsubscribed
// Unsubscribe from idle tasks
CHECK_ERROR_CODE(esp_task_wdt_delete(xTaskGetIdleTaskHandleForCPU(i)), ESP_OK); // from TWDT Unsubscribe from idle tasks
CHECK_ERROR_CODE(esp_task_wdt_status(xTaskGetIdleTaskHandleForCPU(i)), ESP_ERR_NOT_FOUND); // Confirm that the idle task has been unsubscribed
}
// Cancel after all tasks are unsubscribed TWDT
CHECK_ERROR_CODE(esp_task_wdt_deinit(), ESP_OK);
CHECK_ERROR_CODE(esp_task_wdt_status(NULL), ESP_ERR_INVALID_STATE); //Confirm TWDT has been deinitialized confirm TWDT Uninitialized
printf("Complete\n");
}
Reference documents
- ESP32 Practice the watchdog task https://www.codeleading.com/article/25393323890/
边栏推荐
- The whole process realizes the single sign on function and the solution of "canceltoken" of undefined when the request is canceled
- LeetCode 1200. 最小绝对差
- Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
- (中)苹果有开源,但又怎样呢?
- Selenium source code read through · 9 | desiredcapabilities class analysis
- leetcode 24. Exchange the nodes in the linked list in pairs
- 测试周期被压缩?教你9个方法去应对
- 模拟卷Leetcode【普通】1091. 二进制矩阵中的最短路径
- Web界面元素的测试
- 模拟卷Leetcode【普通】1219. 黄金矿工
猜你喜欢
在JEECG-boot代码生成的基础上修改list页面(结合自定义的组件)
Isam2 operation process
Data type of MySQL
联合索引的左匹配原则
记一个基于JEECG-BOOT的比较复杂的增删改功能的实现
Understanding of processes and threads
ESP32 ESP-IDF看门狗TWDT
Full link voltage measurement: building three models
F - True Liars (种类并查集+DP)
[postman] test script writing and assertion details
随机推荐
D - How Many Answers Are Wrong
An article was uncovered to test the truth of outsourcing companies
Black cat takes you to learn UFS protocol Chapter 4: detailed explanation of UFS protocol stack
模拟卷Leetcode【普通】1314. 矩阵区域和
基于JEECG-BOOT的list页面的地址栏参数传递
联合索引的左匹配原则
Past and present lives of QR code and sorting out six test points
LeetCode 729. 我的日程安排表 I
职场进阶指南:大厂人必看书籍推荐
LeetCode 731. 我的日程安排表 II
G - Supermarket
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
Simulation volume leetcode [general] 1405 Longest happy string
黑猫带你学UFS协议第4篇:UFS协议栈详解
sourceInsight中文乱码
Qt:无法定位程序输入点XXXXX于动态链接库。
Data type of MySQL
Set the print page style by modifying style
E - 食物链
Online and offline problems