The code below uses inotify to watch an input pin and trigger when the 
"value" of the pin changes.  Copy the code to inotify.c then:

To build it:
gcc inotify.c -lpthread -o inotify

To run it:
./inotify  /sys/class/gpio/gpio26/value

In this example gpio26 is set up as an input and /sys/class/gpio/gpio26/edge 
has the value "falling".  If you want an interrupt on both rising and 
falling edges, set edge to "both"

This code works for frequencies up to 20 kHz



#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/inotify.h>
#include <limits.h>
#include <time.h>
#include <pthread.h>
 
 #define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

int count = 0;
 
void *bgTask(void* argv)
{
     int inotifyFd, wd, j;
     char buf[BUF_LEN] __attribute__ ((aligned(8)));
     ssize_t numRead;
     char *p;
     struct inotify_event *event;
 
     inotifyFd = inotify_init();                 /* Create inotify instance 
*/
     if (inotifyFd == -1) {
     printf("Unable to initialize inotify\n");
         return 0;
     }
 
wd = inotify_add_watch(inotifyFd, ((char**)argv)[1], IN_ALL_EVENTS);
if (wd == -1) {
printf("bgTask failed to add watch on file %s\n", ((char**)argv)[1]);
return 0;
}

printf("Watching %s using wd %d\n", ((char**)argv)[1], wd);
     
     while (1) {                                  /* Read events forever */
         int n = read(inotifyFd, buf, BUF_LEN);
         if (n <= 0) {
             printf("read() from inotify fd returned 0!");
return 0;
}
count++;
}
 
return 0;
}

 int main(int argc, char *argv[])
 {
     if (argc < 2 || strcmp(argv[1], "--help") == 0) {
         printf("%s filename\n", argv[0]);
return -1;
     }
 
     pthread_t thread;
     pthread_create(&thread, NULL, bgTask, (void*)argv);
 
int beginCnt = count;
while (1) {
sleep(1);
int endCnt = count;
int diff = endCnt - beginCnt;
beginCnt = count;
printf("cnt = %d\n", diff);
}

     return 0;
 }





On Monday, December 25, 2017 at 6:34:31 AM UTC-6, Rohit Karkala wrote:
>
> Hello everyone, 
>
> I want to implement the threading function for GPIO Interrupt in C 
> Program. 
>
> Can anyone help with the code or reference . 
>
>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/873b204d-78d0-4467-bf82-60615a6ef121%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to