当前位置:网站首页>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/
边栏推荐
- 黑猫带你学UFS协议第4篇:UFS协议栈详解
- Manhattan distance and Manhattan rectangle - print back font matrix
- keil MDK中删除添加到watch1中的变量
- Simulation volume leetcode [general] 1296 Divide an array into a set of consecutive numbers
- 10M25DCF484C8G(FPGA) AMY-6M-0002 BGA GPS模块
- 模拟卷Leetcode【普通】1405. 最长快乐字符串
- Delete the variables added to watch1 in keil MDK
- 还在为如何编写Web自动化测试用例而烦恼嘛?资深测试工程师手把手教你Selenium 测试用例编写
- [postman] collections configuration running process
- MySQL之基础知识
猜你喜欢
![[postman] the monitors monitoring API can run periodically](/img/9e/3f6150290b868fc1160b6b01d0857e.png)
[postman] the monitors monitoring API can run periodically

黑猫带你学UFS协议第4篇:UFS协议栈详解

Left matching principle of joint index

Understanding of processes and threads

selenium源码通读·9 |DesiredCapabilities类分析

【Postman】测试(Tests)脚本编写和断言详解

E - food chain

调用链监控Zipkin、sleuth搭建与整合

Data type of MySQL
![[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development](/img/63/dc729d3f483fd6088cfa7b6fb45ccb.png)
[Tera term] black cat takes you to learn TTL script -- serial port automation skill in embedded development
随机推荐
php使用redis实现分布式锁
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module
Understanding of processes and threads
【Postman】测试(Tests)脚本编写和断言详解
RestTemplate、Feign实现Token传递
The latest 2022 review of "graph classification research"
Simulation volume leetcode [general] 1219 Golden Miner
私人云盘部署
自定义指定路由上的Gateway过滤器工厂
Thoughts on data security (Reprint)
Nodejs realizes the third-party login of Weibo
Pat (Grade B) 2022 summer exam
Construction and integration of Zipkin and sleuth for call chain monitoring
在uni-app中使用腾讯视频插件播放视频
使用Nacos管理配置
leetcode 24. 两两交换链表中的节点
Simulation volume leetcode [general] 1091 The shortest path in binary matrix
Hypothesis testing learning notes
B - The Suspects
sourceInsight中文乱码