BeagleBone Black C code for IRQ on P9_23

Aim: Read interrupt from GPIO P9_23 in BeagleBone Black.

I need a c program code to read BeagleBone Black GPIO interrupt. I read some of the Linux base code but didn’t get a solution.

or another way to got my solution.!!

Thank you.

Hi,

I couldn’t remember right now places and docs but to use interrupts you have 2 options.

  1. write a kernel driver.
  2. reading a sysfs device.

Maybe this can help
https://e2e.ti.com/support/legacy_forums/embedded/linux/f/linux-forum-read-only/544879/how-to-get-interrupts-triggered-by-gpio-inputs-for-beaglebone-black

https://e2e.ti.com/support/legacy_forums/embedded/linux/f/linux-forum-read-only/544879/how-to-get-interrupts-triggered-by-gpio-inputs-for-beaglebone-black

1 Like

Still do not get a solution.

You tried Google right???

https://e2e.ti.com/support/legacy_forums/embedded/linux/f/linux-forum-read-only/529207/am335x-gpio-interrupt

Regards,

1 Like

interrupt_test1.c

#include <stdio.h>
#include <rc/i2c.h>
#include <rc/time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <rc/gpio.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h> 

enum 
{
    DIRECTION_UP = 1,
    DIRECTION_DOWN,
    DIRECTION_LEFT,
    DIRECTION_RIGHT 
};

int xaxis = 0;
int yaxis = 0;
static gboolean

onButtonEvent( GIOChannel *channel,
               GIOCondition condition,
               gpointer user_data )
{
//    cerr << "onButtonEvent" << endl;
    int direction = (int)user_data;
    GError *error = 0;
    gsize bytes_read = 0;
    
    g_io_channel_seek_position( channel, 0, G_SEEK_SET, 0 );
    GIOStatus rc = g_io_channel_read_chars( channel,
                                            buf, buf_sz - 1,
                                            &bytes_read,
                                            &error );
    switch( (int)user_data )
    {
        case DIRECTION_UP:
            yaxis++;
            break;
        case DIRECTION_DOWN:
            yaxis--;
            break;
        case DIRECTION_LEFT:
            xaxis--;
            break;
        case DIRECTION_RIGHT:
            xaxis++;
            break;
    }
  //  cerr << " direction:" << direction << " x:" << xaxis << " y:" << yaxis << endl;
    
    // thank you, call again!
    return 1;
}
int main( int argc, char** argv )
{
    GMainLoop* loop = g_main_loop_new( 0, 0 );
    
    int fd = open( "/sys/class/gpio/gpio7/value", O_RDONLY | O_NONBLOCK );
    GIOChannel* channel = g_io_channel_unix_new( fd );
    GIOCondition cond = GIOCondition( G_IO_PRI );
    guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
    
    g_main_loop_run( loop );
}

Compile:

debian@beaglebone:~$ cc -c -Wall -O2 interrupt_test1.c

Error:

debian@beaglebone:~$ cc -c -Wall -O2 interrupt_test1.c
interrupt_test1.c:21:8: error: unknown type name ‘gboolean’
 static gboolean
        ^~~~~~~~
interrupt_test1.c:22:16: error: unknown type name ‘GIOChannel’
 onButtonEvent( GIOChannel *channel,
                ^~~~~~~~~~
interrupt_test1.c:23:16: error: unknown type name ‘GIOCondition’
                GIOCondition condition,
                ^~~~~~~~~~~~
interrupt_test1.c:24:16: error: unknown type name ‘gpointer’
                gpointer user_data )
                ^~~~~~~~
interrupt_test1.c: In function ‘main’:
interrupt_test1.c:41:5: error: unknown type name ‘GMainLoop’
     GMainLoop* loop = g_main_loop_new( 0, 0 );
     ^~~~~~~~~
interrupt_test1.c:41:23: warning: implicit declaration of function ‘g_main_loop_new’ [-Wimplicit-function-declaration]
     GMainLoop* loop = g_main_loop_new( 0, 0 );
                       ^~~~~~~~~~~~~~~
interrupt_test1.c:41:23: warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
interrupt_test1.c:44:5: error: unknown type name ‘GIOChannel’
     GIOChannel* channel = g_io_channel_unix_new( fd );
     ^~~~~~~~~~
interrupt_test1.c:44:27: warning: implicit declaration of function ‘g_io_channel_unix_new’ [-Wimplicit-function-declaration]
     GIOChannel* channel = g_io_channel_unix_new( fd );
                           ^~~~~~~~~~~~~~~~~~~~~
interrupt_test1.c:44:27: warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
interrupt_test1.c:45:5: error: unknown type name ‘GIOCondition’
     GIOCondition cond = GIOCondition( G_IO_PRI );
     ^~~~~~~~~~~~
