Watchdog使用指导

使用流程

使用watchdog的一般流程如图1所示。

图 1 watchdog使用流程图

Watchdog使用指导 - 图1

打开Watchdog设备

在操作Watchdog之前,需要使用WatchdogOpen打开一个Watchdog设备,一个系统可能有多个Watchdog,通过ID号来打开指定的Watchdog设备:

int32_t WatchdogOpen(int16_t wdtId);

表 1 WatchdogOpen参数和返回值描述

参数

参数描述

wdtId

Watchdog设备号

返回值

返回值描述

NULL

打开失败

struct DevHandle类型指针

Watchdog设备句柄

  1. struct DevHandle *handle = NULL;
  2. handle = WatchdogOpen(0); /* 打开0号看门狗设备 */
  3. if (handle == NULL) {
  4. HDF_LOGE("WatchdogOpen: failed, ret %d\n", ret);
  5. return;
  6. }

获取Watchdog状态

int32_t WatchdogGetStatus(struct DevHandle *handle, int32_t *status);

表 2 WatchdogGetStatus参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

status

获取到的启动状态指针

返回值

返回值描述

0

获取成功

负数

获取失败

  1. int32_t ret;
  2. int32_t status;
  3. /* 获取Watchdog启动状态 */
  4. ret = WatchdogGetStatus(handle, &status);
  5. if (ret != 0) {
  6. HDF_LOGE("WatchdogGetStatus: failed, ret %d\n", ret);
  7. return;
  8. }

设置超时时间

int32_t WatchdogSetTimeout(PalHandle *handle, uint32_t seconds);

表 3 WatchdogSetTimeout参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

seconds

超时时间,单位为秒

返回值

返回值描述

0

设置成功

负数

设置失败

  1. int32_t ret;
  2. uint32_t timeOut = 60;
  3. /* 设置超时时间,单位:秒 */
  4. ret = WatchdogSetTimeout(handle, timeOut);
  5. if (ret != 0) {
  6. HDF_LOGE("WatchdogSetTimeout: failed, ret %d\n", ret);
  7. return;
  8. }

获取超时时间

int32_t WatchdogGetTimeout(PalHandle *handle, uint32_t *seconds);

表 4 WatchdogGetTimeout参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

seconds

接收超时时间的指针,单位为秒

返回值

返回值描述

0

获取成功

负数

获取失败

  1. int32_t ret;
  2. uint32_t timeOut;
  3. /* 获取超时时间,单位:秒 */
  4. ret = WatchdogGetTimeout(handle, &timeOut);
  5. if (ret != 0) {
  6. HDF_LOGE("WatchdogGetTimeout: failed, ret %d\n", ret);
  7. return;
  8. }

启动Watchdog

int32_t WatchdogStart(struct DevHandle *handle);

表 5 WatchdogStart参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

返回值

返回值描述

0

启动成功

负数

启动失败

  1. int32_t ret;
  2. /* 启动看门狗 */
  3. ret = WatchdogStart(handle);
  4. if (ret != 0) {
  5. HDF_LOGE("WatchdogStart: failed, ret %d\n", ret);
  6. return;
  7. }

喂狗

int32_t WatchdogFeed(struct DevHandle *handle);

表 6 WatchdogFeed参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

返回值

返回值描述

0

喂狗成功

负数

喂狗失败

  1. int32_t ret;
  2. /* 喂狗 */
  3. ret = WatchdogFeed(handle);
  4. if (ret != 0) {
  5. HDF_LOGE("WatchdogFeed: failed, ret %d\n", ret);
  6. return;
  7. }

停止Watchdog

int32_t WatchdogStop(struct DevHandle *handle);

表 7 WatchdogStop参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

返回值

返回值描述

0

停止成功

负数

停止失败

  1. int32_t ret;
  2. /* 停止看门狗 */
  3. ret = WatchdogStop(handle);
  4. if (ret != 0) {
  5. HDF_LOGE("WatchdogStop: failed, ret %d\n", ret);
  6. return;
  7. }

关闭Watchdog设备

当操作完毕时,使用WatchdogClose关闭打开的设备句柄:

void WatchdogClose(struct DevHandle *handle);

表 8 WatchdogClose参数和返回值描述

参数

参数描述

handle

看门狗设备句柄

  1. /* 关闭看门狗 */
  2. ret = WatchdogClose(handle);