9. Watchdog Operation Guide

9.1. Preparations of Watchdog Are as Follow:

  • Use the kernel released by SDK

9.2. Module Compile

  • Insert module :cv180x: insmod cv180x_wdt.ko, cv181x: insmod cv181x_wdt.ko.

  • To operate the Watchdog, run the Watchdog read and write command in the console or write the Watchdog read and write program in kernel space or user space.

9.2.1. Operation Example

Watchdog uses the standard linux framework to provide a hardware watchdog. Users can use watchdog simply by turning it on, off, or setting timeout. The system restarts when the watchdog timeout occurs.

The Watchdog is disabled by default. Customers can decide whether to enable it. The timeout times that can be specified are 1s, 2s, 5s, 10s, 21s, 42s, and 85s.

When the user input timtout time is 8s, the driver will select a timeout greater than or equal to the value of 10s; If timeout is not set, the driver uses 42s by default.

  • Turn on WATCHDOG

Open the /dev/watchdog device node to start watchdog. You should ping (feed the dog) immediately after opening, otherwise wdt will restart immediately.

int wdt_fd = -1;
wdt_fd = open("/dev/watchdog", O_WRONLY);
if (wdt_fd == -1)
{
     // fail to open watchdog device
}
ioctl(fd, WDIOC_KEEPALIVE, 0);
  • Turn off WATCHDOG

The driver supports “Magic Close”. The magic character ‘V’ must be written to the watchdog device before closing the watchdog.

If the userspace daemon shuts down the device without sending ‘V’, the watchdog will keep counting.If the dog is not fed for a given period of time, it will result in a timeout and the system will restart.

The reference code is as follows:

int option = WDIOS_DISABLECARD;
ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);
if (wdt_fd != -1)
{
   write(wdt_fd, "V", 1);
   close(wdt_fd);
   wdt_fd = -1;
}
  • Set the TIMEOUT value

Set timeout with unit seconds by using the standard IOCTL command WDIOC_SETTIMEOUT. The timeout times that can be specified are 1s, 2s, 5s, 10s, 21s, 42s, and 85s.

#define  WATCHDOG_IOCTL_BASE 'W'
#define  WDIOC_SETTIMEOUT        _IOWR(WATCHDOG_IOCTL_BASE, 6, int)

int timeout = 10;
ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
  • PING watchdog

Ping watchdog through the standard IOCTL command, WDIOC_KEEPALIVE.

while (1) {
   ioctl(fd, WDIOC_KEEPALIVE, 0);
   sleep(1);
}