This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new 1789e685a testing/drivertest: add gpio driver test
1789e685a is described below
commit 1789e685a225b7961d63802a9b60a5ae7de212f2
Author: crafcat7 <[email protected]>
AuthorDate: Sun Feb 12 18:12:05 2023 +0800
testing/drivertest: add gpio driver test
---
testing/drivertest/Makefile | 5 +
testing/drivertest/drivertest_gpio.c | 248 +++++++++++++++++++++++++++++++++++
2 files changed, 253 insertions(+)
diff --git a/testing/drivertest/Makefile b/testing/drivertest/Makefile
index ab26f47f3..8e2c31036 100644
--- a/testing/drivertest/Makefile
+++ b/testing/drivertest/Makefile
@@ -54,4 +54,9 @@ PROGNAME += cmocka_driver_block
MAINSRC += drivertest_block.c
endif
+ifneq ($(CONFIG_DEV_GPIO),)
+PROGNAME += cmocka_driver_gpio
+MAINSRC += drivertest_gpio.c
+endif
+
include $(APPDIR)/Application.mk
diff --git a/testing/drivertest/drivertest_gpio.c
b/testing/drivertest/drivertest_gpio.c
new file mode 100644
index 000000000..c162043da
--- /dev/null
+++ b/testing/drivertest/drivertest_gpio.c
@@ -0,0 +1,248 @@
+/****************************************************************************
+ * apps/testing/drivertest/drivertest_gpio.c
+ *
+ * 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 <nuttx/config.h>
+
+#include <sys/ioctl.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+#include <nuttx/ioexpander/gpio.h>
+
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdint.h>
+#include <cmocka.h>
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+#define GPIOTEST_MAXVALUE 5
+
+struct pre_build_s
+{
+ char *gpio_a;
+ char *gpio_b;
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: show_usage
+ ****************************************************************************/
+
+static void show_usage(FAR const char *progname, int exitcode)
+{
+ printf("Usage: %s -a <gpio_a>[dev/gpio0] -b"
+ " <gpio_b>[dev/gpio1] \n", progname);
+ printf("Where:\n");
+ printf(" -a <gpio_a> gpio_a location [default location: dev/gpio0].\n");
+ printf(" -b <gpio_b> gpio_b location [default location: dev/gpio1].\n");
+ exit(exitcode);
+}
+
+/****************************************************************************
+ * Name: parse_commandline
+ ****************************************************************************/
+
+static void parse_commandline(int argc, FAR char **argv,
+ FAR struct pre_build_s *pre_build)
+{
+ int option;
+
+ while ((option = getopt(argc, argv, "a:b:")) != ERROR)
+ {
+ switch (option)
+ {
+ case 'a':
+ pre_build->gpio_a = optarg;
+ break;
+ case 'b':
+ pre_build->gpio_b = optarg;
+ break;
+ case '?':
+ printf("Unknown option: %c\n", optopt);
+ show_usage(argv[0], EXIT_FAILURE);
+ break;
+ }
+ }
+}
+
+/****************************************************************************
+ * Name: gpiotest_randbin
+ ****************************************************************************/
+
+static inline bool gpiotest_randbin(void)
+{
+ int value = rand() % 18;
+ if (value > 9)
+ {
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+
+ return true;
+}
+
+/****************************************************************************
+ * Name: setup
+ ****************************************************************************/
+
+static int setup(FAR void **state)
+{
+ FAR struct pre_build_s *pre_build;
+ time_t t;
+ pre_build = (struct pre_build_s *)*state;
+
+ /* Seed the random number generated */
+
+ srand((unsigned)time(&t));
+ *state = pre_build;
+ return 0;
+}
+
+/****************************************************************************
+ * Name: gpiotest01
+ ****************************************************************************/
+
+static void gpiotest01(FAR void **state)
+{
+ FAR struct pre_build_s *pre_build;
+ int fd_a;
+ int fd_b;
+ bool outvalue;
+ bool invalue;
+ int ret;
+ int i;
+
+ pre_build = (struct pre_build_s *)*state;
+
+ fd_a = open(pre_build->gpio_a, O_RDWR);
+ assert_false(fd_a < 0);
+ fd_b = open(pre_build->gpio_b, O_RDWR);
+ assert_false(fd_b < 0);
+
+ /* Test the input function
+ * Method: Output from gpio_a to gpio_b and
+ * compare the read values.
+ */
+
+ /* gpio_a to gpio_b */
+
+ ret = ioctl(fd_a, GPIOC_SETPINTYPE, (unsigned long) GPIO_OUTPUT_PIN);
+ assert_false(ret < 0);
+ ret = ioctl(fd_b, GPIOC_SETPINTYPE, (unsigned long) GPIO_INPUT_PIN);
+ assert_false(ret < 0);
+
+ for (i = 0; i < GPIOTEST_MAXVALUE; i++)
+ {
+ /* Set GPIO level */
+
+ outvalue = gpiotest_randbin();
+ assert_false(ret < 0);
+ ret = ioctl(fd_a, GPIOC_WRITE, (unsigned long) outvalue);
+ assert_false(ret < 0);
+
+ /* Read Pin Test */
+
+ ret = ioctl(fd_b, GPIOC_READ, (unsigned long)((uintptr_t)&invalue));
+ assert_false(ret < 0);
+
+ printf("[__Verify__] outvalue is %d, invalue is %d\n",
+ outvalue, invalue);
+ assert_int_equal(invalue, outvalue);
+ }
+
+ ret = ioctl(fd_b, GPIOC_SETPINTYPE, (unsigned long) GPIO_OUTPUT_PIN);
+ assert_false(ret < 0);
+ ret = ioctl(fd_a, GPIOC_SETPINTYPE, (unsigned long) GPIO_INPUT_PIN);
+ assert_false(ret < 0);
+
+ /* gpio_b to gpio_a */
+
+ for (i = 0; i < GPIOTEST_MAXVALUE; i++)
+ {
+ /* Set GPIO level */
+
+ outvalue = gpiotest_randbin();
+ assert_false(ret < 0);
+ ret = ioctl(fd_b, GPIOC_WRITE, (unsigned long) outvalue);
+ assert_false(ret < 0);
+
+ /* Read Pin Test */
+
+ ret = ioctl(fd_a, GPIOC_READ, (unsigned long)((uintptr_t)&invalue));
+ assert_false(ret < 0);
+
+ printf("[__Verify__] outvalue is %d, invalue is %d\n",
+ outvalue, invalue);
+ assert_int_equal(invalue, outvalue);
+ }
+
+ close(fd_a);
+ close(fd_b);
+}
+
+/****************************************************************************
+ * Name: teardown
+ ****************************************************************************/
+
+static int teardown(FAR void **state)
+{
+ return 0;
+}
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: drivertest_gpio_main
+ ****************************************************************************/
+
+int main(int argc, FAR char *argv[])
+{
+ FAR struct pre_build_s pre_build =
+ {
+ .gpio_a = "dev/gpio0",
+ .gpio_b = "dev/gpio1"
+ };
+
+ parse_commandline(argc, argv, &pre_build);
+ const struct CMUnitTest tests[] =
+ {
+ cmocka_unit_test_prestate_setup_teardown(gpiotest01, setup,
+ teardown, &pre_build),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}