Main Content

Transmitting Data over the SPI Interface

The typical workflow for transmitting data over the SPI interface involves adaptor discovery, connection, communication, and cleanup. Discovery can be done only at the adaptor level. You must have a Total Phase Aardvark adaptor or an NI-845x adaptor board installed to use the spi interface.

Transmit Data Over SPI Using Aardvark

This example shows how to communicate with an EEPROM chip on a circuit board, with a board index of 0 and using port 0.

  1. Ensure that the Aardvark adaptor is installed so that you can use the spi interface.

  2. Look at the adaptor properties.

    Make sure that you have the Aardvark software driver installed and that the aardvark.dll is on your MATLAB® path. For details, see SPI Interface Usage Requirements and Guidelines.

  3. Create the SPI object called S, using these properties:

    % Vendor = aardvark
    % BoardIndex = 0
    % Port = 0
    
    S = spi('aardvark', 0, 0);

    You must provide these three parameters to create the object.

  4. Look at the object properties.

    When you create the spi object, default communication settings are used, as shown here. To change any of these settings, see Using Properties on the SPI Object for more information and a list of the properties.

  5. Connect to the chip.

    connect(S);
  6. 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 that contains the number of values to read
    numData = 5; 
    
    % Read the binary data from the chip
    data = read(S, numData);
  7. Disconnect the SPI device and clean up by clearing the object.

    disconnect(S); 
    clear('S');

Transmit Data Over SPI Using NI-845x

This example shows how to communicate with an EEPROM chip on a circuit board, with a board index of 0 and using port 0.

  1. Ensure that the NI-845x adaptor is installed so that you can use the spi interface.

  2. Look at the NI-845x adaptor properties.

    Make sure that you have the NI-845x software driver installed. For details, see SPI Interface Usage Requirements and Guidelines.

  3. Create the SPI object called s2, using these properties:

    % Vendor = ni845x
    % BoardIndex = 0
    % Port = 0
    
    s2 = spi('ni845x', 0, 0);

    You must provide these three parameters to create the object.

  4. Look at the object properties.

    When you create the spi object, default communication settings are used, as shown here. To change any of these settings, see Using Properties on the SPI Object for more information and a list of the properties.

  5. Connect to the chip.

    connect(s2);
  6. 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(s2, dataToWrite);
    
    % Create a variable that contains the number of values to read
    numData = 5; 
    
    % Read the binary data from the chip
    data = read(s2, numData);
    
    ans = 
    
         0     0     0     0     0
  7. Disconnect the SPI device and clean up by clearing the object.

    disconnect(s2); 
    clear('s2');

SPI Functions

You can use these functions with the spi object.

Note

SPI is a full duplex communication protocol, and data must be written in order to read data. You can use the read function to write dummy data to the device. The write function flushes the data returned by the device. The writeAndRead function does the read and write together.

FunctionPurpose
instrhwinfo

Check that the Aardvark and/or NI-845x adaptor is installed.

instrhwinfo('spi')

Look at the adaptor properties.

instrhwinfo('spi', 'Aardvark')

instrhwinfo('spi', 'ni845x')

spiinfo

Returns information about devices and displays the information on a per vendor basis.

info = spiinfo()

connect

Connect the SPI object to the device. Use this syntax:

connect(spiObject);

read

Synchronously read binary data from the device. To read data, first create a variable, such as numData, to specify the size of the data to read. In this case, create the variable to read 5 bytes. Then use the read function as shown here, where spiObject is the name of your object. This process is also shown in step 6 of the previous example. The precision of the data is UINT8.

numData = 5;

read(spiObject, numData);

Or you can use this syntax:

A = read(spiObject, size)

write

Synchronously write binary data to the device. To write data, first create a variable, such as dataToWrite. In this case, create the data [3 0 0 0]. Then use the write function as shown here, where spiObject is the name of your object. This process is also shown in step 6 of the previous example. The precision of the data written is UINT8

dataToWrite = [3 0 0 0];

write(spiObject, dataToWrite);

writeAndRead

Synchronously do a simultaneous read and write of binary data with the device. In this case, the function synchronously writes the data specified by the variable dataToWrite to the device in binary format, then synchronously reads from the device and returns the data to the variable data, as shown here, where spiObject is the name of your object. The precision of the data written and read is UINT8.

dataToWrite = [3 0 0 0];

data = writeAndRead(spiObject, dataToWrite)

disconnect

Disconnect SPI object from the device. Use this syntax:

disconnect(spiObject);

Note

To get a list of options you can use on a function, press the Tab key after entering a function on the MATLAB command line. The list expands, and you can scroll to choose a property or value. For information about using this advanced tab completion feature, see Using Tab Completion for Functions.