Main Content

enableSPI

Enable SPI interface

Description

example

enableSPI(mypi) enables the SPI bus. You can then use the Raspberry Pi™ hardware board pins SPI0_MOSI (GPIO 10), SPI0_MISO (GPIO 9), and SPI0_SCLK (GPIO 11) as SPI pins and interface the Raspberry Pi hardware boards with any SPI devices to exchange data. The SPI bus is enabled by default. To disable SPI, use disableSPI.

Examples

collapse all

Enable and disable the SPI interface on the Raspberry Pi kernel and use the Raspberry Pi hardware board pins for SPI functionality and GPIO, respectively. Write and read data from the EEPROM SPI IC 25AA080 interfaced with the Raspberry Pi hardware board and exchange data. For more information on IC 25AA080, refer to its datasheet. You can also exchange data between the Raspberry Pi and other SPI devices. For more information, refer to the datasheet of the specific SPI device. Perform these steps on the Raspberry Pi Linux® terminal to ensure that the SPI interface is enabled in the hardware kernel.

  1. Run this command:

    sudo raspi-config

  2. Select Interfacing Options > SPI.

    Raspberry Pi Kernel SPI Interface

  3. Select Yes when prompted to enable the SPI interface.

    Raspberry Pi Kernel SPI Interfacing enabling options

  4. Select Yes when prompted to automatically load the SPI kernel module.

  5. Select Finish.

  6. Select Yes when prompted to reboot.

Tip

You can also enable the SPI interface using the Raspberry Pi Resource Monitor App.

To create a connection from MATLAB® to the Raspberry Pi board, execute this command at the MATLAB Command Window.

mypi=raspi;
           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18734
               BoardName: 'Raspberry Pi 3 Model B+'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4 5 6 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
    AvailableSPIChannels: {‘CE0’,’CE1’}
       AvailableI2CBuses: {'i2c-1'}
             I2CBusSpeed: 100000
        AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
GPIO header pins MISO (GPIO 9), MOSI (GPIO 10), and SCLK (GPIO 11) are used for SPI functionality. GPIO pins 9, 10, and 11 are not listed in the AvailableDigitalPins property as the SPI functionality is enabled by default and the pins are currently being utilized by the SPI functionality.

To display the pin mapping of the Raspberry Pi hardware board, execute this command at the MATLAB Command Window.

showPins(mypi);
SPI pins callout for Raspberry Pi 3 Model B+ GPIO

The revised pin map of the Raspberry Pi hardware board shows the location of the SPI pins, SPI_MISO (GPIO 9), SPI_MOSI (GPIO 10), and SPI_SCLK (GPIO 11), on the CE1 bus.

After physically connecting your SPI device to the three SPI pins of the Raspberry Pi hardware board, establish a connection to the SPI device through the CE1 bus.

eeprom = spidev(mypi, 'CE1')
eeprom = 

  spidev with properties:

                 Channel: CE1            
                    Mode: 0               
             BitsPerWord: 8               
                   Speed: 500000          

Enable write and read operation on EEPROM.

enableCmd = 6;

Write data to EEPROM with the precision of uint8.

writeCmd = 2;
address = [0 0];
dataIn = [01 02 03]; 
dummyData = zeros(1,length(dataIn));
writeRead(eeprom, enableCmd, 'uint8');
dataOut = writeRead(eeprom, [writeCmd address dataIn], 'uint8');
dataOut =

  1×6 uint8 row vector

  0   0   0   0   0   0

Read data from EEPROM with the precision of uint8.

readCmd = 3;
dataOut = writeRead(eeprom, [readCmd address dummyData], 'uint8');
dataOut =

  1×6 uint8 row vector

  0   0   0   1   2   3

Clear the active SPI device connection before disabling it. After you disable the SPI functionality, you can use the Raspberry Pi hardware board pins as GPIO pins.

clear eeprom;
disableSPI(mypi);
mypi=raspi;
        DeviceAddress: 'raspberrypi-hysdu8X38o'
                 Port: 18734
            BoardName: 'Raspberry Pi 3 Model B+'
        AvailableLEDs: {'led0'}
 AvailableDigitalPins: [4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27]
 AvailableSPIChannels: {}
    AvailableI2CBuses: {'i2c-1'}
          I2CBusSpeed: 100000
     AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)
The GPIO 7, 8, 9, 10, and 11 pins are now available and can be used as GPIO pins. No SPI buses are available for SPI interfacing.

Before using SPI again, enable the SPI functionality.

enableSPI(mypi);

Input Arguments

collapse all

Connection to a Raspberry Pi hardware board, specified as a raspi object.

Introduced in R2014a