当前位置:网站首页>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/
边栏推荐
- Thoughts on data security (Reprint)
- Properties file
- Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
- Summary of anomaly detection methods
- Reading notes of effective managers
- Simulation volume leetcode [general] 1109 Flight reservation statistics
- Pat (Grade B) 2022 summer exam
- ESP32 ESP-IDF看门狗TWDT
- Leaflet map
- JDBC requset corresponding content and function introduction
猜你喜欢
B - The Suspects
keil MDK中删除添加到watch1中的变量
Redis 核心技术与实战之 基本架构:一个键值数据库包含什么?
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
二维码的前世今生 与 六大测试点梳理
mysql按照首字母排序
Nodejs realizes the third-party login of Weibo
Basic knowledge of MySQL
浅谈专项测试之弱网络测试
Black cat takes you to learn EMMC Protocol Part 10: EMMC read and write operation details (read & write)
随机推荐
[postman] dynamic variable (also known as mock function)
Postman core function analysis - parameterization and test report
LeetCode 1200. 最小绝对差
Digital triangle model acwing 1015 Picking flowers
LeetCode 732. 我的日程安排表 III
模拟卷Leetcode【普通】1062. 最长重复子串
CoordinatorLayout+NestedScrollView+RecyclerView 上拉底部显示不全
Manhattan distance and Manhattan rectangle - print back font matrix
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[C language] string left rotation
Still worrying about how to write web automation test cases? Senior test engineers teach you selenium test case writing hand in hand
Thoughts on data security (Reprint)
[postman] collections configuration running process
Simulation volume leetcode [general] 1314 Matrix area and
Manhattan distance sum - print diamond
二维码的前世今生 与 六大测试点梳理
對數據安全的思考(轉載)
F - True Liars (种类并查集+DP)
leaflet 地图
Web界面元素的测试