[BeagleBone] help using GPIO pins with C

Hello all,

I am currently working with the BeagleBone as part of a school project. We are transmitting various signals between two BeagleBones which are connected over the internet. To implement this, we need to read the signals through a GPIO bank and output a similar signal through a different GPIO bank on the other side of the network.

We are looking for resources to help interface with the GPIOs in a daemon, or possibly a kernel module. We are using the default version of Angstrom that ships with the board. We plan to program in C. Any help is much appreciated.

Thanks!

John

Here is my sample code for it. Hope it does the trick for you. Any
questions, feel free to ask. Developed for Angstrom C. It's messy but
it works.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define UNEXPORT fopen( "/sys/class/gpio/unexport",
"w" )
#define EXPORT fopen( "/sys/class/gpio/export", "w" )
#define GSTRING( s, i, g ) sprintf( s, "/sys/class/gpio/gpio%d
%s", i, g );
#define GDIRECTION "/direction"
#define GVALUE "/value"
#define GOPEN( s ) fopen( (s), "w" )

int gpio_init ( int pin )
{
        FILE *p = NULL;
        char gpio[50];

        if( ( p = EXPORT ) == NULL )
        {
                printf( "Problem opening gpio%d\n", pin );
                return -1;
        }
        fprintf( p, "%d", pin );
        fclose( p );

        GSTRING( gpio, pin, GDIRECTION );
        if( ( p = GOPEN( gpio ) ) == NULL )
        {
                printf( "Problem opening gpio%d\n %s\n", pin, gpio );
                return -1;
        }
        fprintf( p, "out");
        fclose( p );

        return 0;
}
int gpio_clean ( int pin )
{
        FILE *p = NULL;
        if( ( p = UNEXPORT) == NULL )
        {
                printf( "Problem closing gpio%d\n", pin );
                return -1;
        }
        fprintf( p, "%d", pin );
        fclose( p );
        return 0;
}
int gpio_control ( int pin, int value )
{
        FILE *p = NULL;
        char gpio[50];

        GSTRING( gpio, pin, GVALUE );
        if( ( p = GOPEN( gpio ) ) == NULL )
        {
                printf( "Problem controlling gpio%d\n", pin );
                return -1;
        }
        fprintf( p , "%d", value );
        pclose( p );
}

Hello,

I am also working on a little project of mine and I also would want to
use C/C++ programming for beaglebone. Until now I've been able to
setup a cross compiling environment and to generate a simple "hello
world" program executable for ARMv7.

I used the tutorial http://www.lvr.com/eclipse1.htm
As a next step I will also try to toggle a user LED using cross
compiling.

John,

Here are a couple of good links in using userspace sysfs to access the GPIO and “C” example.

Mark

There is also discussion of accessing GPIO pins from user space in the
BeagleBoard community FAQ: http://elinux.org/BeagleBoardFAQ#Programming_GPIO_pins