This chapter introduces the use of the youyeetooRJ watchdog.
# View the node
ls -l /dev/watchdog
# Write any content (except uppercase 'V') to enable the watchdog --- it needs to be fed every 44 seconds
echo A > /dev/watchdog
# Enable the watchdog --- the kernel feeds the watchdog every 21 seconds
echo V > /dev/watchdog
#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, ¤t_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;
}
``` The executable file is compiled using the cross-compiler included in the 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`, and then pushed to the board for execution.
The result is as follows:
```bash
$ ./a.out 5
Timeout is set to 5 seconds
Current watchdog timeout is 5 seconds
feed the dog
feed the dog
The WDT precision only has 16 levels, and the counts of adjacent levels differ significantly, therefore precise counting is not possible.
| Command line request timeout | Timeout obtained by the program via ioctl | Final timeout set by watchdog |
|-----------------|-------------|-------------|
| > 89 | timeout_get = timeout_request | 89 |
| 44 < timeout_request <= 89 | 89 | 89 |
| 22 < timeout_request <= 44 | 44 | 44 |
| 11 < timeout_request <= 22 | 22 | 22 |
| 5 < timeout_request <= 11 | 11 | 11 |
| 2 < timeout_request <= 5 | 5 | 5 |
| timeout_request = 2 | 2 | 2 |
| timeout_request = 1 | 1 | 1 |