Running multiple programs accessing GPIO simultaneously?

1,216 views
Skip to first unread message

pjc123

unread,
Dec 3, 2012, 5:31:46 PM12/3/12
to bcm...@googlegroups.com

Mike,

 

Question. There may be a better way to do this, but here goes. I am using your library and I have a program that polls a switch every 3 seconds to check if it has been pressed, at which point I run the Linux shutdown command. The reason I need this is because I am running the pi headless, and I want to be able to reboot in case I get myself in a situation where for instance the wifi signal is lost, or there is a system lockup, etc, etc, because I have no keyboard or monitor attached. I would like to have that program run continuously, so I will set it up in the init scripts. Now, I want to run a second program to do all the input/output GPIO interaction I need as part of the project I am working on, and this would use different GPIO pins. Is it possible to run two programs at the same time to access the GPIO ports? If so, do I perform a single !bcm2835_init() in just one program? What about bcm2835_close() or setting ports as inputs and outputs, or other commands? Ideally, I would like to do all this in a single program, but I suspect interrupts of some sort would be required.

 

Thanks,

 

PJC123

Mike McCauley

unread,
Dec 3, 2012, 8:20:12 PM12/3/12
to bcm...@googlegroups.com
Hello,

Hmmm, I expect you might see problems (rarely) if you have multiple processes
doing GPIO access at the same time. There is the possibility that 2 processes
might overlap.
But, Im bot sure and Ive never tried it.
You could use a semaphore to control access to the GPIO from multiple
processes?

My bcm library does not support interrupts at all.

Cheers.
--
Mike McCauley mi...@open.com.au
Open System Consultants Pty. Ltd
9 Bulbul Place Currumbin Waters QLD 4223 Australia http://www.open.com.au
Phone +61 7 5598-7474 Fax +61 7 5598-7070

Radiator: the most portable, flexible and configurable RADIUS server
anywhere. SQL, proxy, DBM, files, LDAP, NIS+, password, NT, Emerald,
Platypus, Freeside, TACACS+, PAM, external, Active Directory, EAP, TLS,
TTLS, PEAP, TNC, WiMAX, RSA, Vasco, Yubikey, MOTP, HOTP, TOTP,
DIAMETER etc. Full source on Unix, Windows, MacOSX, Solaris, VMS, NetWare etc.

Message has been deleted

pjc123

unread,
Dec 4, 2012, 7:17:34 AM12/4/12
to bcm...@googlegroups.com

 

 

Mike,
 
     Thanks;  I suspected that might cause a problem.
 
pjc123 

Arjan

unread,
Dec 4, 2012, 10:16:20 AM12/4/12
to bcm...@googlegroups.com
Hi,

It seems to me that it would be doable running multiple programs accessing GPIO pins simultaneously.
You can have a bcm2835_init / bcm2835_close for each program.
When your programs are accessing the same GPIO pins, you could work with semaphores.

Regards, Arjan

pjc123

unread,
Dec 5, 2012, 6:48:18 AM12/5/12
to bcm...@googlegroups.com
Am I reading this as stating that I do not need semaphores as long as I am not accessing the same exact GPIO pins in each program, because that is my case, as I will be accessing different pins in each program?  I have never worked with semaphores, but if I do need them for every situation, then I will Google around for some example programs.

Arjan

unread,
Dec 5, 2012, 7:35:55 AM12/5/12
to bcm...@googlegroups.com
Correct. When there is no pin state dependency across different programs (or threads) , then there is no need to use semaphores.

Op woensdag 5 december 2012 12:48:18 UTC+1 schreef pjc123 het volgende:

pjc123

unread,
Dec 5, 2012, 6:36:17 PM12/5/12
to bcm...@googlegroups.com

Thanks to everyone.  I will give it a try when I get the chance.

pjc123

unread,
Mar 8, 2013, 12:25:01 PM3/8/13
to bcm...@googlegroups.com
So, it has been a while, but I finally fabricated my project and was able to give this a test today, and just wanted to report back that it works great.  I only define and access the reboot switch GPIO port in the first executable and run it at boot in the background using rc.local (Later on I might move it to the init scripts).  It will indeed still work if I push the reset button while in the middle of accessing the GPIO ports in my second program, initiating a clean shutdown.  The only minor problem is that the second program isn't exiting very cleanly for obvious reasons, so i hope that does not lead to some sort of corruption.  I suspect that there must be a way around that though.
Reply all
Reply to author
Forward
0 new messages