Scratch
Posts: 13
Joined: Sun Oct 07, 2012 2:40 am

GPIO safe use

Sun Nov 18, 2012 6:37 pm

Hi all,

In setting a GPIO as an input as in the first example here: http://elinux.org/RPi_Low-level_peripherals, will this then leave the GPIO as an input even after the registers have been unmapped?

If so is there a place I can put a script to run a C program that will set all the GPIOs to high impedance at very early start up (before any of them output anything) so I wont damage external devices.

thanks,
S

Scratch
Posts: 13
Joined: Sun Oct 07, 2012 2:40 am

Re: GPIO safe use

Sun Nov 18, 2012 7:38 pm

Sorry I don't mean to bump, but the forum wont let me edit...?

I can use a script to set inputs, if I could disable the pull ups in a script too that would be nicer than executing another program I think, does anyone know how this is done?

Is there an issue (other than being overwritten) with putting something like this in /etc/rc.sysinit? would that achieve the desired effect?

TBH anything that makes the inputs go straight from power off to high impedance will make me very happy.

thanks!

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: GPIO safe use

Tue Nov 20, 2012 5:25 pm

Scratch wrote:Sorry I don't mean to bump, but the forum wont let me edit...?

I can use a script to set inputs, if I could disable the pull ups in a script too that would be nicer than executing another program I think, does anyone know how this is done?

Is there an issue (other than being overwritten) with putting something like this in /etc/rc.sysinit? would that achieve the desired effect?

TBH anything that makes the inputs go straight from power off to high impedance will make me very happy.

thanks!
If you have wiringPi installed, then you can do this in a bash script:

for i in `seq 0 7`; do gpio mode $i in ; gpio mode $i tri; done

They'll stay as inputs until the next program changes them.

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

Scratch
Posts: 13
Joined: Sun Oct 07, 2012 2:40 am

Re: GPIO safe use

Tue Nov 20, 2012 8:06 pm

Thanks for the reply,

Unfortunately I am trying to avoid additional libraries, pretty much because I want to learn about how things work at a low level, and have already written the rest of my project without them.

Also is there a place I can put this script where it will be executed before anything is outputted?

Thanks again

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: GPIO safe use

Tue Nov 20, 2012 8:12 pm

Scratch wrote:Thanks for the reply,

Unfortunately I am trying to avoid additional libraries, pretty much because I want to learn about how things work at a low level, and have already written the rest of my project without them.

Also is there a place I can put this script where it will be executed before anything is outputted?

Thanks again
'gpio' is a program (although it uses the wiringPi library), so in that sense it stands alone...

But if you want to write your own GPIO access routines, there's plenty online to help you - including the source code to wiringPi, and many others.

You put the script in /etc/init.d and arrange it to be started at the right place/time - lookup all the Debian admin stuff for that - it's fairly straighforwards.

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

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

Re: GPIO safe use

Tue Nov 20, 2012 8:13 pm

Won't that vary by distribution? The safest way (still not necessarily safe) would be to have your own boot image.

User avatar
gordon@drogon.net
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: GPIO safe use

Tue Nov 20, 2012 8:20 pm

joan wrote:Won't that vary by distribution? The safest way (still not necessarily safe) would be to have your own boot image.
You mean there's a distribution other than Debian? ;-)

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

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

Re: GPIO safe use

Tue Nov 20, 2012 8:32 pm

gordon@drogon.net wrote:
joan wrote:Won't that vary by distribution? The safest way (still not necessarily safe) would be to have your own boot image.
You mean there's a distribution other than Debian? ;-)

-Gordon
...and every one will have its opinion of the best state for the gpios.

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

Re: GPIO safe use

Tue Nov 20, 2012 8:38 pm

Scratch wrote:Also is there a place I can put this script where it will be executed before anything is outputted?
No. Your plan to tri-state the GPIOs as early as possible can not work because your settings will remain only until the next program tries to configure whichever pins you are using.

To avoid pins being configured as outputs you need to choose them carefully and avoid loading any drivers or software that will drive them. And to withstand the weak (~50K) on-chip pull-ups and pull-downs that are enabled during reset you need to make your external hardware safe in the right direction for the pins you are using, or include stronger (say 10K) pull resistors to defeat the internal ones.

Basically there is no simple answer to making all GPIOs permanently safe. Start by deciding how many you need and what pull behaviour you can tolerate. In general, pins that have no special function support are much easier to handle than say the UART TX/RX.

Scratch
Posts: 13
Joined: Sun Oct 07, 2012 2:40 am

Re: GPIO safe use

Tue Nov 20, 2012 8:56 pm

jojopi wrote:No. Your plan to tri-state the GPIOs as early as possible can not work because your settings will remain only until the next program tries to configure whichever pins you are using.
That was what I was afraid of... thanks!

Return to “Advanced users”