From 5088b48b106e88b7039b9856f315b834dde1df29 Mon Sep 17 00:00:00 2001 From: manu Date: Fri, 14 Dec 2018 18:38:10 +0000 Subject: [PATCH] pwm(8): Add percentage value support for duty cycle --- usr.sbin/pwm/pwm.8 | 8 ++++++-- usr.sbin/pwm/pwm.c | 13 ++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/usr.sbin/pwm/pwm.8 b/usr.sbin/pwm/pwm.8 index 08bb1f1c7425..92ef83705ed0 100644 --- a/usr.sbin/pwm/pwm.8 +++ b/usr.sbin/pwm/pwm.8 @@ -67,7 +67,7 @@ Show the configuration of the pwm channel .It Fl p Ar period Configure the period (in nanoseconds) of the pwm channel .It Fl d Ar duty -Configure the duty (in nanoseconds) of the pwm channel +Configure the duty (in nanoseconds or percentage) of the pwm channel .El .Sh EXAMPLES .Bl -bullet @@ -76,9 +76,13 @@ Show the configuration of the pwm channel: .Pp pwm -f /dev/pwmc0 -C .It -Configure a 50000 ns period and a 25000 duty cycles: +Configure a 50000 ns period and a 25000 duty cycle: .Pp pwm -f /dev/pwmc0 -p 50000 -d 25000 +.It +Configure a 50% duty cycle: +.Pp +pwm -f /dev/pwmc0 -d 50% .El .Sh SEE ALSO .Xr pwm 9 , diff --git a/usr.sbin/pwm/pwm.c b/usr.sbin/pwm/pwm.c index ebb4349635fd..62e15bf18455 100644 --- a/usr.sbin/pwm/pwm.c +++ b/usr.sbin/pwm/pwm.c @@ -71,6 +71,7 @@ main(int argc, char *argv[]) int action, ch; cap_rights_t right_ioctl; const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE, PWMMAXCHANNEL}; + char *percent; action = 0; fd = -1; @@ -104,7 +105,9 @@ main(int argc, char *argv[]) if (action & ~(PWM_PERIOD | PWM_DUTY)) usage(); action = PWM_DUTY; - duty = strtol(optarg, NULL, 10); + duty = strtol(optarg, &percent, 10); + if (*percent != '\0' && *percent != '%') + usage(); break; case 'c': if (channel != -1) @@ -199,8 +202,12 @@ main(int argc, char *argv[]) case PWM_DUTY: if (period != -1) state.period = period; - if (duty != -1) - state.duty = duty; + if (duty != -1) { + 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");