libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
esp_watchdog.hpp
1#pragma once
2
3#include "esp_def.hpp"
4
5#include <cstdint>
6
7#include "hal/wdt_hal.h"
8#include "soc/soc_caps.h"
9#include "watchdog.hpp"
10
11namespace LibXR
12{
13
14#if CONFIG_ESP_TASK_WDT_EN && !CONFIG_ESP_TASK_WDT_USE_ESP_TIMER
15#define LIBXR_ESP_WDT_MWDT0_RESERVED 1
16#else
17#define LIBXR_ESP_WDT_MWDT0_RESERVED 0
18#endif
19
20#if SOC_TIMER_GROUPS >= 2
21#if CONFIG_ESP_INT_WDT
22#define LIBXR_ESP_WDT_MWDT1_RESERVED 1
23#else
24#define LIBXR_ESP_WDT_MWDT1_RESERVED 0
25#endif
26#else
27#define LIBXR_ESP_WDT_MWDT1_RESERVED 1
28#endif
29
30#if (LIBXR_ESP_WDT_MWDT1_RESERVED == 0)
31constexpr wdt_inst_t kESP32WatchdogDefaultInstance = WDT_MWDT1;
32#elif (LIBXR_ESP_WDT_MWDT0_RESERVED == 0)
33constexpr wdt_inst_t kESP32WatchdogDefaultInstance = WDT_MWDT0;
34#else
35constexpr wdt_inst_t kESP32WatchdogDefaultInstance = WDT_RWDT;
36#endif
37
38static_assert(!(kESP32WatchdogDefaultInstance == WDT_MWDT0 &&
39 LIBXR_ESP_WDT_MWDT0_RESERVED),
40 "LibXR ESP32Watchdog selected MWDT0, but MWDT0 is reserved by ESP-IDF.");
41static_assert(!(kESP32WatchdogDefaultInstance == WDT_MWDT1 &&
42 LIBXR_ESP_WDT_MWDT1_RESERVED),
43 "LibXR ESP32Watchdog selected MWDT1, but MWDT1 is reserved by ESP-IDF.");
44
45class ESP32Watchdog : public Watchdog
46{
47 public:
48 explicit ESP32Watchdog(uint32_t timeout_ms = 1000, uint32_t feed_ms = 250);
49
50 ErrorCode SetConfig(const Configuration& config) override;
51 ErrorCode Feed() override;
52 ErrorCode Start() override;
53 ErrorCode Stop() override;
54
55 private:
56 static bool IsInstanceSupported(wdt_inst_t instance);
57 ErrorCode InitializeHardware();
58 ErrorCode ApplyConfiguration();
59
60 wdt_hal_context_t hal_{};
61 const wdt_inst_t instance_ = kESP32WatchdogDefaultInstance;
62 bool initialized_ = false;
63 bool started_ = false;
64};
65
66} // namespace LibXR
67
68#undef LIBXR_ESP_WDT_MWDT0_RESERVED
69#undef LIBXR_ESP_WDT_MWDT1_RESERVED
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
通用看门狗(Watchdog)抽象接口 General Watchdog interface for both thread and task style usage
Definition watchdog.hpp:14
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
看门狗配置结构体 Configuration for the watchdog
Definition watchdog.hpp:27