Documentation Center

  • Trial Software
  • Product Updates

spi

Create SPI object

Syntax

  • S = spi(Vendor,BoardIndex,Port); example

Description

example

S = spi(Vendor,BoardIndex,Port); constructs an spi object associated with Vendor, BoardIndex, and Port. Vendor must be set to 'aardvark', since you need to use a Total Phase Aardvark adaptor 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 host adaptor.

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 the link for "Using Properties on the SPI Object" at the end of the Examples section.

Examples

expand all

Communicate With SPI Device

This example shows how to create a SPI object and communicate with a SPI device.

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 = 

               Vendor: 'aardvark'
    VendorDescription: 'Total Phase I2C Driver'
    VendorLibraryName: 'aardvark.dll'
    InstalledBoardIds:  0
ObjectConstructorName:  '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);

Input Arguments

expand all

Vendor — Adaptor board vendor‘aardvark'

Adaptor board vendor, specified as the character string 'aardvark'. You need to use a Total Phase Aardvark adaptor 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

BoardIndex — Board index of your hardware0

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 — Port number of your hardware0

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

Was this topic helpful?