diff --git a/subsys/task_wdt/CMakeLists.txt b/subsys/task_wdt/CMakeLists.txt index 586eec92dd..6f94461d20 100644 --- a/subsys/task_wdt/CMakeLists.txt +++ b/subsys/task_wdt/CMakeLists.txt @@ -1,3 +1,4 @@ # SPDX-License-Identifier: Apache-2.0 zephyr_sources_ifdef(CONFIG_TASK_WDT task_wdt.c) +zephyr_sources_ifdef(CONFIG_TASK_WDT_SHELL task_wdt_shell.c) diff --git a/subsys/task_wdt/Kconfig b/subsys/task_wdt/Kconfig index d1429cffe9..b7f0b158af 100644 --- a/subsys/task_wdt/Kconfig +++ b/subsys/task_wdt/Kconfig @@ -58,4 +58,10 @@ config TASK_WDT_HW_FALLBACK_DELAY kernel timer. This is especially important if the hardware watchdog is clocked by an inaccurate low-speed RC oscillator. +config TASK_WDT_SHELL + bool "Task watchdog shell utilities" + depends on SHELL + help + Activate shell module that provides Task watchdog commands. + endif diff --git a/subsys/task_wdt/task_wdt_shell.c b/subsys/task_wdt/task_wdt_shell.c new file mode 100644 index 0000000000..6d172fd8ae --- /dev/null +++ b/subsys/task_wdt/task_wdt_shell.c @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2024 Vogl Electronic GmbH + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +static int cmd_init(const struct shell *sh, size_t argc, char *argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + +#ifdef CONFIG_TASK_WDT_HW_FALLBACK + const struct device *const wdt = DEVICE_DT_GET(DT_ALIAS(watchdog0)); +#else + const struct device *const wdt = NULL; +#endif + + shell_fprintf(sh, SHELL_INFO, "Init task watchdog ...\n"); + + int ret = task_wdt_init(wdt); + + if (ret < 0) { + shell_fprintf(sh, SHELL_ERROR, "Failed to init task watchdog: %d\n", ret); + return ret; + } + + return 0; +} + +static int cmd_add(const struct shell *sh, size_t argc, char **argv) +{ + if (argc != 2) { + shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n"); + return -EINVAL; + } + + shell_fprintf(sh, SHELL_INFO, "Add task watchdog channel\n"); + + uint32_t period = atoi(argv[1]) * MSEC_PER_SEC; + + int ret = task_wdt_add(period, NULL, NULL); + + if (ret < 0) { + shell_fprintf(sh, SHELL_ERROR, "Failed to add task watchdog channel: %d\n", ret); + return ret; + } + + shell_fprintf(sh, SHELL_INFO, "Task watchdog channel: %d\n", ret); + + shell_fprintf(sh, SHELL_NORMAL, + "Use \"task_wdt feed %d\" to feed this channel\n" + "and \"task_wdt del %d\" to delete this channel\n", + ret, ret); + + return 0; +} + +static int cmd_feed(const struct shell *sh, size_t argc, char **argv) +{ + if (argc != 2) { + shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n"); + return -EINVAL; + } + + shell_fprintf(sh, SHELL_INFO, "Feed task watchdog channel %s\n", argv[1]); + + int ret = task_wdt_feed(atoi(argv[1])); + + if (ret < 0) { + shell_fprintf(sh, SHELL_ERROR, "Failed to add task watchdog channel: %d\n", ret); + return ret; + } + + return 0; +} + +static int cmd_del(const struct shell *sh, size_t argc, char **argv) +{ + if (argc != 2) { + shell_fprintf(sh, SHELL_ERROR, "Invalid number of arguments\n"); + return -EINVAL; + } + + shell_fprintf(sh, SHELL_INFO, "Delete task watchdog channel %s\n", argv[1]); + + int ret = task_wdt_delete(atoi(argv[1])); + + if (ret < 0) { + shell_fprintf(sh, SHELL_ERROR, "Failed to delete task watchdog channel: %d\n", ret); + return ret; + } + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE( + sub_task_wdt, + SHELL_CMD(init, NULL, "Initialize task watchdog", cmd_init), + SHELL_CMD(add, NULL, "Install new timeout (time in seconds)", cmd_add), + SHELL_CMD(feed, NULL, "Feed specified watchdog channel", cmd_feed), + SHELL_CMD(del, NULL, "Delete task watchdog channel", cmd_del), + SHELL_SUBCMD_SET_END); + +SHELL_CMD_REGISTER(task_wdt, &sub_task_wdt, "Task watchdog commands", NULL);