2016-08-01 21:53:20 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/gpio/gpio_main.c
|
2016-08-01 21:53:20 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* 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
|
2016-08-01 21:53:20 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-01 21:53:20 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* 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.
|
2016-08-01 21:53:20 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/ioexpander/gpio.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void show_usage(FAR const char *progname)
|
|
|
|
{
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr, "USAGE: %s [-w <signo>] [-o <value>] <driver-path>\n",
|
|
|
|
progname);
|
2016-08-01 23:27:48 +02:00
|
|
|
fprintf(stderr, " %s -h\n", progname);
|
2016-08-01 21:53:20 +02:00
|
|
|
fprintf(stderr, "Where:\n");
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr, "\t<driver-path>: The full path to the GPIO pin "
|
|
|
|
"driver.\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
"\t-w <signo>: Wait for an signal if this is an interrupt pin.\n");
|
|
|
|
fprintf(stderr,
|
|
|
|
"\t-o <value>: Write this value (0 or 1) if this is an output "
|
|
|
|
"pin.\n");
|
2016-08-01 21:53:20 +02:00
|
|
|
fprintf(stderr, "\t-h: Print this usage information and exit.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* gpio_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
FAR char *devpath = NULL;
|
|
|
|
enum gpio_pintype_e pintype;
|
|
|
|
bool havesigno = false;
|
|
|
|
bool invalue;
|
|
|
|
bool outvalue = false;
|
|
|
|
bool haveout = false;
|
|
|
|
int signo = 0;
|
|
|
|
int ndx;
|
|
|
|
int ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/* Parse command line */
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing required arguments\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ndx = 1;
|
|
|
|
if (strcmp(argv[ndx], "-h") == 0)
|
|
|
|
{
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[ndx], "-w") == 0)
|
|
|
|
{
|
|
|
|
havesigno = true;
|
|
|
|
|
|
|
|
if (++ndx >= argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing argument to -o\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
signo = atoi(argv[ndx]);
|
|
|
|
|
|
|
|
if (++ndx >= argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing required <driver-path>\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:27:48 +02:00
|
|
|
if (ndx < argc && strcmp(argv[ndx], "-o") == 0)
|
2016-08-01 21:53:20 +02:00
|
|
|
{
|
|
|
|
if (++ndx >= argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing argument to -o\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-08-01 23:27:48 +02:00
|
|
|
if (strcmp(argv[ndx], "0") == 0)
|
2016-08-01 21:53:20 +02:00
|
|
|
{
|
|
|
|
outvalue = false;
|
|
|
|
haveout = true;
|
|
|
|
}
|
2016-08-01 23:27:48 +02:00
|
|
|
else if (strcmp(argv[ndx], "1") == 0)
|
2016-08-01 21:53:20 +02:00
|
|
|
{
|
|
|
|
outvalue = true;
|
|
|
|
haveout = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Invalid argument to -o\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (++ndx >= argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: Missing required <driver-path>\n");
|
|
|
|
show_usage(argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
devpath = argv[ndx];
|
|
|
|
printf("Driver: %s\n", devpath);
|
|
|
|
|
|
|
|
/* Open the pin driver */
|
|
|
|
|
|
|
|
fd = open(devpath, O_RDWR);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
fprintf(stderr, "ERROR: Failed to open %s: %d\n", devpath, errcode);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the pin type */
|
|
|
|
|
|
|
|
ret = ioctl(fd, GPIOC_PINTYPE, (unsigned long)((uintptr_t)&pintype));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr, "ERROR: Failed to read pintype from %s: %d\n",
|
|
|
|
devpath, errcode);
|
2016-08-01 21:53:20 +02:00
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the pin value */
|
|
|
|
|
|
|
|
ret = ioctl(fd, GPIOC_READ, (unsigned long)((uintptr_t)&invalue));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr, "ERROR: Failed to read value from %s: %d\n",
|
|
|
|
devpath, errcode);
|
2016-08-01 21:53:20 +02:00
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the test based on the pintype and on command line options */
|
|
|
|
|
|
|
|
switch (pintype)
|
|
|
|
{
|
|
|
|
case GPIO_INPUT_PIN:
|
|
|
|
{
|
2020-07-09 12:10:49 +02:00
|
|
|
printf(" Input pin: Value=%u\n",
|
|
|
|
(unsigned int)invalue);
|
2016-08-01 21:53:20 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2020-07-09 11:50:14 +02:00
|
|
|
case GPIO_INPUT_PIN_PULLUP:
|
|
|
|
{
|
2020-07-09 12:10:49 +02:00
|
|
|
printf(" Input pin (pull-up): Value=%u\n",
|
|
|
|
(unsigned int)invalue);
|
2020-07-09 11:50:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_INPUT_PIN_PULLDOWN:
|
|
|
|
{
|
2020-07-09 12:10:49 +02:00
|
|
|
printf(" Input pin (pull-down): Value=%u\n",
|
|
|
|
(unsigned int)invalue);
|
2020-07-09 11:50:14 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-08-01 21:53:20 +02:00
|
|
|
case GPIO_OUTPUT_PIN:
|
2020-07-09 11:50:14 +02:00
|
|
|
case GPIO_OUTPUT_PIN_OPENDRAIN:
|
2016-08-01 21:53:20 +02:00
|
|
|
{
|
|
|
|
printf(" Output pin: Value=%u\n", (unsigned int)invalue);
|
|
|
|
|
|
|
|
if (haveout)
|
|
|
|
{
|
|
|
|
printf(" Writing: Value=%u\n", (unsigned int)outvalue);
|
|
|
|
|
|
|
|
/* Write the pin value */
|
|
|
|
|
|
|
|
ret = ioctl(fd, GPIOC_WRITE, (unsigned long)outvalue);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to write value %u from %s: %d\n",
|
2020-11-11 01:39:23 +01:00
|
|
|
(unsigned int)outvalue, devpath, errcode);
|
2016-08-01 21:53:20 +02:00
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-read the pin value */
|
|
|
|
|
2020-07-09 12:10:49 +02:00
|
|
|
ret = ioctl(fd, GPIOC_READ,
|
|
|
|
(unsigned long)((uintptr_t)&invalue));
|
2016-08-01 21:53:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to re-read value from %s: %d\n",
|
2016-08-01 21:53:20 +02:00
|
|
|
devpath, errcode);
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Verify: Value=%u\n", (unsigned int)invalue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GPIO_INTERRUPT_PIN:
|
|
|
|
{
|
|
|
|
printf(" Interrupt pin: Value=%u\n", invalue);
|
|
|
|
|
|
|
|
if (havesigno)
|
|
|
|
{
|
2018-11-08 14:50:42 +01:00
|
|
|
struct sigevent notify;
|
2016-08-01 21:53:20 +02:00
|
|
|
struct timespec ts;
|
|
|
|
sigset_t set;
|
|
|
|
|
2018-11-08 14:50:42 +01:00
|
|
|
notify.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
notify.sigev_signo = signo;
|
|
|
|
|
2016-08-01 21:53:20 +02:00
|
|
|
/* Set up to receive signal */
|
|
|
|
|
2018-11-08 14:50:42 +01:00
|
|
|
ret = ioctl(fd, GPIOC_REGISTER, (unsigned long)¬ify);
|
2016-08-01 21:53:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2018-11-08 14:50:42 +01:00
|
|
|
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to setup for signal from %s: %d\n",
|
2016-08-01 21:53:20 +02:00
|
|
|
devpath, errcode);
|
2018-11-08 14:50:42 +01:00
|
|
|
|
2016-08-01 21:53:20 +02:00
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait up to 5 seconds for the signal */
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
sigemptyset(&set);
|
|
|
|
sigaddset(&set, signo);
|
2016-08-01 21:53:20 +02:00
|
|
|
|
|
|
|
ts.tv_sec = 5;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
|
2017-03-02 05:40:43 +01:00
|
|
|
ret = sigtimedwait(&set, NULL, &ts);
|
2020-01-02 13:09:50 +01:00
|
|
|
ioctl(fd, GPIOC_UNREGISTER, 0);
|
2016-08-01 21:53:20 +02:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
|
|
|
if (errcode == EAGAIN)
|
|
|
|
{
|
|
|
|
printf(" [Five second timeout with no signal]\n");
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr, "ERROR: Failed to wait signal %d "
|
|
|
|
"from %s: %d\n", signo, devpath, errcode);
|
2016-08-01 21:53:20 +02:00
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Re-read the pin value */
|
|
|
|
|
2020-07-09 12:10:49 +02:00
|
|
|
ret = ioctl(fd, GPIOC_READ,
|
|
|
|
(unsigned long)((uintptr_t)&invalue));
|
2016-08-01 21:53:20 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2020-07-09 12:10:49 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
"ERROR: Failed to re-read value from %s: %d\n",
|
2016-08-01 21:53:20 +02:00
|
|
|
devpath, errcode);
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(" Verify: Value=%u\n", (unsigned int)invalue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "ERROR: Unrecognized pintype: %d\n", (int)pintype);
|
|
|
|
close(fd);
|
2018-08-13 15:47:26 +02:00
|
|
|
return EXIT_FAILURE;
|
2016-08-01 21:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|