Watchdog doesn't work with ESP32 3.0.1

Hello. I'm trying to use expressif "esp_task_wdt" hardware functions with ESP32 3.0.1 and Arduino IDE 2.3.2 but is impossible to add and manage current task WDT. This is an example:

#include "esp_task_wdt.h"

#define SDBG                 Serial
#define ESP_WDT_TIMEOUT      20      // Seconds

esp_task_wdt_config_t twdt_config;
time_t LoopVar = 0;

void setup() {
int ret;

	SDBG.begin(115200,SERIAL_8N1);
	SDBG.setDebugOutput(false);
	delay(1000);
	SDBG.println();
	SDBG.println("-----------------------------------------------------------------");

	// Initialize WATCHDOG
	twdt_config.timeout_ms = 20000;
	twdt_config.idle_core_mask = (1 << ESP.getChipCores()) - 1;
	twdt_config.trigger_panic = true;
	SDBG.printf("SETUP: timeout_ms(%d) idle_core_mask(0x%02X) trigger_panic(%d)\r\n", twdt_config.timeout_ms, twdt_config.idle_core_mask, twdt_config.trigger_panic);
//	esp_task_wdt_init(ESP_WDT_TIMEOUT, true);             //ERROR - working only with ESP32 2.0.17 and older
	ret = esp_task_wdt_reconfigure(&twdt_config);        // Working only with ESP32 3.0.1
	SDBG.printf("SETUP: WDT Enabled - 0x%04X\r\n", ret);
	ret = esp_task_wdt_add(NULL);                         //add current thread to WDT watch
	delay(100);
	SDBG.printf("SETUP: WDT Task Enabled - 0x%04X\r\n", ret);

//	esp_task_wdt_deinit();                                //ERROR - working only with ESP32 2.0.17 and older
//  delay(100);
//	SDBG.println(F("SETUP: WDT SERIAL Disabled"));

	SDBG.println(F("SETUP: END"));
}

void loop() {
boolean result;

	if (time(NULL) > LoopVar) {
		LoopVar = time(NULL) + 2;
		SDBG.printf("***LOOP*** (%u)\r\n", millis());
	}

// *** WATCHDOG del dispositivo ------------------------------------------------
//	if (esp_task_wdt_status(NULL) == ESP_OK);             //ERROR - working only with ESP32 2.0.17 and older
		esp_task_wdt_reset();                              //ERROR - working only with ESP32 2.0.17 and older
}

The task resets after 20 seconds. Can someone help me?

The following works for me. The code reconfigures the watch dog timeout to 20 seconds and then resets it every 5 seconds. All will be OK until you enter 's' into the Serial Monitor. This will cause the task to stop resetting the watch dog. There will then be a reset once the watch dog timer expires.

#include "Arduino.h"
#include "esp_task_wdt.h"

void testTask(void * parms);

void setup() {
	Serial.begin(115200);
	vTaskDelay(200);
	Serial.println("Starting");
	esp_task_wdt_config_t twdt_config = {
			.timeout_ms = 20000,
			.idle_core_mask = (1 << portNUM_PROCESSORS) - 1,    // Bitmask of all cores
			.trigger_panic = true,
	};
	ESP_ERROR_CHECK(esp_task_wdt_reconfigure(&twdt_config));
	xTaskCreatePinnedToCore(testTask, "Test Task", 2000, NULL, 5, NULL, ARDUINO_RUNNING_CORE);
}

void loop() {
	vTaskDelete(NULL);
}

void testTask(void * parms) {
	int64_t prevMicros = esp_timer_get_time();
	const int64_t yieldTime = 5000000;
	bool resetWdt = true;

    ESP_ERROR_CHECK(esp_task_wdt_add(NULL));
    ESP_ERROR_CHECK(esp_task_wdt_status(NULL));

	for(;;) {
		int64_t currentMicros = esp_timer_get_time();
		if((resetWdt)&&(currentMicros-prevMicros > yieldTime)) {
			prevMicros = currentMicros;
			Serial.println("Resetting Watchdog");
			esp_task_wdt_reset();
			vTaskDelay(1); // Allow Idle Task to run and do its watch dog reset
		}

		if(Serial.read() == 's') {
			Serial.println("Halting Watch Dog Resets");
			resetWdt = false;
		}
	}
}

The fundamental problem with the code you originally posted is that you've subscribed Core 1's Idle Task to the watch dog timer:

But, you never give this Idle task a chance to run because your loop() function running in loopTask never enters the Blocked state. loopTask has a Priority of 1 while the Idle Task has priority 0, so it never runs.

Thanks a lot. Changing ".idle_core_mask" value ( = 0) everything is working

You should leave Core 0's Idle Task subscribed to the watch dog timer:
twdt_config.idle_core_mask = 1;

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.