matt.b
Posts: 93
Joined: Fri Oct 19, 2012 7:23 am

Setup the pull up resistor in the shell?

Wed Jul 03, 2013 1:06 pm

Hi,

is there any possibility to setup the pull pu resistor of a gpio pin using the shell? Maybe the same way like changing a pins value.

Thanks
matt

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 1:13 pm

I'm not 100% sure what you're asking but you can work the GPIO in bash scripts no problem, although I would prefer to do in Python myself - but that is just me;
Check out; http://www.raspberrypi.org/phpBB3//view ... 34&t=16621

matt.b
Posts: 93
Joined: Fri Oct 19, 2012 7:23 am

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 1:17 pm

Thanks,

but i can't find a hint how to setup the pull up resistors!?
Here's the description how to use the pins:
http://elinux.org/RPi_Low-level_periphe ... ing_system
But i want to use the resistors additionally.

Thanks

User avatar
Davespice
Forum Moderator
Forum Moderator
Posts: 1662
Joined: Fri Oct 14, 2011 8:06 pm
Location: The Netherlands
Contact: Twitter

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 1:35 pm

I do remember an Article in MagPi issue 2 about this;
http://issuu.com/themagpi/docs/the_magpi_issue_2_final
See pages 10 to 14. I think they used Python though.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 2:35 pm

matt.b wrote:Hi,

is there any possibility to setup the pull pu resistor of a gpio pin using the shell? Maybe the same way like changing a pins value.

Thanks
matt
Yes.

If you install wiringPi, then you can use the gpio command to set the pull-up (or pull-down) resistors.

It's not possible to read their state though, so you need to explicitly set them as needed. They are stored in non-volatile memory, so are supposed to remain the same states between power cycles too.

Quick way to get wiringPi (Raspbian)

Code: Select all

  sudo apt-get install git-core # if not have it already
  cd ; git clone git://git.drogon.net/wiringPi
  cd wiringPi
  ./build
  gpio -v # test
  gpio -g readall
then to set pin 17 high:

Code: Select all

  gpio -g mode 17 in
  gpio -g mode 17 up
Once you have wiringPi & the gpio command, you can use that to toggle pin values too rather than echo ...> /sys/class/gpio, etc. e.g.

Code: Select all

  gpio -g mode 18 out
  gpio -g write 18 1
and so on. type man gpio for full details.


-Gordon
--
Gordons projects: https://projects.drogon.net/

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

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 3:26 pm

[email protected] wrote:They are stored in non-volatile memory, so are supposed to remain the same states between power cycles too.
The pull states survive a hardware reset, and they may be retained in "power-down mode when the core is off", whatever that means. They are not non-volatile and do not survive a power cycle. At power on they fall into the states given in table 6-31.

Anyway, firmware disables most pulls early in the boot sequence. So they do not survive a reboot in practice.

matt.b
Posts: 93
Joined: Fri Oct 19, 2012 7:23 am

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 4:24 pm

Thanks for all your help.

I already know wiringPi. But i want to make a programm which doesn't need any other librarys or programms additionally.
I just saw the C example how to control the pull ups on http://elinux.org/RPi_Low-level_periphe ... er_Example.

To check the functionallity i created a C file on my raspberry and copied all the code above the pull up example in it, compiled it and ran it. This works well.

Next step was to include the 8 lines to control the pull ups. But i think therefor at least two #defines is missing. I get the following errors:

Code: Select all

error: "GPIO_PULL" undeclared (first use in this function)
error: "GPIO_PULLCLK" undeclared (first use in this function)
Where can i get the information about the mapping of the pull up registers of the bcm2708 and how i have to define them in the c code?

Thanks a lot!

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 4:38 pm

matt.b wrote:Thanks for all your help.

I already know wiringPi. But i want to make a programm which doesn't need any other librarys or programms additionally.
I just saw the C example how to control the pull ups on http://elinux.org/RPi_Low-level_periphe ... er_Example.

To check the functionallity i created a C file on my raspberry and copied all the code above the pull up example in it, compiled it and ran it. This works well.

Next step was to include the 8 lines to control the pull ups. But i think therefor at least two #defines is missing. I get the following errors:

Code: Select all

error: "GPIO_PULL" undeclared (first use in this function)
error: "GPIO_PULLCLK" undeclared (first use in this function)
Where can i get the information about the mapping of the pull up registers of the bcm2708 and how i have to define them in the c code?

Thanks a lot!
You could read the wiringPi source code..

-Gordon
--
Gordons projects: https://projects.drogon.net/

MrEngman
Posts: 3844
Joined: Fri Feb 03, 2012 2:17 pm
Location: Southampton, UK

Re: Setup the pull up resistor in the shell?

Wed Jul 03, 2013 6:30 pm

Well hardware details of how the pullups/pulldowns are set up are in the document http://www.raspberrypi.org/wp-content/u ... herals.pdf

Now you probably just need to mangle the code to allow you to do what you want.


MrEngman
Simplicity is a prerequisite for reliability. Edsger W. Dijkstra

