This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

OMAPL138 GPIO interrupt problem

I want to set GP1[1] as a interrupt mode, When a  trigger comes,it comes to execute a function,like: 

void __irq  fun(void)

{

 printf("A interrupt comes.\n");

}

How to do it?  Should I modify board-da850.c,gpio.c,da850.c files?  

Whether I need use request_irq function to request a interrupt or not?

Regards

He

  • I use sysfs interface like below,but it doesn't work, Can someone help?

    int main(int argc, char *argv[])
    {
    int fd;
    if((fd = open("/sys/class/gpio/export", O_WRONLY))<0){
    printf("open1 error.\n");
    exit(1);
    }
    write(fd, "17", 3); // Include null!
    close(fd);

    if((fd = open("/sys/class/gpio/gpio17/direction", O_WRONLY))<0){
    printf("open2 error.\n");
    exit(1);
    }
    write(fd, "in", 4); //设置为输入
    close(fd);

    if((fd=open("/sys/class/gpio/gpio17/edge",O_WRONLY))<0){
    printf("open3 error.\n");
    exit(1);
    }
    write(fd,"falling",4); //设置为下降沿

    struct pollfd fds;
    if((fds.fd=open("/sys/class/gpio/gpio17/value",O_RDONLY))<0){
    printf("open value error.\n");
    exit(1);
    }

    fds.events=POLLPRI | POLLERR;
    while(fds.events){
    printf("hello...\n");
    if(poll(&fds,1,-1)<=0){
    printf("poll error.\n");
    return 1;
    }
    printf("there comes a tragger.\n");
    close(fd);
    }
    return(0);
    }

  • Some of your string writes have count numbers that don't match the string length. You should use strlen() to calculate this for you at runtime or sizeof() at compile time. You might need a dummy read to initiailze the driver. Seems TI drivers need this. Never needed to do this on Atmel platforms. Example code:

    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/poll.h>

    int writestring(const char *name, const char *string)
    {
      int fd;

      if((fd = open(name, O_WRONLY))<0){
        printf("open %s error.\n", name);
        return(-1);
      }
      write(fd, string, strlen(string)+1); // Include null!
      close(fd);
      return(0);
    }

    int main(int argc, char *argv[])
    {
      int           fd;
      struct pollfd fds;
      char          value[2];

      if(writestring("/sys/class/gpio/export",           "17"))      return(1);
      if(writestring("/sys/class/gpio/gpio17/direction", "in"))      return(1);
      if(writestring("/sys/class/gpio/gpio17/edge",      "falling")) return(1);

      if((fd=open("/sys/class/gpio/gpio17/value",O_RDONLY))<0){
        printf("open value error.\n");
        return(1);
      }

      read(fd, value, 2);     /* Dummy read to clear IRQ on TI platforms. */
      lseek(fd, 0, SEEK_SET); /* Rewind to beginning to read again. */

      fds.fd      = fd;
      fds.events  = POLLPRI;
      fds.revents = 0;

      printf("Waiting for events %x\n", fds.events);
      if(poll(&fds,1,-1)<=0){
        printf("poll error.\n");
      }

      printf("Got events %x.\n", fds.revents);

      read(fd, value, 2); /* Read the value */
      printf("Value is %s.\n", value);

      close(fd);

      return(0);
    }

    I did not compile the above code. There may be syntax errors. Some GPIO drivers will report a POLLERR event even if everything seems to be working. Try it with and without.

  • Hi Norman

    Thank you for your help.

    I added below two lines,it works very well now. 

    read(fd, value, 2);     /* Dummy read to clear IRQ on TI platforms. */
    lseek(fd, 0, SEEK_SET); /* Rewind to beginning to read again. */

    But I don't know what's it mean: read(fd, value, 2);     /* Dummy read to clear IRQ on TI platforms. */

    Why the read function can clear IRQ?

    Regards

    He

  • I haven't looked deep into the GPIO implementation. I believe the first dummy read initiializes the edge detect. I am guessing the code needs the initial state of the GPIO before it can determine a transition has occured. All guesses.

    I am not sure if you need to read() after every poll() to "reset" the detect.  Does your code work with just poll() in a loop? Never checked myself as my code always needed the value.

  • Hi Norman

    I used the Gpio-int-test.c examples . The links is: https://www.ridgerun.com/developer/wiki/index.php/Gpio-int-test.c

    When I execue the program: ./gpio-int-test  17

    It occurs a interrupt once I execue the program.

    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 < 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);
     read(gpio_fd,value,2); //Dummy read to clear IRQ on TI platforms    // I added 
    lseek(gpio_fd,0,SEEK_SET); //Rewind to beginning to read again. // I added timeout = POLL_TIMEOUT; while (1) { 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);

                      .............................................

    When I added the two red lines,it won't occur a interrupt.

    I don't know why.

    Thank you  very much!

    Regards

    He