PX4 Firmware
PX4 Autopilot Software http://px4.io
test_adc.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_adc.c
36  * Test for the analog to digital converter.
37  */
38 
39 #include <px4_platform_common/time.h>
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/posix.h>
42 #include <px4_platform_common/log.h>
43 
44 #include <sys/types.h>
45 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <errno.h>
51 
52 #include "tests_main.h"
53 
54 #include <drivers/drv_adc.h>
55 
56 int test_adc(int argc, char *argv[])
57 {
58  int fd = px4_open(ADC0_DEVICE_PATH, O_RDONLY);
59 
60  if (fd < 0) {
61  PX4_ERR("ERROR: can't open ADC device");
62  return 1;
63  }
64 
65  for (unsigned i = 0; i < 5; i++) {
66  /* make space for a maximum number of channels */
68  /* read all channels available */
69  ssize_t count = px4_read(fd, data, sizeof(data));
70 
71  if (count < 0) {
72  goto errout_with_dev;
73  }
74 
75  unsigned channels = count / sizeof(data[0]);
76 
77  for (unsigned j = 0; j < channels; j++) {
78  printf("%d: %u ", data[j].am_channel, data[j].am_data);
79  }
80 
81  printf("\n");
82  px4_usleep(150000);
83  }
84 
85  printf("\t ADC test successful.\n");
86 
87 errout_with_dev:
88 
89  if (fd != 0) { px4_close(fd); }
90 
91  return OK;
92 }
px4_adc_msg_t
Definition: drv_adc.h:56
#define PX4_MAX_ADC_CHANNELS
Definition: drv_adc.h:52
ADC driver interface.
#define ADC0_DEVICE_PATH
Definition: drv_adc.h:59
ssize_t px4_read(int fd, void *buffer, size_t buflen)
uint8_t * data
Definition: dataman.cpp:149
Tests declaration file.
int fd
Definition: dataman.cpp:146
int px4_open(const char *path, int flags,...)
#define OK
Definition: uavcan_main.cpp:71
int test_adc(int argc, char *argv[])
Definition: test_adc.c:56
int px4_close(int fd)