Raspberry Pi 5 IoT In C - SPI with GPIO 5
Written by Harry Fairhead   
Sunday, 21 September 2025
Article Index
Raspberry Pi 5 IoT In C - SPI with GPIO 5
GPIO 5 Functions
SPI Read/Write Function
Loopback Example Using GPIO5

Loopback Example Using GPIO5

Using the functions listed above it is easy to implement the loopback example given earlier:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Gpio5.h"
int main(int argc, char **argv)
{
    rp1_Init();
    gpio_set_function(8, GPIO_FUNC_SPI);  // CS0
    gpio_set_function(7, GPIO_FUNC_SPI);  // CS1
    gpio_set_function(3, GPIO_FUNC_SPI);  // CS2
    gpio_set_function(4, GPIO_FUNC_SPI);  // CS3
    gpio_set_function(9, GPIO_FUNC_SPI);  // MISO
    gpio_set_function(10, GPIO_FUNC_SPI); // MOSI
    gpio_set_function(11, GPIO_FUNC_SPI); // SCLK
    spi_init(SPI0, 5000000);
    spi_set_format(SPI0, 8, SPI_CPOL_0, SPI_CPHA_0,
                                       SPI_MSB_FIRST);
    spi_set_slave(SPI0, 0);
    uint8_t wBuff[1] = {0xAA};
    uint8_t rBuff[1];
    int n = spi_write_read_blocking(
SPI0, wBuff, rBuff, 1); printf(" %X %X %d \n ", wBuff[0], rBuff[0], n); return (EXIT_SUCCESS); }

Notice that we set all of the CS lines that SPI0 supports but only use CS0. We could include the setting up of the GPIO lines in the spi_init function, but doing it this way allows the selection of which CS lines to initialize within the user program.

In chapter but not in this extract

  • The MCP3008
  • Connecting to the Pi
  • Basic Configuration
  • The Protocol
  • MCP3008 in Gpio5
  • How Fast
  • The MCP3008 Driver 
  • Problems


Summary

  • The Pi 5 has six SPI controllers but not all can be used at the same time.

  • 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.

  • The hardware SPI controllers are based on Synopsys DW_apb_ssi and this is well documented.

  • It is easy to extend Gpio5 to work with SPI with many advantages.

  • Using the other SPI interfaces on the Pi 5 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 be sent to read the data.

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

You can find the complete Gpio5 at its Github repo:

 https://github.com/IOPress/Gpio5,

at the book’s web page:

https://iopress.info/index.php/books/raspberry-pi-5-iot-in-c

Raspberry Pi 5 IoT In C
Drivers and Gpio5

By Harry Fairhead

CIoTPi5360
Buy from Amazon.

Contents

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

 <ASIN:1871962943>

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


Safe C++ Loses Out To Profiles
17/09/2025

We do need to do something about C++, but what do you expect when a language is controlled by a committee? Certainly not a revolution.



Microsoft Open Sources 6502 Basic
05/09/2025

Microsoft has released the source code of Basic 1.1 for the 6502 microprocessor on GitHub. The page with the code says the assembly language source code represents one of the most historically signifi [ ... ]


More News

pico book

 

Comments




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



Last Updated ( Wednesday, 24 September 2025 )