SPI Bus is a way to transfer data between 2 or more digital circuits. There is typically one master circuit that is in charge of initiating transfers. And then there are one or more slaves on the bus. The BrainPad MicroComputer is always the bus master. A slave can be an accelerometer or a display for example.
When a master talks to a slave, it asserts the salve’s CS (Chip Select) pin. Then the data is exchanged between the two sides. The master will clock the data (usually 8 bit) using the CLK clock pin. The data comes out on the MOSI (Master Out Slave In) pin. At the same time, the data is clocked in on the MISO (Master In Slave Out) pin.
SPI hardware implementation is nothing but two shift registers, one clocking data out and one clocking in.
Some slaves only receives data and do it only needs CLK and MOSI. An example would be a display that would only needs to receive information to show on the screen and it does not send any data back.
When accessing SPI, there is always exchange of a byte between a master and salve, and it is happening simultaneously. It is not possible to only send or only receive. This is shown in the SpiByte() function, which takes a byte and returns a byte.
r = SpiByte(5) # Exchange 5 with the slave and place the results in variable r
The chip select is left to the user to handle. Chip select goes low before the transaction and goes high after.
DWrite(0,0) # Select r = SpiByte(5) # Exchange 5 with the slave and place the results in variable r DWrite(0,1) # Unselect
Display Stream Handling
Since using SPI bus with displays is very common, a special functions were added that converts bitmap data on-the-fly. For example, some displays are 16BPP (16 bits per pixel) but due to memory limitation and transfer speed, the graphics engine can be constructed using 4BPP and then a special stream can converts the data internally then send 16BPP to the display.
This is only available in streams when using hosted coding languages, like Python and .NET C#.
Where is the “bus” in SPI bus? Data is delivered down the wire by one as a stream of data. In computer world, this is called a data bus. What other busses are used around us? Think of how a Keyboard is wired to a PC.