mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Marc Reilly <marc@cpdesign.com.au>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH v3 1/3] commands: add pwm manipulation command
Date: Tue, 30 May 2023 11:27:45 +0200	[thread overview]
Message-ID: <20230530092745.GZ17518@pengutronix.de> (raw)
In-Reply-To: <20230529110627.31257-2-marc@cpdesign.com.au>

On Mon, May 29, 2023 at 09:06:25PM +1000, 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    | 143 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 155 insertions(+)
>  create mode 100644 commands/pwm.c
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index 4d3ff631a8..cc7585bbb2 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: pwm_set [dDPfwis]
> +
> +	  Controls the pwm values such as period and duty cycle
> +
>  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..94bbbaf1be
> --- /dev/null
> +++ b/commands/pwm.c
> @@ -0,0 +1,143 @@
> +// 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 *devname = NULL;
> +	int duty = -1, period = -1;
> +	int freq = -1, width = -1;
> +	int invert_polarity = -1, stop = -1;

better bool invert_polarity = false, stop = false.

> +	int opt;
> +
> +	while ((opt = getopt(argc, argv, "d:D:P:f:w:is")) > 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':
> +			freq = simple_strtol(optarg, NULL, 0);
> +			break;
> +		case 'w':
> +			width = simple_strtol(optarg, NULL, 0);
> +			break;
> +		case 'i':
> +			invert_polarity = 1;
> +			break;
> +		case 's':
> +			stop = 1;
> +			break;
> +		}
> +	}
> +
> +	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;
> +	}

You should also check if both freq and period are given:

	if (freq >= 0 && period >= 0)
		printf("Specify period or frequency, not both\n");

likewise width and duty:

	if (width >= 0 && duty >= 0)
		printf("Specify width or duty cycle, not both\n");

> +	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;
> +	}
> +
> +	pwm_get_state(pwm, &state);
> +
> +	if ((state.period_ns == 0)
> +			&& (freq < 0) && (duty < 0) && (period < 0)) {

It's already an error when:

	state.period_ns == 0 && freq < 0 && period < 0

No need to check for duty here.

> +		printf(" need to know some timing info; freq or duty/period\n");
> +		pwm_free(pwm);
> +		return COMMAND_ERROR_USAGE;
> +	}

Maybe just print the current PWM state when no actions are given?

> +
> +	if (invert_polarity >= 0)
> +		state.polarity = invert_polarity;
> +
> +	/* period */
> +	if (freq > 0) {
> +		state.p_enable = true;
> +		state.period_ns = HZ_TO_NANOSECONDS(freq);
> +		if (width < 0) {
> +			width = 50;
> +		}

I think this shouldn't be here, at least I don't see why it shouldn't be
possible to configure a PWM with -f x -D y.

> +	} else if (period > 0) {
> +		state.p_enable = true;
> +		state.period_ns = period;
> +	}
> +
> +	/* duty */
> +	if (width >= 0) {
> +		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;
> +	}
> +
> +	error = pwm_apply_state(pwm, &state);
> +
> +	if (stop > 0) {
> +		state.p_enable = false;
> +		error = pwm_apply_state(pwm, &state);
> +	}

No need to call pwm_apply_state() twice when the PWM shall be stopped.
This should do it:

	if (stop > 0)
		state.p_enable = false;

	error = pwm_apply_state(pwm, &state);

Sascha

-- 
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 |



  reply	other threads:[~2023-05-30  9:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-29 11:06 Adding PWM commands Marc Reilly
2023-05-29 11:06 ` [PATCH v3 1/3] commands: add pwm manipulation command Marc Reilly
2023-05-30  9:27   ` Sascha Hauer [this message]
2023-05-29 11:06 ` [PATCH v3 2/3] commands: pwm: add pwm_get command Marc Reilly
2023-05-30  9:32   ` Sascha Hauer
2023-05-29 11:06 ` [PATCH v3 3/3] include: pwm: minor function doc fix for pwm_set_relative_duty_cycle() Marc Reilly

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230530092745.GZ17518@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=marc@cpdesign.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox