Raspberry Pi CM5 IoT In C - Getting Started With SPI
Written by Harry Fairhead   
Monday, 29 December 2025
Article Index
Raspberry Pi CM5 IoT In C - Getting Started With SPI
SPI Protocol
SPIDev
Full Listing

The CM5 has SPI and you can use it via Linux drivers or directly.  In this article we look at how to use it via a driver. This is an extract from the newly-published Raspberry Pi Compute Module 5 IoT In C

Raspberry Pi Compute Module 5
IoT In C
Using Linux
Drivers and Gpio5

By Harry Fairhead

CIoTCM5360
Buy from Amazon.

Contents

  1. The CM5 For The IoT
  2. Setting Up the CM5
  3. C and Visual Studio Code
  4. Drivers: A First Program
  5. The GPIO Character Drive
  6. GPIO Using I/O Control
  7. GPIO Events
  8. GPIO Hardware With Gpio5 
          Extract:  GPIO Registers 
  9. Some Electronics
  10. The Device Tree
  11. Pulse Width Modulation
        
    Extract: PWM Using GPIO5
  12. SPI Devices
         Extract: Getting Started With SPI ***NEW!!!
  13. I2C Driver and Gpio5
  14. Sensor Drivers – Linux IIO & hwmon
  15. 1-Wire Bus
  16. The PIO
        
    Extract: Getting Started With PIO
  17. Going Further With Drivers
  18. Almost Real-Time Linux
  19. Appendix I Gpio5

 <ASIN:1871962951> 

SPI Devices

The CM5 offers two standard ways of connecting more sophisticated devices in hardware – the Serial Peripheral Interface or SPI bus and the I2C or I-⁠squared-C bus. In addition there is a Linux driver which supports the non-standard 1-Wire bus in software. The following chapters focus on these buses rather than specific devices. The advantage of a bus is that, once you know how to use it, connecting compatible devices is more or less the same task, irrespective of device.

There are drivers for some specific SPI devices and if such a driver exists you should use it. More information on how to do this is given later. However, if a driver doesn’t exist it isn’t difficult to interface to an SPI device at a lower level via the Linux SPI driver. First, however, we need to know something about how SPI works. If you just want to use a device driver, skip this section until you need it.

SPI Bus Basics

In the hardware configuration most used for the Pi, there is a single master and, at most, two slaves.

The signal lines are:

  • MOSI (Master Output Slave Input), i.e. data to the slave

  • MISO (Master Input Slave Output), i.e. data to the master


  • SCLK (Serial Clock), which is always generated by the master

In general, there can also be any number of SS (Slave Select), CE (Chip Enable) or CS (Chip Select) lines, which are usually set low to select which slave is being addressed. Notice that unlike other buses, I2C for example, there are no SPI standard commands or addresses, only bytes of data. However, slave devices do interpret some of the data as commands to do something or send some particular data.

SPI Interfaces

The CM5 has six SPI interfaces that can be accessed via the exposed GPIO lines.

SPI0 GPIO Mode 0

Function

Pin

GPIO

MOSI

19

GPIO10

MISO

21

GPIO09

SCLK

23

GPIO11

CE0

24

GPIO08

CE1

26

GPIO07

‍CE2

5

GPIO03

‍CE3

7

GPIO04

 

SPI1 GPIO Mode 0

Function

Pin

GPIO

MOSI

38

GPIO20

MISO

35

GPIO19

SCLK

40

GPIO21

CE0

12

GPIO18

CE1

11

GPIO17

CE2

36

GPIO16

‍CE3

13

GPIO27 (mode 8)

 

SPI2 GPIO Mode 8

Function

Pin

GPIO

MOSI

3

GPIO02

MISO

28

GPIO01

SCLK

5

GPIO03

CE0

27

GPIO00

CE1

18

GPIO24

 

 

SPI3 GPIO Mode 8‍

Function

Pin

GPIO

MOSI

3

GPIO02

MISO

28

GPIO01

SCLK

5

GPIO03

CE0

27

GPIO00

CE1

22

GPIO25

SPI4 GPIO Mode 8

Function

Pin

GPIO

MOSI

19

GPIO10

MISO

21

GPIO09

SCLK

23

GPIO11

CE0

24

GPIO08

SPI5 GPIO Mode 8

Function

Pin

GPIO

MOSI

8

GPIO14

MISO

33

GPIO13

SCLK

10

GPIO15

CE0

32

GPIO12

CE1

37

GPIO26

Notice that some SPI controllers share GPIO lines and therefore cannot be used at the same time, e.g SPI2 and SPI3, SPI4 and SPI0. For these and other reasons it is better to use SPI0 and SPI1 if at all possible.



Last Updated ( Wednesday, 31 December 2025 )