Pi IoT In C Using Linux Drivers - The SPI Driver
Written by Harry Fairhead   
Monday, 31 May 2021
Article Index
Pi IoT In C Using Linux Drivers - The SPI Driver
A Loopback Example
Pi SPI Interfaces

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:

SPI0

Function

Pin

 

MOSI

19

GPIO10

MISO

21

GPIO09

SCLK

23

GPIO11

CE0

24

GPIO08

CE1

26

GPIO07

SPI1

Function

Pin

 

MOSI

38

GPIO20

MISO

35

GPIO19

SCLK

40

GPIO21

CE0

12

GPIO18

CE1

11

GPIO17

CE2

36

GPIO16

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:

SPI3

Function

Pin

 

MOSI

3

GPIO2

MISO

28

GPIO1

SCLK

5

GPIO3

CE0

27

GPIO0

CE1

18

GPIO24

SPI4

Function

Pin

 

MOSI

31

GPIO6

MISO

29

GPIO5

SCLK

26

GPIO7

CE0

7

GPIO4

CE1

22

GPIO25

SPI5

Function

Pin

 

MOSI

8

GPIO14

MISO

33

GPIO13

SCLK

10

GPIO15

CE0

32

GPIO12

CE1

37

GPIO26

SPI6

Function

Pin

 

MOSI

38

GPIO20

MISO

35

GPIO19

SCLK

40

GPIO21

CE0

12

GPIO18

CE1

13

GPIO27

 

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:

spi1-3cs, cs0_pin=pin1,cs1_pin=pin2,cs2_pin=pin3

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 MCP3008
  • Connecting To The Pi
  • Basic Configuration
  • The Protocol
  • How Fast

Problems

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.

Summary

  • All versions of the Raspberry Pi have two usable hardware Serial Peripheral Interface (SPI) buses. The Pi 4 has four additional ones.

  • Making SPI work with any particular device has four steps:

    1. Discover how to connect the device to the SPI pins. This is a matter of identifying pinouts and what chip selects are supported.

    2. Find out how to configure the Pi's SPI bus to work with the device. This is mostly a matter of clock speed and mode.

    3. Identify the commands that you need to send to the device to get it to do something and what data it sends back as a response.

    4. Find, or work out, the relationship between the raw reading, the voltage, and the quantity the voltage represents.

  • The Linux SPI driver can be used to interface to any SPI device as long as you know what commands to send and what the data sent back means.

  • The SPI driver is supported by the SPIdev header which provides the basic tools to work with the ioctl interface.

  • The SPI driver uses general GPIO lines as chip select lines and not the built-in hardware.

  • Using the other SPI interfaces on the Pi 4 is just a matter of selecting the appropriate driver.

  • The MCP3000 range of A-to-D converters is very easy to use via SPI. It can be used directly from the SPI bus by sending commands and reading the data generated.

  • There is also a specific MCP3008 driver which allows you to work with the device without having to know anything about SPI or the commands that have to sent to read the data.

  • The MCP3008 driver is an example of an IIO device.

 

 

Raspberry Pi IoT In C Using Linux Drivers

By Harry Fairhead

Cdrivers360

Buy from Amazon.

Contents

  1.  Choosing A Pi For IoT

  2. C and Visual Studio Code

  3.  Drivers: A First Program

  4.  The GPIO Character Driver
         Extract: GPIO Character Driver

  5. GPIO Using I/O Control

  6.  GPIO Events

  7.  The Device Tree
        Extract: The DHT22

  8.  Some Electronics

  9.  Pulse Width Modulation
    Extract:  The PWM Driver 

  10. SPI Devices
    Extract: The SPI Driver 

  11. I2C Basics

  12. The I2C Linux Driver ***NEW!

     

  13. Advanced I2C

  14. Sensor Drivers – Linux IIO & Hwmon
      Extract: Hwmon  

  15. 1-Wire Bus
      Extract: 1-Wire And The DS18B20 

  16. Going Further With Drivers

  17. Appendix I

 <ASIN:1871962641>

<ASIN:B08W9V7TP9>

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


Prompt Engineering Techniques To Make You An Expert
18/11/2024

Introducing a GitHub repository full of hot tips and instructions on how to build the perfect prompt presented in a collection of Jupiter Notebooks.



Flutter Forked As Flock
05/11/2024

One of developers who worked on the Flutter team at Google has created an open-source form of the framework. Matt Carroll says Flock will be "Flutter+", will remain constantly up to date with Flutter, [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:187196265X>

<ASIN:1871962692>

<ASIN:1871962609>

 <ASIN:1871962617>

<ASIN:1871962455>

<ASIN:1871962463>



Last Updated ( Monday, 31 May 2021 )