RT-Thread RTOS
An open source embedded real-time operating system
|
The hardware watchdog timer is a timer whose timing output is connected to the reset terminal of the circuit. In a productized embedded system, in order to automatically reset the system under abnormal conditions, it generally needs a watchdog.
When the watchdog was started, the counter starts counting automatically. If it is not reset counter value before the counter overflows, the counter overflow will generate a reset signal to the CPU to restart the system. When the system is running normally, it is necessary to clear the watchdog counter within the time interval allowed by the watchdog (commonly known as "feeding the dog"), and the reset signal will not be generated. If the program can "feed the dog" on time,the system does not go wrong,otherwise the system will reset.
In general, users can feed the dog in the idlehook function and key function of RT-Thread.
The application accesses the watchdog hardware through the I/O device management interface provided by RT-Thread. The related interfaces are as follows:
Function | Description |
---|---|
rt_device_find() | Find the device handle based on the device name of the watchdog device |
rt_device_init() | Initialize the watchdog device |
rt_device_control() | Control the watchdog device |
rt_device_close() | Close the watchdog device |
The application obtains the device handle based on the watchdog device's name, and then it can operate the watchdog device. The function for finding a device is as follows:
Function | Description |
---|---|
name | the name of the watchdog device |
return | —— |
device handle | finding the corresponding device and then return to the corresponding device handle |
RT_NULL | no corresponding device object found |
An usage example is as follows:
The watchdog device need to be initialized before using, which can be done by the following function:
Function | Description |
---|---|
dev | handle of the watchdog device |
return | —— |
RT_EOK | the device succeeded initializing |
-RT_ENOSYS | initialization failed, the watchdog device driver initialization function is empty |
other error code | the device failed to open |
An example is as follows:
The application can configure the watchdog device using the command control word, which can be done by the following function:
Function | Description |
---|---|
dev | handle of the watchdog device |
cmd | the command word |
arg | controlled parameter |
return | —— |
RT_EOK | function executed successfully |
-RT_ENOSYS | execution failed, the dev is empty |
other error code | execution failed |
The command control word ‘'cmd’` can take the following macro definition values:
An example of setting the overflow time of the watchdog is as follows:
An example of feeding a dog in an idle thread hook function is as follows:
When the application completes the operation of the watchdog, it can close the watchdog device:
Function | Description |
---|---|
dev | handle of the watchdog device |
return | —— |
RT_EOK | close the device successfully |
-RT_ERROR | The device has been completely shut down and cannot be closed repeatedly |
other error code | fail to close the device |
Closing the device interface and opening the device interface need to match each other. When you open the device, you need to close the device once correspondingly, so that the device will be completely shut down, otherwise the device will remain unclosed.
The specific use of the watchdog device can be referred to the following sample code. The main steps of the sample code are as follows: