Zephyr 應用在 FPB-RA6E2 開發板上的評測:多線程與看門狗
本文章旨在評估使用 Zephyr RTOS 在 Renesas FPB-RA6E2 開發板上實現多線程調度與硬件看門狗功能的應用。評估內容包括任務調度、看門狗初始化流程、主程序邏輯的詳細解析,以及實驗現象與數據分析。
1. 硬件連接與引腳定義
本實驗涉及的任務調度和看門狗功能不依賴特定的物理引腳,但需要確保開發板支持硬件看門狗外設。
| 功能 | 物理引腳 (Pin) | 信號定義 | 接線說明 |
|---|---|---|---|
| 看門狗 | 內部外設 | WDT Timer | 無需外部連接 |
2. 軟件環境配置
2.1 Device Tree Overlay
設備樹用于定義硬件看門狗的初始狀態。以下是關鍵配置:
看門狗配置
wdt: wdt@40083400 {
compatible = "renesas,ra-wdt"; /* in externalzephyrdtsarmrenesasrara6ra6-cm33-common.dtsi:542 */
reg = < 0x40083400 0x200 >; /* in externalzephyrdtsarmrenesasrara6ra6-cm33-common.dtsi:543 */
clocks = < &pclkb 0x0 0x0 >; /* in externalzephyrdtsarmrenesasrara6ra6-cm33-common.dtsi:544 */
status = "okay"; /* in externalzephyrboardsrenesasfpb_ra6e2fpb_ra6e2.dts:135 */
};
};
};
2.2 Kconfig 配置 (prj.conf)
確保啟用了多線程和看門狗驅動支持:
CONFIG_MULTITHREADING=y
CONFIG_WATCHDOG=y
CONFIG_TASK_WDT=y
CONFIG_LOG=y
3. 代碼邏輯分析
3.1 主程序代碼(main.c)
#include < zephyr/kernel.h >
#include < zephyr/device.h >
#include < zephyr/drivers/watchdog.h >
#include < zephyr/sys/reboot.h >
#include < zephyr/task_wdt/task_wdt.h >
#include < zephyr/sys/printk.h >
#include < stdbool.h >
/*
* To use this sample, either the devicetree's /aliases must have a
* 'watchdog0' property, or one of the following watchdog compatibles
* must have an enabled node.
*
* If the devicetree has a watchdog node, we get the watchdog device
* from there. Otherwise, the task watchdog will be used without a
* hardware watchdog fallback.
*/
#if DT_NODE_HAS_STATUS_OKAY(DT_ALIAS(watchdog0))
#define WDT_NODE DT_ALIAS(watchdog0)
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_window_watchdog)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(st_stm32_window_watchdog)
#elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_watchdog)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(st_stm32_watchdog)
#elif DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_wdt)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(nordic_nrf_wdt)
#elif DT_HAS_COMPAT_STATUS_OKAY(espressif_esp32_watchdog)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(espressif_esp32_watchdog)
#elif DT_HAS_COMPAT_STATUS_OKAY(silabs_gecko_wdog)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(silabs_gecko_wdog)
#elif DT_HAS_COMPAT_STATUS_OKAY(nxp_wdog32)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(nxp_wdog32)
#elif DT_HAS_COMPAT_STATUS_OKAY(microchip_xec_watchdog)
#define WDT_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(microchip_xec_watchdog)
#else
#define WDT_NODE DT_INVALID_NODE
#endif
static void task_wdt_callback(int channel_id, void *user_data)
{
printk("Task watchdog channel %d callback, thread: %sn",
channel_id, k_thread_name_get((k_tid_t)user_data));
/*
* If the issue could be resolved, call task_wdt_feed(channel_id) here
* to continue operation.
*
* Otherwise we can perform some cleanup and reset the device.
*/
printk("Resetting device...n");
sys_reboot(SYS_REBOOT_COLD);
}
int main(void)
{
int ret;
const struct device *const hw_wdt_dev = DEVICE_DT_GET_OR_NULL(WDT_NODE);
printk("Task watchdog sample application.n");
if (!device_is_ready(hw_wdt_dev)) {
printk("Hardware watchdog not ready; ignoring it.n");
ret = task_wdt_init(NULL);
} else {
ret = task_wdt_init(hw_wdt_dev);
}
if (ret != 0) {
printk("task wdt init failure: %dn", ret);
return 0;
}
/* passing NULL instead of callback to trigger system reset */
int task_wdt_id = task_wdt_add(1100U, NULL, NULL);
while (true) {
printk("Main thread still alive...n");
task_wdt_feed(task_wdt_id);
k_sleep(K_MSEC(1000));
}
return 0;
}
/*
* This high-priority thread needs a tight timing
*/
void control_thread(void)
{
int task_wdt_id;
int count = 0;
printk("Control thread started.n");
/*
* Add a new task watchdog channel with custom callback function and
* the current thread ID as user data.
*/
task_wdt_id = task_wdt_add(100U, task_wdt_callback,
(void *)k_current_get());
while (true) {
if (count == 50) {
printk("Control thread getting stuck...n");
k_sleep(K_FOREVER);
}
task_wdt_feed(task_wdt_id);
k_sleep(K_MSEC(50));
count++;
}
}
K_THREAD_DEFINE(control, 1024, control_thread, NULL, NULL, NULL, -1, 0, 1000);
3.2 核心流程
多線程調度
- 創建高優先級線程
control_thread,用于模擬實時任務。 - 主線程運行低優先級任務,定期喂養看門狗。
- 當高優先級線程卡住時,觸發看門狗回調或系統復位。
看門狗流程
- 初始化硬件看門狗或任務看門狗。
- 為每個線程注冊獨立的看門狗通道,并設置超時時間。
- 定期調用
task_wdt_feed函數喂養看門狗。 - 如果某個線程未能及時喂養,觸發回調函數或系統復位。
3.3 關鍵 API 使用
以下是代碼中使用的關鍵 API:
多線程管理
K_THREAD_DEFINE(thread_name, stack_size, entry_point, arg1, arg2, arg3, prio, options, delay);
thread_name: 線程名稱。stack_size: 線程棧大小。entry_point: 線程入口函數。prio: 線程優先級。
看門狗初始化
int task_wdt_init(const struct device *hw_wdt_dev);
hw_wdt_dev: 硬件看門狗設備句柄(可為空)。
看門狗通道管理
int task_wdt_add(uint32_t timeout_ms, task_wdt_callback_t callback, void *user_data);
void task_wdt_feed(int channel_id);
timeout_ms: 超時時間(毫秒)。callback: 超時回調函數。channel_id: 看門狗通道 ID。
4. 實驗現象與數據分析
4.1 多線程調度行為
- 主線程 : 每秒打印一次日志,表明其仍在運行。
- 高優先級線程 : 模擬實時任務,每 50 毫秒喂養看門狗,運行 50 次后故意卡住。
終端應顯示如下數據流:
*** Booting Zephyr OS build v4.2.0 ***
Task watchdog sample application.
[00:00:00.005,000] [0m< inf > wdt_renesas_ra: actual window min = 0.00 ms[0m
[00:00:00.012,000] [0m< inf > wdt_renesas_ra: actual window max = 671.00 ms[0m
[00:00:00.019,000] [0m< inf > wdt_renesas_ra: wdt timeout was set successfully[0m
Main thread still alive...
Control thread started.
Main thread still alive...
Main thread still alive...
Main thread still alive...
Control thread getting stuck...
Task watchdog channel 1 callback, thread: control
Resetting d?*** Booting Zephyr OS build v4.2.0 ***
Task watchdog sample application.
[00:00:00.005,000] [0m< inf > wdt_renesas_ra: actual window min = 0.00 ms[0m
[00:00:00.012,000] [0m< inf > wdt_renesas_ra: actual window max = 671.00 ms[0m
[00:00:00.019,000] [0m< inf > wdt_renesas_ra: wdt timeout was set successfully[0m
Main thread still alive...
Control thread started.
4.2 看門狗復位行為
- 正常運行 : 看門狗被定期喂養,系統保持穩定。
- 異常情況 : 高優先級線程卡住后,看門狗超時觸發回調或系統復位。
- 視覺效果 : 使用調試器觀察復位信號,確認系統重啟。
5. 測評總結
本程序成功演示了 Renesas RA6E2 在 Zephyr RTOS 下的多線程調度與看門狗功能。通過模擬高優先級任務卡住的場景,驗證了任務看門狗對系統穩定性的保護作用。代碼結構清晰,適配了最新的驅動 API,適用于初學者學習和開發者快速驗證硬件功能。
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
單片機
+關注
關注
6076文章
45494瀏覽量
670267 -
看門狗
+關注
關注
10文章
610瀏覽量
72934 -
開發板
+關注
關注
26文章
6289瀏覽量
118050 -
Zephyr
+關注
關注
0文章
58瀏覽量
6579
發布評論請先 登錄
相關推薦
熱點推薦
STM32中的獨立看門狗和窗口看門狗是什么
在早期的MCU中是沒有看門狗這種東西的,所以產品就很容易出現死機,跑飛的情況。為了避免這種情況的出現,后期的MCU都集成了看門狗的功能。但是目前看門狗發展到今天基本上分為兩大類:獨立看門狗
【瑞薩FPB-RA6E2試用】【瑞薩FPB-RA6E2】看門狗(Watchdog Timer, WDT)個人理解及資料整理
【瑞薩FPB-RA6E2】看門狗(Watchdog Timer, WDT)個人理解及資料整理
瑞薩
發表于 01-14 11:09
【瑞薩FPB-RA6E2試用】Zephyr看門狗(WDT)機制
程序卡死未按時喂狗,看門狗就會直接切斷并復位MCU的電源軌,讓系統重啟。
在傳統的裸機開發中,配置看門狗需要翻閱芯片手冊,操作復雜的控制寄存器和分頻器。而在Zephyr中,無論底層是瑞
發表于 02-20 10:35
stm32看門狗時間計算 獨立看門狗和窗口看門狗的特性是什么
本文為您講解STM看門狗時間計算(時限)與頻率計算,獨立看門狗和窗口看門狗的特性、區別與聯系。
發表于 10-10 10:41
?9382次閱讀
STM32看門狗配置(獨立看門狗IWDG和窗口看門狗WWDG)
stm32自帶兩個看門狗模塊,獨立看門狗IWDG和窗口看門狗WWDG。看門狗主要作用是可用來檢測和解決由軟件錯誤引起的故障;當計數器達到給定的超時值時,觸發一個中斷(僅適用于窗口型
發表于 11-09 17:17
?8839次閱讀
MCU獨立看門狗與窗口看門狗的區別
早期的MCU沒有看門狗,就容易引起有些產品死機了不能重啟工作。為了避免這個問題,后期的MCU在內部集成了看門狗的功能。為了滿足更多使用場景,現在很多MCU都集成了兩個看門狗:獨立看門狗
發表于 10-28 20:06
?8次下載
STM32中的獨立看門狗和窗口看門狗
一、前言 在早期的MCU中是沒有看門狗這種東西的,所以產品就很容易出現死機,跑飛的情況。為了避免這種情況的出現,后期的MCU都集成了看門狗的功能。但是目前看門狗發展到今天基本上分為兩大類:獨立
STM32中的獨立看門狗和窗口看門狗
在早期的MCU中是沒有看門狗這種東西的,所以產品就很容易出現死機,跑飛的情況。為了避免這種情況的出現,后期的MCU都集成了看門狗的功能。但是目前看門狗發展到今天基本上分為兩大類:獨立看門狗
瑞薩e2studio----獨立看門狗IWDT
概述本篇文章主要介紹如何使用e2studio對瑞薩進行獨立看門狗IWDT配置,并且配置RTC時鐘產生1s的周期中斷,通過串口打印查看獨立看門狗IWDT的計數值。
【瑞薩RA × Zephyr評測】多線程和看門狗
評論