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 v4 1/2] commands: add pwm manipulation command
Date: Tue, 6 Jun 2023 11:07:55 +0200	[thread overview]
Message-ID: <20230606090755.GU18491@pengutronix.de> (raw)
In-Reply-To: <20230602045306.31258-2-marc@cpdesign.com.au>

Hi Marc,

I applied this with some small changes.

On Fri, Jun 02, 2023 at 02:53:05PM +1000, Marc Reilly wrote:
> +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);
> +			break;
> +		case 'i':
> +			invert_polarity = true;
> +			break;
> +		case 's':
> +			stop = true;
> +			break;
> +		case 'v':
> +			verbose = true;
> +			break;

		default:
			return COMMAND_ERROR_USAGE;

> +		}
> +	}
> +
> +	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;
> +	}
> +
> +	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");


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

-- 
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-06-06  9:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-02  4:53 Adding PWM Commands (v4) Marc Reilly
2023-06-02  4:53 ` [PATCH v4 1/2] commands: add pwm manipulation command Marc Reilly
2023-06-06  9:07   ` Sascha Hauer [this message]
2023-06-02  4:53 ` [PATCH v4 2/2] 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=20230606090755.GU18491@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