πΌπ» embassy-task-watchdog π‘οΈ
A robust, flexible watchdog management library for embedded systems that multiplexes multiple task watchdogs into a single hardware watchdog timer, preventing system lockups when tasks fail to respond.
β¨ Key Features
- π Hardware Agnostic API: Implements a consistent, asynchronous interface across different embedded microcontrollers by leveraging
embassy. - π Task Multiplexing: Consolidates multiple independent task watchdogs into a single hardware watchdog, triggering if any task fails to check in.
- π Compile-time Task Management: The
embassy_task_watchdog::taskmacro replacesembassy_executor::task, and automatically registers the task with the Watchdog. - π¦ No-alloc Mode: All memory allocations are performed during compilation.
- β±οΈ Configurable Timeouts: Individual timeout durations for each registered task.
- π§ͺ
no_stdCompatible: Designed for resource-constrained embedded environments without an operating system. - π¦ Compile-time Total Tasks Check: By default, the library supports 32 watchdog tasks. The limit can be changed
by setting the
EMBASSY_TASK_WATCHDOG_MAX_TASKSvariable either in your.cargo/config.toml, or by passing it as an environment variable to cargo, e.g.EMBASSY_TASK_WATCHDOG_MAX_TASKS=8 cargo build. The check is disabled in debug builds to prevent errors in IDEs, but exceeding the number of tasks will trigger a compiler error in the release build.
π Quick Start
Examples are provided for Raspberry Pi series of microcontrollers, as well as the STM32 microcontrollers using embassy. The examples support the Pico, Pico 2 and STM32F103C8 (blue pill).
First, install Rust
Add the appropriate target(s):
Next, install probe-rs
Now connect your Pico/Pico 2/STM32F103C8 device to a connected debug probe, and go into one of:
- For RP2040/Pi Pico:
examples/task-pico - For RP235XA/Pi Pico 2:
examples/task-pico2 - For STM32F103C8 (blue pill):
examples/task-stm32
Then execute
To understand how to use embassy-task-watchdog yourself, check out one of the examples:
task-pico- A very basic Pi Pico async exampletask-pico2- A very basic Pi Pico 2 async exampletask-stm32- A very basic Blue Pill async example
π Usage
The library supports the embassy-executor asynchronous API.
To use in your project, add the following line to your Cargo.toml file:
= { = "0.0.2", = ["rp"] } # additionally, supports defmt for logging, stm32 for STM32 devices...
π οΈ Features
rp: For Raspberry Pi Pico (RP2040) / Pico 2 (RP235xA) or RP235xB based devices. Set up for the correct chip in yourCargo.tomlby selecting the correct feature in theembassy-rpdependency.defmt-embassy-rp:defmtdebugging for the Embassy executors.
stm32: For STM32 series of devices. Set up for the correct chip in yourCargo.tomlby selecting the correct feature in theembassy-stm32dependency.defmt-embassy-stm32:defmtdebugging for Embassy executors.
defmt: Enabledefmt::Formatfor theembassy-task-watchdogcrate.defmg-messages: Enable log messages from theembassy-task-watchdogcrate.
π§ Core Concepts
- Task Registration: Each monitored task is registered with its own timeout period. The registration is automatic.
- Feeding: Tasks must feed, or pet, the watchdog within their timeout period to prevent a reset.
- Task Multiplexing: The library efficiently manages multiple task timeouts through a single hardware watchdog, triggering if any individual task fails to check in.
β‘Asynchronous API using Embassy
Tasks feed the watchdog asynchronously, powered by Embassy:
// Imports
use ;
// Setup
let = create_watchdog!;
// Spawn the task and pass the watchdog
spawner.must_spawn;
// Spawn the hardware watchdog runner and pass the watchdogrunner
spawner.must_spawn;
// Define the hardware watchdog runner task
async !
// In your application tasks
async !
// Implement other tasks
ποΈ Platform Support
The crate includes first-class support for:
- RP2040 and RP2350 (Raspberry Pi Pico and Pico 2) via the
rpfeature. - STM32 family via the
stm32feature. defmtfordefmtbased logging.
π License
Licensed under either of the following, at your option:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
π₯ Inspiration
This work is inspired heavily by the task-watchdog
crate by Piers Finlayson, which provides
a task multiplexing watchdog for embedded systems. It has not been maintained in almost
a year (last commit was on April 10, 2025). This crate is a fork of that work, with the following
goals:
- Update the codebase to be compatible with the latest versions of Rust and Embassy, and to use modern Rust features and idioms.
- Automate the task registration process with a procedural macro, to reduce boilerplate and make it easier to use.
- Get rid of custom task identifier types through the
Idtrait that had to be manually managed. - Allow compile-time flexibility without allocations, with as many checks as possible.
To achieve these goals, the codebase has been refactored to limit its scope to embassy-based async applications, which is the primary use case for this crate. The API has been redesigned to be more ergonomic and easier to use, while still providing the same core functionality of multiplexing multiple task watchdogs into a single hardware watchdog timer.