9

I am just getting started with using the GPIO on the Raspberry Pi for various projects. There are many breakout boards for the RPi and many of those have pins in order to stack boards on top of each other. It seems like the GPIO pins just go straight through and all the boards have the same pins in common. To me this means that all the boards in a stack receive the exact same signals from the RPi and should behave accordingly. This seems like this can produce a lot of errors with commands meant for one board affecting the way another board operates, but apparently this does not happen.

So how does the Raspberry Pi know which board is getting which particular commands? Is there some sort of initialization command that only a particular board recognizes and then starts accepting the rest of the commands?

cspirou
  • 387
  • 1
  • 5
  • 11

3 Answers3

6

Computers use a system called busing to connect multiple peripheral devices to the CPU via the same set of wires. I2C and SPI are examples of busing systems used with the pi GPIO pins, which enable multiple devices on the same physical pins (managed by the internal bus) but using different addresses. Addressing is an abstraction created by the bus system protocol. It is a little bit like how you can have multiple networked applications using the same physical connection to the internet, all running simultaneously.

For example, looking at the RTC Pi Plus, one of the stackable boards from your example link:

enter image description here

Notice on the near side the five connections in a box. These correspond to pins on the other side. They're labelled:

  • 5V = Power, sharable with other devices.
  • GND = Ground, also common.
  • SDA and SCL = I2C bus pins; on the pi that's pins 3 and 5. You can have quite a number of devices on the I2C bus all using these same two pins for communication.
  • SQW = I believe this stands for "square wave" and maybe one of our more electronically knowledgeable members will leave a nice comment explaining its purpose.

In other words, most of the pins aren't used by this board at all. They are just there to allow for stacking. The ones that it does use are all (or mostly, still don't know about SQW) sharable simultaneously with other devices.

The real limit to stacking this way is going to be the amount of power that can be supplied vs. what is consumed, not the number of pins involved. Maybe it would become a limitation if you included additional power supplies, but I think before that you'd have to move the stack outside and start using a ladder to work on it. ;)

goldilocks
  • 60,325
  • 17
  • 117
  • 234
3

You can only have one HAT board so there is no such issue. There are some boards where you can connect more than one but those are not HAT boards.

salmon
  • 126
  • 3
1

So how does the Raspberry Pi know which board is getting which particular commands?

The Pi cannot know. You have to make sure.

The thing is, if you connect multiple extension boards, you will have to take care that signals lines are either not used more than once or implement a busing scheme that can work with multiple devices sharing some lines (as @goldilocks already suggested). To get a feeling how this could be done, I would recommend to read up on the old ISA bus. Something that could be implemented using GPIO as data and address lines (and if one is up for the pain).

Is there some sort of initialization command that only a particular board recognizes and then starts accepting the rest of the commands?

While HATs are out of the question, the identification scheme they use is worth a look. Note that there are no stacked HATs* (per Raspberry Pi Blog: Stackable HATs featured in the specification discussion – but eventually it was thrown out due to the large increase in complexity of autoconfig and potential for user error.). So at any given time only one HAT will be connected, have its EEPROM read out, and made its information available at the device tree.


* It is possible to make stackable hats if they are of the same type and thus do not require multiple and differing identification, e.g. the Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi.
Ghanima
  • 15,958
  • 17
  • 65
  • 125