Skip to content

Commit f8e4afb

Browse files
Zhangshoukuixiaoxiang781216
authored andcommitted
optimize GPIO test for sim test pass
cmocka_driver_gpio //gpio input/output is tested by default cmocka_driver_gpio -a /dev/gpio0 -b /dev/gpio1 -l // test loop cmocka_driver_gpio -a /dev/gpio0 -b /dev/gpio0 // gpio input/output is tested by default Signed-off-by: zhangshoukui <[email protected]>
1 parent b4def30 commit f8e4afb

File tree

1 file changed

+70
-8
lines changed

1 file changed

+70
-8
lines changed

testing/drivertest/drivertest_gpio.c

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ struct pre_build_s
4949
{
5050
FAR char *gpio_a;
5151
FAR char *gpio_b;
52+
bool loop;
5253
};
5354

5455
/****************************************************************************
@@ -62,10 +63,11 @@ struct pre_build_s
6263
static void show_usage(FAR const char *progname, int exitcode)
6364
{
6465
printf("Usage: %s -a <gpio_a>[dev/gpio0] -b"
65-
" <gpio_b>[dev/gpio1] \n", progname);
66+
" <gpio_b>[dev/gpio1] -l [loop test]\n", progname);
6667
printf("Where:\n");
6768
printf(" -a <gpio_a> gpio_a location [default location: dev/gpio0].\n");
6869
printf(" -b <gpio_b> gpio_b location [default location: dev/gpio1].\n");
70+
printf(" -l <loop test> [default: Test the input and output of GPIO ]\n");
6971
exit(exitcode);
7072
}
7173

@@ -78,7 +80,7 @@ static void parse_commandline(int argc, FAR char **argv,
7880
{
7981
int option;
8082

81-
while ((option = getopt(argc, argv, "a:b:")) != ERROR)
83+
while ((option = getopt(argc, argv, "a:b:l")) != ERROR)
8284
{
8385
switch (option)
8486
{
@@ -88,6 +90,9 @@ static void parse_commandline(int argc, FAR char **argv,
8890
case 'b':
8991
pre_build->gpio_b = optarg;
9092
break;
93+
case 'l':
94+
pre_build->loop = true;
95+
break;
9196
case '?':
9297
printf("Unknown option: %c\n", optopt);
9398
show_usage(argv[0], EXIT_FAILURE);
@@ -133,10 +138,55 @@ static int setup(FAR void **state)
133138
}
134139

135140
/****************************************************************************
136-
* Name: gpiotest01
141+
* Name: drivertest_gpio_one
142+
****************************************************************************/
143+
144+
static void drivertest_gpio_one(FAR void **state)
145+
{
146+
FAR struct pre_build_s *pre_build;
147+
bool outvalue;
148+
bool invalue;
149+
int fd[2];
150+
int ret;
151+
int i;
152+
int j;
153+
154+
pre_build = (FAR struct pre_build_s *)*state;
155+
fd[0] = open(pre_build->gpio_a, O_RDWR);
156+
assert_false(fd[0] < 0);
157+
158+
fd[1] = open(pre_build->gpio_b, O_RDWR);
159+
assert_false(fd[1] < 0);
160+
161+
/* Test Single GPIO I/O functionality */
162+
163+
for (i = 0; i < 2; i++)
164+
{
165+
ret = ioctl(fd[i], GPIOC_SETPINTYPE, GPIO_INPUT_PIN | GPIO_OUTPUT_PIN);
166+
assert_false(ret < 0);
167+
for (j = 0; j < GPIOTEST_MAXVALUE; j++)
168+
{
169+
outvalue = gpiotest_randbin();
170+
ret = ioctl(fd[i], GPIOC_WRITE, outvalue);
171+
assert_false(ret < 0);
172+
173+
ret = ioctl(fd[i], GPIOC_READ, &invalue);
174+
assert_false(ret < 0);
175+
176+
printf("[input and output test] outvalue is %d, invalue is %d\n",
177+
outvalue, invalue);
178+
assert_int_equal(invalue, outvalue);
179+
}
180+
181+
close(fd[i]);
182+
}
183+
}
184+
185+
/****************************************************************************
186+
* Name: drivertest_gpio_loop
137187
****************************************************************************/
138188

139-
static void gpiotest01(FAR void **state)
189+
static void drivertest_gpio_loop(FAR void **state)
140190
{
141191
FAR struct pre_build_s *pre_build;
142192
int fd_a;
@@ -179,7 +229,7 @@ static void gpiotest01(FAR void **state)
179229
ret = ioctl(fd_b, GPIOC_READ, (unsigned long)((uintptr_t)&invalue));
180230
assert_false(ret < 0);
181231

182-
printf("[__Verify__] outvalue is %d, invalue is %d\n",
232+
printf("[Loop test] outvalue is %d, invalue is %d\n",
183233
outvalue, invalue);
184234
assert_int_equal(invalue, outvalue);
185235
}
@@ -205,7 +255,7 @@ static void gpiotest01(FAR void **state)
205255
ret = ioctl(fd_a, GPIOC_READ, (unsigned long)((uintptr_t)&invalue));
206256
assert_false(ret < 0);
207257

208-
printf("[__Verify__] outvalue is %d, invalue is %d\n",
258+
printf("[Loop test] outvalue is %d, invalue is %d\n",
209259
outvalue, invalue);
210260
assert_int_equal(invalue, outvalue);
211261
}
@@ -233,16 +283,28 @@ static int teardown(FAR void **state)
233283

234284
int main(int argc, FAR char *argv[])
235285
{
286+
void (*drivertest_gpio)(FAR void **state);
236287
FAR struct pre_build_s pre_build =
237288
{
238289
.gpio_a = "dev/gpio0",
239-
.gpio_b = "dev/gpio1"
290+
.gpio_b = "dev/gpio1",
291+
.loop = false
240292
};
241293

242294
parse_commandline(argc, argv, &pre_build);
295+
296+
if (pre_build.loop)
297+
{
298+
drivertest_gpio = drivertest_gpio_loop;
299+
}
300+
else
301+
{
302+
drivertest_gpio = drivertest_gpio_one;
303+
}
304+
243305
const struct CMUnitTest tests[] =
244306
{
245-
cmocka_unit_test_prestate_setup_teardown(gpiotest01, setup,
307+
cmocka_unit_test_prestate_setup_teardown(drivertest_gpio, setup,
246308
teardown, &pre_build),
247309
};
248310

0 commit comments

Comments
 (0)