Please post ALL technical questions on the forum. Please Do Not send private messages.

User avatar
blachanc
Posts: 456
Joined: Sat Jan 26, 2013 5:03 am
Location: Quebec,canada(french)

Re: Setup the pull up resistor in the shell?

Sat Jul 26, 2014 3:03 am

Hi All,

I do not understand why nobody was willing to help the OP enabling a pull up via a shell command.

I honestly do not get why there is so much push back (by lack of collaboration), when it comes to use the pull up as a shell command.
I sure could have used the answer, as it would have saved me 30 minutes of installation on raspbmc. (next time, it will take me 10 minutes)

Context:
On a B+:
I am currently trying to see if we could keep the USB @ high power during reboot. (Via a pull up on GPIO38)
I am doing this under raspbmc, because this is the use model I am trying to validate (remove the ugly hub that bugs my old mother)

Anyway,
I managed to compile wiringpi in raspbmc, and now am proceeding with the trial....

Code: Select all

NOOBS
Offline and network install
Version: 1.3.9
Release date: 2014-07-08
No sure my raspbmc install can be called raspbmc anymore. Maybe I should call it wirebmc :lol:

Code: Select all


git clone git://git.drogon.net/wiringPi
-bash: git: No such file or directory
sudo apt-get install git
Unable to fetch some archives, maybe run apt-get update
sudo apt-get update
sudo apt-get install git
 cd ; git clone git://git.drogon.net/wiringPi
  cd wiringPi
./build
-bash: make: No such file or directory
sudo apt-get install make
sudo apt-get install gcc
./build
SUCCESS....
The experiment results shows the pull up does not survive reboots:

USB at 1.2A during reboot & in other OS (NOT RASPIAN)


Benoit
Autism/Asperger syndrome: what is your score on this quiz?
http://www.raspberrypi.org/forums/viewtopic.php?f=62&t=70191

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 12:47 am

blachanc wrote:I do not understand why nobody was willing to help the OP enabling a pull up via a shell command.

I honestly do not get why there is so much push back (by lack of collaboration), when it comes to use the pull up as a shell command.
You seem to be confusing "setting a high value" on a GPIO pin, with "enabling the pull-up resistor" on a GPIO pin - they're two different things ;)
http://raspi.tv/2013/rpi-gpio-basics-6- ... pull-downs

User avatar
blachanc
Posts: 456
Joined: Sat Jan 26, 2013 5:03 am
Location: Quebec,canada(french)

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 4:47 am

AndrewS wrote:
blachanc wrote:I do not understand why nobody was willing to help the OP enabling a pull up via a shell command.

I honestly do not get why there is so much push back (by lack of collaboration), when it comes to use the pull up as a shell command.
You seem to be confusing "setting a high value" on a GPIO pin, with "enabling the pull-up resistor" on a GPIO pin - they're two different things ;)
http://raspi.tv/2013/rpi-gpio-basics-6- ... pull-downs
hi andrew,

no, I do not beleive I an confusing driving vs pullup, but maybe I am not using gpio the proper way.

it was an experiement (with a b+): I need to set the pullup in the hope that GPIO 38 will remain high during the reboot,
because wathever you drive gpio38 to in the os is lost at reboot (refer to the linked post in my original post to understand
the potential permanent aspec of the pullup).
dom wrote: The 1.2A current limit is controlled by GPIO38. Set it high to enable high current mode.

In theory it can be maintained across reboots using the gpio pull-ups / pull-downs.


thanks
Ben
Autism/Asperger syndrome: what is your score on this quiz?
http://www.raspberrypi.org/forums/viewtopic.php?f=62&t=70191

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

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 6:17 am

blachanc wrote:I do not understand why nobody was willing to help the OP enabling a pull up via a shell command.
WiringPi's gpio command is the best way to control advanced functionality such as pulls, drive strength, and pwm from the shell. To my knowledge there is no alternative that would be easier to install or offer other advantages.

Any good GPIO library also allows you to make a program to set pulls, and of course you can run your own program from the shell. But to write the program without using an existing library you would need to read the datasheet and other resources, and be confident in C.
The experiment results shows the pull up does not survive reboots:
GPIO38 is an output pin. Pulling it up is not going to help if it is being driven low.

First you need to configure the GPU firmware to drive the pin high. If you are using NOOBS, you must configure the firmware that lives in the NOOBS partition, as well as the firmware in the boot partition of the OS you are using.

Then, setting a pull up as well may prevent the pin from floating in the brief period during reset and while the firmware is loading.

Incidentally, a year ago in this thread I said that firmware disables most pulls, so they do not survive a reboot in practice. That is no longer true. For pins that belong to the user, pulls really do survive a reboot now.

I still doubt that a pull on GPIO38 will survive, however. That pin belongs to the firmware, and it is specified as no_pulling in the device tree. A pull will survive during reset, and hopefully for long enough for the firmware to read config.txt.

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

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 8:08 am

