1#include "esp_watchdog.hpp"
5#include "esp_private/periph_ctrl.h"
6#include "hal/mwdt_ll.h"
7#include "hal/timer_ll.h"
15constexpr uint32_t MWDT_TICK_US = 500;
16constexpr uint32_t MWDT_TICKS_PER_MS = 1000 / MWDT_TICK_US;
18bool GetMwdtGroupInfo(wdt_inst_t instance,
int* group_id, periph_module_t* periph)
20 if (instance == WDT_MWDT0)
23 *periph = PERIPH_TIMG0_MODULE;
27#if SOC_TIMER_GROUPS >= 2
28 if (instance == WDT_MWDT1)
31 *periph = PERIPH_TIMG1_MODULE;
41ESP32Watchdog::ESP32Watchdog(uint32_t timeout_ms, uint32_t feed_ms)
47bool ESP32Watchdog::IsInstanceSupported(wdt_inst_t instance)
53#if SOC_TIMER_GROUPS >= 2
64ErrorCode ESP32Watchdog::InitializeHardware()
71 if (!IsInstanceSupported(instance_))
76 if (instance_ != WDT_RWDT)
79 periph_module_t periph = PERIPH_TIMG0_MODULE;
80 if (!GetMwdtGroupInfo(instance_, &group_id, &periph))
85 PERIPH_RCC_ACQUIRE_ATOMIC(periph, ref_count)
89 timer_ll_enable_bus_clock(group_id,
true);
90 timer_ll_reset_register(group_id);
95 const uint32_t prescaler = instance_ == WDT_RWDT ? 0U : MWDT_LL_DEFAULT_CLK_PRESCALER;
96 wdt_hal_init(&hal_, instance_, prescaler,
false);
101ErrorCode ESP32Watchdog::ApplyConfiguration()
108 uint64_t ticks64 = 0U;
109 if (instance_ == WDT_RWDT)
111 const uint32_t slow_clock_hz = rtc_clk_slow_freq_get_hz();
112 if (slow_clock_hz == 0U)
116 ticks64 =
static_cast<uint64_t
>(
timeout_ms_) * slow_clock_hz / 1000U;
120 ticks64 =
static_cast<uint64_t
>(
timeout_ms_) * MWDT_TICKS_PER_MS;
122 if ((ticks64 == 0) || (ticks64 > UINT32_MAX))
127 const uint32_t stage0_ticks =
static_cast<uint32_t
>(ticks64);
129 wdt_hal_write_protect_disable(&hal_);
130 wdt_hal_config_stage(&hal_, WDT_STAGE0, stage0_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
131 wdt_hal_config_stage(&hal_, WDT_STAGE1, 0, WDT_STAGE_ACTION_OFF);
132 wdt_hal_config_stage(&hal_, WDT_STAGE2, 0, WDT_STAGE_ACTION_OFF);
133 wdt_hal_config_stage(&hal_, WDT_STAGE3, 0, WDT_STAGE_ACTION_OFF);
134 wdt_hal_write_protect_enable(&hal_);
156 return ApplyConfiguration();
161 if (!initialized_ || !started_)
166 wdt_hal_write_protect_disable(&hal_);
168 wdt_hal_write_protect_enable(&hal_);
180 ret = ApplyConfiguration();
186 wdt_hal_write_protect_disable(&hal_);
187 wdt_hal_enable(&hal_);
189 wdt_hal_write_protect_enable(&hal_);
203 wdt_hal_write_protect_disable(&hal_);
204 wdt_hal_disable(&hal_);
205 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
自动喂狗间隔
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ 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)