Skip to content

Commit 38b2594

Browse files
committed
docs: 新增 Watchdog 驱动与 STM32 看门狗代码生成的文档
1 parent 81dbdac commit 38b2594

File tree

4 files changed

+244
-0
lines changed

4 files changed

+244
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
id: watchdog
3+
title: 看门狗
4+
sidebar_position: 11
5+
---
6+
7+
# Watchdog(看门狗)
8+
9+
`LibXR::Watchdog` 提供通用看门狗(Watchdog)抽象接口,支持配置溢出时间、自动喂狗周期等参数,并提供启动、停止和手动喂狗等控制接口,适配多线程和定时任务等不同运行环境。
10+
11+
## 接口概览
12+
13+
### 配置结构体
14+
15+
```cpp
16+
struct Configuration {
17+
uint32_t timeout_ms; // 看门狗溢出时间(毫秒)
18+
uint32_t feed_ms; // 自动喂狗周期(毫秒)
19+
};
20+
```
21+
22+
### 构造与配置
23+
24+
```cpp
25+
Watchdog();
26+
virtual ~Watchdog();
27+
28+
virtual ErrorCode SetConfig(const Configuration& config) = 0;
29+
```
30+
31+
### 控制接口
32+
33+
```cpp
34+
virtual ErrorCode Start() = 0;
35+
virtual ErrorCode Stop() = 0;
36+
virtual ErrorCode Feed() = 0;
37+
```
38+
39+
### 自动喂狗辅助函数
40+
41+
```cpp
42+
static void ThreadFun(Watchdog* wdg);
43+
static void TaskFun(Watchdog* wdg);
44+
```
45+
46+
- `ThreadFun`:用于线程环境中的自动喂狗循环;
47+
- `TaskFun`:适用于定时轮询任务系统中的自动喂狗函数。
48+
49+
## 特性总结
50+
51+
- 支持设置溢出时间与自动喂狗周期;
52+
- 提供手动 `Feed` 接口与自动喂狗辅助函数;
53+
- 适用于多种嵌入式运行模型(如 RTOS 线程、定时任务);
54+
- 平台无关,便于在不同硬件平台上统一使用;
55+
- 可扩展,派生类实现具体底层驱动逻辑。

