From 7b1706657565b8f25e8a6e875a7652a1899d5025 Mon Sep 17 00:00:00 2001 From: zys Date: Wed, 4 Mar 2026 15:01:15 +0800 Subject: [PATCH] feat: add watchdog --- include/watchdog.hpp | 19 ++++++++++++------- prj.conf | 3 ++- src/main.cpp | 5 +++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/include/watchdog.hpp b/include/watchdog.hpp index cd072d2..de1f4f3 100644 --- a/include/watchdog.hpp +++ b/include/watchdog.hpp @@ -2,25 +2,30 @@ #define __ZPP_WATCHDOG_HPP #include #include +#include namespace app { class WatchDogConfig { public: -#if 0 WatchDogConfig() { - if (!device_is_ready(m_wtd)) { + if (!device_is_ready(m_wdt)) { __ASSERT_NO_MSG(0); } - wdt_install_timeout(m_wtd, &wdt_timeout_cfg); + chan_id = wdt_install_timeout(m_wdt, &cfg); - if (auto err = wdt_setup(m_wtd, WDT_OPT_PAUSE_HALTED_BY_DBG); err < 0) { + if (auto err = wdt_setup(m_wdt, WDT_OPT_PAUSE_HALTED_BY_DBG); err < 0) { __ASSERT_NO_MSG(0); } - wdt_timeout_cfg cfg; } + auto Feed() { wdt_feed(m_wdt, chan_id); } private: - const device *m_wtd{DEVICE_DT_GET(DT_ALIAS(watchdog0))}; -#endif + const device *m_wdt{DEVICE_DT_GET(DT_ALIAS(watchdog0))}; + static constexpr wdt_timeout_cfg cfg{ + .window{0, 1000}, + .callback = nullptr, + .flags = WDT_FLAG_RESET_SOC, + }; + int chan_id; }; } // namespace app diff --git a/prj.conf b/prj.conf index af710e3..3da563d 100644 --- a/prj.conf +++ b/prj.conf @@ -10,4 +10,5 @@ CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_PMC_COM=y CONFIG_HWINFO=y -CONFIG_REBOOT=y \ No newline at end of file +CONFIG_REBOOT=y +CONFIG_WATCHDOG=y \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 546c98a..2ec5d9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,9 +14,9 @@ enum Command { kTemp = 0x00, kGetId, kRunningState }; enum RunningState {}; constexpr auto kDeviceIdSize = 20; enum StatusId : uint8_t { kRunning, kPause, kError, kStandby }; +volatile bool flag{false}; } // namespace -volatile bool flag{false}; auto main(void) -> int { pmc.SetRxCallback(kGetId, [](uart_com::DataType data) -> void { uint8_t buffer[kDeviceIdSize]; @@ -30,7 +30,7 @@ auto main(void) -> int { zpp::println("Status Error: {}", zpp::error_str(code)); }); }); - + auto wdt = app::WatchDogConfig{}; sensor_trigger tri{.type = SENSOR_TRIG_DATA_READY, .chan = SENSOR_CHAN_AMBIENT_TEMP}; if (auto r = sensor_trigger_set( @@ -52,6 +52,7 @@ auto main(void) -> int { flag = false; } + wdt.Feed(); } return 0; } \ No newline at end of file