Can all the D0-D7 pins be read with a single read?

I have an application where 6 lines from sensors come into the Photon’s “D” pins. On the Arduino, you could access a register called “PIND” (IIRC) and that would read all 8 pins and return a byte. Does the Photon have an equivalent capability?

I quickly glanced at the link @ScruffR suggested in a past post – “Understanding the STM32F0’s GPIO part 1” – and it looks like the chip has this capability, but is it available to the software? And what’s the mapping? Is this connected to HAL stuff by any chance. I see references here and there to HAL but I haven’t found any doc yet. (Hey, I’m new! I’m sure there’s stuff I haven’t found yet.)

As far as I know, you can’t do it with a single read, because the “D” pins are not all connected to the same port. D0 through D4 are on GPIO port B and D5 through D7 are on port A (on the Photon). You can get a single byte of data representing D0 through D7 (D0 most significant digit) by reading both ports, masking out the parts you don’t want, and or’ing them together.

int dPinValues = (GPIOB -> IDR & 0B0000000011111000) | (GPIOA -> IDR & 0B1110000000000000) >> 13;

3 Likes

@randyenger,

Why don’t you use pinReadFast. It take 25 nano seconds or so, so 8 reads will be about 220 nano seconds.
You find documentation here: https://docs.particle.io/reference/firmware/photon/#pinreadfast-

@marcus, pinReadFast() internally uses the IDR of the ports, so you would perform eight reads and multiple binary operations vs. two reads and two binary operations.

@randyenger, you can find the pin mapping here
https://docs.particle.io/datasheets/photon-datasheet/#pin-out-diagrams

@randyenger, pinReadFast() reads the port associated with the specified GPIO pin using the IDR register. You could read multiple pins at the same time from a given port using the IDR and the correct bit mask. Then, you have to read the second port and put all the bits together. As @ScruffR points out, using pinReadFast() is the most effective way (short of having the pins on a single port). Besides, since you can read each pin very fast, you can handle each sensor input without having to logically mask each port bit. :smile:

First, many thanks for the quick replies. I now know some areas to read that I just glossed over before. And what’s already been mentioned itself is very helpful.

Second, the definitions of things like GPIOA and D7 come from somewhere, but when I tried the particle-offline-compiler, they weren’t automatically included/loaded, so things don’t compile. Where does one get these from?

-Randy

You should #include "application.h" to get the definitions in place.

Ah! Thanks. Well, hmm, tried that and I’m still getting “D7 undefined” and “unknown type name TCPServer” … But you’ve given me some ideas on things to research, so let me do that before taking up any more of your time.

-Randy