Main Content

writeRead

Write data to and read data from SPI device

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

Description

example

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

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

The writeRead method stops when it finishes writing and reading the specified data from the device.

This method 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 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 and get the channels by using enableSPI.

enableSPI(mypi)
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 to make additional GPIO pins available.

disableSPI(mypi)

Input Arguments

collapse all

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

Example: myspidevice

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')]

Note

If the data is bigger than uint8, first the lower byte is transferred, followed by the higher byte.

Data precision, specified as one if the following character vectors:

  • 'uint8'

  • 'uint16'

  • 'uint32'

The precision must match the size of the SPI device register.

Example: 'uint8'

Data Types: char

Output Arguments

collapse all

Data from SPI device, returned as a row vector.

Extended Capabilities