Skip to content

Commit 5410617

Browse files
authored
Set ESP32 watchdog to loop task (esphome#2846)
1 parent 607601b commit 5410617

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

esphome/components/esp32/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,16 @@ async def to_code(config):
311311
)
312312
add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_DEFAULT", False)
313313
add_idf_sdkconfig_option("CONFIG_COMPILER_OPTIMIZATION_SIZE", True)
314+
314315
# Increase freertos tick speed from 100Hz to 1kHz so that delay() resolution is 1ms
315316
add_idf_sdkconfig_option("CONFIG_FREERTOS_HZ", 1000)
316317

318+
# Setup watchdog
319+
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT", True)
320+
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_PANIC", True)
321+
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0", False)
322+
add_idf_sdkconfig_option("CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1", False)
323+
317324
cg.add_platformio_option("board_build.partitions", "partitions.csv")
318325

319326
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():

esphome/components/esp32/core.cpp

+18-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,17 @@
66
#include <freertos/FreeRTOS.h>
77
#include <freertos/task.h>
88
#include <esp_idf_version.h>
9+
#include <esp_task_wdt.h>
910
#include <soc/rtc.h>
1011

1112
#if ESP_IDF_VERSION_MAJOR >= 4
1213
#include <hal/cpu_hal.h>
1314
#endif
1415

16+
#ifdef USE_ARDUINO
17+
#include <esp32-hal.h>
18+
#endif
19+
1520
void setup();
1621
void loop();
1722

@@ -29,24 +34,24 @@ void arch_restart() {
2934
yield();
3035
}
3136
}
32-
void IRAM_ATTR HOT arch_feed_wdt() {
33-
#ifdef USE_ARDUINO
37+
38+
void arch_init() {
39+
// Enable the task watchdog only on the loop task (from which we're currently running)
40+
#if defined(USE_ESP_IDF)
41+
esp_task_wdt_add(nullptr);
42+
// Idle task watchdog is disabled on ESP-IDF
43+
#elif defined(USE_ARDUINO)
44+
enableLoopWDT();
45+
// Disable idle task watchdog on the core we're using (Arduino pins the process to a core)
3446
#if CONFIG_ARDUINO_RUNNING_CORE == 0
35-
#ifdef CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0
36-
// ESP32 uses "Task Watchdog" which is hooked to the FreeRTOS idle task.
37-
// To cause the Watchdog to be triggered we need to put the current task
38-
// to sleep to get the idle task scheduled.
39-
delay(1);
47+
disableCore0WDT();
4048
#endif
49+
#if CONFIG_ARDUINO_RUNNING_CORE == 1
50+
disableCore1WDT();
4151
#endif
42-
#endif // USE_ARDUINO
43-
44-
#ifdef USE_ESP_IDF
45-
#ifdef CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0
46-
delay(1);
4752
#endif
48-
#endif // USE_ESP_IDF
4953
}
54+
void IRAM_ATTR HOT arch_feed_wdt() { esp_task_wdt_reset(); }
5055

5156
uint8_t progmem_read_byte(const uint8_t *addr) { return *addr; }
5257
uint32_t arch_get_cpu_cycle_count() {

esphome/components/esp8266/core.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void arch_restart() {
2020
yield();
2121
}
2222
}
23+
void arch_init() {}
2324
void IRAM_ATTR HOT arch_feed_wdt() {
2425
ESP.wdtFeed(); // NOLINT(readability-static-accessed-through-instance)
2526
}

esphome/core/application.h

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "esphome/core/defines.h"
66
#include "esphome/core/preferences.h"
77
#include "esphome/core/component.h"
8+
#include "esphome/core/hal.h"
89
#include "esphome/core/helpers.h"
910
#include "esphome/core/scheduler.h"
1011

@@ -47,6 +48,7 @@ namespace esphome {
4748
class Application {
4849
public:
4950
void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix) {
51+
arch_init();
5052
this->name_add_mac_suffix_ = name_add_mac_suffix;
5153
if (name_add_mac_suffix) {
5254
this->name_ = name + "-" + get_mac_address().substr(6);

esphome/core/hal.h

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ uint32_t micros();
3939
void delay(uint32_t ms);
4040
void delayMicroseconds(uint32_t us); // NOLINT(readability-identifier-naming)
4141
void __attribute__((noreturn)) arch_restart();
42+
void arch_init();
4243
void arch_feed_wdt();
4344
uint32_t arch_get_cpu_cycle_count();
4445
uint32_t arch_get_cpu_freq_hz();

sdkconfig.defaults

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ CONFIG_PARTITION_TABLE_CUSTOM=y
99
#CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
1010
CONFIG_PARTITION_TABLE_SINGLE_APP=n
1111
CONFIG_FREERTOS_HZ=1000
12+
CONFIG_ESP_TASK_WDT=y
13+
CONFIG_ESP_TASK_WDT_PANIC=y
14+
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
15+
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=n
1216

1317
# esp32_ble
1418
CONFIG_BT_ENABLED=y

0 commit comments

Comments
 (0)