How to reset the GPIO bus in C

Feb 15, 2018 5 Replies

Hello Group Members,



I am a new user/programmer for the Raspberry Pi. I have found a pwm program in python that dims and brights an led. I have also found a program in C that does the same thing - when run as sudo.



When I exit the C program, the led stays lit. I have a program in python that clears up the bus using GPIO.cleanup()



This works fine, and turns the led off, etc.



I am trying to write/find an equivalent program in C that will cleanup the gpio the same way.



Any tips or links will help



Thanks



ewholz


As I recall it is all done by manipulating files under /sys/class/gpio/. My rough recollection is you find a directory named for you GPIO chip, look for an "export" file in there and then tell the system which pins you want for what (eg, input or output) by writing to that file. Thereupon the system creates a new file to read/write to that connects with that pin. The cleanup step involves writing to the "unexport" file.

There are standard Unix file permission issues involved in reading / writing to the /sys/... files. Check the group ownership and your group membership.

The are some finnicky things like the pin numbers are completely different in different places. The pin number on the board is not the pin number for the export/unexport file. And the pin numbers in the python libraries are not the same as the pin numbers in either (IIRC).

I loathe python, so I hate that all of the GPIO guides seem to be written using the python libraries.

Elijah

------ did some Perl GPIO stuff a few years ago

You could use Tcl (either by itself or from C) to do this. You would want to use the piio package for Tcl. There is a meta-package being built, TclForPi that will pull things together and make it even easier.

+----------------------------------------------------------------------+ | Gerald W. Lester, President, KNG Consulting LLC | | Email: Gerald.Lester@kng-consulting.net | +----------------------------------------------------------------------+

formatting link

The biggest threat to humanity comes from socialism, which has utterly diverted our attention away from what really matters to our existential survival, to indulging in navel gazing and faux moral investigations into what the world ought to be, whilst we fail utterly to deal with what it actually is.

On exit, turn GPIO's you used as outputs back into inputs, and then unexport all the GPIOs you used.

I setup an atexit function to do this in my C programs.

I also have a -c (clear) option which does only this, and can be used to clean up if the program crashed and didn't get to run the atexit functions last time.

Andrew Gabriel [email address is not usable -- followup in the newsgroup]

Which C library are you using?

If it's wiringPi, then simply change the pin mode to input before your program ends.

-Gordon

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required