Watchdog
LibXR supports automatic management of STM32 Independent Watchdog (IWDG), including scheduled feed tasks or dedicated threads. It is recommended to enable hardware watchdogs in all production firmware to improve system reliability.
Overview
- Supports automatic code generation and configuration for IWDG instances.
- Two auto-feed modes: timer task or dedicated thread, suitable for bare-metal or RTOS.
- Behavior can be customized via YAML config (feed period, thread stack, priority, etc).
Watchdog Code Example
// Create and initialize watchdog instance
STM32Watchdog iwdg1(&hiwdg1, 1000, 250); // 1s timeout, feed interval 250ms
// Auto-feed (Option 1: as timer task, good for bare-metal or simple polling)
auto iwdg1_task = Timer::CreateTask(iwdg1.TaskFun, reinterpret_cast<LibXR::Watchdog *>(&iwdg1), 250);
Timer::Add(iwdg1_task);
Timer::Start(iwdg1_task);
// Auto-feed (Option 2: as thread, suitable for RTOS)
LibXR::Thread iwdg1_thread;
iwdg1_thread.Create(reinterpret_cast<LibXR::Watchdog *>(&iwdg1), iwdg1.ThreadFun, "iwdg1_wdg", 1024,
static_cast<LibXR::Thread::Priority>(3));
Configuration File
Configure watchdog behavior via .config.yaml
:
# IWDG instance and parameters
IWDG:
iwdg1:
timeout_ms: 1000 # Timeout in milliseconds
feed_interval_ms: 250 # Feed interval in milliseconds
# Global watchdog options
Watchdog:
run_as_thread: true # Whether to run as thread (otherwise uses timer)
feed_interval_ms: 1024 # Stack size for thread (only effective if thread enabled)
thread_stack_depth: 3 # Thread priority (only effective if thread enabled)
thread_priority: 250 # Feed interval in ms for timer mode (default 250)
- If
RunAsThread: true
, each enabled IWDG generates a thread; thread parameters are global.- If
RunAsThread: false
, each watchdog uses a periodic timer task.
Code Generation Command
After editing .config.yaml
, regenerate the code:
xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp
Notes
The MX_IWDG_Init()
function generated by STM32CubeMX will enable the watchdog immediately, and it cannot be reconfigured or disabled except by reset. You can disable this function's generation and call in CubeMX under Project Manager → Advanced Settings.