Skip to content

Commit 3da3e61

Browse files
authored
feat(task/timer): allow watchdog to start even if task/timer has not (#329)
* feat(task/timer): allow watchdog to start even if task/timer has not * update example to show use of start_watchdog before starting the timer, and to use it on a oneshot timer
1 parent c64cb02 commit 3da3e61

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

components/task/src/task.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,6 @@ bool Task::start_watchdog() {
112112
logger_.debug("Watchdog not enabled in the configuration, cannot start watchdog!");
113113
return false;
114114
#else
115-
if (!started_) {
116-
logger_.warn("Task not started, cannot start watchdog!");
117-
return false;
118-
}
119115
if (watchdog_started_) {
120116
logger_.debug("Watchdog already started!");
121117
return false;

components/timer/example/main/timer_example.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,16 @@ extern "C" void app_main(void) {
300300
logger.info("High resolution timer 1 started: {}", started);
301301

302302
// make another HighResolutionTimer
303+
auto timer2_fn = [&]() {
304+
// sleep here to ensure watchdog triggers
305+
std::this_thread::sleep_for(350ms);
306+
// we don't want to stop, so return false
307+
return false;
308+
};
303309
auto high_resolution_timer2 =
304310
espp::HighResolutionTimer({.name = "High Resolution Timer 2",
305-
.callback = timer_fn,
311+
.callback = timer2_fn,
306312
.log_level = espp::Logger::Verbosity::DEBUG});
307-
period_us = 1000 * 500; // 500ms, which is longer than the watchdog period
308-
started = high_resolution_timer2.start(period_us);
309-
logger.info("High resolution timer 2 started: {}", started);
310313

311314
// configure the task watchdog
312315
static constexpr bool panic_on_watchdog_timeout = false;
@@ -315,7 +318,13 @@ extern "C" void app_main(void) {
315318
// start the watchdog timer for this timer
316319
high_resolution_timer2.start_watchdog();
317320

318-
std::this_thread::sleep_for(550ms);
321+
// ensure we can run the watchdog on a oneshot timer which is started after
322+
// we start the watchdog
323+
period_us = 1000 * 100;
324+
started = high_resolution_timer2.oneshot(period_us);
325+
logger.info("High resolution timer 2 started: {}", started);
326+
327+
std::this_thread::sleep_for(400ms);
319328

320329
std::error_code ec;
321330
std::string watchdog_info = espp::Task::get_watchdog_info(ec);

components/timer/src/high_resolution_timer.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ bool HighResolutionTimer::start_watchdog() {
6868
logger_.debug("Watchdog timer already running");
6969
return false;
7070
}
71-
if (!is_running()) {
72-
logger_.error("Cannot start watchdog timer, timer is not running");
73-
return false;
74-
}
7571
auto err = esp_task_wdt_add_user(get_name().c_str(), &wdt_handle_);
7672
if (err != ESP_OK) {
7773
logger_.error("Failed to start watchdog timer: {}", esp_err_to_name(err));

0 commit comments

Comments
 (0)