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:
parent
f31ebb053b
commit
ce28cd29df
@ -44,8 +44,10 @@ examples/adc
|
||||
and this value is ignored. Otherwise, this number of samples is
|
||||
collected and the program terminates. Default: Samples are collected
|
||||
indefinitely.
|
||||
- CONFIG_EXAMPLES_ADC_SAMPLESIZE - The number of bytes to read in one sample.
|
||||
Default: 8
|
||||
- CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
|
||||
Default: 4
|
||||
- CONFIG_EXAMPLES_ADC_SAMPLEWIDTH - The width (in bits) of the on ADC sample.
|
||||
Default: 16
|
||||
|
||||
examples/buttons
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
@ -54,8 +54,10 @@
|
||||
* and this value is ignored. Otherwise, this number of samples is
|
||||
* collected and the program terminates. Default: Samples are collected
|
||||
* indefinitely.
|
||||
* CONFIG_EXAMPLES_ADC_SAMPLESIZE - The number of bytes to read in one sample.
|
||||
* Default: 8
|
||||
* CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
|
||||
* Default: 4
|
||||
* CONFIG_EXAMPLES_ADC_SAMPLEWIDTH - The width (in bits) of the on ADC sample.
|
||||
* Default: 16
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_ADC
|
||||
@ -66,10 +68,43 @@
|
||||
# define CONFIG_EXAMPLES_ADC_DEVPATH "/dev/adc0"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_EXAMPLES_ADC_SAMPLESIZE
|
||||
# define CONFIG_EXAMPLES_ADC_SAMPLESIZE 8
|
||||
#ifndef CONFIG_EXAMPLES_ADC_GROUPSIZE
|
||||
# define CONFIG_EXAMPLES_ADC_GROUPSIZE 4
|
||||
#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 ********************************************************************/
|
||||
|
||||
#ifdef CONFIG_CPP_HAVE_VARARGS
|
||||
@ -94,6 +129,18 @@
|
||||
* 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
|
||||
****************************************************************************/
|
||||
@ -111,6 +158,6 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void adc_devinit(void);
|
||||
int adc_devinit(void);
|
||||
|
||||
#endif /* __APPS_EXAMPLES_ADC_ADC_H */
|
||||
|
@ -94,8 +94,9 @@
|
||||
|
||||
int MAIN_NAME(int argc, char *argv[])
|
||||
{
|
||||
uintt_t sample[CONFIG_EXAMPLES_ADC_SAMPLESIZE];
|
||||
uint8_t sample[ADC_SAMPLE_SIZE];
|
||||
ssize_t nbytes;
|
||||
FAR adc_sample_t *ptr;
|
||||
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
|
||||
long nsamples;
|
||||
#endif
|
||||
@ -131,6 +132,7 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
errval = 1;
|
||||
goto errout;
|
||||
}
|
||||
message(MAIN_STRING "Hardware initialized. Opening the ADC device\n");
|
||||
|
||||
/* Open the ADC device for reading */
|
||||
|
||||
@ -147,6 +149,8 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
* ADC samples.
|
||||
*/
|
||||
|
||||
message(MAIN_STRING "Entering the main loop\n");
|
||||
|
||||
#if defined(CONFIG_NSH_BUILTIN_APPS)
|
||||
for (; nsamples > 0; nsamples--)
|
||||
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
|
||||
@ -161,10 +165,10 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
|
||||
msgflush();
|
||||
|
||||
/* Read one sample */
|
||||
/* Read one sample of size ADC_SAMPLE_SIZE */
|
||||
|
||||
nbytes = read(fd, sample,CONFIG_EXAMPLES_ADC_SAMPLESIZE ));
|
||||
ivdbg("Bytes read: %d\n", nbytes);
|
||||
nbytes = read(fd, sample, ADC_SAMPLE_SIZE);
|
||||
message("Bytes read: %d\n", nbytes);
|
||||
|
||||
/* Handle unexpected return values */
|
||||
|
||||
@ -181,10 +185,10 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
|
||||
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",
|
||||
nbytes, CONFIG_EXAMPLES_ADC_SAMPLESIZE);
|
||||
nbytes, ADC_SAMPLE_SIZE);
|
||||
}
|
||||
|
||||
/* Print the sample data on successful return */
|
||||
@ -192,16 +196,17 @@ int MAIN_NAME(int argc, char *argv[])
|
||||
else
|
||||
{
|
||||
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:
|
||||
close(fd);
|
||||
errout_with_dev:
|
||||
|
||||
errout:
|
||||
message("Terminating!\n");
|
||||
msgflush();
|
||||
|
Loading…
x
Reference in New Issue
Block a user