Add support for testing multiple ADC, PWM, and QE devices

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4993 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2012-07-30 16:51:43 +00:00
parent 52fe89eb98
commit 6653351013
7 changed files with 301 additions and 67 deletions

View File

@ -263,3 +263,5 @@
return value from spawned applications (provided by Mike Smith)
* apps/nslib: Lock the schedule while starting built-in applications
in order to eliminate race conditions (also from Mike Smith).
* apps/examples/adc, pwm, and qencoder: Add support for testing
devices with multiple ADC, PWM, and QE devices.

View File

@ -45,7 +45,7 @@ examples/adc
Specific configuration options for this example include:
CONFIG_EXAMPLES_ADC_DEVPATH - The path to the ADC device. Default: /dev/adc0
CONFIG_EXAMPLES_ADC_DEVPATH - The default path to the ADC device. Default: /dev/adc0
CONFIG_EXAMPLES_ADC_NSAMPLES - If CONFIG_NSH_BUILTIN_APPS
is defined, then the number of samples is provided on the command line
and this value is ignored. Otherwise, this number of samples is
@ -1059,7 +1059,7 @@ examples/pwm
Specific configuration options for this example include:
CONFIG_EXAMPLES_PWM_DEVPATH - The path to the PWM device. Default: /dev/pwm0
CONFIG_EXAMPLES_PWM_DEVPATH - The path to the default PWM device. Default: /dev/pwm0
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 seconds.

View File

