2026-05-30 00:24:31 +08:00
|
|
|
/**
|
|
|
|
|
* @file watchdog.cpp
|
|
|
|
|
* @brief APP 独立看门狗(FWDGT)实现。
|
|
|
|
|
*
|
|
|
|
|
* 时钟源: IRC40K (~40 KHz)
|
|
|
|
|
* 预分频: CONFIG_FWDGT_PSC, 重载值: CONFIG_FWDGT_RLD
|
|
|
|
|
* 超时 = (PSC / 40000) * (RLD + 1) 秒
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "device/watchdog.h"
|
|
|
|
|
#include "sys/config.h"
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
2026-09-02 02:38:35 +08:00
|
|
|
uint8_t Watchdog::init()
|
2026-05-30 00:24:31 +08:00
|
|
|
{
|
2026-09-02 02:38:35 +08:00
|
|
|
/* FWDGT 的 PSC/RLD 写入经 LSI(IRC40K) 时钟域同步(PUD/RUD 由硬件清零)。
|
|
|
|
|
* BOOT 只检测 FWDGT 复位标志、从不启动看门狗,冷启动时 LSI 未开——
|
2026-09-20 22:58:09 +08:00
|
|
|
* 直接配置会让 PUD/RUD 永远等不到清零(库超时返回 ERROR)。
|
2026-09-02 02:38:35 +08:00
|
|
|
* 必须先手动开 LSI 并等就绪,再配置。 */
|
|
|
|
|
rcu_osci_on(RCU_IRC40K);
|
|
|
|
|
if (rcu_osci_stab_wait(RCU_IRC40K) != SUCCESS) {
|
|
|
|
|
return kFaultLsiNotReady;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* PSC/RLD 配置函数自带 ErrStatus(等待 PUD/RUD 清零带超时);
|
2026-09-20 22:58:09 +08:00
|
|
|
* 必须检查返回值,否则写入失败会让看门狗实际未按预期运行。 */
|
2026-05-30 00:24:31 +08:00
|
|
|
fwdgt_write_enable();
|
2026-09-02 02:38:35 +08:00
|
|
|
uint8_t fault = kFaultOk;
|
|
|
|
|
if (fwdgt_prescaler_value_config((uint16_t)CONFIG_FWDGT_PSC) != SUCCESS) {
|
|
|
|
|
fault = kFaultPsc;
|
|
|
|
|
}
|
|
|
|
|
if (fwdgt_reload_value_config((uint16_t)CONFIG_FWDGT_RLD) != SUCCESS) {
|
|
|
|
|
if (fault == kFaultOk) {
|
|
|
|
|
fault = kFaultRld;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-05-30 00:24:31 +08:00
|
|
|
fwdgt_write_disable();
|
|
|
|
|
fwdgt_enable();
|
2026-09-02 02:38:35 +08:00
|
|
|
/* 使能后确认无挂起更新(PUD/RUD 应已清零) */
|
|
|
|
|
if ((FWDGT_STAT & (FWDGT_STAT_PUD | FWDGT_STAT_RUD)) != 0U) {
|
|
|
|
|
if (fault == kFaultOk) {
|
|
|
|
|
fault = kFaultPending;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fault;
|
2026-05-30 00:24:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Watchdog::feed()
|
|
|
|
|
{
|
|
|
|
|
fwdgt_counter_reload();
|
|
|
|
|
}
|