1#include "stm32_watchdog.hpp"
3#if defined(HAL_IWDG_MODULE_ENABLED)
7STM32Watchdog::STM32Watchdog(IWDG_HandleTypeDef* hiwdg, uint32_t timeout_ms,
8 uint32_t feed_ms, uint32_t clock)
9 : hiwdg_(hiwdg), clock_(clock)
12 const ErrorCode config_result = SetConfig({timeout_ms, feed_ms});
13 ASSERT(config_result == ErrorCode::OK);
29 } TABLE[] = {{0, 4}, {1, 8}, {2, 16}, {3, 32}, {4, 64}, {5, 128}, {6, 256}};
32 uint16_t best_rlr = 0xFFF;
35 for (
const auto& item : TABLE)
37 uint32_t prescaler = item.div;
38 uint32_t reload = (config.
timeout_ms * lsi) / (1000 * prescaler);
50 best_rlr =
static_cast<uint16_t
>(reload);
63 hiwdg_->Init.Prescaler = best_pr;
64 hiwdg_->Init.Reload = best_rlr;
65#if defined(IWDG_WINDOW_DISABLE)
69 hiwdg_->Init.Window = IWDG_WINDOW_DISABLE;
88 if (HAL_IWDG_Init(
hiwdg_) != HAL_OK)
uint32_t clock_
LSI clock in Hz.
ErrorCode Stop() override
停止看门狗 / Stop the watchdog
ErrorCode Start() override
启动看门狗 / Start the watchdog
IWDG_HandleTypeDef * hiwdg_
STM32 HAL IWDG handle.
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
@ NOT_SUPPORT
不支持 / Not supported
@ FAILED
操作失败 / Operation failed
@ 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)