Updated STM32 ADC driver

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4189 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-12-16 00:32:11 +00:00
parent f31ebb053b
commit ce28cd29df
3 changed files with 70 additions and 16 deletions

View File

@ -44,8 +44,10 @@ examples/adc
and this value is ignored. Otherwise, this number of samples is and this value is ignored. Otherwise, this number of samples is
collected and the program terminates. Default: Samples are collected collected and the program terminates. Default: Samples are collected
indefinitely. indefinitely.
- CONFIG_EXAMPLES_ADC_SAMPLESIZE - The number of bytes to read in one sample. - CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
Default: 8 Default: 4
- CONFIG_EXAMPLES_ADC_SAMPLEWIDTH - The width (in bits) of the on ADC sample.
Default: 16
examples/buttons examples/buttons
^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^

View File

@ -54,8 +54,10 @@
* and this value is ignored. Otherwise, this number of samples is * and this value is ignored. Otherwise, this number of samples is
* collected and the program terminates. Default: Samples are collected * collected and the program terminates. Default: Samples are collected
* indefinitely. * indefinitely.
* CONFIG_EXAMPLES_ADC_SAMPLESIZE - The number of bytes to read in one sample. * CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
* Default: 8 * Default: 4
* CONFIG_EXAMPLES_ADC_SAMPLEWIDTH - The width (in bits) of the on ADC sample.
* Default: 16
*/ */
#ifndef CONFIG_ADC #ifndef CONFIG_ADC
@ -66,10 +68,43 @@
# define CONFIG_EXAMPLES_ADC_DEVPATH "/dev/adc0" # define CONFIG_EXAMPLES_ADC_DEVPATH "/dev/adc0"
#endif #endif
#ifndef CONFIG_EXAMPLES_ADC_SAMPLESIZE #ifndef CONFIG_EXAMPLES_ADC_GROUPSIZE
# define CONFIG_EXAMPLES_ADC_SAMPLESIZE 8 # define CONFIG_EXAMPLES_ADC_GROUPSIZE 4
#endif #endif
#ifndef CONFIG_EXAMPLES_ADC_SAMPLEWIDTH
# define CONFIG_EXAMPLES_ADC_SAMPLEWIDTH 16
#endif
/* Sample characteristics ***************************************************/
#undef ADC_SAMPLE_BYTEWIDTH
#if CONFIG_EXAMPLES_ADC_SAMPLEWIDTH <= 8
# undef CONFIG_EXAMPLES_ADC_SAMPLEWIDTH
# define CONFIG_EXAMPLES_ADC_SAMPLEWIDTH 8
# define ADC_SAMPLE_BYTEWIDTH 1
# define SAMPLE_FMT "%02x"
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH <= 16
# undef CONFIG_EXAMPLES_ADC_SAMPLEWIDTH
# define CONFIG_EXAMPLES_ADC_SAMPLEWIDTH 16
# define ADC_SAMPLE_BYTEWIDTH 2
# define SAMPLE_FMT "%04x"
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH <= 24
# undef CONFIG_EXAMPLES_ADC_SAMPLEWIDTH
# define CONFIG_EXAMPLES_ADC_SAMPLEWIDTH 24
# define ADC_SAMPLE_BYTEWIDTH 3
# define SAMPLE_FMT "%06x"
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH <= 32
# undef CONFIG_EXAMPLES_ADC_SAMPLEWIDTH
# define CONFIG_EXAMPLES_ADC_SAMPLEWIDTH 32
# define ADC_SAMPLE_BYTEWIDTH 4
# define SAMPLE_FMT "%08x"
#else
# error "Unsupported sample width"
#endif
#undef ADC_SAMPLE_SIZE (CONFIG_EXAMPLES_ADC_GROUPSIZE * ADC_SAMPLE_BYTEWIDTH)
/* Debug ********************************************************************/ /* Debug ********************************************************************/
#ifdef CONFIG_CPP_HAVE_VARARGS #ifdef CONFIG_CPP_HAVE_VARARGS
@ -94,6 +129,18 @@
* Public Types * Public Types
****************************************************************************/ ****************************************************************************/
#if CONFIG_EXAMPLES_ADC_SAMPLEWIDTH == 8
typedef uint8_t adc_sample_t;
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH == 16
typedef uint16_t adc_sample_t;
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH == 24
typedef uint32_t adc_sample_t;
#elif CONFIG_EXAMPLES_ADC_SAMPLEWIDTH == 32
typedef uint32_t adc_sample_t;
#else
# error "Unsupported sample width"
#endif
/**************************************************************************** /****************************************************************************
* Public Variables * Public Variables
****************************************************************************/ ****************************************************************************/
@ -111,6 +158,6 @@
* *
****************************************************************************/ ****************************************************************************/
void adc_devinit(void); int adc_devinit(void);
#endif /* __APPS_EXAMPLES_ADC_ADC_H */ #endif /* __APPS_EXAMPLES_ADC_ADC_H */

