Skip to content

Commit

Permalink
Watchdog: fixed failed initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed May 26, 2024
1 parent 88be290 commit f3e3c64
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build_flags = -std=gnu++2b
-I conf
-Wno-deprecated-declarations
-Wno-attributes
-D CORE_DEBUG_LEVEL=1
-D CORE_DEBUG_LEVEL=0
build_src_flags = -Wformat=2
-Wformat-truncation
-Wall
Expand Down
21 changes: 15 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,19 @@ namespace fabomatic
if (!initialized)
{
constexpr auto msecs = std::chrono::duration_cast<std::chrono::milliseconds>(conf::tasks::WATCHDOG_TIMEOUT).count();
constexpr esp_task_wdt_config_t conf{.timeout_ms = msecs, .idle_core_mask = 0, .trigger_panic = true};
esp_task_wdt_init(&conf); // enable panic so ESP32 restarts
ESP_LOGI(TAG, "taskEspWatchdog - initialized %lld milliseconds", msecs);
esp_task_wdt_add(NULL); // add current thread to WDT watch
initialized = true;
constexpr esp_task_wdt_config_t conf{.timeout_ms = msecs, .idle_core_mask = 1, .trigger_panic = true};
esp_task_wdt_deinit();
auto result = esp_task_wdt_init(&conf); // enable panic so ESP32 restarts
if (result == ESP_OK)
{
ESP_LOGI(TAG, "taskEspWatchdog - initialized %lld milliseconds", msecs);
esp_task_wdt_add(NULL); // add current thread to WDT watch
initialized = true;
}
else
{
ESP_LOGE(TAG, "taskEspWatchdog - failed to initialize with %lld milliseconds", msecs);
}
}
esp_task_wdt_reset(); // Signal the hardware watchdog
}
Expand Down Expand Up @@ -470,7 +478,8 @@ void setup()
if (!hw_init)
{
// If hardware initialization failed, wait for OTA for 3 minutes
constexpr esp_task_wdt_config_t conf{.timeout_ms = 60 * 3 * 1000, .idle_core_mask = 0, .trigger_panic = true};
constexpr esp_task_wdt_config_t conf{.timeout_ms = 60 * 3 * 1000, .idle_core_mask = 1, .trigger_panic = true};
esp_task_wdt_deinit();
esp_task_wdt_init(&conf); // enable panic so ESP32 restarts
esp_task_wdt_add(NULL); // add current thread to WDT watch
while (true)
Expand Down
18 changes: 13 additions & 5 deletions test/test_mqtt/test_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,19 @@ namespace fabomatic::tests
{
if (!initialized)
{
constexpr auto msecs = std::chrono::duration_cast<std::chrono::seconds>(conf::tasks::WATCHDOG_TIMEOUT).count();
constexpr esp_task_wdt_config_t conf{.timeout_ms = msecs, .idle_core_mask = 0, .trigger_panic = true};
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, esp_task_wdt_init(&conf), "taskEspWatchdog - esp_task_wdt_init failed");
ESP_LOGI(TAG3, "taskEspWatchdog - initialized %lld seconds", msecs);
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, esp_task_wdt_add(NULL), "taskEspWatchdog - esp_task_wdt_add failed");
constexpr uint32_t msecs{std::chrono::duration_cast<std::chrono::milliseconds>(conf::tasks::WATCHDOG_TIMEOUT).count()};
constexpr esp_task_wdt_config_t conf{.timeout_ms = msecs, .idle_core_mask = 1, .trigger_panic = true};

auto deinit = esp_task_wdt_deinit();
TEST_ASSERT_EQUAL_INT_MESSAGE(ESP_OK, deinit, "taskEspWatchdog - esp_task_wdt_deinit failed");

auto result = esp_task_wdt_init(&conf);
TEST_ASSERT_EQUAL_INT_MESSAGE(ESP_OK, result, "taskEspWatchdog - esp_task_wdt_init failed");

ESP_LOGI(TAG3, "taskEspWatchdog - initialized %lu seconds", msecs);

auto result2 = esp_task_wdt_add(NULL);
TEST_ASSERT_EQUAL_INT_MESSAGE(ESP_OK, result2, "taskEspWatchdog - esp_task_wdt_add failed");
initialized = true;
}
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, esp_task_wdt_reset(), "taskEspWatchdog - esp_task_wdt_reset failed");
Expand Down

0 comments on commit f3e3c64

Please sign in to comment.