watchdog application

Table of contents

1. Watchdog Introduction

struct watchdog_info

2. Watchdog of imx6u 

3. Control the watchdog

         Turn on the watchdog device

Enable/disable watchdog: WDIOC_SETOPTIONS

 Get/set timeout: WDIOC_GETTIMEOUT, WDIOC_SETTIMEOUT

Feed the dog: WDIOC_KEEPALIVE

4. Watchdog application programming

verify


1. Watchdog Introduction

        What is a watchdog? In a productized embedded system, in order to enable the system to automatically reset under abnormal conditions, it is generally necessary to introduce a watchdog. The watchdog is actually a counter that can be reset within a certain period of time. When the watchdog is started, the counter starts counting automatically. After a certain period of time, if it is not reset, the counter overflows and generates a reset signal to the CPU to restart the system (commonly known as "bitten by a dog"). When the system is running normally, it is necessary to clear the watchdog counter (commonly known as "feeding the dog") within the time interval allowed by the watchdog to prevent the reset signal from being generated. If there is no problem with the system, the program guarantees to "feed the dog" on time. Once the program runs away and there is no "feed the dog", the system will be "bitten" and reset.
        The watchdog is actually a counter that can be reset/reset within a certain period of time, generally called a watchdog timer
(or watchdog timer); if the watchdog timer is not reset within the specified time, the counter overflows It will generate a reset signal to the CPU to restart the system. Of course, some watchdogs can only generate an interrupt signal without resetting the system.

struct watchdog_info

The struct watchdog_info structure describes the information of the watchdog device and is defined as follows

struct watchdog_info {
__u32 options; /* Options the card/driver supports */
__u32 firmware_version; /* Firmware version of the card */
__u8 identity[32]; /* Identity of the board */
};

The options field records which functions or options the device supports;
the firmware_version field records the firmware version number of the device;
the identity field is a descriptive string.

 The focus is on the options field, which describes which functions and options the device supports. The values ​​of this field are as follows

#define WDIOF_OVERHEAT 0x0001 /* Reset due to CPU overheat */
#define WDIOF_FANFAULT 0x0002 /* Fan failed */
#define WDIOF_EXTERN1 0x0004 /* External relay 1 */
#define WDIOF_EXTERN2 0x0008 /* External relay 2 */
#define WDIOF_POWERUNDER 0x0010 /* Power bad/power fault */
#define WDIOF_CARDRESET 0x0020 /* Card previously reset the CPU */
#define WDIOF_POWEROVER 0x0040 /* Power over voltage */
#define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
#define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
#define WDIOF_PRETIMEOUT 0x0200 /* Pretimeout (in seconds), get/set */
#define WDIOF_ALARMONLY 0x0400 /* Watchdog triggers a management or other                                                                 external alarm not a reboot */
#define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */

 Common values: WDIOF_SETTIMEOUT indicates that the device supports setting the timeout period; WDIOF_KEEPALIVEPING indicates that the device supports the "feed the dog" operation, that is, reset the watchdog timer

2. Watchdog of imx6u 

        The I.MX6ULL SoC integrates two watchdog timers (WDOG): WDOG1 and WDOG2; WDOG2 is used for security purposes, while WDOG1 is a common watchdog that supports generating interrupt signals and resetting the registers in the CPU Linux system
        . Watchdog peripherals will generate corresponding device nodes (device files) in the /dev/ directory. The name of the device node is usually watchdogX (X represents a number 0, 1, 2, 3, etc.), such as /dev/ watchdog0, /dev/watchdog1, etc., through which device nodes can control the watchdog peripherals.

         watchdog0 is actually the device node corresponding to WDOG1 of I.MX6U

        There may be multiple watchdog devices registered in the system, and the /dev/watchdog device node represents the default watchdog device of the system; usually this refers to watchdog0, so, in the above figure, /dev/watchdog is actually equal to /dev /watchdog0, which means that they both represent the same hardware peripheral

