Skip to content

Most visited

Recently visited

navigation

SpiDevice

SpiDevice

public abstract class SpiDevice
extends Object implements Closeable

java.lang.Object
   ↳ com.google.android.things.pio.SpiDevice


Controls a SpiDevice. Opening a SPI device takes ownership of it for the whole system, preventing anyone else from opening/accessing the device until you call close(). Forgetting to call close() will prevent anyone (including the same process/app) from using the device.

Summary

Constants

int MODE0

Mode constant used to configure a SPI device in mode 0: Clock Polarity (CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 0.

int MODE1

Mode constant used to configure a SPI device in mode 1: Clock Polarity (CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 1.

int MODE2

Mode constant used to configure a SPI device in mode 2: Clock Polarity (CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 0.

int MODE3

Mode constant used to configure a SPI device in mode 3: Clock Polarity (CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 1.

Public constructors

SpiDevice()

Public methods

abstract void close()

Close and release the SPI device.

void read(byte[] buffer, int length)

Read a byte array from the device.

abstract void setBitJustification(boolean lsbFirst)

Set the bit justification.

abstract void setBitsPerWord(int bitsPerWord)

Set the number of bits per word.

abstract void setCsChange(boolean change)

Set the chip select behavior.

abstract void setDelay(int delayUs)

Set the delay between transfers.

abstract void setFrequency(int frequencyHz)

Set the frequency of the bus.

abstract void setMode(int mode)

Set the SPI mode.

abstract void transfer(byte[] txBuffer, byte[] rxBuffer, int length)

Transfer data to and from the device.

void write(byte[] buffer, int length)

Write a byte array to the device.

Inherited methods

From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

MODE0

int MODE0

Mode constant used to configure a SPI device in mode 0: Clock Polarity (CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 0.

Constant Value: 0 (0x00000000)

MODE1

int MODE1

Mode constant used to configure a SPI device in mode 1: Clock Polarity (CPOL/CKP) set to 0 and Clock Phase (CPHA) set to 1.

Constant Value: 1 (0x00000001)

MODE2

int MODE2

Mode constant used to configure a SPI device in mode 2: Clock Polarity (CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 0.

Constant Value: 2 (0x00000002)

MODE3

int MODE3

Mode constant used to configure a SPI device in mode 3: Clock Polarity (CPOL/CKP) set to 1 and Clock Phase (CPHA) set to 1.

Constant Value: 3 (0x00000003)

Public constructors

SpiDevice

SpiDevice ()

Public methods

close

void close ()

Close and release the SPI device. Must be called in order for the SPI device to be released and be made available to other users.

Throws
IOException if the SpiDevice is already closed.

read

void read (byte[] buffer, 
                int length)

Read a byte array from the device.

Parameters
buffer byte: Buffer to read into.
length int: Number of bytes to read, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

setBitJustification

void setBitJustification (boolean lsbFirst)

Set the bit justification. The bit justification is the ordering of bits in a byte (also called bit endianness).

Parameters
lsbFirst boolean: If true, use least significant bit first else use most significant bit first.
Throws
IOException

setBitsPerWord

void setBitsPerWord (int bitsPerWord)

Set the number of bits per word.

Parameters
bitsPerWord int: Number of bits per word.
Throws
IOException

setCsChange

void setCsChange (boolean change)

Set the chip select behavior. Affects chipselect after this transfer completes.

Parameters
change boolean: If true, the chip may stay selected until the next transfer.
Throws
IOException

setDelay

void setDelay (int delayUs)

Set the delay between transfers. The delay in microseconds after a transfer before (optionally) changing the chip select status and completing.

Parameters
delayUs int: Delay in microseonds.
Throws
IOException

setFrequency

void setFrequency (int frequencyHz)

Set the frequency of the bus. The frequencies supported is board dependent and can usually be found in the board documentation.

Parameters
frequencyHz int: Frequency to set in Hertz. Must be positive.
Throws
IOException
IllegalArgumentException
IOException

setMode

void setMode (int mode)

Set the SPI mode.

Parameters
mode int: Mode to set. One of MODE0, MODE1, MODE2, MODE3.
Throws
IOException

transfer

void transfer (byte[] txBuffer, 
                byte[] rxBuffer, 
                int length)

Transfer data to and from the device.

Parameters
txBuffer byte: Data to send to the device, or null to receive only.
rxBuffer byte: Data read from the device, or null to send only.
length int: Number of bytes to transfer, may not be larger than the size of txBuffer or rxBuffer if they are non-null.
Throws
IOException
IllegalArgumentException
IOException

See also:

write

void write (byte[] buffer, 
                int length)

Write a byte array to the device.

Parameters
buffer byte: Buffer to write.
length int: Number of bytes to write, may not be larger than the buffer size.
Throws
IOException
IllegalArgumentException
IOException

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.