Adding PWM commands
Thanks again all for comments so far.
Changes in v3:
- fix minor typos
- remove unused 'error' var in pwm_get
Changes in v2:
- remove '-n' option to specify device by number
- fix freeing resources on all return paths
- add pwm_get command
PATCH v3 1/3 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 | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 commands/pwm.c
commands/Kconfig +11 -0
@@ -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
commands/Makefile +1 -0
@@ -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 +143 -0
143 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [PATCH v3 1/3] commands: add pwm manipulation command
Quoted 63 lines · Marc Reilly
This introduces a command to set parameters for a pwm device.
Signed-off-by Marc Reilly <[email protected]>--- commands/Kconfig | 11 ++++ commands/Makefile | 1 + commands/pwm.c | 143 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 commands/pwm.ccommands/Kconfig +11
-0@@ -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 LEDcommands/Makefile +1
-0@@ -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.ocommands/pwm.c new file +13
-0@@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// SPDX-FileCopyrightText: © 2023 Marc Reilly <[email protected]> + +/* 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))
commands/pwm.c line 23
Quoted 10 lines
commands/pwm.c new file +10
-0+ +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.
Quoted 26 lines
commands/pwm.c new file +26
-0+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 ; + }
commands/pwm.c line 59
Quoted 10 lines
commands/pwm.c new file +10
-0+ } + +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");Quoted 5 lines
commands/pwm.c new file +5
-0+if (width >100 ) { +printf (" width (%% duty cycle) can't be more than 100%%\n" ); +return COMMAND_ERROR_USAGE ; + } +
commands/pwm.c line 74
Quoted 10 lines
commands/pwm.c new file +10
-0+ 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.
commands/pwm.c line 78
Quoted 4 lines
commands/pwm.c new file +4
-0+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?
commands/pwm.c line 89
Quoted 11 lines
commands/pwm.c new file +11
-0+ +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.
Quoted 11 lines
commands/pwm.c new file +11
-0+ }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" );
commands/pwm.c line 110
Quoted 10 lines
commands/pwm.c new file +10
-0+ + 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
Signature · 4 lines
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 |
PATCH v3 2/3 commands: pwm: add pwm_get command
In reply to Marc Reilly--- commands/Kconfig | 7 ++++++ commands/pwm.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)
commands/Kconfig +7 -0
@@ -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
commands/pwm.c +56 -0
59 lines not on this page · raw message
Signature · 1 line
2.35.3
Re: [PATCH v3 2/3] commands: pwm: add pwm_get command
Quoted 59 lines · Marc Reilly
Signed-off-by Marc Reilly <[email protected]>--- commands/Kconfig | 7 ++++++ commands/pwm.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+)commands/Kconfig +7
-0@@ -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 LEDcommands/pwm.c +27
-0@@ -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 ;
commands/pwm.c BAREBOX_CMD_START(pwm_set) · line 180
Quoted 10 lines
commands/pwm.c +10
-0+ } + +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
Signature · 4 lines
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 |
PATCH v3 3/3 include: pwm: minor function doc fix for pwm_set_relative_duty_cycle()
In reply to Marc Reilly--- include/pwm.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/pwm.h +1 -1
@@ -71,7 +71,7 @@ unsigned int pwm_get_period(struct pwm_device *pwm);* @scale: scale in which @duty_cycle is expressed * * This functions converts a relative into an absolute duty cycle (expressed -* in nanoseconds), and puts the result in state->duty_cycle. +* in nanoseconds), and puts the result in state->duty_ns. * * For example if you want to configure a 50% duty cycle, call: *
Signature · 1 line
2.35.3