ESPHome  2024.12.2
watchdog.cpp
Go to the documentation of this file.
1 #include "watchdog.h"
2 
4 #include "esphome/core/log.h"
5 
6 #include <cinttypes>
7 #include <cstdint>
8 #ifdef USE_ESP32
9 #include "esp_idf_version.h"
10 #include "esp_task_wdt.h"
11 #endif
12 #ifdef USE_RP2040
13 #include "hardware/watchdog.h"
14 #include "pico/stdlib.h"
15 #endif
16 
17 namespace esphome {
18 namespace watchdog {
19 
20 static const char *const TAG = "http_request.watchdog";
21 
22 WatchdogManager::WatchdogManager(uint32_t timeout_ms) : timeout_ms_(timeout_ms) {
23  if (timeout_ms == 0) {
24  return;
25  }
26  this->saved_timeout_ms_ = this->get_timeout_();
27  this->set_timeout_(timeout_ms);
28 }
29 
31  if (this->timeout_ms_ == 0) {
32  return;
33  }
34  this->set_timeout_(this->saved_timeout_ms_);
35 }
36 
37 void WatchdogManager::set_timeout_(uint32_t timeout_ms) {
38  ESP_LOGV(TAG, "Adjusting WDT to %" PRIu32 "ms", timeout_ms);
39 #ifdef USE_ESP32
40 #if ESP_IDF_VERSION_MAJOR >= 5
41  esp_task_wdt_config_t wdt_config = {
42  .timeout_ms = timeout_ms,
43  .idle_core_mask = 0x03,
44  .trigger_panic = true,
45  };
46  esp_task_wdt_reconfigure(&wdt_config);
47 #else
48  esp_task_wdt_init(timeout_ms / 1000, true);
49 #endif // ESP_IDF_VERSION_MAJOR
50 #endif // USE_ESP32
51 
52 #ifdef USE_RP2040
53  watchdog_enable(timeout_ms, true);
54 #endif
55 }
56 
57 uint32_t WatchdogManager::get_timeout_() {
58  uint32_t timeout_ms = 0;
59 
60 #ifdef USE_ESP32
61  timeout_ms = (uint32_t) CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
62 #endif // USE_ESP32
63 
64 #ifdef USE_RP2040
65  timeout_ms = watchdog_get_count() / 1000;
66 #endif
67 
68  ESP_LOGVV(TAG, "get_timeout: %" PRIu32 "ms", timeout_ms);
69 
70  return timeout_ms;
71 }
72 
73 } // namespace watchdog
74 } // namespace esphome
WatchdogManager(uint32_t timeout_ms)
Definition: watchdog.cpp:22
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7