3. Control the watchdog

        It is actually very simple to control the watchdog at the application layer. It can be done through the ioctl() function. In the application program, the header file <linux/watchdog.h> needs to be included. This header file defines some ioctl command macros. A different instruction macro means requesting a different operation to the device, as follows:

#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
#define WDIOC_GETSTATUS _IOR(WATCHDOG_IOCTL_BASE, 1, int)
#define WDIOC_GETBOOTSTATUS _IOR(WATCHDOG_IOCTL_BASE, 2, int)
#define WDIOC_GETTEMP _IOR(WATCHDOG_IOCTL_BASE, 3, int)
#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int)
#define WDIOC_SETPRETIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
#define WDIOC_GETPRETIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 9, int)
#define WDIOC_GETTIMELEFT _IOR(WATCHDOG_IOCTL_BASE, 10, int)

The more commonly used commands are as follows

ioctl command illustrate
WDIOC_GETSUPPORT Get which functions the watchdog supports
WDIOC_SETOPTIONS Used to enable or disable the watchdog
WDIOC_KEEPALIVE dog feeding operation
WDIOC_SETTIMEOUT Set watchdog timeout
WDIOC_GETTIMEOUT Get watchdog timeout

 Turn on the watchdog device

open("/dev/watchdog", "O_RDWR")

 It should be noted that when calling open() to open the watchdog device, even if the watchdog timer is not enabled in the program, so after opening the device, you need to use the command to stop the watchdog timer, and wait until all settings are completed Restart the watchdog timer

Enable/disable watchdog: WDIOC_SETOPTIONS

Use the WDIOC_SETOPTIONS command to enable or stop the watchdog timer, as follows:

ioctl(int fd, WDIOC_SETOPTIONS, int *option);

 The option pointer points to an int type variable, which can take the following values:

#define WDIOS_DISABLECARD 0x0001 /*Turn off the watchdog timer*/
#define WDIOS_ENABLECARD 0x0002 /*Turn on the watchdog timer*/

 Get/set timeout: WDIOC_GETTIMEOUT, WDIOC_SETTIMEOUT

Use the WDIOC_GETTIMEOUT command to get the timeout time currently set by the device, and use it as follows:

ioctl(int fd, WDIOC_GETTIMEOUT, int *timeout);

 Use the WDIOC_SETTIMEOUT command to set the timeout period of the watchdog, as follows:

ioctl(int fd, WDIOC_SETTIMEOUT, int *timeout);

 The timeout period is in seconds. When setting the timeout period, it cannot exceed its maximum value, otherwise the ioctl() call will fail

Feed the dog: WDIOC_KEEPALIVE

After the watchdog timer is started, it is necessary to "feed the dog" before it times out, otherwise the timer overflow and timeout will cause the system to reset or generate an interrupt signal. Feed the dog through the WDIOC_KEEPALIVE command. The usage is as follows:

ioctl(int fd, WDIOC_KEEPALIVE, NULL);

4. Watchdog application programming

Define a watchdog device information structure, timeout time, dog feeding time, file descriptor, and op to transfer values

        First judge whether the command line parameters are equal, the parameters are running app and timeout time

         As mentioned above, the timer will start after the watchdog is turned on, so the watchdog timer must be turned off first, and the system will be restarted when the time is up.

         Get the timeout from the command line, if the timeout is less than 1 second, the default is 1 second, and then set the timeout

         After setting the timeout time, the watchdog will start timing. Here, the dog feeding time is set to 100 milliseconds before the timeout, and the dog will be fed when it is 100 milliseconds before the timeout.

verify

run app

 After the program is executed, the watchdog timer starts, and the program will continue to feed the dog to reset the timer to ensure that the program will not restart

 Press Ctrl + C to end the program. Ending the program means that the dog feeding has stopped, and the watchdog timer has not stopped, which
will cause the timer to overflow and reset and restart

 After pressing Ctrl+C to terminate the process, the kernel prints out the message "watchdog watchdog0: watchdog did not stop!", indicating that the watchdog timer is still counting and has not stopped, and then the system will be restarted

Guess you like

Origin blog.csdn.net/weixin_46829095/article/details/129629350