Main Content

spidev

Connection to SPI device on Raspberry Pi hardware

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

Description

This object represents a connection from the MATLAB® software to the SPI device on the Raspberry Pi® hardware. To exchange data with the SPI device, use this object with the functions listed in Object Functions.

Creation

Description

myspidevice = spidev(mypi,channel) creates a connection, myspidevice, from the MATLAB software to the chip select pin, Channel, of the SPI device on the Raspberry Pi hardware.

example

myspidevice = spidev(mypi,channel,mode,speed,BitsPerWord) sets one or more of the optional Mode, Speed, and BitsPerWord properties.

example

Input Arguments

expand all

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

Properties

expand all

This property is read-only.

The SPI channel on the hardware for communication, specified as a character vector. This property is set by the channel input argument. It cannot be changed after object creation

Example: 'CE0'

Data Types: char

This property is read-only.

The mode of SPI communication used by the SPI bus, specified as a number between 0 and 3. The mode is defined by clock polarity (CPOL) and phase (CPHA) values.

ModeCPOLCPHA
000
101
210
311

This property is set by the mode input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 0

Data Types: double

This property is read-only.

The number of SPI bits per word, specified as a scalar.

Example: 8

Data Types: double

This property is read-only.

Speed of SPI communication (clock cycles) in Hertz, specified as a scalar. This property is set by the speed input argument. If not specified as an input argument, it takes the default value. It cannot be changed after object creation.

Example: 500000

Data Types: double

Object Functions

writeReadWrite data to and read data from SPI device

Examples

collapse all

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

Create a connection from the MATLAB software to the Raspberry Pi board.

mypi = raspi
mypi = 

  raspi with properties:

         DeviceAddress: '192.168.1.101'              
                  Port: 18734                         
             BoardName: 'Raspberry Pi 2 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'}                     
      AvailableWebcams: {}                            
           I2CBusSpeed: 100000                        

  Supported peripherals

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

Enable SPI using the Hardware Setup screen and get the channels.

mypi.AvailableSPIChannels
ans =

  1×2 cell array  

    {'CE0'}    {'CE1'}

Show the location of the SPI pins, GPIO 10 (SPI0_SDO), GPIO 9 (SPI0_SDI), and GPIO 11 (SPI0_SCLK), on the GPIO header.

showPins(mypi)

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

myspidevice = spidev(mypi,'CE1',0)
myspidevice = 

SPIdev with properties:

               Channel: CE1            
                  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(myspidevice,[hex2dec('08') hex2dec('D4')])
out = 
         7 211

If you are not using SPI, disable SPI using the Hardware Setup screen to make additional GPIO pins available.

Extended Capabilities