Skip to content

Commit

Permalink
feat(task/timer): allow watchdog to start even if task/timer has not (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
finger563 authored Sep 20, 2024
1 parent c64cb02 commit 3da3e61
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 0 additions & 4 deletions components/task/src/task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ bool Task::start_watchdog() {
logger_.debug("Watchdog not enabled in the configuration, cannot start watchdog!");
return false;
#else
if (!started_) {
logger_.warn("Task not started, cannot start watchdog!");
return false;
}
if (watchdog_started_) {
logger_.debug("Watchdog already started!");
return false;
Expand Down
19 changes: 14 additions & 5 deletions components/timer/example/main/timer_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,16 @@ extern "C" void app_main(void) {
logger.info("High resolution timer 1 started: {}", started);

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

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

std::this_thread::sleep_for(550ms);
// ensure we can run the watchdog on a oneshot timer which is started after
// we start the watchdog
period_us = 1000 * 100;
started = high_resolution_timer2.oneshot(period_us);
logger.info("High resolution timer 2 started: {}", started);

std::this_thread::sleep_for(400ms);

std::error_code ec;
std::string watchdog_info = espp::Task::get_watchdog_info(ec);
Expand Down
4 changes: 0 additions & 4 deletions components/timer/src/high_resolution_timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ bool HighResolutionTimer::start_watchdog() {
logger_.debug("Watchdog timer already running");
return false;
}
if (!is_running()) {
logger_.error("Cannot start watchdog timer, timer is not running");
return false;
}
auto err = esp_task_wdt_add_user(get_name().c_str(), &wdt_handle_);
if (err != ESP_OK) {
logger_.error("Failed to start watchdog timer: {}", esp_err_to_name(err));
Expand Down

0 comments on commit 3da3e61

Please sign in to comment.