@ -48,7 +48,7 @@
/* Configuration ************************************************************/
/* CONFIG_NSH_BUILTIN_APPS - Build the ADC test as an NSH built-in function.
* Default: Built as a standalone problem
* CONFIG_EXAMPLES_ADC_DEVPATH - The path to the ADC device. Default: /dev/adc0
* CONFIG_EXAMPLES_ADC_DEVPATH - The default path to the ADC device. Default: /dev/adc0
* CONFIG_EXAMPLES_ADC_NSAMPLES - If CONFIG_NSH_BUILTIN_APPS
* is defined, then the number of samples is provided on the command line
* and this value is ignored. Otherwise, this number of samples is
@ -94,6 +94,15 @@
* Public Types
****************************************************************************/
struct adc_state_s
{
bool initialized;
FAR char *devpath;
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
int count;
#endif
};
/****************************************************************************
* Public Variables
****************************************************************************/

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/adc/adc_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -44,6 +44,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
@ -76,6 +77,8 @@
* Private Data
****************************************************************************/
static struct adc_state_s g_adcstate;
/****************************************************************************
* Public Data
****************************************************************************/
@ -84,6 +87,137 @@
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: adc_devpath
****************************************************************************/
static void adc_devpath(FAR struct adc_state_s *adc, FAR const char *devpath)
{
/* Get rid of any old device path */
if (adc->devpath)
{
free(adc->devpath);
}
/* Then set-up the new device path by copying the string */
adc->devpath = strdup(devpath);
}
/****************************************************************************
* Name: adc_help
****************************************************************************/
#ifdef CONFIG_NSH_BUILTIN_APPS
static void adc_help(FAR struct adc_state_s *adc)
{
message("Usage: adc [OPTIONS]\n");
message("\nArguments are \"sticky\". For example, once the ADC device is\n");
message("specified, that device will be re-used until it is changed.\n");
message("\n\"sticky\" OPTIONS include:\n");
message(" [-p devpath] selects the ADC device. "
"Default: %s Current: %s\n",
CONFIG_EXAMPLES_ADC_DEVPATH, g_adcstate.devpath ? g_adcstate.devpath : "NONE");
message(" [-n count] selects the samples to collect. "
"Default: 1 Current: %d\n", adc->count);
message(" [-h] shows this message and exits\n");
}
#endif
/****************************************************************************
* Name: arg_string
****************************************************************************/
#ifdef CONFIG_NSH_BUILTIN_APPS
static int arg_string(FAR char **arg, FAR char **value)
{
FAR char *ptr = *arg;
if (ptr[2] == '\0')
{
*value = arg[1];
return 2;
}
else
{
*value = &ptr[2];
return 1;
}
}
#endif
/****************************************************************************
* Name: arg_decimal
****************************************************************************/
#ifdef CONFIG_NSH_BUILTIN_APPS
static int arg_decimal(FAR char **arg, FAR long *value)
{
FAR char *string;
int ret;
ret = arg_string(arg, &string);
*value = strtol(string, NULL, 10);
return ret;
}
#endif
/****************************************************************************
* Name: parse_args
****************************************************************************/
#ifdef CONFIG_NSH_BUILTIN_APPS
static void parse_args(FAR struct adc_state_s *adc, int argc, FAR char **argv)
{
FAR char *ptr;
FAR char *str;
long value;
int index;
int nargs;
for (index = 1; index < argc; )
{
ptr = argv[index];
if (ptr[0] != '-')
{
message("Invalid options format: %s\n", ptr);
exit(0);
}
switch (ptr[1])
{
case 'n':
nargs = arg_decimal(&argv[index], &value);
if (value < 0)
{
message("Count must be non-negative: %ld\n", value);
exit(1);
}
adc->count = (uint32_t)value;
index += nargs;
break;
case 'p':
nargs = arg_string(&argv[index], &str);
adc_devpath(adc, str);
index += nargs;
break;
case 'h':
adc_help(adc);
exit(0);
default:
message("Unsupported option: %s\n", ptr);
adc_help(adc);
exit(1);
}
}
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@ -97,50 +231,63 @@ int MAIN_NAME(int argc, char *argv[])
struct adc_msg_s sample[CONFIG_EXAMPLES_ADC_GROUPSIZE];
size_t readsize;
ssize_t nbytes;
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
long nloops;
#endif
int fd;
int errval = 0;
int ret;
int i;
/* Check if we have initialized */
if (!g_adcstate.initialized)
{
/* Initialization of the ADC hardware is performed by logic external to
* this test.
*/
message(MAIN_STRING "Initializing external ADC device\n");
ret = adc_devinit();
if (ret != OK)
{
message(MAIN_STRING "adc_devinit failed: %d\n", ret);
errval = 1;
goto errout;
}
/* Set the default values */
adc_devpath(&g_adcstate, CONFIG_EXAMPLES_ADC_DEVPATH);
#ifdef CONFIG_EXAMPLES_ADC_NSAMPLES
g_adcstate.count = CONFIG_EXAMPLES_ADC_NSAMPLES;
#else
g_adcstate.count = 1;
#endif
g_adcstate.initialized = true;
}
/* Parse the command line */
#ifdef CONFIG_NSH_BUILTIN_APPS
parse_args(&g_adcstate, argc, argv);
#endif
/* If this example is configured as an NX add-on, then limit the number of
* samples that we collect before returning. Otherwise, we never return
*/
#if defined(CONFIG_NSH_BUILTIN_APPS)
nloops = 1;
if (argc > 1)
{
nloops = strtol(argv[1], NULL, 10);
}
message(MAIN_STRING "nloops: %d\n", nloops);
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
message(MAIN_STRING "nloops: %d\n", CONFIG_EXAMPLES_ADC_NSAMPLES);
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
message(MAIN_STRING "g_adcstate.count: %d\n", g_adcstate.count);
#endif
/* Initialization of the ADC hardware is performed by logic external to
* this test.
*/
message(MAIN_STRING "Initializing external ADC device\n");
ret = adc_devinit();
if (ret != OK)
{
message(MAIN_STRING "adc_devinit failed: %d\n", ret);
errval = 1;
goto errout;
}
/* Open the ADC device for reading */
message(MAIN_STRING "Hardware initialized. Opening the ADC device\n");
fd = open(CONFIG_EXAMPLES_ADC_DEVPATH, O_RDONLY);
message(MAIN_STRING "Hardware initialized. Opening the ADC device: %s\n",
g_adcstate.devpath);
fd = open(g_adcstate.devpath, O_RDONLY);
if (fd < 0)
{
message(MAIN_STRING "open %s failed: %d\n",
CONFIG_EXAMPLES_ADC_DEVPATH, errno);
message(MAIN_STRING "open %s failed: %d\n", g_adcstate.devpath, errno);
errval = 2;
goto errout_with_dev;
}
@ -150,9 +297,9 @@ int MAIN_NAME(int argc, char *argv[])
*/
#if defined(CONFIG_NSH_BUILTIN_APPS)
for (; nloops > 0; nloops--)
for (; g_adcstate.count > 0; g_adcstate.count--)
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
for (nloops = 0; nloops < CONFIG_EXAMPLES_ADC_NSAMPLES; nloops++)
for (g_adcstate.count = 0; g_adcstate.count < CONFIG_EXAMPLES_ADC_NSAMPLES; g_adcstate.count++)
#else
for (;;)
#endif
@ -176,7 +323,7 @@ int MAIN_NAME(int argc, char *argv[])
if (errval != EINTR)
{
message(MAIN_STRING "read %s failed: %d\n",
CONFIG_EXAMPLES_ADC_DEVPATH, errval);
g_adcstate.devpath, errval);
errval = 3;
goto errout_with_dev;
}

