9. Watchdog Operation Guide

This documentDescriptionin CV184x on the platformThrough Linux Watchdog Operation , DriverConfiguration、Common ioctl andExampleCode。

9.1. Preparation

1. Kernel Requirements

Use SDK kernel,andEnsurefollowingConfigurationalready (Driver KernelBuild,No need to insmod):

CONFIG_WATCHDOG=y
CONFIG_WATCHDOG_CORE=y
CONFIG_DW_WATCHDOG=y

2. UseMethod

in ThroughCommand LineOperation /dev/watchdog Device,orinKernel space/User spacePrograminThrough open、ioctl、write、close etc.InterfaceOperation can。

9.2. Operation Flow

UseFlowas follows:

  1. EnableDevice :Enable /dev/watchdog,Enable , 。

  2. (Optional)SettingsTimeout :Through WDIOC_SETTIMEOUT Set the timeout(seconds);notSettings UseDriverDefaultValue(42 seconds)。

  3. :Enableafter WDIOC_KEEPALIVE , inDefault/SettingsTimeout 。

  4. Period :in Run ,inTimeout to before WDIOC_KEEPALIVE ; must beforeTimeout 。

  5. DisableDevice :not Need to , Execute: WDIOC_SETOPTIONS WDIOS_DISABLECARD Disable , write(fd, "V", 1) Write , after close(fd); DisableFileDescription after ,Timeoutwill 。

Need to beforeTimeoutor ,caninon Flowin Use WDIOC_GETTIMEOUTWDIOC_GETTIMELEFT

9.3. Operation Example

Overview

  • Watchdog Linux ,Provide 。UserThroughEnableDevice、Set the timeout、 (keepalive)orDisableDevice canUse。

  • Timeout ,Systemwill 。

  • DefaultStatus : DefaultDisable, User 。

  • canSettingsofTimeout (Unit:seconds):1、2、5、10、21、42、85。IfSettingsofValuenotinon Table,Driver etc. Valueof ;for exampleSettings 8 seconds ,Actual 10 seconds。If SettingsTimeout,DriverDefaultUse 42 seconds。

followingExampleneedInclude File <linux/watchdog.h>,ioctl Macro(If WDIOC_SETTIMEOUTWDIOC_KEEPALIVE) FileProvide,No need to Definition。

9.3.1. Enable Watchdog and feed it immediately

Enable /dev/watchdog , 。Enableafter , inTimeout System 。

/* needInclude File:stdio.h, stdlib.h, unistd.h, fcntl.h, string.h, errno.h, sys/ioctl.h, linux/watchdog.h */
int wdt_fd;
int ret;

wdt_fd = open("/dev/watchdog", O_WRONLY);
if (wdt_fd < 0) {
    printf("Enable /dev/watchdog Failure: %s\n", strerror(errno));
    return -1;
}
printf("[Steps 1] Enable /dev/watchdog Success, fd=%d\n", wdt_fd);

ret = ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);
if (ret != 0)
    printf("Warning: Enableafter  ioctl Return %d: %s\n", ret, strerror(errno));
else
    printf("      EnableafteralreadyExecute  (WDIOC_KEEPALIVE)\n\n");

9.3.2. Set the timeout

Through ioctl WDIOC_SETTIMEOUT Set the timeout,Unitisseconds。canSettingsValueis:1、2、5、10、21、42、85; Value ,Driver etc. Valueof SupportValue。

int timeout = 10;   /* Unit:seconds,Support 1/2/5/10/21/42/85 */
int ret = ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
if (ret != 0) {
    printf("[Steps 2] SettingsTimeoutFailure: %s\n", strerror(errno));
} else {
    printf("[Steps 2] SettingsTimeout %d secondsSuccess,DriverActual : %d seconds\n\n", 10, timeout);
}

9.3.3. Feed the Dog (Keepalive)

inTimeout to before,need WDIOC_KEEPALIVE , Timeoutwill System 。 beforeSettingsofTimeout 。

const int keepalive_loops = 5;   /*  ,can is while(running) etc. */
int i;

printf("[Steps 3]  Period (  %d  ,  1 seconds)\n", keepalive_loops);
for (i = 0; i < keepalive_loops; i++) {
    ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);
    printf("        %d  \n", i + 1);
    sleep(1);
}
printf("\n");

