esp32开发与应用(看门狗测试)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

不管是mcu,还是soc,看门狗都是系统常用的一种监控手段。看门狗初始化之后,如果不能在指定的时间内重启,那么系统会默认当前cpu已经跑飞,立即重启系统。这对于一些无人值守的设备,具有很大的价值。当然很多时候,看门狗也不能滥用,对于一般bug、有规律的bug、低概率的bug,最好还是找出root cause,只有实在解决不了的问题,才动用watchdog,这样比较好。

1、esp32的看门狗

相比较其他mcu的看门狗,esp32的看门狗不仅可以监控主task,还可以监控一般的task。这也就是说,不管是哪个task,只要在规定的时间内都没有完成喂狗动作,那么都会造成系统重启。

2、利用ai编写代码

知道这个基本原理之后,就可以让ai给我们编写一段sample代码。告诉ai,用esp-idf编写一段看门狗的demo,这样不出意外的话,我们就可以看到这样的代码,

复制代码
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_task_wdt.h"

// Monitored task
void monitored_task(void *pvParameters) {
    // 1. Add the current task to TWDT watch list
    TaskHandle_t current_task = xTaskGetCurrentTaskHandle();
    esp_task_wdt_add(current_task);
    printf("Monitored task has been added to TWDT.\n");
    
    while (1) {
        // 2. Perform the core work of the task...
        printf("Monitored task is running...\n");
        
        // 3. Feed the watchdog periodically (e.g., every 2 seconds)
        esp_task_wdt_reset();
        printf("Monitored task fed the watchdog.\n");
        vTaskDelay(pdMS_TO_TICKS(2000));
    }
    
    // Delete from TWDT when the task ends (this is never reached in infinite loop)
    esp_task_wdt_delete(current_task);
    vTaskDelete(NULL);
}

void app_main(void) {
    // Initialize TWDT
    esp_task_wdt_config_t twdt_config = {
        .timeout_ms = 5000,                  // 5 seconds timeout
        .idle_core_mask = (1 << portNUM_PROCESSORS) - 1, // Monitor idle tasks on all cores
        .trigger_panic = true,               // Trigger panic (and restart) on timeout
    };
    esp_task_wdt_init(&twdt_config);
    
    // Optionally add the main task (app_main) to the watch list
    esp_task_wdt_add(NULL);  // Add the current (main) task to TWDT
    
    // Create a custom monitored task
    xTaskCreate(monitored_task, "monitored_task", 4096, NULL, 5, NULL);
    
    while (1) {
        // Main task also feeds the watchdog
        esp_task_wdt_reset();
        printf("Main task fed the watchdog.\n");
        vTaskDelay(pdMS_TO_TICKS(3000));
    }
}

3、编译、下载和修改

测试的时候分成两步。第一步,先完成编译、下载和测试。看看编译有没有问题,下载是不是ok,观察运行是不是对的。完成这一步之后,就可以做第二步。即,修改代码。比如主task关闭喂狗,或者是子task关闭喂狗,看看对系统有没有影响,有什么样的影响。

这样反复多测试几次,就会对看门狗程序有比较深刻的体会。

相关推荐
倔强的石头_9 小时前
KES Studio 与 DMS 辅助开发管理实践:让接入过程可视化
数据库
BK贝壳10 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
富贵儿99810 小时前
NL2SQL 实践手记 06:四个角色怎样合作——拆解一个 Multi-Agent 查询系统
数据库·sql·ai·自然语言处理·langchain
吴名氏.11 小时前
全栈跃迁:Java 后端 27 讲【3】Vue3工程化脚手架
java·开发语言·springboot·全栈工程师
codeejun11 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
JellyfishMIX12 小时前
AgentScope-Java 核心机制(内循环) ReAct loop 源码分析
java·后端·agent
Decay__13 小时前
PostgreSQL 监控体系构建 —— postgres_exporter + Prometheus + Grafana 全栈实战
运维·数据库·sql·postgresql
小此方13 小时前
QT从入门到精通(一)Qt新手保姆级教程:从官网安装到项目实战,四步带你彻底搞懂Qt框架
开发语言·qt
Rain的Java大神之路13 小时前
Spring Boot 4 把自动配置拆成 47 个模块后,我的 `dependency:tree` 翻天了:一份从排查到治理的完整实战手册
java·spring boot·后端
瑶池酒剑仙14 小时前
C++重构与代码质量完全指南:从设计模式、SOLID原则到代码审查与静态分析的工程实践(所赖君子见机)
c语言·开发语言·c++·设计模式·重构·哈希算法·visual studio