1#include "esp_watchdog.hpp"
5#include "esp_private/periph_ctrl.h"
6#include "hal/mwdt_ll.h"
7#include "hal/timer_ll.h"
14constexpr uint32_t kWdtTickUs = 500;
15constexpr uint32_t kWdtTicksPerMs = 1000 / kWdtTickUs;
17bool GetMwdtGroupInfo(wdt_inst_t instance,
int* group_id, periph_module_t* periph)
19 if (instance == WDT_MWDT0)
22 *periph = PERIPH_TIMG0_MODULE;
26#if SOC_TIMER_GROUPS >= 2
27 if (instance == WDT_MWDT1)
30 *periph = PERIPH_TIMG1_MODULE;
40ESP32Watchdog::ESP32Watchdog(uint32_t timeout_ms, uint32_t feed_ms)
46bool ESP32Watchdog::IsInstanceSupported(wdt_inst_t instance)
52#if SOC_TIMER_GROUPS >= 2
63ErrorCode ESP32Watchdog::InitializeHardware()
70 if (!IsInstanceSupported(instance_))
75 if (instance_ != WDT_RWDT)
78 periph_module_t periph = PERIPH_TIMG0_MODULE;
79 if (!GetMwdtGroupInfo(instance_, &group_id, &periph))
84 PERIPH_RCC_ACQUIRE_ATOMIC(periph, ref_count)
88 timer_ll_enable_bus_clock(group_id,
true);
89 timer_ll_reset_register(group_id);
94 const uint32_t prescaler = instance_ == WDT_RWDT ? 0U : MWDT_LL_DEFAULT_CLK_PRESCALER;
95 wdt_hal_init(&hal_, instance_, prescaler,
false);
100ErrorCode ESP32Watchdog::ApplyConfiguration()
107 const uint64_t ticks64 =
static_cast<uint64_t
>(
timeout_ms_) * kWdtTicksPerMs;
108 if ((ticks64 == 0) || (ticks64 > UINT32_MAX))
113 const uint32_t stage0_ticks =
static_cast<uint32_t
>(ticks64);
115 wdt_hal_write_protect_disable(&hal_);
116 wdt_hal_config_stage(&hal_, WDT_STAGE0, stage0_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
117 wdt_hal_config_stage(&hal_, WDT_STAGE1, 0, WDT_STAGE_ACTION_OFF);
118 wdt_hal_config_stage(&hal_, WDT_STAGE2, 0, WDT_STAGE_ACTION_OFF);
119 wdt_hal_config_stage(&hal_, WDT_STAGE3, 0, WDT_STAGE_ACTION_OFF);
120 wdt_hal_write_protect_enable(&hal_);
142 return ApplyConfiguration();
147 if (!initialized_ || !started_)
152 wdt_hal_write_protect_disable(&hal_);
154 wdt_hal_write_protect_enable(&hal_);
166 ret = ApplyConfiguration();
172 wdt_hal_write_protect_disable(&hal_);
173 wdt_hal_enable(&hal_);
175 wdt_hal_write_protect_enable(&hal_);
189 wdt_hal_write_protect_disable(&hal_);
190 wdt_hal_disable(&hal_);
191 wdt_hal_write_protect_enable(&hal_);
ErrorCode Start() override
启动看门狗 / Start the watchdog
ErrorCode Stop() override
停止看门狗 / Stop the watchdog
ErrorCode Feed() override
立即手动喂狗 Feed the watchdog immediately
ErrorCode SetConfig(const Configuration &config) override
初始化硬件并设置超时时间 Initialize hardware and set overflow time
uint32_t auto_feed_interval_ms
自动喂狗间隔
@ INIT_ERR
初始化错误 | Initialization error
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
看门狗配置结构体 Configuration for the watchdog
uint32_t feed_ms
自动喂狗周期 Auto feed interval (ms, < timeout_ms)
uint32_t timeout_ms
看门狗溢出时间 Watchdog overflow time (ms)