• sales

    +86-0755-88291180

RPi Tutorial Series: SPI User Guide

SPI is another communication interface on Raspberry Pi. This section explains how to use a SPI interface on the Pi.

Enable SPI




Configure your Pi and enable the SPI

sudo raspi-config

Select Advanced Options -> SPI -> <YES> to enable the SPI driver by kernel. Then you can check if the SPI is enabled:

lsmod

If SPI enabled, the terminal echoes an spi-bcm2835 device. You can check the GPIO with the command:

gpio readall

you get:

 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 1 |  3 || 4  |   |      | 5V      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 1 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 1 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 1 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 1 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 1 | IN   | CE1     | 11  | 7   |
 |   0 |  30 |   SDA.0 |   IN | 1 | 27 || 28 | 1 | IN   | SCL.0   | 31  | 1   |
 |   5 |  21 | GPIO.21 |   IN | 1 | 29 || 30 |   |      | 0v      |     |     |
 |   6 |  22 | GPIO.22 |   IN | 1 | 31 || 32 | 0 | IN   | GPIO.26 | 26  | 12  |
 |  13 |  23 | GPIO.23 |   IN | 0 | 33 || 34 |   |      | 0v      |     |     |
 |  19 |  24 | GPIO.24 |   IN | 0 | 35 || 36 | 0 | IN   | GPIO.27 | 27  | 16  |
 |  26 |  25 | GPIO.25 |   IN | 0 | 37 || 38 | 0 | IN   | GPIO.28 | 28  | 20  |
 |     |     |      0v |      |   | 39 || 40 | 0 | IN   | GPIO.29 | 29  | 21  |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+

The SPI interface composed with these pins: SCLK (Clock), CE (Chip select), MOMI (Master Out Master In) and MIMO (Master In Master Out).

SPI Programming

A 0.96" OLED is embedded within Pioneer600. The OLED is communicated with the MCU via SPI and you can download the examples from File:Pioneer600-Code.tar.gz. This example is too long to paste, so we just explain some spi functions briefly. See detailed program in the demo code directory Pioneer600/OLED/. You should install some specific libraries before trying these examples, see: Libraries Installation for RPi.

BCM2835

  • bcm2835_spi_begin(); // Start SPI operations. Forces RPi SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1) to alternate function ALT0, which enables those pins for SPI interface. You should call bcm2835_spi_end() when all SPI funcitons are complete to return the pins to their default functions. See: http://www.airspayce.com/mikem/bcm2835/group__spi.html
  • bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // Set bit order as MSB First
  • bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // Sets the SPI data mode, CPOL = 0, CPHA = 0
  • bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_128); // Sets the SPI clock divider
  • bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // Sets the chip select pin(s) When an bcm2835_spi_transfer() is made,
  • bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // Sets the chip select pin CS0 polarity as active LOW.
  • bcm2835_spi_transfer(uint_t value); // Transfers one byte
  • bcm2835_spi_transfernb(char *tbuf,char *rbuf,uint32_t len); // Transfers any number of bytes
TAG: Modbus RTU 8-ch Relay Module (D) Raspberry Pi Refractive Prism MPW7 Raspberry Pi 5 PCIe to WIFI7 Adapter Board HAT Pi5 For Google TPU BE200 AX210 AI 7inch Round LCD HDMI Capacitive 7 inch Touchscreen Display 1080x1080 For Raspberry Pi/Jetson Nano/mini PC Raspberry Pi Pico 2 P2350-Plus Smart Car Kit PicoGo2 Autonomous Driving Learning Car LuckFox Core3566 240x280 Raspberry Pi 5 Raspberry Pi CM5 Gigabit/2.5G Dual Ethernet Expansion Board with RJ45 GPIO IO Base HDMI to LVDS DPI Interface WiFi Wireless ESP32 DDSM Motor Driver HAT For Direct Drive Servo Hub Motors For Raspberry Pi Raspberry Pi RP2040 LoRa-HF ESP32-P4 Module Core Board PSRAM 32MB Flash 16MB N16R32 Onboard ESP32-C6 USB TO 2.5G Ethernet Port Converter RJ45 RTL8156B Driver-Free For Windows/macOS/Linux/Android Arduino Thermal imaging camera Raspberry Pi Pico rp2040 Display image 1.3inch LCD ST7789 change bmp to hex Milk-V Duo User Guide 0.85inch Screen Raspberry Pi LCD Display Screen 1.3inch LCD with Game Button AND Audio Buzzer For Zero 2W / 3B / 4B / PI5 / RP2040-PiZero