Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A20 => Topic started by: andyw on September 17, 2020, 04:51:23 PM

Title: A20 buster GPIO export in C
Post by: andyw on September 17, 2020, 04:51:23 PM
I can export GPIO's and do what I want quite easily -

echo 244 > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio244/direction
echo "rising" > /sys/class/gpio/gpio244/edge

But if I try to do it in C -

#define SYSFS_GPIO_DIR "/sys/class/gpio"
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);

fd returns -1

Please could somebody tell me what I am doing wrong - does a kernel module need to be loaded first?

Thanks

Andy.
Title: Re: A20 buster GPIO export in C
Post by: martinayotte on September 17, 2020, 05:00:30 PM
What your code do is kind of non-sense :
fd = open("/sys/class/gpio" "/export", O_WRONLY);
You need to provide proper path into a single string ...
Title: Re: A20 buster GPIO export in C
Post by: andyw on September 17, 2020, 05:14:27 PM
The code was copied from an example

#define SYSFS_GPIO_DIR "/sys/class/gpio"
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);

If you change it to

fd = open("/sys/class/gpio/export", O_WRONLY);

The result is the same.
Title: Re: A20 buster GPIO export in C
Post by: JohnS on September 17, 2020, 08:43:21 PM
You're doing it all as root?

John
Title: Re: A20 buster GPIO export in C
Post by: andyw on September 18, 2020, 09:02:11 AM
No, I am doing it over VisualGDB as olimex login.

Probably the problem!

Title: Re: A20 buster GPIO export in C
Post by: JohnS on September 18, 2020, 12:22:28 PM
Look at the permissions on those files!

You can also look at errno :)

John
Title: Re: A20 buster GPIO export in C
Post by: andyw on September 18, 2020, 01:09:54 PM
Was easiest to change the file permissions ;)

Should do this more often as keep getting a bit rusty!

Thanks!

Andy.