A20 buster GPIO export in C

Started by andyw, September 17, 2020, 04:51:23 PM

Previous topic - Next topic

andyw

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.

martinayotte

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 ...

andyw

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.

JohnS


andyw

No, I am doing it over VisualGDB as olimex login.

Probably the problem!


JohnS

Look at the permissions on those files!

You can also look at errno :)

John

andyw

Was easiest to change the file permissions ;)

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

Thanks!

Andy.