diff --git a/CODEOWNERS b/CODEOWNERS index 3856ce1a9d..757619a8f2 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -161,6 +161,7 @@ /drivers/serial/uart_xlnx_ps.c @wjliang /drivers/net/ @jukkar @tbursztyka /drivers/ptp_clock/ @jukkar +/drivers/pwm/pwm_shell.c @henrikbrixandersen /drivers/spi/ @tbursztyka /drivers/spi/spi_ll_stm32.* @superna9999 /drivers/spi/spi_rv32m1_lpspi* @karstenkoenig diff --git a/drivers/pwm/CMakeLists.txt b/drivers/pwm/CMakeLists.txt index d23ce60d91..fcb48f18bb 100644 --- a/drivers/pwm/CMakeLists.txt +++ b/drivers/pwm/CMakeLists.txt @@ -16,3 +16,4 @@ zephyr_library_sources_ifdef(CONFIG_PWM_MCUX pwm_mcux.c) zephyr_library_sources_ifdef(CONFIG_PWM_XEC pwm_mchp_xec.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE pwm_handlers.c) +zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c) diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig index b76028b886..3072eb20f2 100644 --- a/drivers/pwm/Kconfig +++ b/drivers/pwm/Kconfig @@ -14,6 +14,12 @@ module = PWM module-str = pwm source "subsys/logging/Kconfig.template.log_config" +config PWM_SHELL + bool "Enable PWM shell" + depends on SHELL + help + Enable the PWM related shell commands. + config PWM_0 bool "Enable PWM port 0" diff --git a/drivers/pwm/pwm_shell.c b/drivers/pwm/pwm_shell.c new file mode 100644 index 0000000000..f8f3cdce8a --- /dev/null +++ b/drivers/pwm/pwm_shell.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2019 Vestas Wind Systems A/S + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @file + * @brief PWM shell commands. + */ + +#include +#include +#include + +struct args_index { + u8_t device; + u8_t pwm; + u8_t period; + u8_t pulse; +}; + +static const struct args_index args_indx = { + .device = 1, + .pwm = 2, + .period = 3, + .pulse = 4, +}; + +static int cmd_cycles(const struct shell *shell, size_t argc, char **argv) +{ + struct device *dev; + u32_t period; + u32_t pulse; + u32_t pwm; + int err; + + dev = device_get_binding(argv[args_indx.device]); + if (!dev) { + shell_error(shell, "PWM device not found"); + return -EINVAL; + } + + pwm = strtoul(argv[args_indx.pwm], NULL, 0); + period = strtoul(argv[args_indx.period], NULL, 0); + pulse = strtoul(argv[args_indx.pulse], NULL, 0); + + err = pwm_pin_set_cycles(dev, pwm, period, pulse); + if (err) { + shell_error(shell, "failed to setup PWM (err %d)", + err); + return err; + } + + return 0; +} + +static int cmd_usec(const struct shell *shell, size_t argc, char **argv) +{ + struct device *dev; + u32_t period; + u32_t pulse; + u32_t pwm; + int err; + + dev = device_get_binding(argv[args_indx.device]); + if (!dev) { + shell_error(shell, "PWM device not found"); + return -EINVAL; + } + + pwm = strtoul(argv[args_indx.pwm], NULL, 0); + period = strtoul(argv[args_indx.period], NULL, 0); + pulse = strtoul(argv[args_indx.pulse], NULL, 0); + + err = pwm_pin_set_usec(dev, pwm, period, pulse); + if (err) { + shell_error(shell, "failed to setup PWM (err %d)", err); + return err; + } + + return 0; +} + +static int cmd_nsec(const struct shell *shell, size_t argc, char **argv) +{ + struct device *dev; + u32_t period; + u32_t pulse; + u32_t pwm; + int err; + + dev = device_get_binding(argv[args_indx.device]); + if (!dev) { + shell_error(shell, "PWM device not found"); + return -EINVAL; + } + + pwm = strtoul(argv[args_indx.pwm], NULL, 0); + period = strtoul(argv[args_indx.period], NULL, 0); + pulse = strtoul(argv[args_indx.pulse], NULL, 0); + + err = pwm_pin_set_nsec(dev, pwm, period, pulse); + if (err) { + shell_error(shell, "failed to setup PWM (err %d)", err); + return err; + } + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(pwm_cmds, + SHELL_CMD_ARG(cycles, NULL, " " + "", cmd_cycles, 5, 0), + SHELL_CMD_ARG(usec, NULL, " " + "", cmd_usec, 5, 0), + SHELL_CMD_ARG(nsec, NULL, " " + "", cmd_nsec, 5, 0), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(pwm, &pwm_cmds, "PWM shell commands", NULL);