Hi,
int main(int argc, char **argv)
{
struct pollfd fdset[1];
int nfds = 1;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio;
int len;
if (argc < 2) {
printf("Usage: gpio-int <gpio-pin>\n\n");
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
exit(-1);
}
gpio = atoi(argv[1]);
gpio_export(gpio);
gpio_set_dir(gpio, 0);
gpio_set_edge(gpio, "rising");
gpio_fd = gpio_fd_open(gpio);
timeout = POLL_TIMEOUT;
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = gpio_fd;
fdset[0].events = POLLIN;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) {
printf(".");
}
if (fdset[0].revents & POLLIN) {
len = read(fdset[0].fd, buf, MAX_BUF);
printf("\npoll() GPIO %d interrupt occurred\n", gpio);
}
fflush(stdout);
}
gpio_fd_close(gpio_fd);
return 0;
}
Web,
// interrupt_gpio44.c
// catches interrupt on p8_12 (gpio44)
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
#define MAX_BINARY_VALUE 8
#define MAX_PATH 64
#define MAX_VALUE 256
#define MAX_RECORDED_DATA 4096
int g_interrupted = 0;
void sig_handler(int signo)
{
if ( signo == SIGINT )
g_interrupted = 1;
}
//============================================================================
//============================================================================
// GPIO RELATED
//============================================================================
//============================================================================
//============================================================================
int set_value( char* sys_str, char* value )
{
int handle = 0;
if ( ( handle = open( sys_str, O_WRONLY ) ) < 0 )
return 0;
write( handle, value, sizeof(char) * strlen( value ) );
close( handle );
return 1;
}
//============================================================================
int get_value( char* sys_str, char* value, int max_length )
{
int handle = 0;
if ( ( handle = open( sys_str, O_RDONLY ) ) < 0 )
return 0;
read( handle, value, sizeof(char) * max_length - 1 );
close( handle );
return 1;
}
//============================================================================
// return microseconds
inline unsigned long get_micros( struct timeval time_value )
{
return ( time_value.tv_sec * 1000000 + time_value.tv_usec );
}
//============================================================================
int main( int argc, char* argv[] )
{
struct timeval curr_time, start_time;
char gpio_value[ MAX_BINARY_VALUE ];
int fd[ 1 ];
struct pollfd pfd[ 1 ];
int count = 0;
int ready;
static unsigned long recorded_time[ MAX_RECORDED_DATA ];
static unsigned char recorded_value[ MAX_RECORDED_DATA ];
int i;
memset( gpio_value, 0, MAX_BINARY_VALUE );
if ( signal( SIGINT, sig_handler ) == SIG_ERR )
printf( "\nCannot assign SIGINT.\n" );
set_value( "/sys/class/gpio/export", "44" );
set_value( "/sys/class/gpio/gpio44/direction", "in" );
set_value( "/sys/class/gpio/gpio44/edge", "both" ); // none, rising, falling or both
fd[ 0 ] = open("/sys/class/gpio/gpio44/value", O_RDONLY );
pfd[ 0 ].fd = fd[ 0 ];
pfd[ 0 ].events = POLLPRI;
pfd[ 0 ].revents = 0;
gettimeofday( &start_time, 0 );
curr_time.tv_sec = start_time.tv_sec;
curr_time.tv_usec = start_time.tv_usec;
printf( "Press ctrl-C to terminate and print output.\n" );
// new POLL
while ( !g_interrupted && count < MAX_RECORDED_DATA )
{
ready = poll( pfd, 1, -1 );
if ( pfd[ 0 ].revents != 0 )
{
gettimeofday( &curr_time, 0 );
lseek( *fd, 0, 0 );
if ( read( *fd, gpio_value, MAX_BINARY_VALUE ) < 0 )
*gpio_value = '?';
recorded_time[ count ] = get_micros( curr_time );
recorded_value[ count ] = *gpio_value;
count++;
}
}
printf( "\n" );
for ( i = 1; i < count; i++ )
printf( "%6d\t%lu\tp8_12: %c\n", i, recorded_time[ i ] - recorded_time[ i - 1 ], recorded_value[ i ] );
return 0;
}
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.