Skip to contentSkip to conversation

commands: add pwm manipulation command

PATCH Superseded Marc Reilly 2 participants 2 messages May 27, 2023 – May 28, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 commands: add pwm manipulation command

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

---
 commands/Kconfig  |  11 ++++
 commands/Makefile |   1 +
 commands/pwm.c    | 149 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 commands/pwm.c
commands/Kconfig +11 -0
index 4d3ff631a8..f90111bd42 100644
@@ -1929,6 +1929,17 @@ config CMD_I2C
 		  -w		use word (16 bit) wide access
 		  -v		verbose
 
+config CMD_PWM
+	bool
+	depends on PWM
+	prompt "PWM commands: pwm_set"
+	help
+	  pwm_set - set pwm state
+
+	  Usage: pwm_set [ndDPfwis]
+
+ 	  Controls the pwm values such as period and duty cycle
+
 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 +149 -0

149 lines not on this page · raw message

Signature · 1 line
2.35.3
#1 lore raw

Hi, not really a Barebox developer but I figured I might chime in.

Quoted 4 lines · Marc Reilly

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

---

...

Quoted 4 lines
commands/pwm.c new file +4 -0
+	if ((n < 0) && !devname) {
+		printf(" need to specify a device\n");
+		return COMMAND_ERROR_USAGE;
+	}

commands/pwm.c line 67

Quoted 10 lines
commands/pwm.c new file +10 -0
+	if ((freq == 0) || (period == 0)) {
+		printf(" period or freqency needs to be non-zero\n");
+		return COMMAND_ERROR_USAGE;
+	}
+
+	if (!devname) {
+		sprintf(namebuf, "pwm%d", n);
+	} else {
+		strcpy(namebuf, devname);
+	}

Is devname capped to namebuf length? It might be better refer to devname instead of namebuf and point devname to namebuf when you use namebuf, otherwise point it to the the optarg.

Is it even worth supporting refering by number instead of just having scripts type pwmX?

commands/pwm.c line 71

Quoted 3 lines
commands/pwm.c new file +3 -0
+	pwm = pwm_request(namebuf);
+	if (!pwm) {
+		printf("pwm device %s not found\n", namebuf);

No space at the start?

commands/pwm.c line 79

Quoted 8 lines
commands/pwm.c new file +8 -0
+		return -ENODEV;
+	}
+
+	pwm_get_state(pwm, &state);
+
+	if ((state.period_ns == 0)
+			&& (freq < 0) && (duty < 0) && (period < 0)) {
+		printf(" need to know some timing info; freq or dury/period\n");

No pwm_free?

Quoted 20 lines
commands/pwm.c new file +20 -0
+		return COMMAND_ERROR_USAGE;
+	}
+
+	if (polarity >= 0)
+		state.polarity = polarity;
+
+	/* period */
+	if (freq > 0) {
+		state.p_enable = true;
+		state.period_ns = HZ_TO_NANOSECONDS(freq);
+		if (width < 0) {
+			width = 50;
+		}
+	} else if (period > 0) {
+		state.p_enable = true;
+		state.period_ns = period;
+	}
+
+	/* duty */
+	if (width >= 0) {

commands/pwm.c line 109

Quoted 10 lines
commands/pwm.c new file +10 -0
+		if (width > 100)
+			width = 100;
+
+		pwm_set_relative_duty_cycle(&state, width, 100);
+	} else if (duty >= 0) {
+		if (duty > state.period_ns)
+			printf(" warning: duty_ns is greater than period\n");
+
+		state.duty_ns = duty;
+	}

It might be worth moving the width and duty checks to up with the opt parsing and make a width > 100 and error.

Jookia.