Threading for Beagle Bone

Hello everyone,

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

Can anyone help with the code or reference .

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

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

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;
}