Phil_R
Posts: 5
Joined: Sun Jul 29, 2012 11:21 pm

write to GPIO in parallel

Tue Nov 27, 2012 11:37 pm

I'm currently using quick2wire to write to individual pins on the GPIO... but want to set multiple pins high or low at once.

Is it possible somehow (using quick2wire.gpio or not) to write a byte to 8 pins at a time?

(I gather it can be done using C, but I'm learning python and I'm not too keen to learn another language at the same time!)

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

Re: write to GPIO in parallel

Tue Nov 27, 2012 11:48 pm

Not without a C library.

texy
Forum Moderator
Forum Moderator
Posts: 5160
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: write to GPIO in parallel

Wed Nov 28, 2012 1:13 pm

Phil_R wrote:I'm currently using quick2wire to write to individual pins on the GPIO... but want to set multiple pins high or low at once.

Is it possible somehow (using quick2wire.gpio or not) to write a byte to 8 pins at a time?

(I gather it can be done using C, but I'm learning python and I'm not too keen to learn another language at the same time!)
Why do you need it to be 'instantaneous' - isn't a few microseconds between pins quick enough?
You could always implement a buffer, and use a spare GPIO line to enable the outputs (all at the same time as it were)?

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

Phil_R
Posts: 5
Joined: Sun Jul 29, 2012 11:21 pm

Re: write to GPIO in parallel

Wed Nov 28, 2012 2:23 pm

Thanks for the replies

It's not so much a timing thing, as trying to make the code simpler.

Rather than reading from a stream of data and saying "here's the data for pin 13, send it to pin 13. Here's the data for pin 7, send it to pin 7" I was hoping to say "send this data to the pins. Send this data to the pins"

I guess inelegant code is preferable to having to learn C any time soon (though I will see what a C library is).

davidmam
Posts: 101
Joined: Tue Dec 06, 2011 4:13 pm

Re: write to GPIO in parallel

Wed Nov 28, 2012 11:13 pm

You could wrap the GPIO pin IO in a function that takes a bit string corresponding to the pins in question. Might have to be a bit fancy and include an element to include an indication of which bits should change.

As a simple example assume 8 pins.

PIN1HIGH= 1000000010000000
PIN1LOW= 1000000000000000
PIN2HIGH= 0100000001000000
PIN2LOW= 0!00000000000000

and so on

These can be set as constants.

Then you can call something like

setGPIO(PIN1LOW|PIN2HIGH)

which would pass the value 1100000001000000 indicating that bits 1 and 2 from the second half of the bitstring should be considered

if bitstring & PIN1HIGH==PIN1HIGH:
set pin 1 high

Wrap it all in a single function/class and make your code very simple and readable. There will be a defined order for setting outputs. If you want instant and simultaneous setting then use a SIPO chip where you can toggle in the desired output, then latch all outputs at once.

vidmantas
Posts: 1
Joined: Sun Apr 09, 2017 11:08 pm

Re: write to GPIO in parallel

Sun Apr 09, 2017 11:21 pm

I had this issue,
i made restart relays with mutliplexer chip, which uses only 3 pins to restart one of 7 relays, but problem was that setting pins sequentially was activating for very short time another relays, so i made little adjustment on doWrite function in gpio/gpio.c file

now i can write to multiple pins at once, it happens very fast, other relays stays in peace

gpio write multiple {number of pins} {pin index} {1|0}
exmpl. set high 10,11,12 pins: gpio write multiple 3 10 1 11 1 12 1



https://pastebin.com/5fgw7f1K
tested on Odroid C2
Vidmantas Norkus

Danilk
Posts: 108
Joined: Wed Feb 01, 2017 7:47 pm
Location: Russia, St-Peterburg

Re: write to GPIO in parallel

Mon Apr 10, 2017 8:17 am

using mux as OE for 6 ADCn chips on channels=mux outputs
ch1=0000 0010, ch2=0000 0100…
A,B,C = BCM outputs

Code: Select all

A=21
B=20
C=16
Mux=(C,B,A)
#---------------------
def SetChannel(Ch):
        X=Ch>>2
        Y=(Ch>>1) & 1
        Z=Ch & 1
        GPIO.output(Mux,(X,Y,Z))
Last edited by Danilk on Mon Apr 10, 2017 9:36 am, edited 1 time in total.

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

Re: write to GPIO in parallel

Mon Apr 10, 2017 8:24 am

The above are just variations on the theme of write GPIO x, write GPIO y, write GPIO z in sequence. They are not writing in parallel.

You can set multiple GPIO in parallel and you can clear multiple GPIO in parallel, but you need to write directly to the GPIO set/clear registers (as in set_bank_1 and clear_bank_1).

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

Re: write to GPIO in parallel

Mon Apr 10, 2017 8:49 am

I've not tried this, but google just found this for me ...

http://python-periphery.readthedocs.io/ ... index.html

It has a Memory Mapped IO feature http://python-periphery.readthedocs.io/ ... /mmio.html which should allow you to read and write directly to the GPIO control registers.

Details of port addresses and bit assignments in this :
https://www.raspberrypi.org/wp-content/ ... herals.pdf


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
PeterO
Posts: 5066
Joined: Sun Jul 22, 2012 4:14 pm

Re: write to GPIO in parallel

Mon Apr 10, 2017 3:58 pm

Ok, I've just tested this and it works.
But if you want to use it "root-less" you need to change referencies to "/dev/mem" to "/dev/gpiomem" in the library source before you install it.

I installed it like this

Code: Select all

git clone https://github.com/vsergeev/python-periphery.git
cd python-periphery

**** Need to edit mmio.py here **************

python setup.py install

Then I put a couple of LED on GPIO 5 and 6, and ran this code..

Code: Select all

#!/usr/bin/python

from periphery import MMIO
import time

# Open the GPIO register page
# This is the right address for a PI3
gpio_mmio = MMIO(0x3F200000, 0x1000)


# Read the current pin input/output states
bits = gpio_mmio.read32(0x00);
print "bits = ",bits


#Clear the bits for pins 5 and 6 
bits &= ~( (7<<15) | (7<<18) )
print "bits = ",bits
gpio_mmio.write32(0x00,bits);

#Set pins 5 and 6 to outputs
bits |= (1<<15 | 1<<18)
print "bits = ",bits
gpio_mmio.write32(0x00,bits);

for num in range(1,20):
# Turn pins 5 and 6 ON   (0x60 = 1<<5 + 1<<6)
    gpio_mmio.write32(0x1C,0x60)
    time.sleep(1)
# Turn pins 5 and 6 OFF
    gpio_mmio.write32(0x28,0x60)
    time.sleep(1)

# Tidy up the librayy handle
gpio_mmio.close()
The document at https://www.raspberrypi.org/wp-content/ ... herals.pdf
Explains the port addresses and bit assignments for controling the pin directions (tables 6.3,6.4,6.5) and setting or clearing the pins (table 6.1)

HTH

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
joan
Posts: 14348
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: write to GPIO in parallel

Mon Apr 10, 2017 5:26 pm

PeterO wrote:Ok, I've just tested this and it works.
...
It's just doing the same as the set/clear bank calls I mentioned above.

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

Re: write to GPIO in parallel

Mon Apr 10, 2017 5:32 pm

joan wrote:
PeterO wrote:Ok, I've just tested this and it works.
...
It's just doing the same as the set/clear bank calls I mentioned above.
There is not even a hint that they were function calls ! I know what the set/clear banks are so I didn't follow the links.

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

Return to “Python”