forked from apache/nuttx-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrivertest_adc.c
235 lines (196 loc) · 7.03 KB
/
drivertest_adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/****************************************************************************
* apps/testing/drivers/drivertest/drivertest_adc.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <syslog.h>
#include <sys/time.h>
#include <stdlib.h>
#include <cmocka.h>
#include <nuttx/analog/adc.h>
#include <nuttx/analog/ioctl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define OPTARG_TO_VALUE(value, type, base) \
do { \
FAR char* ptr; \
value = (type)strtoul(optarg, &ptr, base); \
if (*ptr != '\0') { \
printf("Parameter error: -%c %s\n", ch, optarg); \
adc_help(argv[0]); \
} \
} while (0)
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
struct adc_state_s
{
char devpath[PATH_MAX]; /* device to adc device path */
int adc_diff; /* adc value difference */
int duration; /* duration of sampling adc in seconds */
bool soft_trigger; /* soft trigger : true or false */
};
/****************************************************************************
* Private Functions
****************************************************************************/
static void adc_help(FAR char *process_name)
{
printf("Usage: %s [OPTIONS]\n", process_name);
printf(" -p adc device path , default : /dev/adc0\n");
printf(" -d sample adc changes over set-up value. default : 200\n");
printf(" -t duration of adc test [second] , default: 10s\n");
printf(" -m adc conversion method, 1 -- soft trigger, "
"0 -- interrupt , default: 1\n");
exit(-1);
}
/****************************************************************************
* Name: parse_commandline
****************************************************************************/
static void parse_commandline(FAR struct adc_state_s *adc_state, int argc,
FAR char **argv)
{
int ch;
int converted;
while ((ch = getopt(argc, argv, "p:d:m:t:h")) != ERROR)
{
switch (ch)
{
case 'p':
strlcpy(adc_state->devpath, optarg, sizeof(adc_state->devpath));
adc_state->devpath[sizeof(adc_state->devpath) - 1] = '\0';
break;
case 'd':
OPTARG_TO_VALUE(converted, int, 10);
if (converted < 0 || converted > 5000)
{
printf("sample adc value changes over: %d\n", converted);
adc_help(argv[0]);
}
adc_state->adc_diff = (uint8_t)converted;
break;
case 't':
OPTARG_TO_VALUE(converted, int, 10);
if (converted < 1 || converted > INT_MAX)
{
printf("Duty out of range: %d\n", converted);
adc_help(argv[0]);
}
adc_state->duration = (int)converted;
break;
case 'm':
OPTARG_TO_VALUE(converted, uint8_t, 10);
adc_state->soft_trigger = converted ? true : false;
break;
case '?':
printf("Unsupported option: %s\n", optarg);
case 'h':
adc_help(argv[0]);
break;
}
}
}
int32_t adc_read_one_sample(int fd, bool soft_trigger)
{
struct adc_msg_s sample;
int ret;
int nbytes;
/* software trigger to start one ADC conversion */
if (soft_trigger)
{
ret = ioctl(fd, ANIOC_TRIGGER, 0);
assert_return_code(ret, OK);
}
/* Read one samples */
nbytes = read(fd, &sample, sizeof(struct adc_msg_s));
/* Handle unexpected return values */
assert_true(nbytes == sizeof(struct adc_msg_s));
return sample.am_data;
}
/****************************************************************************
* Name: test_case_adc
****************************************************************************/
static void test_case_adc(FAR void** state)
{
int fd;
bool succ = false;
int32_t value1;
int32_t value2;
struct timeval tv1;
struct timeval tv2;
struct timeval res;
FAR struct adc_state_s *adc_state;
adc_state = (FAR struct adc_state_s *)*state;
/* Open the ADC device for reading */
fd = open(adc_state->devpath, O_RDONLY);
assert_true(fd > 0);
value1 = adc_read_one_sample(fd, adc_state->soft_trigger);
/* ADC sample value should be changed in duration [seconds] */
gettimeofday(&tv1, NULL);
while (true)
{
value2 = adc_read_one_sample(fd, adc_state->soft_trigger);
if (abs(value2 - value1) > adc_state->adc_diff)
{
succ = true;
break;
}
gettimeofday(&tv2, NULL);
timersub(&tv2, &tv1, &res);
if (res.tv_sec >= adc_state->duration)
{
printf("adc test timed out\n");
break;
}
usleep(adc_state->soft_trigger ? 1000000 : 200000);
}
close(fd);
assert_true(succ);
}
/****************************************************************************
* drivertest_adc_main
****************************************************************************/
int main(int argc, FAR char *argv[])
{
/* Initialize the state data */
struct adc_state_s adc_state = {
.devpath = "/dev/adc0",
.adc_diff = 200,
.duration = 10,
.soft_trigger = true,
};
parse_commandline(&adc_state, argc, argv);
const struct CMUnitTest tests[] = {
cmocka_unit_test_prestate(test_case_adc, &adc_state),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}