Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Files gpio-int-test.c, gpio-utils.c, gpio-utils.h are below //GPIO-INT-TEST.c #include #include #include #include #include #include #include #include // Defines signal-handling functions (i.e. trap Ctrl-C) #include gpio-utils.h /****************************************************************
Files gpio-int-test.c, gpio-utils.c, gpio-utils.h are below
//GPIO-INT-TEST.c
#include
#include
#include
#include
#include
#include
#include
#include // Defines signal-handling functions (i.e. trap Ctrl-C)
#include "gpio-utils.h"
/****************************************************************
* Constants
****************************************************************/
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
/****************************************************************
* Global variables
****************************************************************/
int keepgoing = 1; // Set to 0 when ctrl-c is pressed
/****************************************************************
* signal_handler
****************************************************************/
void signal_handler(int sig);
// Callback called when SIGINT is sent to the process (Ctrl-C)
void signal_handler(int sig)
{
printf( "Ctrl-C pressed, cleaning up and exiting.. " );
keepgoing = 0;
}
/****************************************************************
* Main
****************************************************************/
int main(int argc, char **argv, char **envp)
{
struct pollfd fdset[2];
int nfds = 2;
int gpio_fd, timeout, rc;
char buf[MAX_BUF];
unsigned int gpio;
int len;
if (argc
printf("Usage: gpio-int ");
printf("Waits for a change in the GPIO pin voltage level or input on stdin ");
exit(-1);
}
// Set the signal callback for Ctrl-C
signal(SIGINT, signal_handler);
gpio = atoi(argv[1]);
// gpio_export(gpio);
gpio_set_dir(gpio, "in");
gpio_set_edge(gpio, "both"); // Can be rising, falling or both
gpio_fd = gpio_fd_open(gpio, O_RDONLY);
timeout = POLL_TIMEOUT;
while (keepgoing) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = STDIN_FILENO;
fdset[0].events = POLLIN;
fdset[1].fd = gpio_fd;
fdset[1].events = POLLPRI;
rc = poll(fdset, nfds, timeout);
if (rc
printf(" poll() failed! ");
return -1;
}
if (rc == 0) {
printf(".");
}
if (fdset[1].revents & POLLPRI) {
lseek(fdset[1].fd, 0, SEEK_SET); // Read from the start of the file
len = read(fdset[1].fd, buf, MAX_BUF);
printf(" poll() GPIO %d interrupt occurred, value=%c, len=%d ",
gpio, buf[0], len);
}
if (fdset[0].revents & POLLIN) {
(void)read(fdset[0].fd, buf, 1);
printf(" poll() stdin read 0x%2.2X ", (unsigned int) buf[0]);
}
fflush(stdout);
}
gpio_fd_close(gpio_fd);
return 0;
}
-
GPIO-UTILS.c
#include "gpio-utils.h"
#include
#include
#include
#include
#include
/****************************************************************
* gpio_export
****************************************************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_unexport
****************************************************************/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd
perror("gpio/export");
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_dir
****************************************************************/
int gpio_set_dir(unsigned int gpio, const char* dir)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd
perror("gpio/direction");
return fd;
}
write(fd, dir, strlen(dir)+1 );
close(fd);
return 0;
}
/****************************************************************
* gpio_set_value
****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd
perror("gpio/set-value");
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
/****************************************************************
* gpio_get_value
****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0') {
*value = 1;
} else {
*value = 0;
}
close(fd);
return 0;
}
/****************************************************************
* gpio_set_edge
****************************************************************/
int gpio_set_edge(unsigned int gpio, const char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd
1. In gpio-int-test.c, why has the "gpio_export(gpio);" line been commented out of the code? Move the files to a development directory. Review the header and code for gpio-utils. Note that we are using open, close, read, and write for l/O. Review "https://en.wikibooks.org/wiki/A_Little_C_Primer/C_File- IO_Through_System_Calls" for details and use of these functions 2. What are the prototypes for these functions? Read the man page for poll. Specifically, look for the parameters, the return value, and the functionality. 3. How is poll used in gpio-int-test? 4. What are the events waited for? 5. How is the return value from poll used? Build gpio-int-test. (make gpis-int-test) perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/****************************************************************
* gpio_fd_open
****************************************************************/
int gpio_fd_open(unsigned int gpio, unsigned int dir)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, dir | O_NONBLOCK );
if (fd
perror("gpio/fd_open");
}
return fd;
}
/****************************************************************
* gpio_fd_close
****************************************************************/
int gpio_fd_close(int fd)
{
return close(fd);
}
GPIO-UTILS.h
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
int gpio_export(unsigned int gpio);
int gpio_unexport(unsigned int gpio);
int gpio_set_dir(unsigned int gpio, const char* dir);
int gpio_set_value(unsigned int gpio, unsigned int value);
int gpio_get_value(unsigned int gpio, unsigned int *value);
int gpio_set_edge(unsigned int gpio, const char *edge);
int gpio_fd_open(unsigned int gpio, unsigned int dir);
int gpio_fd_close(int fd);
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started