drivers: pwm: add PWM shell
Add shell commands for setting PWM period and duty cycle (in cycles, microseconds, or nanoseconds). Signed-off-by: Henrik Brix Andersen <hebad@vestas.com>
This commit is contained in:
parent
cd35a4a753
commit
3a13c97ad7
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
122
drivers/pwm/pwm_shell.c
Normal file
122
drivers/pwm/pwm_shell.c
Normal file
|
@ -0,0 +1,122 @@
|
|||
/*
|
||||
* Copyright (c) 2019 Vestas Wind Systems A/S
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief PWM shell commands.
|
||||
*/
|
||||
|
||||
#include <shell/shell.h>
|
||||
#include <drivers/pwm.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
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, "<device> <pwm> <period in cycles> "
|
||||
"<pulse width in cycles>", cmd_cycles, 5, 0),
|
||||
SHELL_CMD_ARG(usec, NULL, "<device> <pwm> <period in usec> "
|
||||
"<pulse width in usec>", cmd_usec, 5, 0),
|
||||
SHELL_CMD_ARG(nsec, NULL, "<device> <pwm> <period in nsec> "
|
||||
"<pulse width in nsec>", cmd_nsec, 5, 0),
|
||||
SHELL_SUBCMD_SET_END
|
||||
);
|
||||
|
||||
SHELL_CMD_REGISTER(pwm, &pwm_cmds, "PWM shell commands", NULL);
|
Loading…
Reference in a new issue