Add pulse count support to apps/examples/pwm

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@4286 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2012-01-09 18:56:05 +00:00
parent 94e1048f0b
commit f28db5c1c3
4 changed files with 79 additions and 19 deletions

View File

@ -155,3 +155,5 @@
* apps/examples/buttons/main.c: The test needs to up_buttoninit() to
properly configure the button interrupt GPIOs.
* apps/examples/pwm: Add support to test the pulse count option recently
added to the PWM interface.

View File

@ -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
^^^^^^^^^^^^^

View File

@ -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

View File

@ -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 */
sleep(g_pwmstate.duration);
/* Then stop the pulse train */
message("pwm_main: stopping output\n",
info.frequency, info.duty);
ret = ioctl(fd, PWMIOC_STOP, 0);
if (ret < 0)
/* 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
{
message("pwm_main: ioctl(PWMIOC_STOP) failed: %d\n", errno);
goto errout_with_dev;
/* Wait for the specified duration */
sleep(g_pwmstate.duration);
/* Then stop the pulse train */
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;
}
}
close(fd);