9.3.4. Get the current timeout

Through WDIOC_GETTIMEOUT can Driver before ofTimeout (seconds)。

int timeout_sec = 0;
if (ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout_sec) == 0) {
    printf("[Steps 4]  beforeTimeout : %d seconds\n", timeout_sec);
} else {
    printf("[Steps 4] GetTimeout Failure(Optional)\n");
}

9.3.5. Get the remaining time

IfDriverSupport,canThrough WDIOC_GETTIMELEFT under Timeout seconds, or 。

int timeleft_sec = 0;
if (ioctl(wdt_fd, WDIOC_GETTIMELEFT, &timeleft_sec) == 0) {
    printf("[Steps 5]  under Timeout : %d seconds\n\n", timeleft_sec);
} else {
    printf("[Steps 5] Get the remaining timenotSupportorFailure(Optional)\n\n");
}

9.3.6. Disable Watchdog(Magic Close)

DriverSupport Magic Close :inDisableDevicebefore, must Disable , DeviceWrite 'V', after close。 Disable FileDescription , ,Timeout will System 。

WDIOS_DISABLECARDwrite(..., "V", 1)close

int option = WDIOS_DISABLECARD;
int ret;

printf("[Steps 6] Disable Watchdog (Magic Close)\n");
ret = ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);
if (ret != 0)
    printf("      Warning: WDIOC_SETOPTIONS WDIOS_DISABLECARD Return %d: %s\n", ret, strerror(errno));

if (wdt_fd >= 0) {
    if (write(wdt_fd, "V", 1) != 1)
        printf("      Warning: Write  'V' Failure: %s\n", strerror(errno));
    else
        printf("      alreadyWrite  'V'\n");
    close(wdt_fd);
    wdt_fd = -1;
    printf("      already close Device\n");
}
printf("\nProgram \n");

9.3.7. CompleteUser-Space Program Example

