1、程序介紹
本程序是基于OpenHarmony標(biāo)準(zhǔn)系統(tǒng)編寫的平臺(tái)驅(qū)動(dòng)案例:Watchdog
由于開發(fā)板只有1個(gè)Watchdog,且已被OpenHarmony內(nèi)部占用,本案例只能讓讀者熟悉Watchdog相關(guān)接口以及應(yīng)用,無法應(yīng)用呈現(xiàn)。
2、基礎(chǔ)知識(shí)
2.1、Watchdog簡(jiǎn)介
看門狗(Watchdog),又稱看門狗計(jì)時(shí)器(Watchdog timer),是一種硬件計(jì)時(shí)設(shè)備。一般有一個(gè)輸入、一個(gè)輸出,輸入叫做喂狗,輸出連接到系統(tǒng)的復(fù)位端。當(dāng)系統(tǒng)主程序發(fā)生錯(cuò)誤導(dǎo)致未及時(shí)清除看門狗計(jì)時(shí)器的計(jì)時(shí)值時(shí),看門狗計(jì)時(shí)器就會(huì)對(duì)系統(tǒng)發(fā)出復(fù)位信號(hào),使系統(tǒng)從懸停狀態(tài)恢復(fù)到正常運(yùn)作狀態(tài)。
系統(tǒng)正常工作的時(shí)候,每隔一段時(shí)間輸出一個(gè)信號(hào)到喂狗端,給看門狗清零,這個(gè)操作就叫做喂狗。如果超過規(guī)定的時(shí)間不喂狗,看門狗定時(shí)超時(shí),就會(huì)給出一個(gè)復(fù)位信號(hào)到系統(tǒng),使系統(tǒng)復(fù)位。
2.2、Watchdog驅(qū)動(dòng)開發(fā)
2.2.1、Watchdog驅(qū)動(dòng)開發(fā)接口
為了保證上層在調(diào)用Watchdog接口時(shí)能夠正確的操作Watchdog控制器,核心層在//drivers/hdf_core/framework/support/platform/include/watchdog/watchdog_core.h中定義了以下鉤子函數(shù),驅(qū)動(dòng)適配者需要在適配層實(shí)現(xiàn)這些函數(shù)的具體功能,并與鉤子函數(shù)掛接,從而完成適配層與核心層的交互。
WatchdogMethod定義:
struct WatchdogMethod { int32_t (*getStatus)(struct WatchdogCntlr *wdt, int32_t *status); int32_t (*setTimeout)(struct WatchdogCntlr *wdt, uint32_t seconds); int32_t (*getTimeout)(struct WatchdogCntlr *wdt, uint32_t *seconds); int32_t (*start)(struct WatchdogCntlr *wdt); int32_t (*stop)(struct WatchdogCntlr *wdt); int32_t (*feed)(struct WatchdogCntlr *wdt); int32_t (*getPriv)(struct WatchdogCntlr *wdt); // 【可選】如果WatchdogCntlr中的priv成員存在,則按需實(shí)例化 void (*releasePriv)(struct WatchdogCntlr *wdt); // 【可選】};
WatchdogMethod成員的鉤子函數(shù)功能說明:
2.2.2、Watchdog驅(qū)動(dòng)開發(fā)步驟
Watchdog模塊適配HDF框架包含以下四個(gè)步驟:
實(shí)例化驅(qū)動(dòng)入口。
配置屬性文件。
實(shí)例化Watchdog控制器對(duì)象。
驅(qū)動(dòng)調(diào)試。
我們以///drivers/hdf_core/adapter/khdf/linux/platform/watchdog/watchdog_adapter.c為例(該watchdog驅(qū)動(dòng)是建立于Linux Watchdog子系統(tǒng)基礎(chǔ)上創(chuàng)建)。
2.2.2.1、驅(qū)動(dòng)實(shí)例化驅(qū)動(dòng)入口
驅(qū)動(dòng)入口必須為HdfDriverEntry(在 hdf_device_desc.h 中定義)類型的全局變量,且moduleName要和device_info.hcs中保持一致。HDF框架會(huì)將所有加載的驅(qū)動(dòng)的HdfDriverEntry對(duì)象首地址匯總,形成一個(gè)類似數(shù)組的段地址空間,方便上層調(diào)用。一般在加載驅(qū)動(dòng)時(shí)HDF會(huì)先調(diào)用Bind函數(shù),再調(diào)用Init函數(shù)加載該驅(qū)動(dòng)。當(dāng)Init調(diào)用異常時(shí),HDF框架會(huì)調(diào)用Release釋放驅(qū)動(dòng)資源并退出。
Watchdog驅(qū)動(dòng)入口開發(fā)參考:
struct HdfDriverEntry g_hdfWdtchdog = { .moduleVersion = 1, .moduleName = "HDF_PLATFORM_WATCHDOG", // 【必要且與HCS文件中里面的moduleName匹配】 .Bind = HdfWdtBind, // 見Bind參考 .Init = HdfWdtInit, // 見Init參考 .Release = HdfWdtRelease, // 見Release參考};
HDF_INIT(g_hdfWdtchdog); // 調(diào)用HDF_INIT將驅(qū)動(dòng)入口注冊(cè)到HDF框架中
2.2.2.2、配置屬性文件
完成驅(qū)動(dòng)入口注冊(cè)之后,需要在device_info.hcs文件中添加deviceNode描述。deviceNode信息與驅(qū)動(dòng)入口注冊(cè)相關(guān)。本例以一個(gè)Watchdog控制器為例,如有多個(gè)器件信息,則需要在device_info文件增加對(duì)應(yīng)的deviceNode描述。器件屬性值與核心層WatchdogCntlr成員的默認(rèn)值或限制范圍有密切關(guān)系,比如Watchdog設(shè)備號(hào),需要在watchdog_config.hcs文件中增加對(duì)應(yīng)的器件屬性。
在//vendor/lockzhiner/rk3568/hdf_config/khdf/device_info/device_info.hcs文件中添加deviceNode描述:
device_watchdog :: device { // 設(shè)備節(jié)點(diǎn) device0 :: deviceNode { // 驅(qū)動(dòng)的DeviceNode節(jié)點(diǎn) policy = 2; // policy字段是驅(qū)動(dòng)服務(wù)發(fā)布的策略,如果需要面向用戶態(tài),則為2 priority = 20; // 驅(qū)動(dòng)啟動(dòng)優(yōu)先級(jí) permission = 0644; // 驅(qū)動(dòng)創(chuàng)建設(shè)備節(jié)點(diǎn)權(quán)限 moduleName = "HDF_PLATFORM_WATCHDOG"; // 【必要】用于指定驅(qū)動(dòng)名稱,該字段的值必須和驅(qū)動(dòng)入口結(jié)構(gòu)的moduleName值一致 serviceName = "HDF_PLATFORM_WATCHDOG_0"; // 【必要】驅(qū)動(dòng)對(duì)外發(fā)布服務(wù)的名稱,必須唯一。 deviceMatchAttr = "rockchip_rk3568_watchdog_0"; // 【必要】用于配置控制器私有數(shù)據(jù),必須和驅(qū)動(dòng)私有數(shù)據(jù)配置表watchdog_config.hcs中的match_attr值保持一致。 }}
在//vendor/lockzhiner/rk3568/hdf_config/khdf/platform/rk3568_watchdog_config.hcs文件配置器件屬性,其中配置參數(shù)如下:
root { platform { watchdog_config { template watchdog_device { serviceName = "HDF_PLATFORM_WATCHDOG_0"; match_attr = ""; id = 0; }
device_0x12050000 :: watchdog_device { id = 0; match_attr = "rockchip_rk3568_watchdog_0"; } } }}
2.2.2.3、實(shí)例化Watchdog控制器對(duì)象
完成驅(qū)動(dòng)入口注冊(cè)之后,下一步就是以核心層WatchdogCntlr對(duì)象的初始化為核心,包括驅(qū)動(dòng)適配者自定義結(jié)構(gòu)體(傳遞參數(shù)和數(shù)據(jù)),實(shí)例化WatchdogCntlr成員WatchdogMethod(讓用戶可以通過接口來調(diào)用驅(qū)動(dòng)底層函數(shù)),實(shí)現(xiàn)HdfDriverEntry成員函數(shù)(Bind,Init,Release)。
WatchdogCntlr成員鉤子函數(shù)結(jié)構(gòu)體WatchdogMethod的實(shí)例化,其他成員在Init和Bind函數(shù)中初始化。
// 鉤子函數(shù)實(shí)例化static struct WatchdogMethod g_wdtMethod = { .getStatus = WdtAdapterGetStatus, // 獲取看門狗狀態(tài) .start = WdtAdapterStart, // 啟動(dòng)看門狗 .stop = WdtAdapterStop, // 停止看門狗 .setTimeout = WdtAdapterSetTimeout, // 設(shè)置看門狗超時(shí)時(shí)間 .getTimeout = WdtAdapterGetTimeout, // 獲取看門狗超時(shí)時(shí)間 .feed = WdtAdapterFeed, // 喂狗函數(shù) .getPriv = WdtOpenFile, .releasePriv = WdtAdapterClose,};
2.2.2.4、驅(qū)動(dòng)調(diào)試
建議先在Linux下修改確認(rèn),再移植到OpenHarmony。
2.3、Watchdog應(yīng)用開發(fā)
看門狗(Watchdog),又稱看門狗計(jì)時(shí)器(Watchdog timer),是一種硬件計(jì)時(shí)設(shè)備。一般有一個(gè)輸入、一個(gè)輸出,輸入叫做喂狗,輸出連接到系統(tǒng)的復(fù)位端。當(dāng)系統(tǒng)主程序發(fā)生錯(cuò)誤導(dǎo)致未及時(shí)清除看門狗計(jì)時(shí)器的計(jì)時(shí)值時(shí),看門狗計(jì)時(shí)器就會(huì)對(duì)系統(tǒng)發(fā)出復(fù)位信號(hào),使系統(tǒng)從懸停狀態(tài)恢復(fù)到正常運(yùn)作狀態(tài)。
Watchdog接口定義了看門狗操作的通用方法集合,包括:
打開/關(guān)閉看門狗設(shè)備
啟動(dòng)/停止看門狗設(shè)備
設(shè)置/獲取看門狗設(shè)備超時(shí)時(shí)間
獲取看門狗設(shè)備狀態(tài)
喂狗
2.3.1、接口說明
Watchdog模塊提供的主要接口如表1所示,具體API詳見//drivers/hdf_core/framework/include/platform/watchdog_if.h。
Watchdog驅(qū)動(dòng)API接口功能介紹如下所示:
(1)WatchdogOpen
在操作看門狗之前,需要調(diào)用WatchdogOpen打開看門狗設(shè)備,一個(gè)系統(tǒng)可能有多個(gè)看門狗,通過看門狗ID號(hào)來打開指定的看門狗設(shè)備。
DevHandle WatchdogOpen(int16_t wdtId, DevHandle *handle);
WatchdogOpen參數(shù)定義如下:
WatchdogOpen返回值定義如下:
(2)WatchdogClose
當(dāng)所有操作完畢后,調(diào)用WatchdogClose關(guān)閉打開的看門狗設(shè)備。
void WatchdogClose(DevHandle handle);
WatchdogClose參數(shù)定義如下:
(3)WatchdogStart
啟動(dòng)看門狗。
int32_t WatchdogStart(DevHandle handle);
WatchdogStart參數(shù)定義如下:
WatchdogStart返回值定義如下:
(4)WatchdogStop
停止看門狗。
int32_t WatchdogStop(DevHandle handle);
WatchdogStop參數(shù)定義如下:
WatchdogStop返回值定義如下:
(5)WatchdogSetTimeout
設(shè)置超時(shí)時(shí)間。
int32_t WatchdogSetTimeout(DevHandle *handle, uint32_t seconds);
WatchdogSetTimeout參數(shù)定義如下:
WatchdogSetTimeout返回值定義如下:
(6)WatchdogGetTimeout
獲取超時(shí)時(shí)間。
int32_t WatchdogGetTimeout(DevHandle *handle, uint32_t *seconds);
WatchdogGetTimeout參數(shù)定義如下:
WatchdogGetTimeout返回值定義如下:
(7)WatchdogGetStatus
獲取看門狗狀態(tài)。
int32_t WatchdogGetStatus(DevHandle handle, int32_t *status);
WatchdogGetStatus參數(shù)定義如下:
WatchdogGetStatus返回值定義如下:
(8)WatchdogFeed
喂狗。
int32_t WatchdogFeed(DevHandle handle);
WatchdogFeed參數(shù)定義如下:
WatchdogFeed返回值定義如下:
2.2.2、開發(fā)流程
使用Watchdog設(shè)備的一般流程如下圖所示:
3、程序解析
3.1、準(zhǔn)備工作
無
3.2、Linux內(nèi)核解析
3.2.1、創(chuàng)建Linux內(nèi)核Git
請(qǐng)參考《OpenHarmony如何為內(nèi)核打patch》(即Git倉(cāng)庫(kù)的//docs/OpenHarmony如何為內(nèi)核打patch.docx)。
3.2.2、修改設(shè)備樹Watchdog配置
修改//arch/arm64/boot/dts/rockchip/rk3568.dtsi(即該目錄是指已打Patch后的Linux內(nèi)核,不是OpenHarmony主目錄),定義Watchdog啟用,具體如下所示:
wdt: watchdog@fe600000 { compatible = "snps,dw-wdt"; reg = <0x0 0xfe600000 0x0 0x100>; clocks = <&cru TCLK_WDT_NS>, <&cru PCLK_WDT_NS>; clock-names = "tclk", "pclk"; interrupts = ; status = "okay";};
該部分為默認(rèn)啟動(dòng)看門狗。
3.2.3、創(chuàng)建內(nèi)核patch
請(qǐng)參考《OpenHarmony如何為內(nèi)核打patch》(即Git倉(cāng)庫(kù)的//docs/OpenHarmony如何為內(nèi)核打patch.docx)。
3.2.4、替換OpenHarmony的內(nèi)核patch
將制作出的kernel.patch替換到//kernel/linux/patches/linux-5.10/rk3568_patch/kernel.patch即可。
3.2.5、開啟watchdog內(nèi)核配置
在//kernel/linux/config/linux-5.10/arch/arm64/configs/rk3568_standard_defconfig(即該目錄為OpenHarmony主目錄),開啟watchdog的hdf驅(qū)動(dòng),具體如下所示:
CONFIG_DRIVERS_HDF_PLATFORM_WATCHDOG=y
3.3、OpenHarmony配置樹配置
3.3.1、device_info.hcs
//vendor/lockzhiner/rk3568/hdf_config/khdf/device_info/device_info.hcs已定義好,具體如下:
device_watchdog :: device { device0 :: deviceNode { policy = 2; priority = 20; permission = 0644; moduleName = "HDF_PLATFORM_WATCHDOG"; serviceName = "HDF_PLATFORM_WATCHDOG_0"; deviceMatchAttr = "rockchip_rk3568_watchdog_0"; }}
注意:
device0:watchdog一般只需要1個(gè)設(shè)備節(jié)點(diǎn)即可。
policy:policy字段是驅(qū)動(dòng)服務(wù)發(fā)布的策略,如果需要面向用戶態(tài),則為2。
moduleName:用于指定驅(qū)動(dòng)名稱,該字段的值必須和驅(qū)動(dòng)入口結(jié)構(gòu)的moduleName值一直,表示該節(jié)點(diǎn)對(duì)應(yīng)。于//drivers/hdf_core/adapter/khdf/linux/platform/watchdog/watchdog_adapter.c,該驅(qū)動(dòng)是對(duì)接Linux Watchdog子系統(tǒng)。
deviceMatchAttr:用于配置控制器私有數(shù)據(jù),必須和驅(qū)動(dòng)私有數(shù)據(jù)配置表watchdog_config.hcs中的match_attr值保持一致
3.3.2、Watchdog_config.hcs
在//vendor/lockzhiner/rk3568/hdf_config/khdf/platform/watchdog_config.hcs,具體內(nèi)容如下:
root { platform { watchdog_config { template watchdog_device { serviceName = "HDF_PLATFORM_WATCHDOG_0"; match_attr = ""; id = 0; }
device_0x12050000 :: watchdog_device { id = 0; match_attr = "rockchip_rk3568_watchdog_0"; } } }}
注意:
id:表示Linux系統(tǒng)中watchdog的設(shè)備號(hào)(即/dev/watchdog0)。
match_attr:必須與之前的device_info.hcs一致。
3.4、OpenHarmony Watchdog平臺(tái)驅(qū)動(dòng)
在//drivers/hdf_core/adapter/khdf/linux/platform/watchdog/watchdog_adapter.c已編寫對(duì)接Linux Watchdog驅(qū)動(dòng)的相關(guān)代碼,具體內(nèi)容如下:
struct HdfDriverEntry g_hdfWdtchdog = { .moduleVersion = 1, .moduleName = "HDF_PLATFORM_WATCHDOG", .Bind = HdfWdtBind, .Init = HdfWdtInit, .Release = HdfWdtRelease,};
HDF_INIT(g_hdfWdtchdog);
該部分代碼不細(xì)述,感興趣的讀者可以去詳讀。
3.5、應(yīng)用程序
3.5.1、Watchdog_test.c
Watchdog相關(guān)頭文件如下所示:
#include "watchdog_if.h" // watchdog標(biāo)準(zhǔn)接口頭文件
主函數(shù)負(fù)責(zé)看門狗相關(guān)操作。
其中,打開看門狗操作源代碼具體如下:
// 打開看門狗設(shè)備ret = WatchdogOpen(m_watchdog_id, handle);if (ret != 0) { PRINT_ERROR("WatchdogOpen failed and ret = %d\n", ret); goto out;}if (handle == NULL) { PRINT_ERROR("WatchdogOpen failed and handle is null\n"); goto out;}......
設(shè)置和獲取看門狗超時(shí)時(shí)間操作源代碼如下所示:
// 設(shè)置超時(shí)時(shí)間ret = WatchdogSetTimeout(handle, m_watchdog_timeout);if (ret != 0) { PRINT_ERROR("WatchdogSetTimeout failed and ret = %d\n", ret); goto out;}printf("WatchdogSetTimeout Successful and Watchdog timeout = %d\n", m_watchdog_timeout);
// 獲取超時(shí)時(shí)間ret = WatchdogGetTimeout(handle, &timeout);if (ret != 0) { PRINT_ERROR("WatchdogGetTimeout failed and ret = %d\n", ret); goto out;}printf("WatchdogGetTimeout Successful and Watchdog timeout = %d\n", timeout);
啟動(dòng)看門狗操作,如下所示:
// 啟動(dòng)看門狗ret = WatchdogStart(handle);if (ret != 0) { PRINT_ERROR("WatchdogStart failed and ret = %d\n", ret); goto out;}
查看看門狗相關(guān)狀態(tài),如下所示:
// 獲取看門狗狀態(tài),是否啟動(dòng)status = WATCHDOG_STOP;ret = WatchdogGetStatus(handle, &status);if (ret != 0) { PRINT_ERROR("WatchdogGetStatus failed and ret = %d\n", ret); goto out;}printf("WatchdogGetStatus Successful and Watchdog status = %d, WATCHDOG_START = %d, WATCHDOG_STOP = %d\n", status, WATCHDOG_START, WATCHDOG_STOP);
喂狗和停止喂狗操作,如下所示:
// 喂狗for (i = 0; i < m_watchdog_feed_count; i++) { sleep(m_watchdog_feed); printf("Watchdog: feed number = %d and feed time = %d\n", i, m_watchdog_feed); ret = WatchdogFeed(handle); if (ret != 0) { PRINT_ERROR("WatchdogFeed failed and ret = %d\n", ret); goto out; }}
// 停止喂狗ret = WatchdogStop(handle);if (ret != 0) { PRINT_ERROR("WatchdogStop failed and ret = %d\n", ret); goto out;}
關(guān)閉喂狗,如下所示:
WatchdogClose(handle);
3.5.2、BUILD.gn
編寫應(yīng)用程序的BUILD.gn,具體內(nèi)容如下:
import("http://build/ohos.gni")import("http://drivers/hdf_core/adapter/uhdf2/uhdf.gni")
print("samples: compile rk3568_watchdog_test")ohos_executable("rk3568_watchdog_test") { sources = [ "watchdog_test.c" ] include_dirs = [ "$hdf_framework_path/include", "$hdf_framework_path/include/core", "$hdf_framework_path/include/osal", "$hdf_framework_path/include/platform", "$hdf_framework_path/include/utils", "$hdf_uhdf_path/osal/include", "$hdf_uhdf_path/ipc/include", "http://base/hiviewdfx/hilog/interfaces/native/kits/include", "http://third_party/bounds_checking_function/include", ]
deps = [ "$hdf_uhdf_path/platform:libhdf_platform", "$hdf_uhdf_path/utils:libhdf_utils", "http://base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", ]
cflags = [ "-Wall", "-Wextra", "-Werror", "-Wno-format", "-Wno-format-extra-args", ]
part_name = "product_rk3568" install_enable = true}
3.5.3、參與應(yīng)用程序編譯
編輯//vendor/lockzhiner/rk3568/samples/BUILD.gn,開啟編譯選項(xiàng)。具體如下:
"b10_platform_device_watchdog/app:rk3568_watchdog_test",
4、程序編譯
建議使用docker編譯方法,運(yùn)行如下:
hb set -root .hb set# 選擇lockzhiner下的rk3568編譯分支。hb build -f
5、運(yùn)行結(jié)果
運(yùn)行如下:
# rk3568_Watchdog_testWatchdog Params: watchdog id = 0 watchdog timeout sec = 5 watchdog feed sec = 1 watchdog feed count = 5../../vendor/lockzhiner/rk3568/samples/b10_platform_device_watchdog/app/watchdog_test.c, main, 121, error: WatchdogOpen failed and ret = -16WatchdogClose Successful#
注意:
(1)WatchdogOpen返回值為-16,查看//drivers/hdf_core/framework/include/utils/hdf_base.h,具體如下:
/** * @brief Enumerates HDF return value types. */typedef enum { HDF_SUCCESS = 0, /**< The operation is successful. */ HDF_FAILURE = -1, /**< Failed to invoke the OS underlying function. */ HDF_ERR_NOT_SUPPORT = -2, /**< Not supported. */ HDF_ERR_INVALID_PARAM = -3, /**< Invalid parameter. */ HDF_ERR_INVALID_OBJECT = -4, /**< Invalid object. */ HDF_ERR_MALLOC_FAIL = -6, /**< Memory allocation fails. */ HDF_ERR_TIMEOUT = -7, /**< Timeout occurs. */ HDF_ERR_THREAD_CREATE_FAIL = -10, /**< Failed to create a thread. */ HDF_ERR_QUEUE_FULL = -15, /**< The queue is full. */ HDF_ERR_DEVICE_BUSY = -16, /**< The device is busy. */ HDF_ERR_IO = -17, /**< I/O error. */ HDF_ERR_BAD_FD = -18, /**< Incorrect file descriptor. */ HDF_ERR_NOPERM = -19, /**< No permission. */ ......} HDF_STATUS;
如此可知,watchdog被其他程序占用。
-
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1821瀏覽量
85121 -
計(jì)時(shí)器
+關(guān)注
關(guān)注
1文章
417瀏覽量
32610 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
4909瀏覽量
97080 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3646瀏覽量
16071
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論