View File

@ -1,7 +1,7 @@
/****************************************************************************
* examples/pwm/pwm_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -63,13 +63,14 @@
struct pwm_state_s
{
bool initialized;
uint8_t duty;
uint32_t freq;
bool initialized;
FAR char *devpath;
uint8_t duty;
uint32_t freq;
#ifdef CONFIG_PWM_PULSECOUNT
uint32_t count;
uint32_t count;
#endif
int duration;
int duration;
};
/****************************************************************************
@ -90,6 +91,24 @@ static struct pwm_state_s g_pwmstate;
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: pwm_devpath
****************************************************************************/
static void pwm_devpath(FAR struct pwm_state_s *pwm, FAR const char *devpath)
{
/* Get rid of any old device path */
if (pwm->devpath)
{
free(pwm->devpath);
}
/* Then set-up the new device path by copying the string */
pwm->devpath = strdup(devpath);
}
/****************************************************************************
* Name: pwm_help
****************************************************************************/
@ -100,6 +119,9 @@ static void pwm_help(FAR struct pwm_state_s *pwm)
message("\nArguments are \"sticky\". For example, once the PWM frequency is\n");
message("specified, that frequency will be re-used until it is changed.\n");
message("\n\"sticky\" OPTIONS include:\n");
message(" [-p devpath] selects the PWM device. "
"Default: %s Current: %s\n",
CONFIG_EXAMPLES_PWM_DEVPATH, pwm->devpath ? pwm->devpath : "NONE");
message(" [-f addr] selects the pulse frequency. "
"Default: %d Hz Current: %d Hz\n",
CONFIG_EXAMPLES_PWM_FREQUENCY, pwm->freq);
@ -158,6 +180,7 @@ static int arg_decimal(FAR char **arg, FAR long *value)
static void parse_args(FAR struct pwm_state_s *pwm, int argc, FAR char **argv)
{
FAR char *ptr;
FAR char *str;
long value;
int index;
int nargs;
@ -211,6 +234,12 @@ static void parse_args(FAR struct pwm_state_s *pwm, int argc, FAR char **argv)
break;
#endif
case 'p':
nargs = arg_string(&argv[index], &str);
pwm_devpath(pwm, str);
index += nargs;
break;
case 't':
nargs = arg_decimal(&argv[index], &value);
if (value < 1 || value > INT_MAX)
@ -266,6 +295,15 @@ int pwm_main(int argc, char *argv[])
parse_args(&g_pwmstate, argc, argv);
/* Has a device been assigned? */
if (!g_pwmstate.devpath)
{
/* No.. use the default device */
pwm_devpath(&g_pwmstate, CONFIG_EXAMPLES_PWM_DEVPATH);
}
/* Initialization of the PWM hardware is performed by logic external to
* this test.
*/
@ -279,11 +317,10 @@ int pwm_main(int argc, char *argv[])
/* Open the PWM device for reading */
fd = open(CONFIG_EXAMPLES_PWM_DEVPATH, O_RDONLY);
fd = open(g_pwmstate.devpath, O_RDONLY);
if (fd < 0)
{
message("pwm_main: open %s failed: %d\n",
CONFIG_EXAMPLES_PWM_DEVPATH, errno);
message("pwm_main: open %s failed: %d\n", g_pwmstate.devpath, errno);
goto errout;
}
@ -335,8 +372,7 @@ int pwm_main(int argc, char *argv[])
/* 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)

View File

@ -100,9 +100,11 @@
#ifdef CONFIG_NSH_BUILTIN_APPS
struct qe_example_s
{
bool reset; /* True: set the count back to zero */
unsigned int nloops; /* Collect this number of samples */
unsigned int delay; /* Delay this number of seconds between samples */
bool initialized; /* True: QE devices have been initialized */
bool reset; /* True: set the count back to zero */
FAR char *devpath; /* Path to the QE device */
unsigned int nloops; /* Collect this number of samples */
unsigned int delay; /* Delay this number of seconds between samples */
};
#endif

