Demo
26 Nov 2024
Read time: 2 minute(s)
本 Demo 展示了如何使用 Watchdog 设备,并通过
test-wdt
命令进行操作。test-wdt
的详细说明,可查看源代码文件
bsp/examples/test-wdt/test-wdt.c。本 Demo 可以作为 Watchdog
设备的使用参考:#include <rtthread.h>
#include <aic_core.h>
#include <drivers/watchdog.h>
#include <aic_drv_wdt.h>
#include <hal_wdt.h>
#include <getopt.h>
irqreturn_t aic_wdt_irq(int irq, void *arg)
{
rt_kprintf("Watchdog chan0 IRQ happened\n");
return IRQ_HANDLED;
}
static void idle_hook(void)
{
rt_device_t wdt_dev = RT_NULL;
wdt_dev = rt_device_find("wdt");
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_KEEPALIVE, NULL);
}
static void usage(char * program)
{
printf("\n");
printf("Usage: %s [-s timeout] [-p pretimeout] [-c clear threshold] [-g] [-k] [-u]\n",\
program);
printf("\t -s, --set-timeout\tSet a timeout, in second\n");
printf("\t -p, --set-pretimeout\tSet a pretimeout, in second\n");
printf("\t -c, --set-clear threshold\tSet clear threshold,in second(0~3)\n");
printf("\t -g, --get-timeout\tGet the current timeout, in second\n");
printf("\t -k, --keepalive\tKeepalive the watchdog\n");
printf("\t -u, --usage \n");
printf("\n");
}
void test_wdt(int argc, char **argv)
{
int opt;
int timeout = 0;
rt_device_t wdt_dev = RT_NULL;
wdt_dev = rt_device_find("wdt");
rt_device_init(wdt_dev);
optind = 0;
while ((opt = getopt(argc, argv, "s:p:c:gku")) != -1) {
switch (opt) {
case 'c':
timeout = strtoul(optarg, NULL, 10);
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_CLR_THD, &timeout);
rt_kprintf("set clear threshold:%d\n", timeout);
break;
case 's':
timeout = strtoul(optarg, NULL, 10);
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_TIMEOUT, &timeout);
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_START, RT_NULL);
rt_kprintf("set timeout:%d\n", timeout);
break;
case 'g':
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_GET_TIMEOUT, &timeout);
rt_kprintf("timeout:%d\n", timeout);
break;
case 'p':
timeout = strtoul(optarg, NULL, 10);
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_SET_IRQ_TIMEOUT, &timeout);
rt_device_control(wdt_dev, RT_DEVICE_CTRL_WDT_IRQ_ENABLE, &aic_wdt_irq);
rt_kprintf("set pretimeout:%d\n", timeout);
break;
case 'k':
rt_thread_idle_sethook(idle_hook);
rt_kprintf("feed the dog! \n");
break;
case 'u':
default:
usage(argv[0]);
}
}
}
MSH_CMD_EXPORT_ALIAS(test_wdt, test_wdt, Reboot the system);