SpiDevice

public interface SpiDevice
implements Closeable

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 BIT_JUSTIFICATION_LSB_FIRST

Justification constant to transfer the least significant bit first.

int BIT_JUSTIFICATION_MSB_FIRST

Justification constant to transfer the most significant bit first.

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 methods

abstract void close()

Close and release the SPI device.

default String getName()

Returns the I/O name.

default void read(byte[] buffer, int length)

Read a byte array from the device.

abstract void setBitJustification(int justification)

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.

default void write(byte[] buffer, int length)

Write a byte array to the device.

Inherited methods

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

Constants

BIT_JUSTIFICATION_LSB_FIRST

int BIT_JUSTIFICATION_LSB_FIRST

Justification constant to transfer the least significant bit first.

Constant Value: 1 (0x00000001)

BIT_JUSTIFICATION_MSB_FIRST

int BIT_JUSTIFICATION_MSB_FIRST

Justification constant to transfer the most significant bit first.

Constant Value: 0 (0x00000000)

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 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.

getName

String getName ()

Returns the I/O name.

Returns
String

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

setBitJustification

void setBitJustification (int justification)

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

Parameters
justification int: bit justification mode, either BIT_JUSTIFICATION_LSB_FIRST or BIT_JUSTIFICATION_MSB_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

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