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