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 via email to