Legal Law

SPI bus: theory and implementation

SPI stands for “Serial to Peripheral Interface”, and is a hardware and firmware communications protocol developed by Motorola and subsequently adopted by all. The SPI bus is used only on the PCB. Surely some of you will ask yourselves: “Why is the SPI bus used only on the PCB? What prevents us from using it outside the PCB area?” The SPI Bus was specially designed to exchange data between various IC chips, at very high speeds; say, at 180 MHz or even higher. Due to this high-speed aspect, the bus lines cannot be too long, because their reactance gets too high, and the bus becomes unusable. However, if you wish, you can use the SPI bus outside the PCB at low speeds, but this is not very practical: the SPI bus requires 3 or 4 communication lines, which is too much, compared to 1 or 2 lines. that are normally needed to communicate efficiently with field devices located outside the PCB.

Anyway, on the printed circuit board, the SPI bus is very good, because we can practically connect as many integrated circuits (or devices) as we want to the bus. Forgive me for not providing a picture of the SPI Bus, but rest assured, you don’t need one: the SPI Bus is so simple that you’ll understand everything with words.

The next question is: “Why is this SPI Bus particularly useful?” In addition to exchanging data between multiple IC chips, the SPI bus is a method of multiplying microcontroller pins. In other words, if you have a small 8-pin microcontroller, you could control a few hundred digital inputs and outputs with that little monster. This is impressive, and I am sure that many doubt my words. Let’s explain this.

The SPI bus contains three lines and can be on any pin of the general I/O controller. These bus lines are: clock, data input and data output. Also, each IC connected to the SPI bus needs an individual drive line. Things work like this: Suppose we have four devices, A, B, C and D; all of them are connected to the SPI bus lines, and the bus itself is connected to seven controller pins, that is 3 bus lines plus 4 enable lines. When we want to send a message to device C, we first enable its Enable line, then we send the message serially, one bit at a time. At the same time, devices A, B and D do exactly nothing because they are not enabled.

The beauty of the SPI bus is that it is synchronous, which means that when the controller sends the message to an IC, it can also receive data from that IC at the same time. This particular aspect of the SPI protocol is particularly suitable for microcontroller-to-microcontroller communications.

Now, we have seen a small 8-pin microcontroller that can control 4 devices (IC) using 7 pins. Considering that a device of type A, B, C or D can have eight or even sixteen I/O ports, this is still far from the hundreds of I/O that I promised you. The next beautiful thing about the SPI bus is: one device IC can be serialized with many more of the same type! For example, we could have B1, B2, B3, B4, B5, etc. All B# type ICs are serialized together and require only 4 microcontroller pins to work; the Enable line is common to all of them. Next, we can use each type A, B, C, and D device as a group of dozens of similar ICs.

The enable speed of each I/O port on the SPI Bus is slower, by multiplying the microcontroller pins, but always keep in mind that field I/O devices don’t necessarily need speeds of, say 1000 triggers. ON/OFF per second each, simply because most of them can’t handle that speed. However, there are a few very clever firmware techniques like “barrel change” type of functions, which allow us to keep high-speed messages on the SPI bus, even if we have hundreds of I/O. At the same time, the “barrel change” functions allow for better time management within the microcontroller so it has more time to execute other tasks – makes sense to me! To conclude, I think it is now clear that we can build hundreds of efficient I/O lines on a small 8-pin controller.

Beyond this general presentation of the SPI bus, you should be aware that almost all integrated circuits implement the SPI protocol in a particular way. For practical and detailed applications, I suggest you visit my home site on Corollary Theorems. There you’ll find a good tutorial book on working with hardware, firmware, including “barrel change” type of functions, and software design, in general, and some nice and practical implementations of the SPI bus in particular. .

Many microcontrollers have built-in SPI bus hardware modules, but I’ve never really cared much about using them. What I do, I always design, on the PCB and for a microcontroller, one, two or more custom SPI buses, because my custom implementations are much more flexible. Also, the practical implementation of a custom SPI bus, both in hardware and firmware, is really simple – trust me!