How to change the watchdog timer in linux embedded

14,852

Please read the Linux documentation. The standard method of changing the timeout from user space is to use an ioctl().

int timeout = 45;                        /* a time in seconds */
int fd;
fd = open("/dev/watchdog");
ioctl(fd, WDIOC_SETTIMEOUT, &timeout);   /* Send time request to the driver. */

Each watchdog device may have an upper (and possibly lower) limit on that the hardware supports, so you can not set the timeout arbitrarily high. So after setting a timeout, it is good to read back the timeout.

ioctl(fd, WDIOC_GETTIMEOUT, &timeout);   /* Update timeout with driver value. */

Now, the re-read timeout can be used as a kick frequency.

assert(timeout > 2);
while (1) {
  ioctl(fd, WDIOC_KEEPALIVE, 0);
  sleep(timeout-2);
}

You can write your own kicking routine in a script/shell command,

    while [ 1 ] ; do sleep 1; echo V > /dev/watchdog; done

However, the userspace watchdog program is usually used. This should take care of all the esoteric features. You can nice the user space program to a minimum priority and then the system will reset if user space becomes hung up. BusyBox includes a watchdog applet.

Each watchdog driver has separate module parameters and most include a mechanism to set the timeout; use either the kernel command line or module parameter setting mechanism. However, the infra-structure ioctl timeout is more portable if you do not have specific knowledge of your watchdog hardware. The ioctl is probably more future proof, in that your hardware may change.

Sample user space code is included in the Linux samples directory.

Share:
14,852
Memphis
Author by

Memphis

OOP Programmer

Updated on July 18, 2022

Comments

  • Memphis
    Memphis almost 2 years

    I have to use the linux watchdog driver (/dev/watchdog). It works great, I write an character like this:

     echo 1 > /dev/watchdog
    

    And the watchdog start and after an about 1 minute, the system reboot.

    The question is, how can I change the timeout? I have to change the time interval in the driver?

  • artless noise
    artless noise almost 10 years
    Some watchdog devices don't support modifying the timeout. The kernel will try to extend it in these cases by kicking in a timer interrupt. But this case is a little different as ISR lockups will have a smaller threshold than processes. The main point being, timeout is an overloaded concept. Often different sub-systems have different latency requirements. Is it ok for a higher priority soft modem task to block for 2s? Is it ok for a GUI task to block for 2s? Is it ok for a low priority disk defrag to block for 2s?
  • Parthiv Shah
    Parthiv Shah almost 10 years
    I use that WDIOC_SETTIMEOUT flag but. I am not able to set the timeout period for watchdog. the output for ioctl(fd,WDIOC_SETTIMEOUT,&timeout) is nonzero. So bydefault 10 secs timeout period is set.
  • artless noise
    artless noise almost 10 years
    Quite possible. Some hardware doesn't support changing the timeout, as I said. Sometimes, a boot loader may have activated the watchdog and there is no way to de-activate or change the timeout; but this is hardware specific. Do you have a Linux version and know the name of the watchdog device? You can extend the timeout by updating the driver to use a work queue, etc which will kick the watchdog a little faster than the fixed frequency.