mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* Adding a pwm command
@ 2023-05-26  8:23 Marc Reilly
  2023-05-26  8:23 ` [RFC PATCH 1/1] commands: add pwm manipulation command Marc Reilly
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Reilly @ 2023-05-26  8:23 UTC (permalink / raw)
  To: barebox

Hi all, 

I've been playing around with adding a new board to barebox, and its
been a while so I didn't realise that properties could be set on a
driver instance directly (eg 'pwm0.enable=1' or 'pwm0.period_ns=10000')

I started this pwm_set command, if it's useful (ability to set frequency
is helpful) I can submit it properly. It looks like most other commands
have doc duplicated in the Kconfig, any other requests?

Cheers
Marc





^ permalink raw reply	[flat|nested] 3+ messages in thread

* [RFC PATCH 1/1] commands: add pwm manipulation command
  2023-05-26  8:23 Adding a pwm command Marc Reilly
@ 2023-05-26  8:23 ` Marc Reilly
  2023-05-26  9:33   ` Johannes Zink
  0 siblings, 1 reply; 3+ messages in thread
From: Marc Reilly @ 2023-05-26  8:23 UTC (permalink / raw)
  To: barebox

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

Signed-off-by: Marc Reilly <marc@cpdesign.com.au>
---
 commands/Kconfig  |  11 +++++
 commands/Makefile |   1 +
 commands/pwm.c    | 111 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 123 insertions(+)
 create mode 100644 commands/pwm.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 4d3ff631a8..6b7d0bfd79 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -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: i2c_set - set pwm parameters
+
+
 config CMD_LED
 	bool
 	depends on LED
diff --git a/commands/Makefile b/commands/Makefile
index 98625a0373..011ae59427 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -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
diff --git a/commands/pwm.c b/commands/pwm.c
new file mode 100644
index 0000000000..8a26430e19
--- /dev/null
+++ b/commands/pwm.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+// SPDX-FileCopyrightText: © 2023 Marc Reilly <marc@cpdesign.com.au>
+
+/* pwm - pwm commands */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <pwm.h>
+
+#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
+
+static int do_pwm_set(int argc, char *argv[])
+{
+	struct pwm_device *pwm = NULL;
+	struct pwm_state state;
+	int error = 0;
+	char namebuf[128];
+	char *devname = NULL;
+	int n = -1, polarity = -1, duty = -1, period = -1, freq = -1, opt;
+
+	while ((opt = getopt(argc, argv, "n:d:D:P:f:i:")) > 0) {
+		switch (opt) {
+		case 'n':
+			n = simple_strtol(optarg, NULL, 0);
+			break;
+		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':
+			freq = simple_strtol(optarg, NULL, 0);
+			break;
+		case 'i':
+			polarity = simple_strtol(optarg, NULL, 0);
+			break;
+		}
+	}
+
+	if ((n < 0) && !devname) {
+		printf(" need to specify a device\n");
+		return COMMAND_ERROR_USAGE;
+	}
+	if ((freq < 0) && (duty < 0) && (period < 0)) {
+		printf(" need to know some timing info; freq or dury/period\n");
+		return COMMAND_ERROR_USAGE;
+	}
+
+	if (!devname) {
+		sprintf(namebuf, "pwm%d", n);
+	} else {
+		strcpy(namebuf, devname);
+	}
+
+	pwm = pwm_request(namebuf);
+	if (!pwm) {
+		printf("pwm device %s not found\n", namebuf);
+		return -ENODEV;
+	}
+
+	pwm_get_state(pwm, &state);
+	if (polarity >= 0)
+		state.polarity = polarity;
+
+	if (freq > 0) {
+		state.p_enable = true;
+		state.period_ns = HZ_TO_NANOSECONDS(freq);
+		pwm_set_relative_duty_cycle(&state, 50, 100);
+	} else if (duty > 0) {
+		state.p_enable = true;
+		state.period_ns = period;
+		state.duty_ns = duty;
+	} else {
+		state.p_enable = false;
+	}
+
+	error = pwm_apply_state(pwm, &state);
+
+	pwm_free(pwm);
+
+	return error;
+}
+
+BAREBOX_CMD_HELP_START(pwm_read)
+BAREBOX_CMD_HELP_TEXT("Sets a pwm device parameters.")
+BAREBOX_CMD_HELP_TEXT(" Specify the device either by device name or pwm number")
+BAREBOX_CMD_HELP_TEXT(" Timings can either be specified via period + duty (on) duration,")
+BAREBOX_CMD_HELP_TEXT("  or via frequency.")
+BAREBOX_CMD_HELP_TEXT("Options:")
+BAREBOX_CMD_HELP_OPT("-d number\t", "device name (eg 'pwm0')")
+BAREBOX_CMD_HELP_OPT("-n number\t", "pwm chip number (default 0)")
+BAREBOX_CMD_HELP_OPT("-D number\t", "duty cycle (ns)")
+BAREBOX_CMD_HELP_OPT("-P number\t", "period (ns)")
+BAREBOX_CMD_HELP_OPT("-f number\t", "frequency (Hz)")
+BAREBOX_CMD_HELP_OPT("-i number\t", "line inverted polarity")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(pwm_set)
+	.cmd		= do_pwm_set,
+	BAREBOX_CMD_DESC("set pwm")
+	BAREBOX_CMD_OPTS("[-dnDPfp]")
+	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
+	BAREBOX_CMD_HELP(cmd_pwm_read_help)
+BAREBOX_CMD_END
-- 
2.35.3




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC PATCH 1/1] commands: add pwm manipulation command
  2023-05-26  8:23 ` [RFC PATCH 1/1] commands: add pwm manipulation command Marc Reilly
