Gpio from userspace in c++

I am trying to access gpio's from userspace in c++.
Toolchain = the one downloaded from narcissus.

Best i can come up with is

system("echo 133 > /sys/class/gpio/export");
SleepMS(5);
File* fdrdy = fopen ("/sys/class/gpio/gpio133");

int DRDY
for ( int i = 0 ; i < 10000 ; )
{
      DRDY = fgetc(fdrdy);
      fseek(fdrdy,0,SEEK_SET);

      if(DRDY == '0')
            {
               ... the variable i is incremented here..
            }
}

I just want to read the status of a gpio.
If its 0, run this code. Or keep polling. Until its zero.
Then run the code. That's all.

I used uboot to mux it. Set its direction as output.

Is there a better way to do this than the code above?

Read somewhere about gpiolib.
But i cant find linux/gpio.h in my toolchain's folder.
:s

Am i missing some basic thing here?

p.s. My beagle isn't handy atm. So this code might be wrong. Haven't
actually tested it. It should work i think...

I am trying to access gpio's from userspace in c++.
Toolchain = the one downloaded from narcissus.

Best i can come up with is

system("echo 133 > /sys/class/gpio/export");
SleepMS(5);
File* fdrdy = fopen ("/sys/class/gpio/gpio133");

int DRDY
for ( int i = 0 ; i < 10000 ; )
{
      DRDY = fgetc(fdrdy);
      fseek(fdrdy,0,SEEK_SET);

      if(DRDY == '0')
            {
               ... the variable i is incremented here..
            }
}

This code is okay and will work over a wide range of different
kernel-versions. If you can live with the fact that you can't wait for a
gpio-event without a active polling loop, then go for it.

There are other ways to do it in a more efficient way: Using
sys/class/gpio with events or binding your GPIO to the input system via
gpio_keys. These may be more efficient but the interface still changes
from kernel-version to kernel-version.

One note to your code: You may not need your fseek-call. It will most
likely work without it.

Read somewhere about gpiolib.
But i cant find linux/gpio.h in my toolchain's folder.
:s

gpiolib is the official kernel-interface to GPIO. It is not accessable
from user-space.

Am i missing some basic thing here?

No.
  Nils

Thank-you
:slight_smile:

I would recommend using standard file access over executing the echo command to read and write the gpio information. The one issue I ran into is simple access control to the part of the filesystem. If I run as root, everything works fine. If I run as a non-root user, I don’t have access. For my purposes, I decided to simply have my process run as root. I’m sure what I should do is figure out how to add my process owner to the correct system permissions group.

This example sets up a series of GPIO ports…

// Export the GPIO lines

FILE * fp = NULL;
for (int index = 131; index < 140; index++)
{
if ((fp = fopen("/sys/class/gpio/export", “ab”)) != NULL)
{
rewind(fp);
fprintf(fp,"%d",index);
fclose(fp);
//printf(“gpio%d exported\n”,index);
}
}

// Configure the GPIO lines

char gpio_file_name[256];
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/direction",130);
for (int index = 131; index < 140; index++)
{
sprintf(gpio_file_name,"/sys/class/gpio/gpio%d/direction",index);
if ((fp = fopen(gpio_file_name, “rb+”)) != NULL)
{
rewind(fp);
fprintf(fp,“out”);
fclose(fp);
//printf(“gpio%d configured for OUTPUT… “,index);
sprintf(gpio_file_name,”/sys/class/gpio/gpio%d/value”,index);
if ((fp = fopen(gpio_file_name, “rb+”)) != NULL)
{
fprintf(fp,“0”);
fclose(fp);
//printf(“value set to “0"”,index);
}
//printf(”\n");
}
}

// Do whatever you want with your GPIO files here…
// I use somethign like

if ((fp[7] = fopen("/sys/class/gpio/gpio137/value", “rb+”)) != NULL)

{
rewind(fp[7]);
// to open each of my individual GPIO files, and then write to it and close it again.

// UnExport your GPIO lines to clean things up.

for (int index = 131; index < 140; index++)
{
FILE * fp = NULL;
if ((fp = fopen("/sys/class/gpio/unexport", “ab”)) != NULL)
{
rewind(fp);
fprintf(fp,"%d",index);
fclose(fp);
//printf(“gpio%d unexported\n”,index);
}
}

Hi guys,

I am trying to access gpio's from userspace in c++.
Toolchain = the one downloaded from narcissus.

...snip...

Read somewhere about gpiolib.
But i cant find linux/gpio.h in my toolchain's folder.
:s

gpiolib is the official kernel-interface to GPIO. It is not accessable
from user-space.

I created a driver which reflects the gpiolib API into user space.

You can find it on my subversion repository here:
http://svn.hylands.org/linux/gpio/

There is a kernel driver, a user-mode library, and an example test app
which uses the library.

I originally did it for the gumstix, and I wrote up a wiki page here:
http://wiki.gumstix.org/index.php?title=User_GPIO_Driver

It should be generic and work on the beagleboard. There isn't anything
gumstix specific in it. I wrote it before the /sys interface existed.
I think it's slightly more efficient as there are fewer kernel to user
transitions, but that's just a gues, not based on any profiling.

I've been using these two links as reference to access gpios (even got
the GPT module working with it) :

http://linuxjunk.blogspot.com/2009/01/beagleboard-gpio-input-driverless.html
http://x4350.blogspot.com/2011/01/digital-io-on-beagleboard-using-gpio.html

They do not use drivers to access gpios, but gives you excellent
options.
Another reference needed will be the 'Technical reference manual' of
the processor (for config values).

The above links directly work with registers in the processor.
Userspace is not supposed to work there.

I don't need to use gpio's much so i'll skip on the driver.
Simple file i/o's is enough.

Although the concept of GPIOLib for userspace is excellent.
It should be included in the kernel.