followingiswillon Steps ofComplete C Program,can BuildRun。needInclude File <linux/watchdog.h>,ioctl Macro FileProvide。

  1/*
  2 * Watchdog Operation Example(  Linux  )
  3 * needInclude File <linux/watchdog.h>,ioctl Macro FileProvide。
  4 */
  5#include <stdio.h>
  6#include <stdlib.h>
  7#include <unistd.h>
  8#include <fcntl.h>
  9#include <string.h>
 10#include <errno.h>
 11#include <sys/ioctl.h>
 12#include <linux/watchdog.h>
 13
 14int main(void)
 15{
 16    int wdt_fd;
 17    int timeout;
 18    int timeout_sec;
 19    int timeleft_sec;
 20    int option;
 21    int ret;
 22    int i;
 23    const int keepalive_loops = 5;  /*  ,can is while(running) etc. */
 24
 25    printf("=== Watchdog Operation Example ===\n\n");
 26
 27    /* Steps 1:Enable Watchdog and feed it immediately */
 28    wdt_fd = open("/dev/watchdog", O_WRONLY);
 29    if (wdt_fd < 0) {
 30        printf("Enable /dev/watchdog Failure: %s\n", strerror(errno));
 31        return -1;
 32    }
 33    printf("[Steps 1] Enable /dev/watchdog Success, fd=%d\n", wdt_fd);
 34
 35    ret = ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);
 36    if (ret != 0)
 37        printf("Warning: Enableafter  ioctl Return %d: %s\n", ret, strerror(errno));
 38    else
 39        printf("      EnableafteralreadyExecute  (WDIOC_KEEPALIVE)\n\n");
 40
 41    /* Steps 2:Set the timeout(Unit:seconds,Support 1/2/5/10/21/42/85) */
 42    timeout = 10;
 43    ret = ioctl(wdt_fd, WDIOC_SETTIMEOUT, &timeout);
 44    if (ret != 0) {
 45        printf("[Steps 2] SettingsTimeoutFailure: %s\n", strerror(errno));
 46    } else {
 47        printf("[Steps 2] SettingsTimeout %d secondsSuccess,DriverActual : %d seconds\n\n", 10, timeout);
 48    }
 49
 50   /* Steps 3:SettingsTimeoutafter ,and Period  */
 51   ret = ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);
 52   if (ret != 0) {
 53       printf("[Steps 3] SettingsTimeoutafter Failure: %s\n", strerror(errno));
 54   } else {
 55       printf("[Steps 3] SettingsTimeoutafteralready \n");
 56   }
 57
 58   printf("[Steps 3]  Period (  %d  ,  1 seconds)\n", keepalive_loops);
 59   for (i = 0; i < keepalive_loops; i++) {
 60       ret = ioctl(wdt_fd, WDIOC_KEEPALIVE, 0);
 61       if (ret != 0) {
 62           printf("        %d  Failure: %s\n", i + 1, strerror(errno));
 63           break;
 64       }
 65       printf("        %d  Success\n", i + 1);
 66       sleep(1);
 67   }
 68   printf("\n");
 69
 70   /* Steps 4:Get the current timeout */
 71   timeout_sec = 0;
 72   if (ioctl(wdt_fd, WDIOC_GETTIMEOUT, &timeout_sec) == 0) {
 73       printf("[Steps 4]  beforeTimeout : %d seconds\n", timeout_sec);
 74   } else {
 75       printf("[Steps 4] GetTimeout Failure(Optional)\n");
 76   }
 77
 78   /* Steps 5:Get the remaining time(Optional, DriverSupport) */
 79   timeleft_sec = 0;
 80   if (ioctl(wdt_fd, WDIOC_GETTIMELEFT, &timeleft_sec) == 0) {
 81       printf("[Steps 5]  under Timeout : %d seconds\n\n", timeleft_sec);
 82   } else {
 83       printf("[Steps 5] Get the remaining timenotSupportorFailure(Optional)\n\n");
 84   }
 85
 86    /* Steps 6:Disable Watchdog(Magic Close)*/
 87    /*  : WDIOS_DISABLECARD -> write("V", 1) -> close */
 88    printf("[Steps 6] Disable Watchdog (Magic Close)\n");
 89    option = WDIOS_DISABLECARD;
 90    ret = ioctl(wdt_fd, WDIOC_SETOPTIONS, &option);
 91    if (ret != 0)
 92        printf("      Warning: WDIOC_SETOPTIONS WDIOS_DISABLECARD Return %d: %s\n", ret, strerror(errno));
 93
 94    if (wdt_fd >= 0) {
 95        if (write(wdt_fd, "V", 1) != 1)
 96            printf("      Warning: Write  'V' Failure: %s\n", strerror(errno));
 97        else
 98            printf("      alreadyWrite  'V'\n");
 99        close(wdt_fd);
100        wdt_fd = -1;
101        printf("      already close Device\n");
102    }
103    printf("\nProgram \n");
104    return 0;
105}

9.3.8. Quick Command-Line Example

followingDescriptionIf inCommand LineunderImplementationandon 「CompleteUser-Space Program Example」 ofFunction。Note :Enable /dev/watchdog afterIfnot ,System inTimeoutafter ,Please inTests Use。SettingsTimeout(WDIOC_SETTIMEOUT)、 Timeout/ (WDIOC_GETTIMEOUT / WDIOC_GETTIMELEFT)needThrough C Programoretc. ToolsComplete; BuildandRunon ofCompleteUser spaceProgramwith Complete Steps。

: Complete Function

BuildandRunon 「CompleteUser-Space Program Example」inofComplete C Program,can Complete:Enableand 、SettingsTimeout、Get beforeTimeoutand 、Period 、Magic Close Disable。

# Build(Example: Fileis watchdog_demo.c ,BuildToolsis arm-none-linux-uclibcgnueabihf-gcc)
arm-none-linux-uclibcgnueabihf-gcc -o watchdog_demo watchdog_demo.c -static
# willRunFileCopyto ,andRun
./watchdog_demo

Steps 1: ( )

in or ofbefore under, , andinTimeoutbefore 。

# Method 1:  watchdog Tools(10 secondsTimeout,after Run)
watchdog -t 10 /dev/watchdog &

# Method 2: Tools  Shell  
( while true; do echo -n '.' > /dev/watchdog; sleep 1; done ) &

Steps 2:Disable Watchdog(Magic Close)

killall watchdog   #   magic close Function, ExecuteDisable Operation