当前位置:网站首页>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/
边栏推荐
猜你喜欢

Digital triangle model acwing 1015 Picking flowers

[no app push general test plan

F - True Liars (种类并查集+DP)

F - true liars (category and search set +dp)

Coordinatorlayout+nestedscrollview+recyclerview pull up the bottom display is incomplete

浅谈专项测试之弱网络测试

(中)苹果有开源,但又怎样呢?

Detailed explanation of P problem, NP problem, NPC problem and NP hard problem

JDBC Requset 对应内容及功能介绍

JWT-JSON WEB TOKEN
随机推荐
MFC on the conversion and display of long string unsigned char and CString
Simulation volume leetcode [general] 1109 Flight reservation statistics
Aike AI frontier promotion (2.13)
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
模拟卷Leetcode【普通】1218. 最长定差子序列
[postman] the monitors monitoring API can run periodically
在uni-app中使用腾讯视频插件播放视频
How to extract login cookies when JMeter performs interface testing
模拟卷Leetcode【普通】1249. 移除无效的括号
调用链监控Zipkin、sleuth搭建与整合
Redis core technology and basic architecture of actual combat: what does a key value database contain?
Pat (Grade B) 2022 summer exam
ESP32 ESP-IDF看门狗TWDT
MFC 动态创建的对话框及改变控件的大小和位置
leetcode 24. 两两交换链表中的节点
E - 食物链
JWT-JSON WEB TOKEN
Play video with Tencent video plug-in in uni app
10m25dcf484c8g (FPGA) amy-6m-0002 BGA GPS module