dd17
Posts: 5
Joined: Sat Apr 23, 2016 5:41 pm

Read GPIO without going to power user mode

Sat Apr 30, 2016 6:08 am

Hi,

I want to read the GPIO port in my 'C' program.

I have seen that there are some functions provided by WiringPi but in order to run the code, I need to use power user mode.

Could anyone please tell me if there is some alternate way of reading the GPIO ports in 'C' without any need of going to power user mode.


Thanks

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 6:20 am

dd17 wrote: I want to read the GPIO port in my 'C' program.
ok, so I presume that you are a C programmer, or shortly plan to be one... good !
dd17 wrote: I have seen that there are some functions provided by WiringPi but in order to run the code, I need to use power user mode.
What do you mean by power user mode? Are you suggesting that you want to be a casual software engineer using C to access the hardware engineering of the RPi (using C) but that you do not want to be a 'power' user? Or, am I misunderstanding you (its happened before) :oops:

dd17 wrote: Could anyone please tell me if there is some alternate way of reading the GPIO ports in 'C' without any need of going to power user mode.
Well, when Ptolemy asked Euclid for an easier way to learn Geometry Euclid replied, "μή εἶναι βασιλικήν ἀτραπόν ἐπί γεωμετρίαν" ehem, "There is no royal road to geometry!"

And I will tell you, there is no way to use C without being a power user... so get a good book and ask lots of questions right here. :mrgreen:


:geek:

Edit: on the other hand, maybe you are referring to super user mode , sudo ?

That's a horse of another color...

Did you mean super user? sudo ?


:ugeek:
marcus
:ugeek:

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 6:30 am

By "power user" I expect you mean "root" or "super user".

Rootless GPIO access has been possible for a while now, so as a first step check that you have an up to date versions of Raspian and the GPIO libraries you are using installed.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 6:35 am

dd17 wrote:Could anyone please tell me if there is some alternate way of reading the GPIO ports in 'C' without any need of going to power user mode.
Yes.

If you upgrade to the latest version of Wiring Pi, and make sure your Raspbian install is up to date (sudo apt-get update && sudo -y apt-get upgrade, then reboot), you should be able to use the GPIO without needing sudo.

That will use the new(ish) /dev/gpiomem interface, instead of the old /dev/mem one, and doesn't need to have root permissions.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 6:40 am

rpdom wrote: If you upgrade to the latest version of Wiring Pi, and make sure your Raspbian install is up to date (sudo apt-get update && sudo -y apt-get upgrade, then reboot), you should be able to use the GPIO without needing sudo.
rpdom, I'm noticing that many recommend sudo rpi-update and often it is not included ; am I correct that the firmware is updated with rpi-update? Also, is a firmware update not required if trying to read the GPIO pin block without sudo?

Thanks.

PS I guess I'm still confused about when the each of the following are 'required'/ MCM recommends all three every time.

sudo apt-get update
sudo apt-get upgrade
sudo rpi-update
marcus
:ugeek:

User avatar
Cancelor
Posts: 776
Joined: Wed Aug 28, 2013 4:09 pm
Location: UK

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 7:31 am

Take a look here >>> http://www.raspberrypi.org/forums/viewt ... 63#p441963

ShiftPlusOne went on to say ......
apt-get upgrade WILL update your firmware to the latest stable version, and that MAY conflict with rpi-update. If you see something on the internet that says otherwise, it's probably outdated information.
Can't find the thread you want? Try googling : YourSearchHere site:raspberrypi.org

User avatar
rpdom
Posts: 17173
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 7:36 am

Basically, you shouldn't run sudo rpi-update unless someone who works for/with the RPF like 6x9 tells you to. Usually it will be used to fix a specific bug that you have found in an update or with a new piece of hardware.

In 99.9% of cases a straight apt-get update and upgrade is sufficient.

I haven't run rpi-update since the early days of the Pi, back when it was the only way to update the firmware/bootloader.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 7:47 am

rpdom wrote:Basically, you shouldn't run sudo rpi-update unless someone who works for/with the RPF like 6x9 tells you to. Usually it will be used to fix a specific bug that you have found in an update or with a new piece of hardware.

In 99.9% of cases a straight apt-get update and upgrade is sufficient.

I haven't run rpi-update since the early days of the Pi, back when it was the only way to update the firmware/bootloader.
Thank yous guys :o for helping me with that ! ... and MCM Element14 needs to update their printed materials which they ship with the PI boards (obviously I ordered mine from the U.S. site; manufactured in PRC) and each of them come with a booklet and a small slip of paper recommending the sudo rpi-update. Thanks again. :)
marcus
:ugeek:

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 7:56 am

As said if all you want to do is manipulate the GPIO without having root privileges use /dev/gpiomem rather than /dev/mem. /dev/gpiomem was created for that very reason.

For a fairly minimal code example see http://abyz.co.uk/rpi/pigpio/examples.h ... _tiny_gpio

(The run instructions in the code says to use sudo, that is a typo which I keep forgetting to correct).

Now corrected.
Last edited by joan on Sat Apr 30, 2016 10:31 am, edited 1 time in total.

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 8:35 am

joan wrote:
For a fairly minimal code example see http://abyz.co.uk/rpi/pigpio/examples.h ... _tiny_gpio
~very nice!

:geek:
marcus
:ugeek:

dd17
Posts: 5
Joined: Sat Apr 23, 2016 5:41 pm

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 8:56 am

joan wrote:As said if all you want to do is manipulate the GPIO without having root privileges use /dev/gpiomem rather than /dev/mem. /dev/gpiomem was created for that very reason.

For a fairly minimal code example see http://abyz.co.uk/rpi/pigpio/examples.h ... _tiny_gpio

(The run instructions in the code says to use sudo, that is a typo which I keep forgetting to correct).
Hi,

Thanks a lot for the link. It was what I was looking for. :)

dd17
Posts: 5
Joined: Sat Apr 23, 2016 5:41 pm

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 9:00 am

I used "power user mode" instead of "super user mode" which created all this fuss.
I will take care of using correct words in my further reply. Then Ptolemy and Euclid might be happy ;)

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 10:18 am

Read this: http://wiringpi.com/wiringpi-update-to-2-29/

You DO NOT need sudo for most regular functions with WiringPI. You simply set and environment variable export WIRINGPI_GPIOMEM=1 and WiringPi does some non-privileged magic.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
jojopi
Posts: 3270
Joined: Tue Oct 11, 2011 8:38 pm

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 10:44 am

As I understand it, the only reasons to avoid gpiomem are if you want to run on an old kernel or image, or your program needs certain features such as hardware PWM.

Since it is your program, not the user, that knows what features it needs, I would be inclined to set the envariable in the source code. At the top:

Code: Select all

#define _POSIX_C_SOURCE 200112L
#include <stdlib.h>
Then before the call to WiringPiSetupGpio():

Code: Select all

    setenv("WIRINGPI_GPIOMEM", "1", 1);

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: Read GPIO without going to power user mode

Sat Apr 30, 2016 2:10 pm

DougieLawson wrote:Read this: http://wiringpi.com/wiringpi-update-to-2-29/

You DO NOT need sudo for most regular functions with WiringPI. You simply set and environment variable export WIRINGPI_GPIOMEM=1 and WiringPi does some non-privileged magic.
about wiringPi, as the linked information is very hard to understand:

does one have to type at console terminal window
export WIRINGPI_GPIOMEM=1
at either new Raspi start or is there a raspi-config file to write it to?
or is that stored forever, if once having been typed?

and is that line all which has to be done, or are there more things additionally?
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

Return to “C/C++”