libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::ESP32Watchdog Class Reference
Inheritance diagram for LibXR::ESP32Watchdog:
[legend]
Collaboration diagram for LibXR::ESP32Watchdog:
[legend]

Public Member Functions

 ESP32Watchdog (uint32_t timeout_ms=1000, uint32_t feed_ms=250)
 
ErrorCode SetConfig (const Configuration &config) override
 初始化硬件并设置超时时间 Initialize hardware and set overflow time
 
ErrorCode Feed () override
 立即手动喂狗 Feed the watchdog immediately
 
ErrorCode Start () override
 启动看门狗 / Start the watchdog
 
ErrorCode Stop () override
 停止看门狗 / Stop the watchdog
 
- Public Member Functions inherited from LibXR::Watchdog
 Watchdog ()
 构造函数 Constructor
 
virtual ~Watchdog ()
 虚析构函数 Virtual destructor
 

Private Member Functions

ErrorCode InitializeHardware ()
 
ErrorCode ApplyConfiguration ()
 

Static Private Member Functions

static bool IsInstanceSupported (wdt_inst_t instance)
 

Private Attributes

wdt_hal_context_t hal_ {}
 
const wdt_inst_t instance_ = kESP32WatchdogDefaultInstance
 
bool initialized_ = false
 
bool started_ = false
 

Additional Inherited Members

- Static Public Member Functions inherited from LibXR::Watchdog
static void ThreadFun (Watchdog *wdg)
 Watchdog 自动喂狗线程函数 Watchdog thread function for auto-feeding.
 
static void TaskFun (Watchdog *wdg)
 Watchdog 自动喂狗定时器任务函数 Watchdog timer task function for auto-feeding.
 
- Data Fields inherited from LibXR::Watchdog
uint32_t timeout_ms_ = 3000
 溢出时间
 
uint32_t auto_feed_interval_ms = 1000
 自动喂狗间隔
 
bool auto_feed_ = false
 是否自动喂狗
 

Detailed Description

Definition at line 45 of file esp_watchdog.hpp.

Constructor & Destructor Documentation

◆ ESP32Watchdog()

LibXR::ESP32Watchdog::ESP32Watchdog ( uint32_t timeout_ms = 1000,
uint32_t feed_ms = 250 )
explicit

Definition at line 40 of file esp_watchdog.cpp.

41{
42 (void)SetConfig({timeout_ms, feed_ms});
43 (void)Start();
44}
ErrorCode Start() override
启动看门狗 / Start the watchdog
ErrorCode SetConfig(const Configuration &config) override
初始化硬件并设置超时时间 Initialize hardware and set overflow time

Member Function Documentation

◆ ApplyConfiguration()

ErrorCode LibXR::ESP32Watchdog::ApplyConfiguration ( )
private

Definition at line 100 of file esp_watchdog.cpp.

101{
102 if (!initialized_)
103 {
104 return ErrorCode::INIT_ERR;
105 }
106
107 const uint64_t ticks64 = static_cast<uint64_t>(timeout_ms_) * kWdtTicksPerMs;
108 if ((ticks64 == 0) || (ticks64 > UINT32_MAX))
109 {
111 }
112
113 const uint32_t stage0_ticks = static_cast<uint32_t>(ticks64);
114
115 wdt_hal_write_protect_disable(&hal_);
116 wdt_hal_config_stage(&hal_, WDT_STAGE0, stage0_ticks, WDT_STAGE_ACTION_RESET_SYSTEM);
117 wdt_hal_config_stage(&hal_, WDT_STAGE1, 0, WDT_STAGE_ACTION_OFF);
118 wdt_hal_config_stage(&hal_, WDT_STAGE2, 0, WDT_STAGE_ACTION_OFF);
119 wdt_hal_config_stage(&hal_, WDT_STAGE3, 0, WDT_STAGE_ACTION_OFF);
120 wdt_hal_write_protect_enable(&hal_);
121
122 return ErrorCode::OK;
123}
uint32_t timeout_ms_
溢出时间
Definition watchdog.hpp:109
@ INIT_ERR
初始化错误 | Initialization error
@ NOT_SUPPORT
不支持 | Not supported
@ OK
操作成功 | Operation successful

◆ Feed()

ErrorCode LibXR::ESP32Watchdog::Feed ( )
overridevirtual

立即手动喂狗 Feed the watchdog immediately

Returns
操作结果的错误码 Error code of the operation

Implements LibXR::Watchdog.

Definition at line 145 of file esp_watchdog.cpp.

146{
147 if (!initialized_ || !started_)
148 {
149 return ErrorCode::INIT_ERR;
150 }
151
152 wdt_hal_write_protect_disable(&hal_);
153 wdt_hal_feed(&hal_);
154 wdt_hal_write_protect_enable(&hal_);
155 return ErrorCode::OK;
156}

◆ InitializeHardware()

ErrorCode LibXR::ESP32Watchdog::InitializeHardware ( )
private

