From owner-svn-src-all@freebsd.org Tue Jun 18 01:15:02 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id C8EF715C9EBF; Tue, 18 Jun 2019 01:15:01 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6C47F8B048; Tue, 18 Jun 2019 01:15:01 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 47F245F68; Tue, 18 Jun 2019 01:15:01 +0000 (UTC) (envelope-from ian@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x5I1F1Q2065852; Tue, 18 Jun 2019 01:15:01 GMT (envelope-from ian@FreeBSD.org) Received: (from ian@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x5I1F1OP065851; Tue, 18 Jun 2019 01:15:01 GMT (envelope-from ian@FreeBSD.org) Message-Id: <201906180115.x5I1F1OP065851@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ian set sender to ian@FreeBSD.org using -f From: Ian Lepore Date: Tue, 18 Jun 2019 01:15:00 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r349166 - head/usr.sbin/pwm X-SVN-Group: head X-SVN-Commit-Author: ian X-SVN-Commit-Paths: head/usr.sbin/pwm X-SVN-Commit-Revision: 349166 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 6C47F8B048 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-1.00)[-0.999,0]; NEURAL_HAM_SHORT(-0.97)[-0.966,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Jun 2019 01:15:02 -0000 Author: ian Date: Tue Jun 18 01:15:00 2019 New Revision: 349166 URL: https://svnweb.freebsd.org/changeset/base/349166 Log: Rearrange the argument checking and processing so that enable and disable can be combined with configuring the period and duty cycle (the same ioctl sets all 3 values at once, so there's no reason to require the user to run the program twice to get all 3 things set). Modified: head/usr.sbin/pwm/pwm.8 head/usr.sbin/pwm/pwm.c Modified: head/usr.sbin/pwm/pwm.8 ============================================================================== --- head/usr.sbin/pwm/pwm.8 Tue Jun 18 00:17:10 2019 (r349165) +++ head/usr.sbin/pwm/pwm.8 Tue Jun 18 01:15:00 2019 (r349166) @@ -31,19 +31,12 @@ .Sh SYNOPSIS .Nm .Op Fl f Ar device -.Fl E -.Nm -.Op Fl f Ar device -.Fl D -.Nm -.Op Fl f Ar device .Fl C .Nm .Op Fl f Ar device -.Fl p Ar period -.Nm -.Op Fl f Ar device -.Fl d Ar duty +.Op Fl D | Fl E +.Op Fl p Ar period +.Op Fl d Ar duty .Sh DESCRIPTION The .Nm @@ -76,19 +69,19 @@ is used. If an unqualified name is provided, .Pa /dev/pwm is automatically prepended. -.It Fl E -Enable the PWM channel. -.It Fl D -Disable the PWM channel. .It Fl C Show the configuration of the PWM channel. -.It Fl p Ar period -Configure the period (in nanoseconds) of the PWM channel +.It Fl D +Disable the PWM channel. .It Fl d Ar duty Configure the duty (in nanoseconds or percentage) of the PWM channel. Duty is the portion of the .Ar period during which the signal is asserted. +.It Fl E +Enable the PWM channel. +.It Fl p Ar period +Configure the period (in nanoseconds) of the PWM channel. .El .Sh EXAMPLES .Bl -bullet @@ -98,9 +91,10 @@ Show the configuration of the PWM channel: pwm -f /dev/pwm/pwmc0.1 -C .Ed .It -Configure a 50000 ns period and a 25000 ns duty cycle: +Configure a 50000 ns period and a 25000 ns duty cycle +and enable the channel: .Bd -literal -pwm -f pwmc1.1 -p 50000 -d 25000 +pwm -f pwmc1.1 -E -p 50000 -d 25000 .Ed .It Configure a 50% duty cycle on the device and channel which Modified: head/usr.sbin/pwm/pwm.c ============================================================================== --- head/usr.sbin/pwm/pwm.c Tue Jun 18 00:17:10 2019 (r349165) +++ head/usr.sbin/pwm/pwm.c Tue Jun 18 01:15:00 2019 (r349166) @@ -66,11 +66,8 @@ static void usage(void) { fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\tpwm [-f dev] -E\n"); - fprintf(stderr, "\tpwm [-f dev] -D\n"); fprintf(stderr, "\tpwm [-f dev] -C\n"); - fprintf(stderr, "\tpwm [-f dev] -p period\n"); - fprintf(stderr, "\tpwm [-f dev] -d duty\n"); + fprintf(stderr, "\tpwm [-f dev] [-D | -E] [-p period] [-d duty[%%]]\n"); exit(1); } @@ -94,14 +91,14 @@ main(int argc, char *argv[]) while ((ch = getopt(argc, argv, "f:EDCp:d:")) != -1) { switch (ch) { case 'E': - if (action) + if (action & (PWM_DISABLE | PWM_SHOW_CONFIG)) usage(); - action = PWM_ENABLE; + action |= PWM_ENABLE; break; case 'D': - if (action) + if (action & (PWM_ENABLE | PWM_SHOW_CONFIG)) usage(); - action = PWM_DISABLE; + action |= PWM_DISABLE; break; case 'C': if (action) @@ -109,17 +106,23 @@ main(int argc, char *argv[]) action = PWM_SHOW_CONFIG; break; case 'p': - if (action & ~(PWM_PERIOD | PWM_DUTY)) + if (action & PWM_SHOW_CONFIG) usage(); - action = PWM_PERIOD; + action |= PWM_PERIOD; period = strtol(optarg, NULL, 10); break; case 'd': - if (action & ~(PWM_PERIOD | PWM_DUTY)) + if (action & PWM_SHOW_CONFIG) usage(); - action = PWM_DUTY; + action |= PWM_DUTY; duty = strtol(optarg, &percent, 10); - if (*percent != '\0' && *percent != '%') + if (*percent == '%') { + if (duty < 0 || duty > 100) { + fprintf(stderr, + "Invalid duty percentage\n"); + usage(); + } + } else if (*percent != '\0') usage(); break; case 'f': @@ -169,49 +172,31 @@ main(int argc, char *argv[]) goto fail; } - switch (action) { - case PWM_ENABLE: - if (state.enable == false) { - state.enable = true; - if (ioctl(fd, PWMSETSTATE, &state) == -1) { - fprintf(stderr, - "Cannot enable the pwm controller\n"); - goto fail; - } - } - break; - case PWM_DISABLE: - if (state.enable == true) { - state.enable = false; - if (ioctl(fd, PWMSETSTATE, &state) == -1) { - fprintf(stderr, - "Cannot disable the pwm controller\n"); - goto fail; - } - } - break; - case PWM_SHOW_CONFIG: + if (action == PWM_SHOW_CONFIG) { printf("period: %u\nduty: %u\nenabled:%d\n", state.period, state.duty, state.enable); - break; - case PWM_PERIOD: - case PWM_DUTY: - if (period != -1) + goto fail; + } else { + if (action & PWM_ENABLE) + state.enable = true; + if (action & PWM_DISABLE) + state.enable = false; + if (action & PWM_PERIOD) state.period = period; - if (duty != -1) { + if (action & PWM_DUTY) { if (*percent != '\0') state.duty = state.period * duty / 100; else state.duty = duty; } + if (ioctl(fd, PWMSETSTATE, &state) == -1) { fprintf(stderr, "Cannot configure the pwm controller\n"); goto fail; } - break; } close(fd);