View File

@ -45,6 +45,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
@ -90,6 +91,24 @@ struct qe_example_s g_qeexample;
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: qe_devpath
****************************************************************************/
static void qe_devpath(FAR const char *devpath)
{
/* Get rid of any old device path */
if (g_qeexample.devpath)
{
free(g_qeexample.devpath);
}
/* The set-up the new device path by copying the string */
g_qeexample.devpath = strdup(devpath);
}
/****************************************************************************
* Name: qe_help
****************************************************************************/
@ -99,6 +118,7 @@ static void qe_help(void)
{
message("\nUsage: qe [OPTIONS]\n\n");
message("OPTIONS include:\n");
message(" [-p devpath] QE device path\n");
message(" [-n samples] Number of samples\n");
message(" [-t msec] Delay between samples (msec)\n");
message(" [-r] Reset the position to zero\n");
@ -152,6 +172,7 @@ static int arg_decimal(FAR char **arg, FAR long *value)
static void parse_args(int argc, FAR char **argv)
{
FAR char *ptr;
FAR char *str;
long value;
int index;
int nargs;
@ -183,6 +204,12 @@ static void parse_args(int argc, FAR char **argv)
index += nargs;
break;
case 'p':
nargs = arg_string(&argv[index], &str);
qe_devpath(str);
index += nargs;
break;
case 't':
nargs = arg_decimal(&argv[index], &value);
if (value < 0 || value > INT_MAX)
@ -231,33 +258,44 @@ int MAIN_NAME(int argc, char *argv[])
int nloops;
#endif
/* Check if we have initialized */
if (!g_qeexample.initialized)
{
/* Initialization of the encoder hardware is performed by logic external to
* this test.
*/
message(MAIN_STRING "Initializing external encoder(s)\n");
ret = qe_devinit();
if (ret != OK)
{
message(MAIN_STRING "qe_devinit failed: %d\n", ret);
exitval = EXIT_FAILURE;
goto errout;
}
/* Set the default values */
qe_devpath(CONFIG_EXAMPLES_QENCODER_DEVPATH);
g_qeexample.initialized = true;
}
/* Parse command line arguments */
#ifdef CONFIG_NSH_BUILTIN_APPS
parse_args(argc, argv);
#endif
/* Initialization of the encoder hardware is performed by logic external to
* this test.
*/
message(MAIN_STRING "Initializing external encoder\n");
ret = qe_devinit();
if (ret != OK)
{
message(MAIN_STRING "qe_devinit failed: %d\n", ret);
exitval = EXIT_FAILURE;
goto errout;
}
/* Open the encoder device for reading */
message(MAIN_STRING "Hardware initialized. Opening the encoder device\n");
fd = open(CONFIG_EXAMPLES_QENCODER_DEVPATH, O_RDONLY);
message(MAIN_STRING "Hardware initialized. Opening the encoder device: %s\n",
g_qeexample.devpath);
fd = open(g_qeexample.devpath, O_RDONLY);
if (fd < 0)
{
message(MAIN_STRING "open %s failed: %d\n",
CONFIG_EXAMPLES_QENCODER_DEVPATH, errno);
message(MAIN_STRING "open %s failed: %d\n", g_qeexample.devpath, errno);
exitval = EXIT_FAILURE;
goto errout_with_dev;
}