|Pi IoT In C Using Linux Drivers - The SPI Driver|
|Written by Harry Fairhead|
|Monday, 31 May 2021|
Page 3 of 3
Pi SPI Interfaces
So far we have used the default SPI0 bus. While this is often sufficient it is time to find out about the other possibilities.
The number of SPI buses a Raspberry Pi has depends on its model. The Pi 3 and Pi Zero have two SPI devices:
The Pi 4 has five SPI interfaces - SPI0, SPI3, SPI4, SPI5 and SPI6 - and two mini-SPI interfaces - SPI1 and SPI 2.
The SP0 and SP1 are the same as already listed. The additional SPI interfaces that are available on the connector are:
All of the SPI interfaces all have a set of drivers that follow a standard pattern, where n is the port number:
spin-1cs, cs0_pin=pin uses a single chip select
spin-2cs, cs0_pin=pin1,cs1_pin=pin2 uses two chip selects
If you don’t specify a pin to be used for the chip select then the default used is the pin shown in the tables above.
SPI1 also has the possibility of using three chip selects:
It is also worth knowing that the drivers do not use the SPI hardware’s chip select implementation. Instead they use a general GPIO line and set it high and low under software control. This means you can use any GPIO line as a chip select, not just the ones supported by the hardware.
You can also use the csm_spidev parameter to prevent the creation of an SPIdev node for the mth chip select.
SPI0 is the same on all models of Pi and this is the one you should use as first choice. You can use the other SPI interfaces on the Pi 4, but notice that some of them use the same GPIO pins, SPI5 and SPI1 for example, and hence cannot be used at the same time.
In chapter but not in book:
The SPI bus is often a real headache because of the lack of a definitive standard, but in most cases you can make it work. The first problem is in discovering the characteristics of the slave device you want to work with. In general, this is solved by a careful reading of the datasheet or perhaps some trial and error, see the next chapter for an example.
If you are working with a single slave then generally things work once you have the SPI bus configuration set correctly. Things are more difficult when there are multiple devices on the same bus. The Raspberry Pi can only directly support two devices, but this is enough to make the task more difficult. Typically you will find SPI devices that don't switch off properly when they are not being addressed. In principle, all SPI devices should present high impedance outputs (i.e. tristate buffers) when not being addressed, but some don't. If you encounter a problem you need to check that the selected slave is able to control the MISO line properly.
Another problem, which is particularly bad for the Pi, is noise. If you are using a USB, or some other power supply that isn't able to supply sufficient instantaneous current draw to the Pi, you will see noise on any or all of the data lines - the CS0/1 lines seem to be particularly sensitive. The solution is to get a better power supply.
If there is a full driver for the device then it is a good idea to give it a try, but many SPI device drivers are poorly supported and documentation is usually non-existent. If you can get a direct driver to work then it will usually be faster and easier, but it might not be as stable as hand-coding your own user-side interaction with the device with the help of SPIdev.
Raspberry Pi IoT In C Using Linux Drivers
By Harry Fairhead
Buy from Amazon.
or email your comment to: email@example.com
|Last Updated ( Monday, 31 May 2021 )|