docs/code_gen/stm32/watchdog.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
3+
id: stm32-code-gen-watchdog
4+
title: 看门狗
5+
sidebar_position: 11
6+
---
7+
8+
9+
# 看门狗
10+
11+
LibXR 支持 **STM32 独立看门狗(IWDG)** 的自动管理,包括定时喂狗任务或独立线程。推荐所有产品固件启用硬件看门狗,以提升系统可靠性。
12+
13+
## 基本说明
14+
15+
* 支持 IWDG 实例自动生成代码与配置。
16+
* 可选自动喂狗方式:**定时任务****独立线程**,适配裸机/RTOS。
17+
* 可通过 YAML 配置自定义喂狗周期、线程栈、优先级等参数。
18+
19+
## 看门狗代码示例
20+
21+
```cpp
22+
// 创建并初始化看门狗实例
23+
STM32Watchdog iwdg1(&hiwdg1, 1000, 250); // 溢出 1s,喂狗周期 250ms
24+
25+
// 自动喂狗(方式一:作为定时任务,适合裸机或简单轮询)
26+
auto iwdg1_task = Timer::CreateTask(iwdg1.TaskFun, reinterpret_cast<LibXR::Watchdog *>(&iwdg1), 250);
27+
Timer::Add(iwdg1_task);
28+
Timer::Start(iwdg1_task);
29+
30+
// 自动喂狗(方式二:作为线程,适合 RTOS)
31+
LibXR::Thread iwdg1_thread;
32+
iwdg1_thread.Create(reinterpret_cast<LibXR::Watchdog *>(&iwdg1), iwdg1.ThreadFun, "iwdg1_wdg", 1024,
33+
static_cast<LibXR::Thread::Priority>(3));
34+
```
35+
36+
## 配置文件说明
37+
38+
通过配置文件灵活控制看门狗行为:
39+
40+
```yaml
41+
# IWDG 外设启用与参数
42+
IWDG:
43+
iwdg1:
44+
timeout_ms: 1000 # 溢出超时时间(毫秒)
45+
feed_ms: 250 # 喂狗周期(毫秒)
46+
47+
# Watchdog 相关全局配置
48+
Watchdog:
49+
RunAsThread: true # 是否作为线程运行(否则定时任务)
50+
ThreadStackDepth: 1024 # 线程栈深度(仅在线程下有效)
51+
ThreadPriority: 3 # 线程优先级(仅在线程下有效)
52+
FeedInterval: 250 # 定时任务喂狗周期(毫秒,默认250)
53+
```
54+
55+
> *`RunAsThread: true`,则每个启用的 IWDG 会自动生成线程,线程参数可全局配置。
56+
> *`RunAsThread: false`,则采用定时任务方式喂狗。
57+
58+
## 生成代码命令
59+
60+
修改 `.config.yaml` 后,重新生成代码:
61+
62+
```bash
63+
xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp
64+
```
65+
66+
## 注意事项
67+
68+
STM32CubeMX生成的MX_IWDG_Init()会直接使能看门狗,而且除了复位无法关闭和重新配置。可以在CubeMX中的Project Manager->Advanced Settings中关闭此函数的生成和调用
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
id: watchdog
3+
title: Watchdog
4+
sidebar_position: 11
5+
---
6+
7+
# Watchdog
8+
9+
`LibXR::Watchdog` provides a general-purpose abstract interface for watchdog functionality. It supports configuring the overflow timeout, auto-feed interval, and provides control methods like start, stop, and manual feeding. It is suitable for multi-threaded environments or timer-based task scheduling systems.
10+
11+
## Interface Overview
12+
13+
### Configuration Structure
14+
15+
```cpp
16+
struct Configuration {
17+
uint32_t timeout_ms; // Watchdog overflow time (milliseconds)
18+
uint32_t feed_ms; // Auto-feed interval (milliseconds)
19+
};
20+
```
21+
22+
### Constructor and Configuration
23+
24+
```cpp
25+
Watchdog();
26+
virtual ~Watchdog();
27+
28+
virtual ErrorCode SetConfig(const Configuration& config) = 0;
29+
```
30+
31+
### Control Interface
32+
33+
```cpp
34+
virtual ErrorCode Start() = 0;
35+
virtual ErrorCode Stop() = 0;
36+
virtual ErrorCode Feed() = 0;
37+
```
38+
39+
### Auto-Feed Helper Functions
40+
41+
```cpp
42+
static void ThreadFun(Watchdog* wdg);
43+
static void TaskFun(Watchdog* wdg);
44+
```
45+
46+
- `ThreadFun`: Used in threaded environments for continuous auto-feeding;
47+
- `TaskFun`: Used in polling/timer task systems for periodic auto-feeding.
48+
49+
## Feature Summary
50+
51+
- Supports configurable overflow timeout and auto-feed interval;
52+
- Provides manual `Feed` function and auto-feed helpers;
53+
- Suitable for various embedded execution models such as RTOS threads or timer tasks;
54+
- Platform-independent, allowing unified usage across different hardware;
55+
- Easily extendable, with implementation-specific logic in derived classes.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
id: stm32-code-gen-watchdog
3+
title: Watchdog
4+
sidebar_position: 11
5+
---
6+
7+
# Watchdog
8+
9+
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.
10+
11+
## Overview
12+
13+
* Supports automatic code generation and configuration for IWDG instances.
14+
* Two auto-feed modes: **timer task** or **dedicated thread**, suitable for bare-metal or RTOS.
15+
* Behavior can be customized via YAML config (feed period, thread stack, priority, etc).
16+
17+
## Watchdog Code Example
18+
19+
```cpp
20+
// Create and initialize watchdog instance
21+
STM32Watchdog iwdg1(&hiwdg1, 1000, 250); // 1s timeout, feed interval 250ms
22+
23+
// Auto-feed (Option 1: as timer task, good for bare-metal or simple polling)
24+
auto iwdg1_task = Timer::CreateTask(iwdg1.TaskFun, reinterpret_cast<LibXR::Watchdog *>(&iwdg1), 250);
25+
Timer::Add(iwdg1_task);
26+
Timer::Start(iwdg1_task);
27+
28+
// Auto-feed (Option 2: as thread, suitable for RTOS)
29+
LibXR::Thread iwdg1_thread;
30+
iwdg1_thread.Create(reinterpret_cast<LibXR::Watchdog *>(&iwdg1), iwdg1.ThreadFun, "iwdg1_wdg", 1024,
31+
static_cast<LibXR::Thread::Priority>(3));
32+
```
33+
34+
## Configuration File
35+
36+
Configure watchdog behavior via `.config.yaml`:
37+
38+
```yaml
39+
# IWDG instance and parameters
40+
IWDG:
41+
iwdg1:
42+
timeout_ms: 1000 # Timeout in milliseconds
43+
feed_ms: 250 # Feed interval in milliseconds
44+
45+
# Global watchdog options
46+
Watchdog:
47+
RunAsThread: true # Whether to run as thread (otherwise uses timer)
48+
ThreadStackDepth: 1024 # Stack size for thread (only effective if thread enabled)
49+
ThreadPriority: 3 # Thread priority (only effective if thread enabled)
50+
FeedInterval: 250 # Feed interval in ms for timer mode (default 250)
51+
```
52+
53+
> * If `RunAsThread: true`, each enabled IWDG generates a thread; thread parameters are global.
54+
> * If `RunAsThread: false`, each watchdog uses a periodic timer task.
55+
56+
## Code Generation Command
57+
58+
After editing `.config.yaml`, regenerate the code:
59+
60+
```bash
61+
xr_gen_code_stm32 -i ./.config.yaml -o ./User/app_main.cpp
62+
```
63+
64+
## Notes
65+
66+
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**.

0 commit comments

Comments
 (0)