Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Jun 2019 01:15:00 +0000 (UTC)
From:      Ian Lepore <ian@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r349166 - head/usr.sbin/pwm
Message-ID:  <201906180115.x5I1F1OP065851@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201906180115.x5I1F1OP065851>