diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-07-30 16:51:43 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2012-07-30 16:51:43 +0000 |
commit | 505963a3568ba0dc23cb0cb7b3efee58f83a49ea (patch) | |
tree | 41f9e4861aa9914bbcf36241710b609ce1681ba1 /apps/examples/adc | |
parent | e3feaf63eccee5e555ab006d7f8216057cd1e31b (diff) | |
download | px4-nuttx-505963a3568ba0dc23cb0cb7b3efee58f83a49ea.tar.gz px4-nuttx-505963a3568ba0dc23cb0cb7b3efee58f83a49ea.tar.bz2 px4-nuttx-505963a3568ba0dc23cb0cb7b3efee58f83a49ea.zip |
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
Diffstat (limited to 'apps/examples/adc')
-rw-r--r-- | apps/examples/adc/adc.h | 11 | ||||
-rw-r--r-- | apps/examples/adc/adc_main.c | 209 |
2 files changed, 188 insertions, 32 deletions
diff --git a/apps/examples/adc/adc.h b/apps/examples/adc/adc.h index 214ecc6c8..9f79db92a 100644 --- a/apps/examples/adc/adc.h +++ b/apps/examples/adc/adc.h @@ -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 ****************************************************************************/ diff --git a/apps/examples/adc/adc_main.c b/apps/examples/adc/adc_main.c index 8373684a4..b88032654 100644 --- a/apps/examples/adc/adc_main.c +++ b/apps/examples/adc/adc_main.c @@ -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 ****************************************************************************/ @@ -85,6 +88,137 @@ ****************************************************************************/ /**************************************************************************** + * 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; - /* 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 - */ + /* Check if we have initialized */ -#if defined(CONFIG_NSH_BUILTIN_APPS) - nloops = 1; - if (argc > 1) + if (!g_adcstate.initialized) { - nloops = strtol(argv[1], NULL, 10); + /* 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; } - message(MAIN_STRING "nloops: %d\n", nloops); -#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES) - message(MAIN_STRING "nloops: %d\n", CONFIG_EXAMPLES_ADC_NSAMPLES); + + /* Parse the command line */ + +#ifdef CONFIG_NSH_BUILTIN_APPS + parse_args(&g_adcstate, argc, argv); #endif - /* Initialization of the ADC hardware is performed by logic external to - * this test. + /* 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 */ - 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; - } +#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES) + message(MAIN_STRING "g_adcstate.count: %d\n", g_adcstate.count); +#endif /* 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; } |