@ 2023-05-26  9:33   ` Johannes Zink
  0 siblings, 0 replies; 3+ messages in thread
From: Johannes Zink @ 2023-05-26  9:33 UTC (permalink / raw)
  To: barebox

Hi Marc,

thanks for your patch.

On 5/26/23 10:23, Marc Reilly wrote:
> This introduces a command to set parameters for a pwm device.
> 
> Signed-off-by: Marc Reilly <marc@cpdesign.com.au>
> ---
>   commands/Kconfig  |  11 +++++
>   commands/Makefile |   1 +
>   commands/pwm.c    | 111 ++++++++++++++++++++++++++++++++++++++++++++++
>   3 files changed, 123 insertions(+)
>   create mode 100644 commands/pwm.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 4d3ff631a8..6b7d0bfd79 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -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: i2c_set - set pwm parameters

this should be pwm_set

> +
> +
>   config CMD_LED
>   	bool
>   	depends on LED
> diff --git a/commands/Makefile b/commands/Makefile
> index 98625a0373..011ae59427 100644
> --- a/commands/Makefile
> +++ b/commands/Makefile
> @@ -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
> diff --git a/commands/pwm.c b/commands/pwm.c
> new file mode 100644
> index 0000000000..8a26430e19
> --- /dev/null
> +++ b/commands/pwm.c
> @@ -0,0 +1,111 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +// SPDX-FileCopyrightText: © 2023 Marc Reilly <marc@cpdesign.com.au>
> +
> +/* pwm - pwm commands */
> +
> +#include <common.h>
> +#include <command.h>
> +#include <errno.h>
> +#include <malloc.h>
> +#include <getopt.h>
> +#include <pwm.h>
> +
> +#define HZ_TO_NANOSECONDS(x) (1000000000UL/(x))
> +
> +static int do_pwm_set(int argc, char *argv[])
> +{
> +	struct pwm_device *pwm = NULL;
> +	struct pwm_state state;
> +	int error = 0;
> +	char namebuf[128];
> +	char *devname = NULL;
> +	int n = -1, polarity = -1, duty = -1, period = -1, freq = -1, opt;
> +
> +	while ((opt = getopt(argc, argv, "n:d:D:P:f:i:")) > 0) {
> +		switch (opt) {
> +		case 'n':
> +			n = simple_strtol(optarg, NULL, 0);
> +			break;
> +		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':
> +			freq = simple_strtol(optarg, NULL, 0);
> +			break;
> +		case 'i':
> +			polarity = simple_strtol(optarg, NULL, 0);
> +			break;
> +		}
> +	}
> +
> +	if ((n < 0) && !devname) {
> +		printf(" need to specify a device\n");
> +		return COMMAND_ERROR_USAGE;
> +	}
> +	if ((freq < 0) && (duty < 0) && (period < 0)) {
> +		printf(" need to know some timing info; freq or dury/period\n");
> +		return COMMAND_ERROR_USAGE;
> +	}
> +
> +	if (!devname) {
> +		sprintf(namebuf, "pwm%d", n);
> +	} else {
> +		strcpy(namebuf, devname);
> +	}
> +
> +	pwm = pwm_request(namebuf);
> +	if (!pwm) {
> +		printf("pwm device %s not found\n", namebuf);
> +		return -ENODEV;
> +	}
> +
> +	pwm_get_state(pwm, &state);
> +	if (polarity >= 0)
> +		state.polarity = polarity;
> +
> +	if (freq > 0) {
> +		state.p_enable = true;
> +		state.period_ns = HZ_TO_NANOSECONDS(freq);
> +		pwm_set_relative_duty_cycle(&state, 50, 100);
> +	} else if (duty > 0) {
> +		state.p_enable = true;
> +		state.period_ns = period;
> +		state.duty_ns = duty;
> +	} else {
> +		state.p_enable = false;

This is probably not doing what you expect it to do. To switch off the 
PWM generation, you need to set

state.p_enable = true;
state.period_ns = <something_larger_than 0>;
state.duty_ns = 0;

Please see [1] for more details.

Best regards,
Johannes

[1] 
https://pengutronix.de/de/blog/2023-03-14-pulse-width-modulation-is-easy-isn-t-it-turning-it-off-and-on-again.html

> +	}
> +
> +	error = pwm_apply_state(pwm, &state);
> +
> +	pwm_free(pwm);
> +
> +	return error;
> +}
> +
> +BAREBOX_CMD_HELP_START(pwm_read)
> +BAREBOX_CMD_HELP_TEXT("Sets a pwm device parameters.")
> +BAREBOX_CMD_HELP_TEXT(" Specify the device either by device name or pwm number")
> +BAREBOX_CMD_HELP_TEXT(" Timings can either be specified via period + duty (on) duration,")
> +BAREBOX_CMD_HELP_TEXT("  or via frequency.")
> +BAREBOX_CMD_HELP_TEXT("Options:")
> +BAREBOX_CMD_HELP_OPT("-d number\t", "device name (eg 'pwm0')")
> +BAREBOX_CMD_HELP_OPT("-n number\t", "pwm chip number (default 0)")
> +BAREBOX_CMD_HELP_OPT("-D number\t", "duty cycle (ns)")
> +BAREBOX_CMD_HELP_OPT("-P number\t", "period (ns)")
> +BAREBOX_CMD_HELP_OPT("-f number\t", "frequency (Hz)")
> +BAREBOX_CMD_HELP_OPT("-i number\t", "line inverted polarity")
> +BAREBOX_CMD_HELP_END
> +
> +BAREBOX_CMD_START(pwm_set)
> +	.cmd		= do_pwm_set,
> +	BAREBOX_CMD_DESC("set pwm")
> +	BAREBOX_CMD_OPTS("[-dnDPfp]")
> +	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
> +	BAREBOX_CMD_HELP(cmd_pwm_read_help)
> +BAREBOX_CMD_END

-- 
Pengutronix e.K.                | Johannes Zink                  |
Steuerwalder Str. 21            | https://www.pengutronix.de/    |
31137 Hildesheim, Germany       | Phone: +49-5121-206917-0       |
Amtsgericht Hildesheim, HRA 2686| Fax:   +49-5121-206917-5555    |




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-05-26  9:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-26  8:23 Adding a pwm command Marc Reilly
2023-05-26  8:23 ` [RFC PATCH 1/1] commands: add pwm manipulation command Marc Reilly
2023-05-26  9:33   ` Johannes Zink

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox