Main Content

spi

Create SPI object

    Description

    An spi object represents a connection between MATLAB® and an SPI controller board. Supported controllers are the Total Phase® Aardvark I2C/SPI™ Host Adapter and the NI™ USB-8451 or USB-8452 I2C/SPI Interface Device.

    Creation

    Description

    s = spi(vendor,boardIndex,port) constructs an spi object associated with vendor, boardIndex, and port. vendor must be set to either 'aardvark', for use with a Total Phase Aardvark™, or to 'ni845x', for use with the NI USB-845x controller board, to use this interface. boardIndex specifies the board index of the hardware and is usually 0. port specifies the port number within the device and must be set to 0.

    SPI, or Serial Peripheral Interface, is a synchronous serial data link standard that operates in full duplex mode. Instrument Control Toolbox™ SPI support lets you open connections with individual chips and to read and write over the connections to individual chips using an Aardvark controller.

    The primary uses for the spi interface involve the write, read, and writeAndRead functions for synchronously reading and writing binary data. To identify SPI devices in the Instrument Control Toolbox, use the instrhwinfo function on the SPI interface, called spi.

    Once the SPI object is created, there are properties that can be used to change communication settings. These includes properties for clock speed, clock phase, and clock polarity. For a list of all the properties and information about setting them, see Using Properties on the SPI Object.

    example

    Input Arguments

    expand all

    Adaptor board vendor, specified as the character vector 'aardvark' or 'ni845x'. You need to use a Total Phase Aardvark adaptor or an NI-845x adaptor board to use the SPI interface. You must specify this as the first argument when you create the spi object.

    Example: s = spi('aardvark',0,0);

    Data Types: char | string

    Board index of your hardware, specified as a numeric value. This is usually 0. You must specify this as the second argument when you create the spi object.

    Example: s = spi('aardvark',0,0);

    Data Types: double

    Port number of your hardware, specified as the number 0. The Aardvark adaptor uses 0 as the port number. You must specify this as the third argument when you create the spi object.

    Example: s = spi('aardvark',0,0);

    Data Types: double

    Object Functions

    readRead binary data from SPI instrument
    writeWrite binary data to SPI instrument
    writeAndReadWrite and read binary data from SPI instrument

    Examples

    collapse all

    This example shows how to create a SPI object and communicate with a SPI device, using an Aardvark adaptor board.

    Ensure that the Aardvark adaptor is installed so that you can use the spi interface, and then look at the adaptor properties.

    instrhwinfo('spi')
    instrhwinfo('spi','aardvark') 
    ans = 
    
               VendorName: 'aardvark'
        VendorDescription: 'Total Phase I2C/SPI Driver'
        VendorLibraryName: 'aardvark.dll'
        InstalledBoardIds:  {[0]}
       BoardSerialNumbers:  {'2237722838'}
       ObjectConstructors:  {'spi('aardvark', 0, 0)'}

    Construct a spi object called S using Vendor 'aardvark', with BoardIndex of 0, and Port of 0.

    s = spi('aardvark',0,0);

    You can optionally change property settings such as BitRate, ClockPhase, or ClockPolarity. For example, set the ClockPhase from the default of FirstEdge.

    s.ClockPhase = 'SecondEdge';

    For a list of all the properties and information about setting them, see the link for “Using Properties on the SPI Object” at the end of the Examples section.

    Connect to the chip.

    connect(s);

    Read and write to the chip.

    % Create a variable containing the data to write
    dataToWrite = [3 0 0 0]; 
    
    % Write the binary data to the chip
    write(s,dataToWrite);
    
    % Create a variable to contain 5 bytes of returned data
    numData = 5;
    
    % Read the binary data from the chip
    read(s,numData);

    Disconnect the SPI device and clean up by clearing the object.

    disconnect(s); 
    clear('s');

    Version History

    Introduced in R2013b