From 6e71466aee0b07c5ff85b40e9fb76aa92187df92 Mon Sep 17 00:00:00 2001 From: px4dev Date: Sun, 17 Mar 2013 12:29:18 -0700 Subject: Adjust the syntax of the 'pwm' command to make it easier to use. --- apps/systemcmds/pwm/Makefile | 2 +- apps/systemcmds/pwm/pwm.c | 58 ++++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 17 deletions(-) (limited to 'apps') diff --git a/apps/systemcmds/pwm/Makefile b/apps/systemcmds/pwm/Makefile index 60885aa0a..5ab105ed1 100644 --- a/apps/systemcmds/pwm/Makefile +++ b/apps/systemcmds/pwm/Makefile @@ -37,6 +37,6 @@ APPNAME = pwm PRIORITY = SCHED_PRIORITY_DEFAULT -STACKSIZE = 2048 +STACKSIZE = 4096 include $(APPDIR)/mk/app.mk diff --git a/apps/systemcmds/pwm/pwm.c b/apps/systemcmds/pwm/pwm.c index bf4a1b579..de7a53199 100644 --- a/apps/systemcmds/pwm/pwm.c +++ b/apps/systemcmds/pwm/pwm.c @@ -71,13 +71,17 @@ usage(const char *reason) warnx("%s", reason); errx(1, "usage:\n" - "pwm [-v] [-d ] [-u ] [-c ] [arm|disarm] [ ...]\n" + "pwm [-v] [-d ] [-u ] [-c ] [arm|disarm] [ ...]\n" " -v Print information about the PWM device\n" " PWM output device (defaults to " PWM_OUTPUT_DEVICE_PATH ")\n" " PWM update rate for channels in \n" - " Bitmask of channels to update at the alternate rate\n" + " Channel group that should update at the alternate rate (may be specified more than once)\n" " arm | disarm Arm or disarm the ouptut\n" - " ... PWM output values in microseconds to assign to the PWM outputs\n"); + " ... PWM output values in microseconds to assign to the PWM outputs\n" + "\n" + "When -c is specified, any channel groups not listed with -c will update at the default rate.\n" + ); + } int @@ -85,19 +89,24 @@ pwm_main(int argc, char *argv[]) { const char *dev = PWM_OUTPUT_DEVICE_PATH; unsigned alt_rate = 0; - uint32_t alt_channels; + uint32_t alt_channel_groups = 0; bool alt_channels_set = false; bool print_info = false; int ch; int ret; char *ep; + unsigned group; + + if (argc < 2) + usage(NULL); while ((ch = getopt(argc, argv, "c:d:u:v")) != EOF) { switch (ch) { case 'c': - alt_channels = strtol(optarg, &ep, 0); - if (*ep != '\0') - usage("bad alt_channel_mask value"); + group = strtoul(optarg, &ep, 0); + if ((*ep != '\0') || (group >= 32)) + usage("bad channel_group value"); + alt_channel_groups |= (1 << group); alt_channels_set = true; break; @@ -109,6 +118,7 @@ pwm_main(int argc, char *argv[]) alt_rate = strtol(optarg, &ep, 0); if (*ep != '\0') usage("bad alt_rate value"); + break; case 'v': print_info = true; @@ -133,11 +143,25 @@ pwm_main(int argc, char *argv[]) err(1, "PWM_SERVO_SET_UPDATE_RATE (check rate for sanity)"); } - /* assign alternate rate to channels */ + /* assign alternate rate to channel groups */ if (alt_channels_set) { - ret = ioctl(fd, PWM_SERVO_SELECT_UPDATE_RATE, alt_channels); + uint32_t mask = 0; + + for (unsigned group = 0; group < 32; group++) { + if ((1 << group) & alt_channel_groups) { + uint32_t group_mask; + + ret = ioctl(fd, PWM_SERVO_GET_RATEGROUP(group), (unsigned long)&group_mask); + if (ret != OK) + err(1, "PWM_SERVO_GET_RATEGROUP(%u)", group); + + mask |= group_mask; + } + } + + ret = ioctl(fd, PWM_SERVO_SELECT_UPDATE_RATE, mask); if (ret != OK) - err(1, "PWM_SERVO_SELECT_UPDATE_RATE (check mask vs. device capabilities)"); + err(1, "PWM_SERVO_SELECT_UPDATE_RATE"); } /* iterate remaining arguments */ @@ -177,30 +201,32 @@ pwm_main(int argc, char *argv[]) err(1, "PWM_SERVO_GET_COUNT"); /* print current servo values */ - printf("PWM output values:\n"); for (unsigned i = 0; i < count; i++) { servo_position_t spos; ret = ioctl(fd, PWM_SERVO_GET(i), (unsigned long)&spos); if (ret == OK) { - printf("%u: %uus\n", i, spos); + printf("channel %u: %uus\n", i, spos); } else { printf("%u: ERROR\n", i); } } /* print rate groups */ - printf("Available alt_channel_mask groups:\n"); for (unsigned i = 0; i < count; i++) { uint32_t group_mask; ret = ioctl(fd, PWM_SERVO_GET_RATEGROUP(i), (unsigned long)&group_mask); if (ret != OK) break; - if (group_mask != 0) - printf(" 0x%x", group_mask); + if (group_mask != 0) { + printf("channel group %u: channels", i); + for (unsigned j = 0; j < 32; j++) + if (group_mask & (1 << j)) + printf(" %u", j); + printf("\n"); + } } - printf("\n"); fflush(stdout); } exit(0); -- cgit v1.2.3