On the Pi it's probably important to distinguish between reboots (where the Pi still has power) and power-cycles. I don't think the pull-up/down settings survive power-cycles.

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

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 8:44 am

I would hazard a guess that the PUD values are stored in NVRAM and would therefore be preserved even after a full power-off. The BCM2835 documentation says that they are "maintained even in power-down mode when the core is off", but I'm not sure about full loss of power.

I'm assuming NVRAM (or similar) as the GPPUDCLKn registers need to be cycled relatively slowly to enable the new state.
[edit]
This should be fairly easy to test by toggling one of the available external GPIOs to pull-up, and measure the voltage on it after a power off and restart, then doing the same with pull-down. Try that a few times with different GPIOs to see if the results are consistent. Actually, no need to measure the voltage, just read the state as it will be an input by default.

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

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 9:05 am

jojopi wrote:The pull states survive a hardware reset, and they may be retained in "power-down mode when the core is off", whatever that means. They are not non-volatile and do not survive a power cycle. At power on they fall into the states given in table 6-31.
However, I have noticed that leaving HDMI connected to a powered display can maintain sufficient voltages on the board for the pulls to stay latched indefinitely. When the board is fully unpowered, the pulls expire very quickly.

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

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 9:15 am

I tried again after making sure the firmware is recent.

They are still not preserved over a power-cycle on my Rev.1 board.

I haven't checked on the other board revisions as they are busy.

Code: Select all

mercury:~$ ssh tom
Linux tom 3.12.25+ #700 PREEMPT Thu Jul 24 17:51:46 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jul 27 09:01:38 2014 from mercury.lan
tom ~ $ sudo pigpiod
tom ~ $ for ((i=21;i<26;i++)); do pigs pud $i d; done # pud down
tom ~ $ for ((i=21;i<26;i++)); do pigs r $i; done
0
0
0
0
0
tom ~ $ for ((i=21;i<26;i++)); do pigs pud $i u; done # pud up
tom ~ $ for ((i=21;i<26;i++)); do pigs r $i; done
1
1
1
1
1
tom ~ $ sudo reboot
Connection to tom closed by remote host.
Connection to tom closed.
mercury:~$ ssh tom
Linux tom 3.12.25+ #700 PREEMPT Thu Jul 24 17:51:46 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jul 27 09:01:51 2014 from mercury.lan
tom ~ $ sudo pigpiod
tom ~ $ for ((i=21;i<26;i++)); do pigs r $i; done # still high after reboot
1
1
1
1
1
tom ~ $ off
Connection to tom closed by remote host.
Connection to tom closed.
mercury:~$ # now switch off
mercury:~$ # now switch on
mercury:~$ ssh tom
Linux tom 3.12.25+ #700 PREEMPT Thu Jul 24 17:51:46 BST 2014 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Jul 27 09:05:37 2014 from mercury.lan
tom ~ $ sudo pigpiod
tom ~ $ for ((i=21;i<26;i++)); do pigs r $i; done # but low after power-cycle
0
0
0
0
0
tom ~ $ 

User avatar
blachanc
Posts: 456
Joined: Sat Jan 26, 2013 5:03 am
Location: Quebec,canada(french)

Re: Setup the pull up resistor in the shell?

Sun Jul 27, 2014 3:37 pm

@ jojopi/joan/rpdom

thank for the detailed info, I learned a lot.
Regarding the pull-ups, I really thoug that there was a stand alone utility/program to access the chip registers that could copied versus installed. But now I get it... case closed.

I also invite you to contribute to the original thread that brought me here in the first place.

USB at 1.2A during reboot & in other OS (NOT RASPIAN)

Ben
Autism/Asperger syndrome: what is your score on this quiz?
http://www.raspberrypi.org/forums/viewtopic.php?f=62&t=70191

LenShustek
Posts: 2
Joined: Tue Aug 07, 2012 3:14 am

Re: Setup the pull up resistor in the shell?

Tue Aug 04, 2015 3:33 am

I think this thread got way off track. It's probably too late now, but the answer to the original question,
"is there any possibility to setup the pull up resistor of a gpio pin using the shell?"
is that after you set the direction with a shell command like
echo "in" > /sys/class/gpio/gpio9/direction
you set the internal pullup resistor on by doing this
echo "high" > /sys/class/gpio/gpio9/direction

Then you can read the status of a switch or button connected between the pin and ground like this
read val < /sys/class/gpio/gpio9/value
without needing to add an external pullup resister.

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

Re: Setup the pull up resistor in the shell?

Tue Aug 04, 2015 4:14 am

LenShustek wrote:you set the internal pullup resistor on by doing this
echo "high" > /sys/class/gpio/gpio9/direction
No! Writing "low" or "high" to direction sets the pin as a hard output. If you do that when you wanted an input with weak pull, it will not work. If external hardware short circuits the output driver, it may damage the Pi.

Writing "low" or "high" to direction is to avoid the glitch of first configuring the pin for output, then setting the value separately. If you can find an old thread where someone asked about that problem, it would be a better one to resurrect.

Return to “General discussion”