Page 1 of 1

Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 1:06 pm
by matt.b
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

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 1:13 pm
by Davespice
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

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 1:17 pm
by matt.b
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

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 1:35 pm
by Davespice
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.

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 2:35 pm
by gordon@drogon.net
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

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 3:26 pm
by jojopi
gordon@drogon.net 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.

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 4:24 pm
by matt.b
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!

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 4:38 pm
by gordon@drogon.net
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

Re: Setup the pull up resistor in the shell?

Posted: Wed Jul 03, 2013 6:30 pm
by MrEngman
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

Re: Setup the pull up resistor in the shell?

Posted: Sat Jul 26, 2014 3:03 am
by blachanc
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

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 12:47 am
by AndrewS
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

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 4:47 am
by blachanc
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

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 6:17 am
by jojopi
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.

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 8:08 am
by joan
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.

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 8:44 am
by rpdom
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.

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 9:05 am
by jojopi
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.

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 9:15 am
by joan
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 ~ $ 

Re: Setup the pull up resistor in the shell?

Posted: Sun Jul 27, 2014 3:37 pm
by blachanc
@ 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

Re: Setup the pull up resistor in the shell?

Posted: Tue Aug 04, 2015 3:33 am
by LenShustek
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.

Re: Setup the pull up resistor in the shell?

Posted: Tue Aug 04, 2015 4:14 am
by jojopi
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.