Skip to main content

D12CWdt

1 Watchdog 编程

本章介绍YY3588看门狗的使用

1.1 命令行简单控制

# 查看节点
ls -l /dev/watchdog

# 写入任意内容(大写字母‘V’除外),开启看门狗 --- 每44秒内需要喂一次狗
echo A > /dev/watchdog

# 开启看门狗 --- 内核每21秒喂一次狗
echo V > /dev/watchdog

1.2 C 语言小 demo

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>

int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <timeout>\n", argv[0]);
return 1;
}

int timeout = atoi(argv[1]);

printf("Timeout is set to %d seconds\n", timeout);
int current_timeout;
int num = 0;
int fd = open("/dev/watchdog", O_RDWR);
if (fd == -1) {
perror("open /dev/watchdog");
exit(EXIT_FAILURE);
}

if (ioctl(fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
perror("ioctl WDIOC_SETTIMEOUT");
close(fd);
exit(EXIT_FAILURE);
}

if (ioctl(fd, WDIOC_GETTIMEOUT, &current_timeout) == -1) {
perror("ioctl WDIOC_GETTIMEOUT");
close(fd);
exit(EXIT_FAILURE);
}
printf("Current watchdog timeout is %d seconds\n", current_timeout);

while (1) {
if (ioctl(fd, WDIOC_KEEPALIVE, NULL) == -1) {
perror("ioctl WDIOC_KEEPALIVE");
close(fd);
exit(EXIT_FAILURE);
}
printf("%d : feed the dog, Ctrl+c to exit.\n", ++num);
sleep(10);
}

close(fd);
printf("Watchdog device closed\n");

return 0;
}

使用SDK中自带的交叉编译器SDK/prebuilts/gcc/linux-x86/aarch64/gcc-arm-10.3-2021.07-x86_64-aarch64-none-linux-gnu/bin/aarch64-none-linux-gnu-gcc编译生成可执行文件,推送到板端运行。
效果如下:

$ ./a.out 5
Timeout is set to 5 seconds
Current watchdog timeout is 5 seconds
feed the dog
feed the dog

1.3 WDT精度说明

WDT精度只有16档,相邻档位计数相差⽐较⼤,因此⽆法精细计数。
关于设置的时长和实际的时间长度参考下表

命令行请求的超时时间程序通过 ioctl 获取的超时时间watchdog最终设置的超时时间
> 89timeout_get = timeout_request89
44 < timeout_request <= 898989
22 < timeout_request <= 444444
11 < timeout_request <= 222222
5 < timeout_request <= 111111
2 < timeout_request <= 555
timeout_request = 222
timeout_request = 111

1.4 暂停功能

# 暂停计数
io -4 0xfd58c000 0x00010001
# 恢复计数
io -4 0xfd58c000 0x00100000