Main Content

writeRead

Write data to and read data from SPI device

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

Description

example

[outputData] = writeRead(spi,data,dataPrecision) writes new data to, and reads data from, an SPI device.

Before reading data from an SPI device, you must write data to the device. Supply the data argument with the number of values that you expect to read.

This function converts data of all other data types to uint8 for transmission.

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

Input Arguments

collapse all

Connection to an SPI device, specified as a spidev object.

Example: spi

Data to write to the SPI device, or the number of values to read from the SPI device, specified as a vector of hexadecimal values.

Example: [hex2dec('08'), hex2dec('D4')]

Data Types: int8 | int16 | int32 | uint8 | uint16 | uint32

Data precision, specified as a character vector. Optional.

The writeRead function converts data to the data type specified by this argument.

Most SPI devices use uint8. Some SPI devices, such as EEPROMs, use uint16.

Example: 'uint8'

Data Types: char

Output Arguments

collapse all

Data from SPI device, returned as a row vector.

Version History

Introduced in R2015a