[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GPIO select() and poll()



You must use the
IO_HIGHALARM and IO_LOWALARM ioctl()'s before select()
will do anything.
From os/linux/include/asm-cris/etraxgpio.h:
#define IO_HIGHALARM 0x4  /* set alarm on high for bits marked by 1 */
#define IO_LOWALARM  0x5  /* set alarm on low for bits marked by 1 */
#define IO_CLRALARM  0x6  /* clear alarm for bits marked by 1 */

Note that the argument is the value an not a pointer, i.e something like
this:

ioctl(fd, _IO(GPIO_IOCTYPE, IO_READ_INBITS), &data);

ioctl(devfd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRALARN), bitmask);
/* Set HIGH alarm for low bits */
ioctl(devfd, _IO(ETRAXGPIO_IOCTYPE, IO_HIGHALARM), ~data & bitmask);
/* Set LOW alarm for high bits */
ioctl(devfd, _IO(ETRAXGPIO_IOCTYPE, IO_LOWALARM), data & bitmask);

/Johan


----- Original Message ----- 
From: "Darrell Hodge" <hodged@xxxxxxx.net>
To: <dev-etrax@xxxxxxx.com>
Sent: Wednesday, June 30, 2004 6:02 AM
Subject: GPIO select() and poll()


> Hello Again:
>
> My project board uses 4 bits of gpioa for input and 4 bits for output.
Rather
> than poll the I/O, I am trying to get select() and/or poll() working.
> I know that I have it configured properly for direction because I can set
and
> read bits. I have include some of my code as-well-as ad simple utility
called
> setbits that allow me to easily test the port.
>
> Any help would be appreciated.
>
> Darrell
>
> The Code:#include <sys/ioctl.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <ctype.h>
> #include <errno.h>
> #include <sys/wait.h>
> #include <sys/poll.h>
> #include <sys/types.h>
> #include "gpio.h"
>
> #define fname   "/dev/gpioa"
> #define INFTIM  -1
>
> void print_bits(int fd, int bit, int maxBits)
> {
>         unsigned long dir=0;
>         unsigned long data=0;
>
>         ioctl(fd, _IO(GPIO_IOCTYPE, IO_SETGET_INPUT), &dir);
>         dir = ~dir;
>         ioctl(fd, _IO(GPIO_IOCTYPE, IO_READ_INBITS), &data);
>         close(fd);
>
>         if (bit == -1) {
>                 int i;
>
>                 for (i = maxBits-1; i >= 0; i--) {
>                         printf("%c", (dir & (1 << i))? 'X' :
>                                (data & (1 << i))? '1' : '0');
>                 }
>         } else {
>                 printf("%c", (dir & (1 << bit))? 'X' :
>                        (data & (1 << bit))? '1' : '0');
>         }
> }
>
>
> int main()
> {
>  int    fd = -1;
>  struct pollfd *fds;
>  int fdcount, rv;
>
>  if ((fd = open("/dev/gpioa", O_RDWR)) < 0)
>  {
>    printf("GPIO: could not open %s\n",fname);
>    exit(1);
>  }
>
>  fdcount = 1;
>  fds = malloc(sizeof(struct pollfd) * fdcount);
>  fds[0].fd = fd; fds[0].events |= POLLIN;
> printf("GPIO: Waiting for change\n");
>
>   /* Call poll and tell it to wait indefinitely for some event */
>   rv = poll(fds, fdcount, INFTIM);
>
>  if (rv == -1)
>  {
>   printf("poll returned error\n");
>   exit(1);
>  }
>
>  printf("Events occurred: %d.", rv);
>
>  print_bits(fd, -1, 4);
>
>  exit(0)
>
>
> ********** select() ***********
>
> #include <sys/ioctl.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <ctype.h>
> #include <errno.h>
> #include <sys/wait.h>
> #include <sys/types.h>
> #include "gpio.h"
>
> #define fname   "/dev/gpioa"
>
> void print_bits(int fd, int bit, int maxBits)
> {
>         unsigned long dir=0;
>         unsigned long data=0;
>
>         ioctl(fd, _IO(GPIO_IOCTYPE, IO_SETGET_INPUT), &dir);
>         dir = ~dir;
>         ioctl(fd, _IO(GPIO_IOCTYPE, IO_READ_INBITS), &data);
>         close(fd);
>
>         if (bit == -1) {
>                 int i;
>
>                 for (i = maxBits-1; i >= 0; i--) {
>                         printf("%c", (dir & (1 << i))? 'X' :
>                                (data & (1 << i))? '1' : '0');
>                 }
>         } else {
>                 printf("%c", (dir & (1 << bit))? 'X' :
>                        (data & (1 << bit))? '1' : '0');
>         }
> }
>
>
> int main()
> {
>  int    fd = -1;
>  int    retVal;
>  fd_set readSet;
>
>  if ((fd = open(fname, O_RDWR)) < 0)
>  {
>    printf("GPIO: could not open %s\n",fname);
>    exit(1);
>  }
>
>  FD_ZERO(&readSet);
>  FD_SET(fd, &readSet);
>
>  printf("GPIO: Waiting for change\n");
>  if ((retVal = select (2,&readSet,NULL, NULL, NULL)) < 0)
>  {
>   printf("Select returned error\n");
>
>  printf("GPIO: back from wait\n");
>  print_bits(fd, -1, 4);
>
>  exit(0);
> }
>
> ************** setbits ****************
> /*
>  * For setting bits on general purpose I/O gpio PA port.
>  */
>
> #include <sys/ioctl.h>
> #include <fcntl.h>
> #include <stdio.h>
> #include <unistd.h>
> #include <stdlib.h>
> #include <string.h>
> #include <ctype.h>
> #include <errno.h>
> #include <sys/wait.h>
> #include <sys/types.h>
> #include <asm/etraxgpio.h>
> #include "gpio.h"
>
>
> void usage(void)
> {
>  fprintf(stderr, "Usage: setbits [ -s bit [ -c bit ] ]\n");
>  exit(1);
> }
>
>
>
> void changeBits(int fd, int bit, int setBit)
> {
>  int bitmap = 0;
>
>  bitmap = 1 << bit;
>
>  if (setBit)
>  {
>   ioctl(fd, _IO(ETRAXGPIO_IOCTYPE, IO_SETBITS), bitmap);
>  }
>   else
>  {
>   ioctl(fd, _IO(ETRAXGPIO_IOCTYPE, IO_CLRBITS), bitmap);
>  }
>
> }
>
> int main(int argc, char **argv)
> {
>         int opt;
>         int bit = -1;
>         int fd;
>         int setBit = 0;
>         char* endptr;
>
>
>         while ((opt = getopt(argc, argv, "c:s:")) != -1) {
>                 switch(opt) {
>                         case 's':
>                                 bit = strtol(optarg, &endptr, 10);
>                                 if ((endptr == optarg) || (*endptr !=
'\0')) {
>                                         usage();
>                                 }
>                                 setBit = 1;
>                                 break;
>                         case 'c':
>                                 bit = strtol(optarg, &endptr, 10);
>                                 if ((endptr == optarg) || (*endptr !=
'\0')) {
>                                         usage();
>                                 }
>                                 setBit = 0;
>                                 break;
>                         default:
>                                 usage();
>                 }
>         }
>
>         if ((fd = open("/dev/gpioa", O_RDWR)) < 0)
>         {
>                 perror("open");
>                 exit(1);
>         } else
>           {
>            changeBits(fd, bit, setBit);
>           }
>
>         printf("\n");
>
>         return 0;
> }
>
>
>