[Transfer] Some thoughts on using watchdog in freeRTOS
[Copy link]
I believe that all embedded software developers should be familiar with watchdogs. In embedded software development, watchdogs are often used to monitor whether the CPU program is running normally. If the CPU program runs abnormally, the watchdog will trigger a reset when the set threshold is reached, so that the entire CPU will be reset and restarted.
The essence of a watchdog is a counter. At the beginning, it is configured to a specific value, and then the counter will keep counting down. When it reaches 0, it will trigger a system reset. Therefore, in order to avoid being reset, it is necessary to reset the watchdog count value at a certain interval, which is what we often call "feeding the dog".
This "feeding the dog" action should occur within the interval of the watchdog's reset threshold. Otherwise, exceeding or reaching this threshold will cause the system to be reset, which is something we do not want to see.
Regarding the use of watchdog in the program, it can be discussed in two situations:
1)裸机中使用看门狗;
2)RTOS环境中使用看门狗。
1. Using watchdog in bare metal
When using a watchdog in a bare metal machine, due to the influence of bare metal programming and the way the program executes, developers usually perform the "feeding" operation of the watchdog in the main loop.
At this time, developers need to reasonably evaluate the execution flow of the code they write and estimate the worst execution time value in the entire main loop, so as to set the threshold time of the watchdog to ensure that in the worst case, the watchdog does not reset the system.
There are two common ways to use watchdog for system monitoring in bare metal:
(1) Insert a dog feed into the code execution flow
This is the simplest way. When developing, the "feeding the dog" of the watchdog is usually inserted into the program execution flow, especially for the program segments that take a long time to execute. For the program segments that take less time to execute, you can add "feeding the dog" in the main loop process.
(2) Use a timer to feed the dog regularly
In a bare metal machine, you can also use a timer to feed the dog regularly. Compared with inserting "feeding the dog" operations in multiple places in the code, this method makes the code look simpler, but it generally requires the use of hardware resources for hardware timing and more timer interrupts. If the program itself is relatively complex, adding more abnormal interrupts may cause some unpredictable problems to system maintenance and stability.
Of course, there are other ways to implement and use it. For the bare metal watchdog "feeding the dog", it is generally simpler, there are relatively fewer things to consider, and it is easier to implement.
2. Using watchdog in RTOS
Using a watchdog in an RTOS is much more complicated, and there are many issues to consider. Because of the existence of the multi-tasking mechanism in the RTOS, in theory, the more complex the program, the more complex the system operation, and the existence of multi-tasking may lead to more changes, some of which may be difficult to predict.
In multitasking, when using a watchdog, we hope to be able to implement the watchdog to monitor the health and survival of each task in the entire system. If a task in the system dies, the watchdog can be used to reset the system, thereby resetting the system to restart the system. When each task is normal, do not reset the watchdog, so as not to affect the normal execution of the system.
Let's briefly discuss how to implement the watchdog "feeding the dog" under RTOS:
(1) Insert "Feed the Dog" into the execution flow of the code
This method is to insert the "feed the dog" operation in the scheduling flow of each task, and then implement the "feed the dog" operation through the scheduling execution of the task. This method is relatively easy to understand and not complicated. As long as the developer is familiar with the code and knows the execution sequence flow of the code, then insert the "feed the dog" operation in the program.
What are the advantages and disadvantages of this method?
advantage:
It is easy to implement. Just call the "feed the dog" operation in each task, and pay attention to "feeding the dog" in the program segment that occupies a long CPU execution time.
shortcoming:
It is not possible to monitor the survival of each task. Because when this method is used, there are multiple places where the dog feeding operation can be performed. At this time, if a task has died, it cannot be fed because it has died, but other tasks can be fed, and the entire system will not be reset, so it is impossible to know whether a task has a problem.
(2) Monitor the survival status of each task and feed the dog
This method is more practical than calling the watchdog feeding operation everywhere. It feeds the watchdog only after confirming that each task is alive and well. If a task is no longer running due to a fault, it will cause the watchdog to time out and fail to feed the watchdog, which will cause the system to be reset by the watchdog.
There are many ways to implement this method, such as using event flag groups, which is easier to implement.
The method is:
1)创建一个“喂狗”的独立任务;
2)申请一个事件标志组,并为每个任务留一个标志位;
3)在每个单独的任务中执行的时候置位属于这个任务的标志位;
4)在“喂狗”的独立任务中检测这些所有的任务所关联的标志位,
如果所有的标志位都置位成立,说明所有的任务都正常,进行一次喂狗操作。反之则引发一次复位。
There is also a problem that needs to be noted in this approach:
Other tasks can be monitored by this independent task of "feeding the dog". Who will monitor this independent task of "feeding the dog"? The answer is definitely the watchdog.
In this independent "feeding the dog" task, in addition to monitoring the event flags of other tasks, it must also be able to perform the "feeding the dog" operation regularly to prevent the system from resetting due to long-term non-feeding of the dog. In this way, the watchdog can monitor the "feeding the dog" task, while the "feeding the dog" task monitors the status of other tasks.
Special note: Because software development under RTOS environment is different from bare metal development after all, RTOS has operations such as blocking, suspending, deleting, and resuming tasks, so the use of watchdog will be more difficult and needs to be considered comprehensively in the actual usage scenarios.
Of course, the article only briefly analyzes some ideas for using watchdogs. There are many ways to do it. If you think the analysis in the article is wrong, please point it out! If you have better ideas, please share them! ! !
|