spidev

Connection to SPI device on BeagleBone Black hardware

Description

This object represents a connection from the MATLAB® software to the SPI device on the BeagleBone® Black hardware. Attach an SPI device to the appropriate pins on the BeagleBone hardware. To exchange data with the SPI device, use this object with the functions listed in Object Functions.

Creation

Syntax

spi = spidev(bbb,channel)
spi = spidev(bbb,channel,mode,speed)

Description

example

spi = spidev(bbb,channel) creates a connection from the MATLAB software to the SPI device on the specified BeagleBone Black hardware and the SPI channel.

example

spi = spidev(bbb,channel,mode,speed) creates a connection from the MATLAB software to the SPI device on the BeagleBone Black hardware using optional arguments to override the default values for mode and speed.

Input Arguments

expand all

BeagleBone Black connection created using beaglebone, specified as an 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: 'spidev1.0'

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.

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 BeagleBone Black hardware.

bbb = beaglebone
bbb = 

  beaglebone with properties:

           DeviceAddress: '192.168.7.2'
               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.

enableSPI(bbb,0)
bbb.AvailableSPIChannels
ans = 

    'spidev1.0'

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.

showAllPins(bbb)

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

Introduced in R2015a