1 /*
2  * Copyright (c) 2019 Vestas Wind Systems A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief PWM shell commands.
10  */
11 
12 #include <shell/shell.h>
13 #include <drivers/pwm.h>
14 #include <stdlib.h>
15 
16 struct args_index {
17 	uint8_t device;
18 	uint8_t pwm;
19 	uint8_t period;
20 	uint8_t pulse;
21 	uint8_t flags;
22 };
23 
24 static const struct args_index args_indx = {
25 	.device = 1,
26 	.pwm = 2,
27 	.period = 3,
28 	.pulse = 4,
29 	.flags = 5,
30 };
31 
cmd_cycles(const struct shell * shell,size_t argc,char ** argv)32 static int cmd_cycles(const struct shell *shell, size_t argc, char **argv)
33 {
34 	pwm_flags_t flags = 0;
35 	const struct device *dev;
36 	uint32_t period;
37 	uint32_t pulse;
38 	uint32_t pwm;
39 	int err;
40 
41 	dev = device_get_binding(argv[args_indx.device]);
42 	if (!dev) {
43 		shell_error(shell, "PWM device not found");
44 		return -EINVAL;
45 	}
46 
47 	pwm = strtoul(argv[args_indx.pwm], NULL, 0);
48 	period = strtoul(argv[args_indx.period], NULL, 0);
49 	pulse = strtoul(argv[args_indx.pulse], NULL, 0);
50 
51 	if (argc == (args_indx.flags + 1)) {
52 		flags = strtoul(argv[args_indx.flags], NULL, 0);
53 	}
54 
55 	err = pwm_pin_set_cycles(dev, pwm, period, pulse, flags);
56 	if (err) {
57 		shell_error(shell, "failed to setup PWM (err %d)",
58 			    err);
59 		return err;
60 	}
61 
62 	return 0;
63 }
64 
cmd_usec(const struct shell * shell,size_t argc,char ** argv)65 static int cmd_usec(const struct shell *shell, size_t argc, char **argv)
66 {
67 	pwm_flags_t flags = 0;
68 	const struct device *dev;
69 	uint32_t period;
70 	uint32_t pulse;
71 	uint32_t pwm;
72 	int err;
73 
74 	dev = device_get_binding(argv[args_indx.device]);
75 	if (!dev) {
76 		shell_error(shell, "PWM device not found");
77 		return -EINVAL;
78 	}
79 
80 	pwm = strtoul(argv[args_indx.pwm], NULL, 0);
81 	period = strtoul(argv[args_indx.period], NULL, 0);
82 	pulse = strtoul(argv[args_indx.pulse], NULL, 0);
83 
84 	if (argc == (args_indx.flags + 1)) {
85 		flags = strtoul(argv[args_indx.flags], NULL, 0);
86 	}
87 
88 	err = pwm_pin_set_usec(dev, pwm, period, pulse, flags);
89 	if (err) {
90 		shell_error(shell, "failed to setup PWM (err %d)", err);
91 		return err;
92 	}
93 
94 	return 0;
95 }
96 
cmd_nsec(const struct shell * shell,size_t argc,char ** argv)97 static int cmd_nsec(const struct shell *shell, size_t argc, char **argv)
98 {
99 	pwm_flags_t flags = 0;
100 	const struct device *dev;
101 	uint32_t period;
102 	uint32_t pulse;
103 	uint32_t pwm;
104 	int err;
105 
106 	dev = device_get_binding(argv[args_indx.device]);
107 	if (!dev) {
108 		shell_error(shell, "PWM device not found");
109 		return -EINVAL;
110 	}
111 
112 	pwm = strtoul(argv[args_indx.pwm], NULL, 0);
113 	period = strtoul(argv[args_indx.period], NULL, 0);
114 	pulse = strtoul(argv[args_indx.pulse], NULL, 0);
115 
116 	if (argc == (args_indx.flags + 1)) {
117 		flags = strtoul(argv[args_indx.flags], NULL, 0);
118 	}
119 
120 	err = pwm_pin_set_nsec(dev, pwm, period, pulse, flags);
121 	if (err) {
122 		shell_error(shell, "failed to setup PWM (err %d)", err);
123 		return err;
124 	}
125 
126 	return 0;
127 }
128 
129 SHELL_STATIC_SUBCMD_SET_CREATE(pwm_cmds,
130 	SHELL_CMD_ARG(cycles, NULL, "<device> <pwm> <period in cycles> "
131 		      "<pulse width in cycles> [flags]", cmd_cycles, 5, 1),
132 	SHELL_CMD_ARG(usec, NULL, "<device> <pwm> <period in usec> "
133 		      "<pulse width in usec> [flags]", cmd_usec, 5, 1),
134 	SHELL_CMD_ARG(nsec, NULL, "<device> <pwm> <period in nsec> "
135 		      "<pulse width in nsec> [flags]", cmd_nsec, 5, 1),
136 	SHELL_SUBCMD_SET_END
137 );
138 
139 SHELL_CMD_REGISTER(pwm, &pwm_cmds, "PWM shell commands", NULL);
140