Based on the principle and use of watchdog in embedded systems

Recently, you need to add a WatchDog to the board to ensure automatic restart when the system fails, so I read some information about using the watchdog in the embedded system, summarized as follows:

First, the principle of watchdog

In a productized embedded system, in order to automatically reset the system 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 starts, the counter starts counting automatically. After a certain period of time, if it is not reset, the counter overflow will generate a reset signal to the CPU to restart the system (commonly known as "dog biting"). When the system is running normally, it is necessary to clear the watchdog counter (commonly known as "feeding dog") within the time interval allowed by the watchdog, and the reset signal is not allowed to be generated. If there is no problem with the system, the program guarantees to "feed the dog" on time. Once the program runs away, there is no "feeding the dog" and the system is "bitten" to reset.

This is like having a hungry dog ​​around you. If you don't give it meat on time, it will eat your meat, you will be eaten... hung up, reincarnation, and completely re-doing. Unfortunately, you have to feed the dog in the afterlife.

Second, the type of watchdog

In today's embedded systems, there are two main types of watchdogs:

1, the CPU inside the watchdog: This type of watchdog is generally a timer in a chip as a watchdog, through the initialization of the program, write the initial value, set the overflow time, and start the timer . The program assigns an initial value (or reset) to the timer on time to avoid being bitten. This kind of watchdog can be disabled (just stop this timer), like the one that bites your dog to "a sunflower acupuncture hand". Most CPUs have a built-in watchdog. The hardware principle can refer to each chip data sheet.

Advantages: You can change the overflow time through the program; you can disable it at any time

Disadvantages: need to be initialized; if the program runs before initialization, start running or run away after disabling, the watchdog can not reset the system, so the role of the watchdog is gone, and the system recovery ability is reduced.

2, independent watchdog chip: This watchdog mainly has a pin for feeding the dog (usually connected to the GPIO of the CPU) and a reset pin (connected to the RESET pin of the system), if not Changing the level of the dog's foot for a certain period of time, the reset pin will change the state to reset the CPU. This kind of watchdog starts working when it is powered on and cannot be disabled. Commonly used chips are: CAT705/CAT706, IMP706, etc., the overflow time is about 1.6 seconds. The hardware principle can refer to each chip data manual and "Linux-based embedded system full dog feeding strategy".

Advantages: no configuration, power on and use. Can not be disabled, the system must feed the dog on time, the system recovery ability is high.

Disadvantages: The overflow time cannot be flexibly configured, it cannot be disabled, and the flexibility is reduced.

Of course, there are so-called software watchdogs, which is actually a kind of monitoring software.

Some important programs must keep it running; and always care about its state - it can't make it deadlock. (Of course, if a main program will have a deadlock, it must be a design or programming mistake. The first thing to do is Debug.) But if time is tight, you can use the software watchdog to temporarily respond.

This monitoring software runs without an interface window and has certain concealment; it periodically determines whether the target process is running in the current system, if not, starts the target process; determines whether the target process is "no response", and if so, terminates the target process. If the target process "no response" exceeds a certain number, restart the entire system. Its purpose is also to reset, but it is the main city reset process, it is really impossible to reset the CPU.

This software is monitoring software and is outside the scope of this article.

Third, the choice of watchdog

In the case of general civilian products, where the stability of the system is not too high, the built-in watchdog can be used to simplify software development and hardware costs.

In the key occasions where industrial control products and system stability are highly demanded, hardware independent watchdogs are basically selected to ensure the system's ability to recover in the face of serious errors. Increased hardware costs, slightly increased the difficulty of software development (must ensure that the dog is kept under normal conditions).

Fourth, the use of the bootloader

If it is a built-in watchdog of the CPU, it can be disabled at this stage, and there is no special operation.

But if it is an independent watchdog chip, it must be fed. Or for insurance, you start the watchdog in the bootloader and you have to feed it.

There are a lot of bootloaders, the basic principle is the same, the process of feeding the dog is basically the same, here uboot as an example to explain how to feed the dog.

The operation of u-boot is divided into the following stages:

The earliest simple initialization, self-copy phase of code relocation

System detailed initialization phase

Console phase (skip if autostart is used)

OS kernel copy (possibly with decompression process), jump into the operating system

Five, use under Linux

After the bootloader loads the kernel, the system begins to take over by the Linux kernel. The work of feeding the dog naturally begins to be borne by the kernel. After the bootloader gives control to the kernel, the time can be divided into the following parts:

Kernel self-extracting stage (compressed kernel only: zImage or bzImage)

Kernel boot before watchdog driver load

After the watchdog driver is loaded and before the root file system is started

After the root file system is started, before the watchdog daemon starts.

After the watchdog daemon starts

For the watchdog built into the CPU, in the 1st to 4th stages, the watchdog generally does not start, and there is no need to feed the dog. Only after the watchdog daemon starts, the daemon opens the watchdog and starts feeding the dog based on the configuration file monitoring the status of other processes. If the monitored process has an unrecoverable problem, the daemon stops feeding the dog and the CPU resets.

For an independent watchdog chip, the system must constantly feed the dog as soon as it is powered up, and take the bootloader's baton to continue feeding the dog.

First, in the first phase, the kernel's self-extracting time is generally long, and the dog code (and possibly the assembly form) must be added during the self-extracting loop. Once running in the process, it will not feed the dog and the CPU will reset.

In the second stage, this period of time is generally not very long, you can not feed the dog. But this is also related to your kernel. If there is a time-consuming module startup at this stage, you can add the dog code to the module's initialization function or the more time-consuming loops and waits. In which modules are added, it is necessary to try to start several times, relying on experiments and experience.

When the watchdog driver is loaded, the dog is usually fed once in the module initialization code.

In phase 3, similar to phase 2, if there is a time-consuming module startup at this stage, you can add the dog code to the module's initialization function or to a more time-consuming loop and wait. However, when the root corpus system is mounted, depending on the nature and size of the file system, it may be necessary to add a dog feed instruction to the file system code.

Stage 4 is a relatively time-consuming phase. Usually, some simple shell commands for feeding the dog are added to the system startup script, for example: "echo V > /dev/watchdog". The added position varies according to the startup process of the system. .

In the fifth stage, everything is done by the watchdog daemon to feed the dog.

Based on the principle and use of watchdog in embedded systems

Private Mould Gaming PC Speaker

Gaming Speaker,Usb Gaming Speaker,Private Mould Gaming Speaker,Gaming Speaker With Knob

Comcn Electronics Limited , https://www.comencnspeaker.com