Definition at line 63 of file esp_watchdog.cpp.

64{
65 if (initialized_)
66 {
67 return ErrorCode::OK;
68 }
69
70 if (!IsInstanceSupported(instance_))
71 {
73 }
74
75 if (instance_ != WDT_RWDT)
76 {
77 int group_id = 0;
78 periph_module_t periph = PERIPH_TIMG0_MODULE;
79 if (!GetMwdtGroupInfo(instance_, &group_id, &periph))
80 {
82 }
83
84 PERIPH_RCC_ACQUIRE_ATOMIC(periph, ref_count)
85 {
86 if (ref_count == 0)
87 {
88 timer_ll_enable_bus_clock(group_id, true);
89 timer_ll_reset_register(group_id);
90 }
91 }
92 }
93
94 const uint32_t prescaler = instance_ == WDT_RWDT ? 0U : MWDT_LL_DEFAULT_CLK_PRESCALER;
95 wdt_hal_init(&hal_, instance_, prescaler, false);
96 initialized_ = true;
97 return ErrorCode::OK;
98}

◆ IsInstanceSupported()

bool LibXR::ESP32Watchdog::IsInstanceSupported ( wdt_inst_t instance)
staticprivate

Definition at line 46 of file esp_watchdog.cpp.

47{
48 switch (instance)
49 {
50 case WDT_MWDT0:
51 return true;
52#if SOC_TIMER_GROUPS >= 2
53 case WDT_MWDT1:
54 return true;
55#endif
56 case WDT_RWDT:
57 return true;
58 default:
59 return false;
60 }
61}

◆ SetConfig()

ErrorCode LibXR::ESP32Watchdog::SetConfig ( const Configuration & config)
overridevirtual

初始化硬件并设置超时时间 Initialize hardware and set overflow time

Parameters
config配置参数 Configuration
Returns
操作结果的错误码 Error code of the operation

Implements LibXR::Watchdog.

Definition at line 125 of file esp_watchdog.cpp.

126{
127 if ((config.timeout_ms == 0) || (config.feed_ms == 0) ||
128 (config.feed_ms > config.timeout_ms))
129 {
130 return ErrorCode::ARG_ERR;
131 }
132
133 timeout_ms_ = config.timeout_ms;
134 auto_feed_interval_ms = config.feed_ms;
135
136 ErrorCode ret = InitializeHardware();
137 if (ret != ErrorCode::OK)
138 {
139 return ret;
140 }
141
142 return ApplyConfiguration();
143}
uint32_t auto_feed_interval_ms
自动喂狗间隔
Definition watchdog.hpp:110
ErrorCode
定义错误码枚举
Definition libxr_def.hpp:64
@ ARG_ERR
参数错误 | Argument error

◆ Start()

ErrorCode LibXR::ESP32Watchdog::Start ( )
overridevirtual

启动看门狗 / Start the watchdog

Implements LibXR::Watchdog.

Definition at line 158 of file esp_watchdog.cpp.

159{
160 ErrorCode ret = InitializeHardware();
161 if (ret != ErrorCode::OK)
162 {
163 return ret;
164 }
165
166 ret = ApplyConfiguration();
167 if (ret != ErrorCode::OK)
168 {
169 return ret;
170 }
171
172 wdt_hal_write_protect_disable(&hal_);
173 wdt_hal_enable(&hal_);
174 wdt_hal_feed(&hal_);
175 wdt_hal_write_protect_enable(&hal_);
176
177 auto_feed_ = true;
178 started_ = true;
179 return ErrorCode::OK;
180}
bool auto_feed_
是否自动喂狗
Definition watchdog.hpp:111

◆ Stop()

ErrorCode LibXR::ESP32Watchdog::Stop ( )
overridevirtual

停止看门狗 / Stop the watchdog

Implements LibXR::Watchdog.

Definition at line 182 of file esp_watchdog.cpp.

183{
184 if (!initialized_)
185 {
186 return ErrorCode::INIT_ERR;
187 }
188
189 wdt_hal_write_protect_disable(&hal_);
190 wdt_hal_disable(&hal_);
191 wdt_hal_write_protect_enable(&hal_);
192
193 auto_feed_ = false;
194 started_ = false;
195 return ErrorCode::OK;
196}

Field Documentation

◆ hal_

wdt_hal_context_t LibXR::ESP32Watchdog::hal_ {}
private

Definition at line 60 of file esp_watchdog.hpp.

60{};

◆ initialized_

bool LibXR::ESP32Watchdog::initialized_ = false
private

Definition at line 62 of file esp_watchdog.hpp.

◆ instance_

const wdt_inst_t LibXR::ESP32Watchdog::instance_ = kESP32WatchdogDefaultInstance
private

Definition at line 61 of file esp_watchdog.hpp.

◆ started_

bool LibXR::ESP32Watchdog::started_ = false
private

Definition at line 63 of file esp_watchdog.hpp.


The documentation for this class was generated from the following files: