Serial Peripheral Interface (SPI) is typically used on external non-volatile memory and graphical displays, where faster data transfer rates and high bandwidth are required. Many sensor devices support SPI in addition to I2C.
SPI shares some characteristics with I2C. It is a synchronous serial interface, which means it relies on a shared clock signal to synchronize data transfer between devices. Because of the shared clock signal, SPI also has a master-slave architecture, where the master controls the triggering of the clock signal and all other connected peripherals are slaves.
Another similarity with I2C is that it supports multiple slaves.
But there are also some differences. While I2C is semi-duplex, SPI supports full-duplex data transfer, meaning the master and slave can simultaneously exchange information. This is achieved by having a wire to transmit from the master to the slaves and another one from the slaves to the master, which makes SPI a minimum 4-wire interface:
- Master Out Slave In (MOSI)
- Master In Slave Out (MISO)
- Shared Clock Signal (CLK or SCL)
- Common Ground Reference (GND)
The other key difference is that the slaves are addressed via hardware instead of software. Each SPI slave has a chip select pin to allow the master to address that particular device. Addressing is necessary for data transfer, since all the slaves share the same bus. Chip select is not mandatory when the bus has a single slave.
And, as for any example with peripherals, when using SPI we need to add the required permission to the manifest:
<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />