Is there a way to read more than 1 port pin simultaneously (like a parallel port read) using wiringpi? I can see wiringpi functions to do a single pin read (digitalRead()) - but in my application I need to read 2+ pins at once. I guess I can read the pins directly via GPLEV0/GPLEV1 registers in the broadcom device - but ideally I'd do it via wiringpi.

  • What is the required latency for your application? If a dozen or so milliseconds is okay, you probably don't have to read them in literal parallel. – goldilocks Feb 17 '18 at 21:26
  • 1
    Unfortunately its sub ms (the pulses are 100 - 200ns wide or so) - I'm trying to read the state of one pin when another changes state (and read the one that changes state). I'm intending to use an interrupt from a pin change to initiate the read. From what I have read the latency can be up to 75ns - so just about doable in theory. – Steve R Feb 18 '18 at 18:29

If you look at the wiringPi source you will see there are a couple of likely functions in wiringPi.c.



The associated comment is

 * digitalWriteByte:
 * digitalReadByte:
 *      Pi Specific
 *      Write an 8-bit byte to the first 8 GPIO pins - try to do it as
 *      fast as possible.
 *      However it still needs 2 operations to set the bits, so any external
 *      hardware must not rely on seeing a change as there will be a change 
 *      to set the outputs bits to zero, then another change to set the 1's
 *      Reading is just bit fiddling.
 *      These are wiringPi pin numbers 0..7, or BCM_GPIO pin numbers
 *      17, 18, 22, 23, 24, 24, 4 on a Pi v1 rev 0-3
 *      17, 18, 27, 23, 24, 24, 4 on a Pi v1 rev 3 onwards or B+, 2, 3, zero

You will probably need to experiment.

  • Looking at the code digitalReadByte (and digitalReadByte2) just assemble a byte by 8 independent pin reads - so its not what I'm looking for. – Steve R Feb 18 '18 at 18:38
  • That's not the full story. It uses 8 reads if you are in sysfs mode but not if you are in other modes. Are you using SYS mode? – joan Feb 18 '18 at 18:43
  • Nope - not in SYS mode. Looking at the code in more depth your right - for non WPI_MODE_GPIO_SYS mode its doing a 32bit read and then the loop is assembling 8 output bits from them. – Steve R Feb 18 '18 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.