Watchdog Timer (WDT) is an important safety mechanism in microcontrollers (MCUs) for detecting and recovering from system failures. Its main function is to automatically reset the MCU in the event of a software failure or program runtime, so that the system can resume normal operation.
Rockchip watchdog driver supports the standard watchdog framework under Linux, and can support the standard watchdog programming method under Linux.
The node “/dev/watchdog0” belongs to the root user by default, you need to use the following commands to give other user groups operating privileges
su
chmod 666 /dev/watchdog0
Turn on the watchdog and perform a feed, if the watchdog is turned on for a period of time (default is about 40s) without feeding the dog, then the system reboot
echo 1 > /dev/watchdog0
Turn off the application layer feeding the dog (the kernel driver automatically feeds the dog)
echo V > /dev/watchdog0
After the watchdog is turned on the watchdog can not be stopped by default, so userspace program to stop running, you need to let the driver to feed the dog
JAVA code is as follows
This code implements the following functions, press the START WDT button, then start a thread, timed (10s) execution of feeding the dog, press the STOP WDT button, shut down the watchdog
Add a button to onCreate
button8.setText("start wdt");
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (wdtThread != null && wdtThread.isAlive()) {
wdtThread.interrupt();
wdtThread = null;
button8.setText("start wdt");
textDisplay.setText("stop watchdog\n");
try {
writeToFile("/dev/watchdog0","V");
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
wdtThread = new Thread(new Runnable() {
@Override
public void run() {
feedWatchdog();
}
});
wdtThread.start();
button8.setText("stop wdt");
textDisplay.setText("start watchdog\n");
}
}
});
}
The feedWatchdog threads are implemented as follows
private void feedWatchdog() {
try {
while (!Thread.currentThread().isInterrupted()) {
Thread.sleep(10000);
try {
writeToFile("/dev/watchdog0","1");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
} catch (InterruptedException e) {
}
}
The read and write functions are implemented as follows
public static void writeToFile(String fileName, String content) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
writer.write(content);
writer.flush();
}
}
public static String readFileToString(String fileName) throws IOException {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
}
return null;
}
This apk can feed the dog and stop the watchdog normally, if the app exits directly after the watchdog is turned on, a system reset will be triggered after a period of time