Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//gpio utils below #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

//gpio utils below

#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

/**************************************************************** * 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

/**************************************************************** * 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

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

/**************************************************************** * 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

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

/**************************************************************** * 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

/**************************************************************** * gpio_fd_close ****************************************************************/

int gpio_fd_close(int fd) { return close(fd); }

//gpio int test below

#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("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

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; }

image text in transcribed

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- O 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? 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- O 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

What is the role of the Joint Commission in health care?

Answered: 1 week ago