[PATCH v3 2/3] commands: pwm: add pwm_get command

Sascha Hauer sha at pengutronix.de
Tue May 30 02:32:32 PDT 2023


On Mon, May 29, 2023 at 09:06:26PM +1000, Marc Reilly wrote:
> Signed-off-by: Marc Reilly <marc at cpdesign.com.au>
> ---
>  commands/Kconfig |  7 ++++++
>  commands/pwm.c   | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 63 insertions(+)
> 
> diff --git a/commands/Kconfig b/commands/Kconfig
> index cc7585bbb2..f2a6d22f69 100644
> --- a/commands/Kconfig
> +++ b/commands/Kconfig
> @@ -1940,6 +1940,13 @@ config CMD_PWM
>  
>  	  Controls the pwm values such as period and duty cycle
>  
> +
> +	  pwm_get - get pwm state
> +
> +	  Usage: pwm_get [d]
> +
> +	  Displays the pwm values such as period and duty cycle
> +
>  config CMD_LED
>  	bool
>  	depends on LED
> diff --git a/commands/pwm.c b/commands/pwm.c
> index 94bbbaf1be..18f65b0a59 100644
> --- a/commands/pwm.c
> +++ b/commands/pwm.c
> @@ -141,3 +141,59 @@ BAREBOX_CMD_START(pwm_set)
>  	BAREBOX_CMD_GROUP(CMD_GRP_HWMANIP)
>  	BAREBOX_CMD_HELP(cmd_pwm_set_help)
>  BAREBOX_CMD_END
> +
> +#define HZ_FROM_NANOSECONDS(x) (1000000000UL/(x))
> +
> +static int do_pwm_get(int argc, char *argv[])
> +{
> +	struct pwm_device *pwm = NULL;
> +	struct pwm_state state;
> +	char *devname = NULL;
> +	int opt;
> +
> +	while ((opt = getopt(argc, argv, "d:")) > 0) {
> +		switch (opt) {
> +		case 'd':
> +			devname = optarg;
> +			break;
> +		}
> +	}
> +
> +	if (!devname) {
> +		printf(" need to specify a device\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);
> +
> +	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));

Not sure if this deserves an extra command. You could print the
information along with the pwm_set command when no action is given
or when an additional -v (for verbose) is given.

When doint this the command could simply be named 'pwm' instead of
'pwm_set'

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 |



More information about the barebox mailing list