Main Content


Enable SPI interface

Add-On Required: This feature requires the MATLAB Support Package for BeagleBone Black Hardware add-on.



enableSPI(bbb, bus_number) enables the SPI bus and configures the GPIO pins for use as SPI pins. After the hardware starts, the SPI bus is disabled by default.

For example, on a BeagleBone Black Rev 00C0 board, enableSPI(bbb, 1) makes P9_21 (SPI0_D0), P9_18 (SPI0_D1), P8_28 (SPI0_CSO), and P8_31 (SPI0_SCLK) available for use as SPI pins.

To disable SPI, restart the hardware.


collapse all

You can connect to and exchange data with an SPI device.

Create a connection from the MATLAB® software to the BeagleBone® Black hardware.

bbb = beaglebone
bbb = 

  beaglebone with properties:

           DeviceAddress: ''
               BoardName: 'BeagleBone Black Rev 00C0'
           AvailableLEDs: {'USR0'  'USR1'  'USR2'  'USR3'}
    AvailableDigitalPins: {1x29 cell}
     AvailableAnalogPins: {'AIN0'  'AIN1'  'AIN2'  'AIN3'  'AIN4'  'AIN5'  'AIN6'}
        AvailablePWMPins: {}
    AvailableSPIChannels: {}
       AvailableI2CBuses: {'i2c-1'}
    AvailableSerialPorts: {}
        AvailableWebcams: {} 

By default, SPI is disabled. AvailableSPIChannels does not show any channels.

Enable SPI to get the channels.

ans = 


Show the location of the SPI pins, P9_21 (SPI0_D0), P9_18 (SPI0_D1), P8_28 (SPI0_CSO), and P8_31 (SPI0_SCLK), on the GPIO header.


After physically connecting your SPI device to three SPI pins, connect to the SPI device.

spi = spidev(bbb,'spidev1.0',0)
spi = 

  spidev with properties:

      Channel: spidev1.0      
         Mode: 0               (0, 1, 2 or 3)
  BitsPerWord: 8               (only 8-bits per word is supported)
        Speed: 500000          (View available speeds)

Write data to and read data from the SPI device.

out = writeRead(spi,[hex2dec('08') hex2dec('D4')])
out = 
         7 211

Input Arguments

collapse all

BeagleBone Black connection created using beaglebone, specified as an object.

Identifies the SPI bus to enable.

SPI bus 0 shares pins with I2C bus 1.

SPI bus 1 shares pins with HDMI, which is enabled by default. Use showPins to view the current pin usage. If HDMI is enabled, disable HDMI before using enableSPI to enable this bus. For information on how to disable HDMI, see the BeagleBone Black hardware documentation.

Example: 0

Version History

Introduced in R2015a