WATCHDOG REFERENCE

Version1.0


1. OVERVIEW

Watchdog adopts the standard linux framework and provides hardware watchdog. The upper application can set the time out time and keep alive by itself.

Watchdog is disabled by default. It is recommended to enable it in the main thread. If operating in other threads, the watchdog will be disabled as the thread is closed.


2. BOOT CONFIG

Enabel watchdog as shows below, without calling other interface, and the default time of timeout is 10s, which can be modified in HW WATCHDOG Timeout interface.


3. KERNEL CONFIG


4. WATCHDOG SETTING


4.1. Enable Watchdog

Turn on /dev/watchdog to start watchdog.

The reference code is as follows:

int wdt_fd = -1;
wdt_fd = open("/dev/watchdog", O_WRONLY);
if (wdt_fd == -1)
{
    // fail to open watchdog device
}

4.2. Disable Watchdog

The reference code is as follows:

int option = WDIOS_DISABLECARD;
        ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);
if (wdt_fd != -1)
    {
        close(wdt_fd);
        wdt_fd = -1;
    }

4.3. Set Timeout

Use the standard IOCTL command WDIOC_SETTIMEOUT to set the timeout, the unit is second, it is recommended that timeout > 5s.

The reference code is as follows:

int timeout = 20;

ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);

4.4. Keep Alive

Command WDIOC_KEEPALIVE input through standard IOCTL. The time is determined according to the set timeout. The input time should be smaller than timeout. The reference code is as follows:

ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);

5. DEMO

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

int WatchDogInit(int timeout)
{
    printf("[%s %d] init watch dog, timeout:%ds\n", __func__,__LINE__, timeout);
    int wdt_fd = -1;
    wdt_fd = open("/dev/watchdog", O_WRONLY);

    if(wdt_fd == -1)
    {
        printf("[%s %d] open /dev/watchdog failed\n", __func__,__LINE__);
        return -1;
    }
    if(-EINVAL == ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout))
    {
        printf("[%s %d] ioctl return -EINVAL\n", __func__, __LINE__);
    }

    return wdt_fd;
}

int WatchDogDeinit(int wdt_fd)
{
    if(wdt_fd <= 0)

    printf("[%s %d] watch dog hasn't been inited\n",__func__,__LINE__);

    int option = WDIOS_DISABLECARD;
    int ret = ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);
    printf("[%s %s] WDIOC_SETOPTIONS %d WDIOS_DISABLECARD=%d\n", __func__,__LINE__, ret, option);

    if(wdt_fd != -1)
    {
        close(wdt_fd);
        wdt_fd = -1;
    }
    return 0;
}

int WatchDogKeepAlive(int wdt_fd)
{
    if(wdt_fd < 0)
    {
        printf("[%s %d] watch dog hasn't been inited\n",__func__,__LINE__);
        return 0;
    }
    if(-EINVAL == ioctl(wdt_fd, WDIOC_KEEPALIVE, 0))
    {
        printf("[%s %d] ioctl return -EINVAL\n", __func__, __LINE__);
        return -1;
    }
    return 0;
}

int main()
{
    int loop = 0;
    int wdt_fd = -1;
    wdt_fd = WatchDogInit(5);

    loop = 3;
    while(loop--)
    {
        sleep(3);
        WatchDogKeepAlive(wdt_fd);
    }

    printf("Stop to feed dog, let system reboot...\n");

    return 0;
}