Threading for Beagle Bone

424 views
Skip to first unread message

Rohit Karkala

unread,
Dec 25, 2017, 7:34:31 AM12/25/17
to BeagleBoard
Hello everyone,

I want to implement the threading function for GPIO Interrupt in C Program.

Can anyone help with the code or reference .



John Syne

unread,
Dec 25, 2017, 2:30:04 PM12/25/17
to beagl...@googlegroups.com
Get this book:

Linux Device Drivers Development by John Madieu


Doesn’t use BBB, but it does cover the area you are interested in. Most important, it was written for Linux Kernel 4.1 and updated to 4.14

Regards,
John




--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/e7b781c7-b700-46c5-a443-3afd57189852%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Johan Henselmans

unread,
Dec 27, 2017, 7:36:00 AM12/27/17
to BeagleBoard
There is gpio-watch from https://github.com/larsks/gpio-watch. Is uses EPoll, only runs on Linux, not on *BSD (which is using kqueue), have a look.

Kind Regards,
Johan Henselmans

thomasl...@gmail.com

unread,
Dec 28, 2017, 4:42:58 PM12/28/17
to BeagleBoard

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;
 }
Reply all
Reply to author
Forward
0 new messages