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

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关闭喂狗看看对系统有没有影响有什么样的影响。这样反复多测试几次就会对看门狗程序有比较深刻的体会。