From ed93fd7399d11571a4cf214fadcd671a8e2f5d50 Mon Sep 17 00:00:00 2001 From: patacongo Date: Mon, 9 Jan 2012 18:56:05 +0000 Subject: Add pulse count support to apps/examples/pwm git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4286 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/README.txt | 8 ++++- apps/examples/pwm/pwm.h | 10 +++++- apps/examples/pwm/pwm_main.c | 72 +++++++++++++++++++++++++++++++++++--------- 3 files changed, 74 insertions(+), 16 deletions(-) (limited to 'apps/examples') diff --git a/apps/examples/README.txt b/apps/examples/README.txt index eae8becaf..887266e2a 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -723,6 +723,8 @@ examples/pwm specific PWM settings might require additional settings). CONFIG_PWM - Enables PWM support. + CONFIG_EXAMPLES_PWM_COUNT - Enabled PWM pulse count support (if the + hardware supports it). CONFIG_NSH_BUILTIN_APPS - Build the PWM test as an NSH built-in function. Default: Not built! The example can only be used as an NSH built-in application @@ -733,7 +735,11 @@ examples/pwm CONFIG_EXAMPLES_PWM_FREQUENCY - The initial PWM frequency. Default: 100 Hz CONFIG_EXAMPLES_PWM_DUTYPCT - The initial PWM duty as a percentage. Default: 50% CONFIG_EXAMPLES_PWM_DURATION - The initial PWM pulse train duration in sectonds. - as a percentage. Default: 5 seconds + as a percentage. Used only if the current pulse count is zero (pulse count + is only supported if CONFIG_PWM_PULSECOUNT is defined). Default: 5 seconds + CONFIG_EXAMPLES_PWM_COUNT - The initial PWM pulse count. This option is + only available if CONFIG_PWM_PULSECOUNT is defined. Default: 0 (i.e., use + the duration, not the count). examples/rgmp ^^^^^^^^^^^^^ diff --git a/apps/examples/pwm/pwm.h b/apps/examples/pwm/pwm.h index 83d87517c..e15b933fc 100644 --- a/apps/examples/pwm/pwm.h +++ b/apps/examples/pwm/pwm.h @@ -53,7 +53,11 @@ * CONFIG_EXAMPLES_PWM_FREQUENCY - The initial PWM frequency. Default: 100 Hz * CONFIG_EXAMPLES_PWM_DUTYPCT - The initial PWM duty as a percentage. Default: 50% * CONFIG_EXAMPLES_PWM_DURATION - The initial PWM pulse train duration in sectonds. - * as a percentage. Default: 5 seconds + * as a percentage. Used only if the current pulse count is zero (pulse count + * is only supported if CONFIG_PWM_PULSECOUNT is defined). Default: 5 seconds + * CONFIG_EXAMPLES_PWM_COUNT - The initial PWM pulse count. This option is + * only available if CONFIG_PWM_PULSECOUNT is defined. Default: 0 (i.e., use + * the duration, not the count). */ #ifndef CONFIG_PWM @@ -80,6 +84,10 @@ # define CONFIG_EXAMPLES_PWM_DURATION 5 #endif +#ifndef CONFIG_EXAMPLES_PWM_COUNT +# define CONFIG_EXAMPLES_PWM_COUNT 0 +#endif + /* Debug ********************************************************************/ #ifdef CONFIG_CPP_HAVE_VARARGS diff --git a/apps/examples/pwm/pwm_main.c b/apps/examples/pwm/pwm_main.c index 71211ab95..501667ae7 100644 --- a/apps/examples/pwm/pwm_main.c +++ b/apps/examples/pwm/pwm_main.c @@ -66,6 +66,9 @@ struct pwm_state_s bool initialized; uint8_t duty; uint32_t freq; +#ifdef CONFIG_PWM_PULSECOUNT + uint32_t count; +#endif int duration; }; @@ -103,6 +106,11 @@ static void pwm_help(FAR struct pwm_state_s *pwm) message(" [-d duty] selcts the pulse duty as a percentage. " "Default: %d %% Current: %d %%\n", CONFIG_EXAMPLES_PWM_DUTYPCT, pwm->duty); +#ifdef CONFIG_PWM_PULSECOUNT + message(" [-n count] selects the pulse count. " + "Default: %d Hz Current: %d\n", + CONFIG_EXAMPLES_PWM_COUNT, pwm->count); +#endif message(" [-t duration] is the duration of the pulse train in seconds. " "Default: %d Current: %d\n", CONFIG_EXAMPLES_PWM_DURATION, pwm->duration); @@ -189,6 +197,20 @@ void parse_args(FAR struct pwm_state_s *pwm, int argc, FAR char **argv) index += nargs; break; +#ifdef CONFIG_PWM_PULSECOUNT + case 'n': + nargs = arg_decimal(&argv[index], &value); + if (value < 0) + { + message("Count must be non-negative: %ld\n", value); + exit(1); + } + + pwm->count = (uint32_t)value; + index += nargs; + break; + +#endif case 't': nargs = arg_decimal(&argv[index], &value); if (value < 1 || value > INT_MAX) @@ -231,9 +253,12 @@ int pwm_main(int argc, char *argv[]) if (!g_pwmstate.initialized) { - g_pwmstate.duty = CONFIG_EXAMPLES_PWM_DUTYPCT; - g_pwmstate.freq = CONFIG_EXAMPLES_PWM_FREQUENCY; - g_pwmstate.duration = CONFIG_EXAMPLES_PWM_DURATION; + g_pwmstate.duty = CONFIG_EXAMPLES_PWM_DUTYPCT; + g_pwmstate.freq = CONFIG_EXAMPLES_PWM_FREQUENCY; + g_pwmstate.duration = CONFIG_EXAMPLES_PWM_DURATION; +#ifdef CONFIG_PWM_PULSECOUNT + g_pwmstate.count = CONFIG_EXAMPLES_PWM_COUNT; +#endif g_pwmstate.initialized = true; } @@ -266,10 +291,18 @@ int pwm_main(int argc, char *argv[]) info.frequency = g_pwmstate.freq; info.duty = ((uint32_t)g_pwmstate.duty << 16) / 100; +#ifdef CONFIG_PWM_PULSECOUNT + info.count = g_pwmstate.count; + + message("pwm_main: starting output with frequency: %d duty: %08x count: %d\n", + info.frequency, info.duty, info.count); +#else message("pwm_main: starting output with frequency: %d duty: %08x\n", info.frequency, info.duty); +#endif + ret = ioctl(fd, PWMIOC_SETCHARACTERISTICS, (unsigned long)((uintptr_t)&info)); if (ret < 0) { @@ -277,7 +310,9 @@ int pwm_main(int argc, char *argv[]) goto errout_with_dev; } - /* Then start the pulse train */ + /* Then start the pulse train. Since the driver was opened in blocking + * mode, this call will block if the count value is greater than zero. + */ ret = ioctl(fd, PWMIOC_START, 0); if (ret < 0) @@ -286,20 +321,29 @@ int pwm_main(int argc, char *argv[]) goto errout_with_dev; } - /* Wait for the specified duration */ + /* It a non-zero count was not specified, then wait for the selected + * duration, then stop the PWM output. + */ + +#ifdef CONFIG_PWM_PULSECOUNT + if (info.count == 0) +#endif + { + /* Wait for the specified duration */ - sleep(g_pwmstate.duration); + sleep(g_pwmstate.duration); - /* Then stop the pulse train */ + /* Then stop the pulse train */ - message("pwm_main: stopping output\n", - info.frequency, info.duty); + message("pwm_main: stopping output\n", + info.frequency, info.duty); - ret = ioctl(fd, PWMIOC_STOP, 0); - if (ret < 0) - { - message("pwm_main: ioctl(PWMIOC_STOP) failed: %d\n", errno); - goto errout_with_dev; + ret = ioctl(fd, PWMIOC_STOP, 0); + if (ret < 0) + { + message("pwm_main: ioctl(PWMIOC_STOP) failed: %d\n", errno); + goto errout_with_dev; + } } close(fd); -- cgit v1.2.3