From 8a5ba4539c2b2e11107a074fe4a5a24e9696dc4b Mon Sep 17 00:00:00 2001 From: Henrik Brix Andersen Date: Sun, 16 Feb 2020 20:55:58 +0100 Subject: [PATCH] drivers: dac: add DAC shell commands Add shell commands for controlling digital-to-analog (DAC) devices. Signed-off-by: Henrik Brix Andersen --- drivers/dac/CMakeLists.txt | 1 + drivers/dac/Kconfig | 6 +++ drivers/dac/dac_shell.c | 100 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 drivers/dac/dac_shell.c diff --git a/drivers/dac/CMakeLists.txt b/drivers/dac/CMakeLists.txt index 7db815c533..630c3058c9 100644 --- a/drivers/dac/CMakeLists.txt +++ b/drivers/dac/CMakeLists.txt @@ -5,4 +5,5 @@ zephyr_library() zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC dac_mcux_dac.c) zephyr_library_sources_ifdef(CONFIG_DAC_MCUX_DAC32 dac_mcux_dac32.c) zephyr_library_sources_ifdef(CONFIG_DAC_STM32 dac_stm32.c) +zephyr_library_sources_ifdef(CONFIG_DAC_SHELL dac_shell.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE dac_handlers.c) diff --git a/drivers/dac/Kconfig b/drivers/dac/Kconfig index 8f17692c49..71ac55747e 100644 --- a/drivers/dac/Kconfig +++ b/drivers/dac/Kconfig @@ -17,6 +17,12 @@ module = DAC module-str = DAC source "subsys/logging/Kconfig.template.log_config" +config DAC_SHELL + bool "Enable DAC shell" + depends on SHELL + help + Enable DAC related shell commands. + source "drivers/dac/Kconfig.mcux" source "drivers/dac/Kconfig.stm32" diff --git a/drivers/dac/dac_shell.c b/drivers/dac/dac_shell.c new file mode 100644 index 0000000000..ec9d10dd3d --- /dev/null +++ b/drivers/dac/dac_shell.c @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2020 Henrik Brix Andersen + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief DAC shell commands. + */ + +#include +#include +#include + +struct args_index { + u8_t device; + u8_t channel; + u8_t reference; + u8_t value; + u8_t buffered; + u8_t resolution; +}; + +static const struct args_index args_indx = { + .device = 1, + .channel = 2, + .reference = 3, + .value = 3, + .buffered = 4, + .resolution = 4, +}; + +static int cmd_setup(const struct shell *shell, size_t argc, char **argv) +{ + struct dac_channel_cfg cfg; + struct device *dac; + int err; + + dac = device_get_binding(argv[args_indx.device]); + if (!dac) { + shell_error(shell, "DAC device not found"); + return -EINVAL; + } + + cfg.channel_id = strtoul(argv[args_indx.channel], NULL, 0); + /* TODO: Allow text input for reference */ + cfg.reference = strtoul(argv[args_indx.reference], NULL, 0); + + if (argc == (args_indx.buffered + 1)) { + cfg.buffered = strtoul(argv[args_indx.buffered], NULL, 2); + } else { + cfg.buffered = false; + } + + err = dac_channel_setup(dac, &cfg); + if (err) { + shell_error(shell, "Failed to setup DAC channel (err %d)", err); + return err; + } + + return 0; +} + +static int cmd_write_value(const struct shell *shell, size_t argc, char **argv) +{ + struct device *dac; + u8_t resolution; + u8_t channel; + u32_t value; + int err; + + dac = device_get_binding(argv[args_indx.device]); + if (!dac) { + shell_error(shell, "DAC device not found"); + return -EINVAL; + } + + channel = strtoul(argv[args_indx.channel], NULL, 0); + value = strtoul(argv[args_indx.value], NULL, 0); + resolution = strtoul(argv[args_indx.resolution], NULL, 0); + + err = dac_write_value(dac, channel, value, resolution); + if (err) { + shell_error(shell, "Failed to write DAC value (err %d)", err); + return err; + } + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(dac_cmds, + SHELL_CMD_ARG(setup, NULL, " [buffered]", + cmd_setup, 4, 1), + SHELL_CMD_ARG(write_value, NULL, " ", + cmd_write_value, 5, 0), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(dac, &dac_cmds, "DAC shell commands", NULL);