interrupt_test1.c:45:25: warning: implicit declaration of function ‘GIOCondition’ [-Wimplicit-function-declaration]
     GIOCondition cond = GIOCondition( G_IO_PRI );
                         ^~~~~~~~~~~~
interrupt_test1.c:45:39: error: ‘G_IO_PRI’ undeclared (first use in this function)
     GIOCondition cond = GIOCondition( G_IO_PRI );
                                       ^~~~~~~~
interrupt_test1.c:45:39: note: each undeclared identifier is reported only once for each function it appears in
interrupt_test1.c:46:5: error: unknown type name ‘guint’; did you mean ‘uint’?
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
     ^~~~~
     uint
interrupt_test1.c:46:16: warning: implicit declaration of function ‘g_io_add_watch’ [-Wimplicit-function-declaration]
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
                ^~~~~~~~~~~~~~
interrupt_test1.c:46:47: error: ‘onButtonEvent’ undeclared (first use in this function)
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
                                               ^~~~~~~~~~~~~
interrupt_test1.c:48:5: warning: implicit declaration of function ‘g_main_loop_run’ [-Wimplicit-function-declaration]
     g_main_loop_run( loop );
     ^~~~~~~~~~~~~~~
interrupt_test1.c:46:11: warning: unused variable ‘id’ [-Wunused-variable]
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
           ^~
debian@beaglebone:~$ cc -c -Wall -O2 interrupt_test1.c
interrupt_test1.c:31:8: error: unknown type name ‘gboolean’
 static gboolean
        ^~~~~~~~
interrupt_test1.c:33:16: error: unknown type name ‘GIOChannel’
 onButtonEvent( GIOChannel *channel,
                ^~~~~~~~~~
interrupt_test1.c:34:16: error: unknown type name ‘GIOCondition’
                GIOCondition condition,
                ^~~~~~~~~~~~
interrupt_test1.c:35:16: error: unknown type name ‘gpointer’
                gpointer user_data )
                ^~~~~~~~
interrupt_test1.c: In function ‘main’:
interrupt_test1.c:69:5: error: unknown type name ‘GMainLoop’
     GMainLoop* loop = g_main_loop_new( 0, 0 );
     ^~~~~~~~~
interrupt_test1.c:69:23: warning: implicit declaration of function ‘g_main_loop_new’ [-Wimplicit-function-declaration]
     GMainLoop* loop = g_main_loop_new( 0, 0 );
                       ^~~~~~~~~~~~~~~
interrupt_test1.c:69:23: warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
interrupt_test1.c:72:5: error: unknown type name ‘GIOChannel’
     GIOChannel* channel = g_io_channel_unix_new( fd );
     ^~~~~~~~~~
interrupt_test1.c:72:27: warning: implicit declaration of function ‘g_io_channel_unix_new’ [-Wimplicit-function-declaration]
     GIOChannel* channel = g_io_channel_unix_new( fd );
                           ^~~~~~~~~~~~~~~~~~~~~
interrupt_test1.c:72:27: warning: initialization of ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
interrupt_test1.c:73:5: error: unknown type name ‘GIOCondition’
     GIOCondition cond = GIOCondition( G_IO_PRI );
     ^~~~~~~~~~~~
interrupt_test1.c:73:25: warning: implicit declaration of function ‘GIOCondition’ [-Wimplicit-function-declaration]
     GIOCondition cond = GIOCondition( G_IO_PRI );
                         ^~~~~~~~~~~~
interrupt_test1.c:73:39: error: ‘G_IO_PRI’ undeclared (first use in this function)
     GIOCondition cond = GIOCondition( G_IO_PRI );
                                       ^~~~~~~~
interrupt_test1.c:73:39: note: each undeclared identifier is reported only once for each function it appears in
interrupt_test1.c:74:5: error: unknown type name ‘guint’; did you mean ‘uint’?
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
     ^~~~~
     uint
interrupt_test1.c:74:16: warning: implicit declaration of function ‘g_io_add_watch’ [-Wimplicit-function-declaration]
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
                ^~~~~~~~~~~~~~
interrupt_test1.c:74:47: error: ‘onButtonEvent’ undeclared (first use in this function)
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
                                               ^~~~~~~~~~~~~
interrupt_test1.c:76:5: warning: implicit declaration of function ‘g_main_loop_run’ [-Wimplicit-function-declaration]
     g_main_loop_run( loop );
     ^~~~~~~~~~~~~~~
interrupt_test1.c:74:11: warning: unused variable ‘id’ [-Wunused-variable]
     guint id = g_io_add_watch( channel, cond, onButtonEvent, 0 );
           ^~
debian@beaglebone:~$

Which header file needs to add or another way !!.

Thank you.

don’t be lazy and put in some work.

Hello,

From here: blogs/example.c at master · tranter/blogs · GitHub .

You can see that it uses libgpiod. Change some items in it to handle the BBB.

Seth

P.S. I think this will be a good starter script to handle such a venture.