Skip to contentSkip to conversation

Adding PWM Commands (v4)

PATCH In review Marc Reilly 2 participants 4 messages Jun 2, 2023 – Jun 6, 2023

lore
Share & tools
lore
#0 lore raw

Thanks again all for comments so far.

Changes in v4:
- rework set/get into single command
- add more cmd arg checking

Changes in v3:
- fix minor typos
- remove unused 'error' var in pwm_get

Changes in v2:
- remove '-n' option to specify device by number
- fix freeing resources on all return paths
- add pwm_get command

#1 lore raw

PATCH v4 1/2 commands: add pwm manipulation command

This introduces a command to set parameters for a pwm device.

---
 commands/Kconfig  |  12 +++
 commands/Makefile |   1 +
 commands/pwm.c    | 199 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 212 insertions(+)
 create mode 100644 commands/pwm.c
commands/Kconfig +12 -0
index 4d3ff631a8..28f9e46a59 100644
@@ -1929,6 +1929,18 @@ config CMD_I2C
 		  -w		use word (16 bit) wide access
 		  -v		verbose
 
+config CMD_PWM
+	bool
+	depends on PWM
+	prompt "PWM commands: pwm"
+	help
+	  pwm - set or show pwm state
+
+	  Usage: pwm [dDPfwisv]
+
+	  Controls the pwm values such as period and duty cycle, or shows
+	  the current values.
+
 config CMD_LED
 	bool
 	depends on LED
commands/Makefile +1 -0
index 98625a0373..011ae59427 100644
@@ -69,6 +69,7 @@ obj-$(CONFIG_CMD_GPIO)		+= gpio.o
 obj-$(CONFIG_CMD_UNCOMPRESS)	+= uncompress.o
 obj-$(CONFIG_CMD_I2C)		+= i2c.o
 obj-$(CONFIG_CMD_SPI)		+= spi.o
+obj-$(CONFIG_CMD_PWM)		+= pwm.o
 obj-$(CONFIG_CMD_MIPI_DBI)	+= mipi_dbi.o
 obj-$(CONFIG_CMD_UBI)		+= ubi.o
 obj-$(CONFIG_CMD_UBIFORMAT)	+= ubiformat.o
commands/pwm.c new file +199 -0

199 lines not on this page · raw message

Signature · 1 line
2.35.3
#2 lore raw

Re: [PATCH v4 1/2] commands: add pwm manipulation command

Hi Marc,

I applied this with some small changes.

Quoted 34 lines · Marc Reilly
commands/pwm.c new file +34 -0
+static int do_pwm_cmd(int argc, char *argv[])
+{
+	struct pwm_device *pwm = NULL;
+	struct pwm_state state, orig_state;
+	int error = 0;
+	char *devname = NULL;
+	int duty = -1, period = -1;
+	int freq = -1, width = -1;
+	bool invert_polarity = false, stop = false;
+	bool use_default_width = false;
+	bool verbose = false;
+	int opt;
+
+	while ((opt = getopt(argc, argv, "d:D:P:f:w:F:isv")) > 0) {
+		switch (opt) {
+		case 'd':
+			devname = optarg;
+			break;
+		case 'D':
+			duty = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'P':
+			period = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'F':
+			/* convenience option for changing frequency without
+			 * having to specify duty width */
+			use_default_width = true;
+			/* fallthrough */
+		case 'f':
+			freq = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'w':
+			width = simple_strtol(optarg, NULL, 0);

commands/pwm.c line 67

Quoted 10 lines
commands/pwm.c new file +10 -0
+			break;
+		case 'i':
+			invert_polarity = true;
+			break;
+		case 's':
+			stop = true;
+			break;
+		case 'v':
+			verbose = true;
+			break;
		default:
			return COMMAND_ERROR_USAGE;
Quoted 30 lines
commands/pwm.c new file +30 -0
+		}
+	}
+
+	if (!devname) {
+		printf(" need to specify a device\n");
+		return COMMAND_ERROR_USAGE;
+	}
+	if ((freq == 0) || (period == 0)) {
+		printf(" period or freqency needs to be non-zero\n");
+		return COMMAND_ERROR_USAGE;
+	}
+	if (freq >= 0 && period >= 0) {
+		printf(" specify period or frequency, not both\n");
+		return COMMAND_ERROR_USAGE;
+	}
+	if (duty >= 0 && width >= 0) {
+		printf(" specify duty or width, not both\n");
+		return COMMAND_ERROR_USAGE;
+	}
+	if (width > 100) {
+		printf(" width (%% duty cycle) can't be more than 100%%\n");
+		return COMMAND_ERROR_USAGE;
+	}
+
+	pwm = pwm_request(devname);
+	if (!pwm) {
+		printf(" pwm device %s not found\n", devname);
+		return -ENODEV;
+	}
+

commands/pwm.c line 107

Quoted 10 lines
commands/pwm.c new file +10 -0
+	pwm_get_state(pwm, &state);
+
+	/* argc will be at least 3 with a valid devname */
+	if (verbose || (argc <= 3)) {
+		printf("pwm params for '%s':\n", devname);
+		printf("  period   : %u (ns)\n", state.period_ns);
+		printf("  duty     : %u (ns)\n", state.duty_ns);
+		printf("  enabled  : %d\n", state.p_enable);
+		printf("  polarity : %s\n", state.polarity == 0 ? "Normal" : "Inverted");
+		printf("  freq     : %lu (Hz)\n", HZ_FROM_NANOSECONDS(state.period_ns));

This results in a division by zero crash on a disabled PWM. I changed this to:

		if (state.period_ns)
			printf("  freq     : %lu (Hz)\n", HZ_FROM_NANOSECONDS(state.period_ns));
		else
			printf("  freq     : -\n");

commands/pwm.c line 187

Quoted 5 lines
commands/pwm.c new file +5 -0
+BAREBOX_CMD_HELP_OPT("-d string", "device name (eg 'pwm0')")
+BAREBOX_CMD_HELP_OPT("-D number", "duty cycle (ns)")
+BAREBOX_CMD_HELP_OPT("-P number", "period (ns)")
+BAREBOX_CMD_HELP_OPT("-f number", "frequency (Hz)")
+BAREBOX_CMD_HELP_OPT("-w number", "duty cycle (%) - the on 'width' of each cycle")

Replaced 'number' with <duty_ns> and similar.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#3 lore raw

PATCH v4 2/2 include: pwm: minor function doc fix for pwm_set_relative_duty_cycle()

In reply to Marc Reilly
---
 include/pwm.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/pwm.h +1 -1
index 643ef4d7ee..02af7d0a5f 100644
@@ -71,7 +71,7 @@ unsigned int pwm_get_period(struct pwm_device *pwm);
  * @scale: scale in which @duty_cycle is expressed
  *
  * This functions converts a relative into an absolute duty cycle (expressed
- * in nanoseconds), and puts the result in state->duty_cycle.
+ * in nanoseconds), and puts the result in state->duty_ns.
  *
  * For example if you want to configure a 50% duty cycle, call:
  *
Signature · 1 line
2.35.3