View File

@ -94,8 +94,9 @@
int MAIN_NAME(int argc, char *argv[]) int MAIN_NAME(int argc, char *argv[])
{ {
uintt_t sample[CONFIG_EXAMPLES_ADC_SAMPLESIZE]; uint8_t sample[ADC_SAMPLE_SIZE];
ssize_t nbytes; ssize_t nbytes;
FAR adc_sample_t *ptr;
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES) #if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
long nsamples; long nsamples;
#endif #endif
@ -131,6 +132,7 @@ int MAIN_NAME(int argc, char *argv[])
errval = 1; errval = 1;
goto errout; goto errout;
} }
message(MAIN_STRING "Hardware initialized. Opening the ADC device\n");
/* Open the ADC device for reading */ /* Open the ADC device for reading */
@ -147,6 +149,8 @@ int MAIN_NAME(int argc, char *argv[])
* ADC samples. * ADC samples.
*/ */
message(MAIN_STRING "Entering the main loop\n");
#if defined(CONFIG_NSH_BUILTIN_APPS) #if defined(CONFIG_NSH_BUILTIN_APPS)
for (; nsamples > 0; nsamples--) for (; nsamples > 0; nsamples--)
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES) #elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
@ -161,10 +165,10 @@ int MAIN_NAME(int argc, char *argv[])
msgflush(); msgflush();
/* Read one sample */ /* Read one sample of size ADC_SAMPLE_SIZE */
nbytes = read(fd, sample,CONFIG_EXAMPLES_ADC_SAMPLESIZE )); nbytes = read(fd, sample, ADC_SAMPLE_SIZE);
ivdbg("Bytes read: %d\n", nbytes); message("Bytes read: %d\n", nbytes);
/* Handle unexpected return values */ /* Handle unexpected return values */
@ -181,10 +185,10 @@ int MAIN_NAME(int argc, char *argv[])
message(MAIN_STRING "Interrupted read...\n"); message(MAIN_STRING "Interrupted read...\n");
} }
else if (nbytes != CONFIG_EXAMPLES_ADC_SAMPLESIZE) else if (nbytes != ADC_SAMPLE_SIZE)
{ {
message(MAIN_STRING "Unexpected read size=%d, expected=%d, Ignoring\n", message(MAIN_STRING "Unexpected read size=%d, expected=%d, Ignoring\n",
nbytes, CONFIG_EXAMPLES_ADC_SAMPLESIZE); nbytes, ADC_SAMPLE_SIZE);
} }
/* Print the sample data on successful return */ /* Print the sample data on successful return */
@ -192,16 +196,17 @@ int MAIN_NAME(int argc, char *argv[])
else else
{ {
message("Sample :\n"); message("Sample :\n");
for (i = 0; i < CONFIG_EXAMPLES_ADC_SAMPLESIZE; i++) ptr = (FAR adc_sample_t*)sample;
for (i = 0; i < CONFIG_EXAMPLES_ADC_GROUPSIZE; i++)
{ {
message("%d: %02x\n", i, sample[i]); message("%d: " SAMPLE_FMT "\n", i, ptr[i]);
} }
} }
} }
errout_with_dev: errout_with_dev:
close(fd); close(fd);
errout_with_dev:
errout: errout:
message("Terminating!\n"); message("Terminating!\n");
msgflush(); msgflush();