当前位置:网站首页>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

  1. ESP32 Practice the watchdog task https://www.codeleading.com/article/25393323890/

原网站

版权声明
本文为[Morning breeze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060602355316.html