blob: f8f3cdce8abe1b5f7075b1199964e4ead0bdeff5 [file] [log] [blame]
/*
* 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);